Project tutorial
WebServerBlink Using Arduino Uno WiFi

WebServerBlink Using Arduino Uno WiFi © MPL-2.0

Arduino.org released a new Arduino uno with built-in WiFi, In this project I show you How to Blink An LED using Web-Server.

  • 19,851 views
  • 5 comments
  • 36 respects

Components and supplies

About this project

In this demonstrative example it is shown how to realize a simple web server, using an Arduino UNO WiFi, to command the switch ON/OFF of a LED.

1. Hardware

  • Arduino UNO WiFi
  • Led
  • 220Ω Resistor
  • wire
  • Breadboard

2.Circuit

You can use the integrated L LED on pin 13 or use another one, in this case connect the LED anode (usually the longer pin) in series to a 220Ω resistor and connect it to the board pin 13.

After connect the cathode to GND, as shown in the picture.

Now plug in the board to PC and upload the sketches below.

3.Code

Upload the below code and access via browser to http://<IP>/arduino/webserver/ orhttp://<hostname>.local/arduino/webserver/ to read the sensors values.


#include <Wire.h>
#include <ArduinoWiFi.h>
/*
on your borwser, you type http://<IP>/arduino/webserver/ or http://<hostname>.local/arduino/webserver/
http://www.arduino.org/learning/tutorials/webserverblink
*/
void setup() {
    pinMode(13,OUTPUT);
    Wifi.begin();
    Wifi.println("WebServer Server is up"); 
}
void loop() {
    while(Wifi.available()){
      process(Wifi);
    }
  delay(50);
}
void process(WifiData client) {
  // read the command
  String command = client.readStringUntil('/');
  // is "digital" command?
  if (command == "webserver") {
    WebServer(client);
  }
  if (command == "digital") {
    digitalCommand(client);
  }
}
void WebServer(WifiData client) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html>");
          client.println("<head> </head>");
          client.print("<body>");
          client.print("Click<input type=button onClick=\"var w=window.open('/arduino/digital/13/1','_parent');w.close();\"value='ON'>pin13 ON<br>");
          client.print("Click<input type=button onClick=\"var w=window.open('/arduino/digital/13/0','_parent');w.close();\"value='OFF'>pin13 OFF<br>");
          client.print("</body>");
          client.println("</html>");
          client.print(DELIMITER); // very important to end the communication !!! 
}
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("Pin D"));
  client.print(pin);
  client.print(F(" set to "));
  client.print(value);
  client.print(EOL);    //char terminator
}

4.OUTPUT

  • Open a browser and type: http://<IP>/arduino/webserver/ or http://<hostname>.local/arduino/webserver/
  • Click ON to go ON the LED 13:
  • Click OFF to go OFF the LED 13:
  • You can command the LED also from the web panel, as shown in the below image:

Note:

Type the IP address or the hostname.local/ of your board on the browser so you will access to the Arduino UNO WiFi web panel.

Code

WebServerC/C++
#include <Wire.h>
#include <ArduinoWiFi.h>
/*
on your borwser, you type http://<IP>/arduino/webserver/ or http://<hostname>.local/arduino/webserver/
 
http://www.arduino.org/learning/tutorials/webserverblink
 
*/
void setup() {
    pinMode(13,OUTPUT);
    Wifi.begin();
    Wifi.println("WebServer Server is up"); 
}
void loop() {
 
    while(Wifi.available()){
      process(Wifi);
    }
  delay(50);
}
 
void process(WifiData client) {
  // read the command
  String command = client.readStringUntil('/');
 
  // is "digital" command?
  if (command == "webserver") {
    WebServer(client);
  }
 
  if (command == "digital") {
    digitalCommand(client);
  }
}
 
void WebServer(WifiData client) {
 
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html>");
          
          client.println("<head> </head>");
          client.print("<body>");
                     
          client.print("Click<input type=button onClick=\"var w=window.open('/arduino/digital/13/1','_parent');w.close();\"value='ON'>pin13 ON<br>");
          client.print("Click<input type=button onClick=\"var w=window.open('/arduino/digital/13/0','_parent');w.close();\"value='OFF'>pin13 OFF<br>");
          
          client.print("</body>");
          client.println("</html>");
          client.print(DELIMITER); // very important to end the communication !!! 
 
}
 
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("Pin D"));
  client.print(pin);
  client.print(F(" set to "));
  client.print(value);
  client.print(EOL);    //char terminator
 
}

Schematics

Circuit
Img 20160325 095734

Comments

Similar projects you might like

Add WiFi to Arduino UNO

by Jefferson Paredes

  • 145,151 views
  • 58 comments
  • 93 respects

Connect to Blynk using ESP8266 as Arduino Uno wifi shield

Project tutorial by Nolan Mathews

  • 71,142 views
  • 11 comments
  • 50 respects

Control Arduino Uno Using ESP8266 WiFi Module and Blynk App

Project tutorial by Adithya TG

  • 5,254 views
  • 10 comments
  • 18 respects

WiFi Weather Station

Project in progress by Dan Fein

  • 31,494 views
  • 17 comments
  • 58 respects

Programming ATtiny85 with Arduino Uno

Project tutorial by Arjun Ganesan

  • 213,045 views
  • 74 comments
  • 184 respects
Add projectSign up / Login