Sunday, June 4, 2017

my First IOT Device

My First IOT Device

Hi There, Hope you all are enthusiast about building you connected (IOT) device.

In the previous post, we have uploaded a blink sample to ESP and learnt how to program the ESP (Wemos) module. In this post, we are going to connect the Wemos programmatically to the Wifi network and control the Wemos via WiFi.

Tools: Nothing new, same tools we used in the previous post.

(You may copy code and refer from my git repo WifiConnect.ino file, if you run out in an issue).

The process flow we are looking here is:

WiFiConnect processflow.png

Following are the code Steps you need to connect the Wemos module to Wifi Router, We will take the code base from the previous Blink code:

YouTube Video, Uploading shortly...

  1. Make sure Arduino is configured as mentioned in the previous post to program ESP modules.
  2. Open the Blink Example as shown in previous post and Save As (whatever you like) in your workspace.
  3. The first line requires the inclusion of a header file ESP8266WiFi.h, This includes everything you would require to connect the ESP to Wifi.
#include <ESP8266WiFi.h>
  1. Define constants to hold your router SSID and Password, and a variable value to hold current value of the LED.
const char* ssid = "ssid name";
const char* password = "ssid password";
int value = LOW;
  1. Create a server at port 80 to listen and respond.
WiFiServer server(80);
  1. In setup() procedure we would connect to wifi and setup our server.
    1. Create serial process, We need Serial to figure out the IP of this device when it is connected to Wifi. We will add a short delay so that serial monitor can initialize.
Serial.begin(115200);
delay(10);
    1. Not necessary but we will initialize the LED to be off.
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
    1. Connect the Wifi to the SSID with the password provided. Don’t forget to set the SSID and password, of your home wifi, in the constants we defined in step 4.
    2. Put a while and a delay to check if wifi connection is successful.
while (WiFi.status() != WL_CONNECTED) {
  delay(500);
}
    1. Now we will use the Serial to send the IP of the device to serial monitor. (You could you serial to send any debug data or info to serial monitor).
Serial.print(WiFi.localIP());
    1. Here we finish with the setup.
  1. The loop() process will continue to listen to client request and process the request as required.
    1. Check if the Server is created and available.
WiFiClient client = server.available();
if (!client) {
  return;
}
    1. Wait and listen to client.
while(!client.available()){
  delay(1);
}
    1. As soon as request is sent by client, read the string sent by client and flush the object.
String request = client.readStringUntil('\r');
client.flush();

    1. Parse the request string and set the LED value as requested by client.
if (request.indexOf("/LED=ON") != -1) {
  digitalWrite(LED_BUILTIN, HIGH);
  value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1){
  digitalWrite(LED_BUILTIN, LOW);
  value = LOW;
}
  1. Now you are all done with the code. From Tools menu open the Serial Monitor.
  2. Upload the the Code to Wemos using the Upload Toolbar button. ( I believe you remember how to upload from the previous post).
  3. Watch the Serial monitor for the IP address of the Wemos when connected.
  4. In you PC or any other device, open browser and type ‘http://<ipAddress>/LED=ON’
  5. Verify the LED on the board turns on.
  6. In the browser type http://<ipAddress>/LED=OFF
  7. Verify that LED on the board turns off.


So here you see that now the device is on your wifi network and accepts commands over http request. 

The next step is to Modify the code to respond the current status of the LED (pin) and create UI  

No comments:

Post a Comment