Turn on/off a Led by smartphone/PC


Description

In this tutorial is shown how to command a Led via a Smartphone/PC using an Arduino UNO WiFi.

Warning:
Please, it's important to upgrade to the latest ESP firmware, using the Arduino UNO firmware updater tool, before to continue this tutorial.

Hardware 

You can also use the component of the  WorkShop kit or simply the Tinkerkit  LED and 3 Wires Female/Male.
Thinkerkit Led

For more details visit our Store.

Circuit

You can use the integrated L Led  on pin 13 or use another one.
The Led has two legs called anode and cathode, as shown in the below image:
LED 
Connect the anode (usually the longer leg) in series to a resistor of 220Ω and connect it to the board on pin 13.
After connect the cathode to GND. The complete circuit is shown in the below picture.
Webserver Blink circuit


Blink circuit



Instead if you want to use a Tinkerkit Led then follow these indication:
Normally the Tinkerkit devices have three pins Vcc(+), the Signal and the Ground (-).
Connect the Vcc pin (+) to 5V, the Ground (-) to GND and the other one to the pin 13 of the board, you can use the wires female/male for the connections, as shown in the below image.


Blink with the Tinkerkit Led

Code

Now connect the board to PC, via USB cable, and upload the code below, using the Arduino IDE (click here to download the sketch):

Warning:
If you are using the Arduino IDE 1.7.x then continue to use the ArduinoWiFi.h library.
Instead if you are using the Arduino IDE 1.8.x then download the UNOWiFiDev.Edition library from Library Manager and modify the code, replacing #include <ArduinoWiFi.h> with #include <UnoWiFiDevEd.h>

/*
Turn on/off a Led by smartphone/PC
Upload the code and open your borwser on http://<IP>/arduino/webserver/ or http://<hostname>.local/arduino/webserver/
for more information:
http://www.arduino.org/learning/tutorials/boards-tutorials/turn-on-off-a-led-by-smartphone-pc

created in November 2016
by sebba[at]arduino[dot]org
*/

//Import libraries
#include <Wire.h>
#include <ArduinoWiFi.h> // if you are using the Arduino IDE 1.8.x then: //#include <UnoWiFiDevEd.h>

#define PIN 13

//SET UP
void setup() {
pinMode(PIN, OUTPUT); //connect the Led on Pin 13
Wifi.begin();
Wifi.println("WebServer Server is up");
}


//LOOP
void loop() {
while (Wifi.available()) {
process(Wifi);//call process() function
}
delay(50);
}

/* FUNCTIONS */


//process()
void process(WifiData client) {
// read the command
String command = client.readStringUntil('/');
if (command == "webserver") {
WebServer(client);// call WebServer() function
}
if (command == "digital") {
digitalCommand(client);//call digitalCommand() function
}
}

//WebServer()
void WebServer(WifiData client) {
client.println(F("<html><head><style>"));

//Stylesheet parameters for the button
client.println(F("input[type=button]{font-size:62px;color:#FFF;width:50%;margin:20% 0 0 25%;}"));
client.println(F(".off{background:#666}"));
client.println(F(".on{background:#00979C}"));
client.println(F("</style>"));

//JavaScript functions that creates an Ajax request to the URL
//URL+'/arduino/digital/'+pin+'/'+value"
//where URL is the IP of the Uno WiFi, pin is the pin to change and value is the logic level
//of the pin
client.println(F("<script>"));

//This function create the command for the AJAX request
client.println(F("function command(event,pin){"));
client.println(F("var value=document.getElementById(event.id).value=='ON'?0:1"));
client.println(F("ajax(event.id,pin,value)}"));

//Create the AJAX request
client.println(F("function ajax(elm, pin, value){"));
client.println(F("var URL_array=document.URL.split('/')"));;
client.println(F("var URL=URL_array[0]+'//'+URL_array[2]"));
client.println(F("url=URL+'/arduino/digital/'+pin+'/'+value"));
client.println(F("xmlhttp=new XMLHttpRequest()"));
client.println(F("xmlhttp.onreadystatechange=function(){"));
client.println(F("if(xmlhttp.readyState==4 && xmlhttp.status==200)"));
client.println(F("var resp=xmlhttp.responseText"));
client.println(F("if(typeof resp==='undefined')return"));
client.println(F("var el1 = document.getElementById(elm)"));
client.println(F("el1.className=resp.includes('D'+pin+':1')?'on':'off'"));
client.println(F("el1.value=resp.includes('D'+pin+':1')?'ON':'OFF'}"));
client.println(F("xmlhttp.open('GET',url,true)"));
client.println(F("xmlhttp.send()"));
client.println(F("}</script>"));

//Print the HTML Code, with the button. Pushing the button it calls the function "command"
client.println(F("</head><body>"));
client.print(F("<input id=button type='button' class='off' onClick='command(this,"));
client.print(PIN);
client.println(F(");' value='OFF'/>"));
client.println(F("</body></html>"));
client.print(DELIMITER); //end communication
client.flush();
}

/*digitalCommand() function
It turn the led and return a message
*/
void digitalCommand(WifiData client) {
int pin, value;
// Read pin number
pin = client.parseInt();
// If the next character is a '/' it means we have an URL
// with a value like: "/digital/13/1"
if (client.read() == '/') {
value = client.parseInt();
digitalWrite(pin, value);
}

// Send feedback to client
client.println("Status: 200 OK\n");
client.print(F("D"));
client.print(pin);
client.print(F(":"));
client.print(value);
client.print(EOL); //char terminator
}

If you want to use an external webserver, for example Apache, then use this other code.

OUTPUT

Click on the button to turn On/Off the Led:

OFF button

ON button

Note: 
If you haven't configured your board then click here.