Friday, 9 February 2018

Blink LED using Arduino

The Internet of things (IoT) is the network of physical devices, vehicles, home appliances and other items embedded with electronics, software, sensors, actuators, and connectivity which enables these objects to connect and exchange data. In this project I am using Arduino Uno board and LED to enable or disable the LED from anywhere in the world. The great thing about using a web interface is that it is cross-platform and it enables you to control your Arduino connected devices with web browsing capabilities, such as smartphones and tablets.

What you need?
1.An Arduino board with an internal LED (usually on pin 13) or a separate LED
2. A webhosting service with PHP support
3. The Arduino IDE
4. The Processing IDE
5. A text editor (e.g. Notepad++)

First, we’ll start with the web interface. It’s function is to give the user the choice to either switch the LED on or off. When either one of them is clicked, the command is written to a "test.txt" file as a 1 (ON) or 0 (OFF) value.
The interface consists of three files; a HTML front-end, a PHP document, and a text file for storing values. So, firstly create these files using a text editor: 
1. index.html
2. led.php
3. LEDstate.txt 


1. Code for index.html

<html>
<head>
<title>LED ON/OFF</title>
</head>
<body>
<font size="10">
  
<b><a href="led.php?state=1">ON LED</a></b> /
<b><a href="led.php?state=0">OFF LED</a></b></font>
</p>
  
</body>
</html>


2. Code for led.php 

<?php
$onoroff = $_GET["state"]; 
$textfile = "LEDstate.txt"
$fileLocation = "$textfile";
$fh = fopen($fileLocation, 'w   ') or die("Something went wrong!");
$stringToWrite = "$onoroff"
fwrite($fh, $stringToWrite); 
fclose($fh);
header("Location: index.html");
?>


3. Arduino Sketch

const int ledPin = 13; // the pin that the LED is attached to - change this if you have a separate LED connected to another pin
int incomingByte;      // a variable to read incoming serial data into
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}
void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  }
}
4. Processing Sketch
import processing.serial.*;
 Serial port;
 void setup()  {
   /* This part must be altered to fit your local settings. The number in brackets after "Serial.list()" is where you declare what COM port your Arduino is connected to.
      If you get error messages, try a different number starting from 0 (e.g. 0, 1, 2, 3...) . */
    port = new Serial(this, Serial.list()[1], 9600);  // Open the port that the Arduino board is connected to, at 9600 baud
}
 void draw() {
  String onoroff[] = loadStrings("http://calibretechnology.com/iot/LEDstate.txt");
  print(onoroff[0]);  // Prints whatever is in the file ("1" or "0")
  if (onoroff[0].equals("1") == true) {
    println(" - TELLING ARDUINO TO TURN LED ON");
    port.write('H'); // Send "H" over serial to set LED to HIGH
  } else {
    println(" - TELLING ARDUINO TO TURN LED OFF");
    port.write('L');  // Send "L" over serial to set LED to LOW
 }
  delay(7000); // Set your desired interval here, in milliseconds
 }
Run your code & Enjoy