WiFi Experiments

The WiFi_RobotsForFun library has been created to simplify the code for working with the wifi. It contains functions for connecting to the wifi with various options such as using DHCP to obtain an IP address automatically or to specify a given static IP address, use a given device name, getting and saving the ssid and password from the EEPROM, and webpage for setting up the wifi connection and information about the wifi connection. Many examples are also included. Look at the file WiFi_RobotsForFun.h inside this library to see all the functions available and to learn how the code is written. The following is a sample list of some of the functions found in the library:

The following are some of the examples from this library:

1 A web server running on the ESP8266 for turning on and off the built-in LED.
  • Download and install the WiFi_RobotsForFun library if you have not already done so.
  • You also need to download and install the DateTime_RobotsForFun library.
  • Open the BasicWebServer example under
    Examples | WiFi_RobotsForFun | BasicWebServer
  • Select the ESP8266 board from the Arduino v1.6.7 IDE menu
       Tools | Board | Generic ESP8266 Module
  • If you are using the ESP8266-01 then you need to uncomment the line #define ESP8266_01 in the code.
  • Select the correct COM port that your wifi module is connected to.
  • Open up the serial monitor and set the baud rate (bottom right corner) to 115200.
  • Click the Upload button.
/* //////////////////////////////////////////////////////////
 * A Basic Web Server using the ESP8266
 * to turn on and off a LED
 */
 
//////////////////////////////////////////////////////////////
//// VCC voltage stuff
  ADC_MODE(ADC_VCC);

//////////////////////////////////////////////////////////////
//// status LED stuff
//#define ESP8266_01  // define this when using the ESP8266-01
#ifdef ESP8266_01
  #define statusLed 1     // for ESP8266-01
#else
  #define statusLed 2     // for ESP8266-12
#endif

//////////////////////////////////////////////////////////////
//// NTP clock stuff
#include "DateTime_RobotsForFun.h"

//////////////////////////////////////////////////////////////
//// WiFi stuff
#include "WiFi_RobotsForFun.h"

//////////////////////////////////////////////////////////////
//// HTML webpage 
void indexHTML() {
  String msg = "";

  // get url arguments and process the command
  // do it here to see the changes reflect in the webpage
  // should not have long time delays otherwise the webpage will be very slow to load
  if (server.hasArg("LED")) {
    if (String(server.arg("LED")) == "ON") {
      digitalWrite(statusLed, LOW);  // turn on LED
      ledStatus = true;
      Serial.println("turn on LED");
    } else if (String(server.arg("LED")) == "OFF") {
      digitalWrite(statusLed, HIGH);  // turn on LED
      ledStatus = false;
      Serial.println("turn off LED");
    }
  }

  msg += "<!DOCTYPE html>";
  msg += "<head>";
  msg += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"; // keep fonts consistent between devices
  
  msg += "<title>ESP8266 Demo</title>";
  msg += "  <style>";
  msg += "    body { background-color: #ffffff; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }";
  msg += "  </style>";
  msg += "</head>";
  
  msg += "<body>";
  msg += "<center><font size = \"3\"><b>Basic Web Server</b></font><br>";
  msg += "This is an ESP8266 demo webserver that allows you to turn on and off a LED.";
  msg += "<br>" + formattedTime();
  msg += "</center>";
  msg += "<hr><br>";

  msg += "Led is currently ";
  if (ledStatus) {
    msg += "<b>on</b>";
    msg += "<br>Turn led ";
    msg += "<a href=\"/\?LED=OFF\"><button style=\"font-size:16pt\">OFF</button></a>";
  } else {
    msg += "<b>off</b>";
    msg += "<br>Turn led ";
    msg += "<a href=\"/\?LED=ON\"><button style=\"font-size:16pt\">ON</button></a>";
  }

  // another way to create the button
  msg += "<br><br>Another way to do it:";
  msg += "<form style=\"font-size:12pt\">";
  msg += "Click ";
  msg += "<input type=\"submit\" name=\"LED\" value=\"ON\" style=\"font-size:16pt\" >";
  msg += " to turn led on";
  msg += "<br>Click ";
  msg += "<input type=\"submit\" name=\"LED\" value=\"OFF\" style=\"font-size:16pt\" >";
  msg += " to turn led off";
  msg += "</form>";

  // wifi setup and info links
  msg += "<br><hr>IP address: ";
  if (wifiIsConnected) {
    msg += "<a href=\"/\">" + WiFi.localIP().toString() + "</a>";
    msg += "<br>Wifi setup and info: ";
    msg += "<a href=\"/setup\">" + WiFi.localIP().toString() + "/setup</a>";
  } else {
    msg += "<a href=\"http://1.2.3.4\">1.2.3.4</a>";
    msg += "<br>Wifi setup and info: ";
    msg += "<a href=\"http://1.2.3.4/setup\">1.2.3.4/setup</a>";
  }

  msg += "</body>";
  msg += "</html>";

  // send webpage to browser
  server.send( 200, "text/html", msg);

  // get url arguments and process the command
  // changes here will not be reflected in the current webpage
  // can have long time delays since the webpage has already been sent back to the client

}


void setup() {
  Serial.begin(115200);
  pinMode(statusLed, OUTPUT);

  setupAP();  // setup access point using default ssid "WiFi setup ESP8266-xxxx" and no password
  
  setupWiFi(); // use DHCP, ssid and password from EEPROM, default device name, and default blocking if cannot connect to wifi
  
  setClock(PST);
  
  // update systemStartTime and systemStartCount
  systemStartTime = now();
  EEPROM.get(EEPROM_ADDRESS_systemStartCount, systemStartCount);  // get systemStartCount from EEPROM
  systemStartCount++;
  EEPROM.put(EEPROM_ADDRESS_systemStartCount, systemStartCount);
  EEPROM.commit();
}


void loop() {
  flashActivityLed();     // show system is alive
  syncClock();            // need to call this frequently to sync the clock
  server.handleClient();  // handle web client request
}
  • After the program is uploaded successfully, it will immediately run and you should see something like this telling you that the ssid and/or password is invalid if this is the first time that you are running this.
  • Also you will notice that the built-in LED on the module will blink slowly once every three seconds (if the wifi connection failed).
ESP8266 webserver monitor output
The first time you run this program, it doesn't know the ssid and password for the wifi that you want to connect to, so you need to browse to the wifi setup page to configure this.
  • Connect your computer or mobile device to the wifi name printed on the serial monitor. In the example, it is WiFi setup ESP8266-D6D7. The last four characters will be different.
  • Bring up a browser and browse to the URL address 1.2.3.4/setup
  • You should see this webpage.
  • Enter your wifi ssid and password, then click the Submit button.
The ESP8266 WiFi setup and info page
The server will try to connect to your wifi again using the ssid and password that you entered.
  • If the wifi connection is successful then you should see this on the serial monitor.
  • Note the IP address that has been assigned by your internet router to the ESP8266 server. In this example it is 192.168.137.100. You will have a different one.
  • Also you will notice that the built-in LED on the module will blink twice every three seconds to show that the wifi is connected.
ESP8266 webserver connected
Using your mobile device (or your computer) you can do either one of the following to browse to the webserver:
  1. Connect your mobile device's wifi to the access point
      WiFi setup ESP8266-xxxx.
    • Bring up a browser and browse to the URL address 1.2.3.4 for the main webpage,
    • or to 1.2.3.4/setup for the wifi setup and info webpage.
    Note that the access point will automatically turn off after 10 minutes.
  2. Connect your mobile device's wifi to the same wifi network that the ESP8266 is connected to.
    • Bring up a browser and browse to the URL address shown in the serial monitor output or in the wifi setup and info webpage. In this example, the URL is 192.168.137.100 for the main webpage,
    • or to 192.168.137.100/setup for the wifi setup and info webpage.




    Note: You can browse to this webserver using any device and from anywhere in the world as long as you have correctly configured the internet router that your ESP8266 webserver module is connected to. Normally, your internet router has a firewall between the outside world and your local network. So you need to open up a port on your router and port forward the network traffic from the outside to where your ESP8266 webserver is connected to. How to do this, however, is beyond the scope of this tutorial. You can find out more about port forwarding here.
ESP8266 webserver setup and info page
Controlling the built-in LED on the module from the main webpage.
  • Bring up a browser and browse to either the URL address 1.2.3.4 if you are connected to the ESP8266 access point, or to 192.168.137.100 (replace this IP address with your IP address) if you are connected to the same wifi network as your ESP8266.
  • You can turn on and off the built-in LED on the ESP8266 module by clicking the ON and OFF buttons on the webpage.
Basic ESP8266 webserver for turning on and off the built-in LED
2 Here is a barebone webserver to control the built-in LED on the ESP8266 module. Replace "your ssid" and "your password" in the code with the ssid and password of the WiFi network that you want to connect to. This webserver does not use the WiFi_RobotsForFun library or the DateTime_RobotsForFun library.
3 Here is a program to synchronize to the network clock using the Network Time Protocol (NTP).
  • Download and install the DateTime_RobotsForFun library if you have not already done so.
  • Open the DateTimeAPWeb example under
    Examples | WiFi_RobotsForFun | DateTimeAPWeb
  • Upload and run the program.
  • Follow the instructions in step 1.
4 Here is a webserver that tells you whether a switch is on or off.
  • Connect the push button switch to pin D12 on the ESP8266-12, or to pin D2 on the ESP8266-01.
  • Connect the other end of the switch to GND.
  • Open the SwitchAPWeb example under
    Examples | WiFi_RobotsForFun | SwitchAPWeb
  • Upload and run the program.
  • Follow the instructions in step 1.
Push button
5 Here is a webserver that tells you whether there is motion or not using the AM312 mini motion sensor.

The AM312 mini motion sensor cannot be connected directly to the ESP8266 because it will give a lot of false positive signals. Need to use a 2N3904 NPN transistor in between. Cannot be replaced with the HC-SR501 motion sensor because it will still gives many false positive signals. Refer to the transistor connections for how to work with a transistor.
  • Connect the E (emitter) of the transistor to GND.
  • Connect the B (base) of the transistor to the motion sensor output signal through a 22K ohm resistor.
  • Connect the C (collector) of the transistor to pin D13 on the ESP8266-12, or to pin D2 on the ESP8266-01.
  • Open the MotionAPWeb example under
    Examples | WiFi_RobotsForFun | MotionAPWeb
  • Upload and run the program.
  • Follow the instructions in step 1.
AM312 mini motion sensor
6 Here is a webserver to control a WS2812 RGB LED ring.
  • Connect the DI pin on the WS2812 RGB LED ring to pin D12 on the ESP8266-12, or to pin D2 on the ESP8266-01.
  • Connect GND to GND.
  • Connect 5V to 5V. Becareful that the LED ring uses 5V but the ESP8266 uses 3.3V.
  • Download and install the WS2812 library.
  • Open the RainbowLightsAPWeb example under
    Examples | WiFi_RobotsForFun | RainbowLightsAPWeb
  • If you are using the ESP8266-01 then you need to uncomment the line #define ESP8266_01 in the code.
  • Upload and run the program.
  • Follow the instructions in step 1.
8 RGB LED ring
7 Here is a webserver to control a 2x16 LCD display.
You need to use the ESP8266-12 WiFi module for this project since it requires six I/O signals.
  • Connect RS on the LCD to pin D5 on the ESP8266-12
  • Connect E on the LCD to pin D4 on the ESP8266-12
  • Connect D4 on the LCD to pin D16 on the ESP8266-12
  • Connect D5 on the LCD to pin D14 on the ESP8266-12
  • Connect D6 on the LCD to pin D12 on the ESP8266-12
  • Connect D7 on the LCD to pin D13 on the ESP8266-12
  • Connect R/W on the LCD to GND
  • Connect Vo on the LCD to 1K Ω resistor (brown-black-red) then to GND
  • Connect A on the LCD to 5V
  • Connect K on the LCD to 150 Ω resistor (brown-green-brown) then to GND
  • Connect VSS on the LCD to GND
  • Connect VDD on the LCD to 5V
  • Open the LCDWebClock example under
    Examples | WiFi_RobotsForFun | LCDWebClock
  • Upload and run the program.
  • Follow the instructions in step 1.
2x16 LCD display
8 Here is a webserver to control a servo motor.
  • Connect the yellow wire on the servo motor to pin D12 on the ESP8266-12, or to pin D2 on the ESP8266-01.
  • Connect the brown wire on the servo to GND
  • Connect the red wire on the servo to +5V
  • Open the ServoWeb example under
    Examples | WiFi_RobotsForFun | ServoWeb
  • If you are using the ESP8266-01 then you need to uncomment the line #define ESP8266_01 in the code.
  • Upload and run the program.
  • Follow the instructions in step 1.
Servo motor
9 Here is a webserver to control a stepper motor.
You need to use the ESP8266-12 WiFi module for this project since it requires four I/O signals.
  • Open the monitor.
  • Copy this program to the Arduino IDE editor.
  • Change the ssid and password in the code to your correct ssid and password.
  • Make sure D2 is not connected to anything.
  • Reset the ESP8266 module by disconnecting and connecting VCC.
  • Upload the program to the ESP8266 module.
  • On the monitor, see what IP address the webserver is assigned to.
  • Browse to the IP address.
  • Connect IN1 on the stepper motor controller to pin D16 on the ESP8266-12.
    Connect IN2 on the stepper motor controller to pin D14 on the ESP8266-12.
    Connect IN3 on the stepper motor controller to pin D12 on the ESP8266-12.
    Connect IN4 on the stepper motor controller to pin D13 on the ESP8266-12.
    Connect the - on the stepper motor controller to GND
    Connect the + on the stepper motor controller to +5V
Stepper motor
11 Here is a webserver that allows you to control a relay.
The relay is just a switch, so you can use the relay to turn on and off any high power electronic device such as your room light.
  • Open the monitor.
  • Copy this program to the Arduino IDE editor.
  • Change the ssid and password in the code to your correct ssid and password.
  • Make sure D2 is not connected to anything.
  • Reset the ESP8266 module by disconnecting and connecting VCC.
  • Upload the program to the ESP8266 module.
  • On the monitor, see what IP address the webserver is assigned to.
  • Browse to the IP address.
  • Connect the IN1 on the relay module to pin D2 on the ESP8266.
    Connect VCC and GND on the relay module to 5V and GND, respectively.

  • Here are the instructions for connecting the relay to a high voltage light.
Relay
12 In order to control any high-power devices such as a speaker, motor or relay using the ESP8266 (which is a 3.3V device) we need to use two transistors as shown on the right.

The first PNP 3906 transistor acts as a switch. It is turned on with a negative low signal from the ESP8266 to the Base. When this transistor is turned on, the positive output signal from the Collector of this transistor will turn on and drive the second NPN 3904 or TIP120 transistor. This second transistor acts as an amplifier to drive a high-voltage device such as a speaker or a motor. Transistor pins          Darlington pins
PNP 3906 or NPN 3904 pins            NPN TIP120 pins

See here for more information about transistors.
Two-transistor connection
21 The Arduino or USB-to-serial adapter is only needed for programming the ESP8266 module. Once the ESP8266 module is programmed, you can run the program on the ESP8266 module all by itself by just connecting it to 3.3V power and GND. Remember, you CANNOT connect the ESP8266 module to 5V. You can use this 5V to 3.3V converter to get the 3.3V power from a USB 5V source. 5v to 3.3v converter
22 Here are other USB adapters for programming and running the ESP8266 module.
23 Here's a very good (much more than a) beginner's guide to the ESP8266.