Building My First IOT Device

Date: 26/07/2017 | Arduino, ESP, ESP-01, ESP8266, IOT, Programming

This tutorial will show you how to flash an ESP-01 chip (or ESP-8266) and program your first IOT device using a 3.3 FTDI breakout board, a USB cable, some wires and a few other little bits and pieces. If you have ever tried doing this yourself you might notice how bullsh*t painful the process is, especially when you start flashing the firmware. There are so many guides available to you with just a google search (this is going to be another one of them), but I felt like they were never clear and some were overly complicated or just never worked. So, let’s try and sum up the process in a few palatable, easy to chew steps. Before you get started you’re going to need a few things, these are :

Hardware you are going to need:

Software you are going to need:

Now it's super important that you get a proper / genuine FDTI programmer. My brain almost melted over a cheap counterfeit version that I brought which kept dying half way through the flashing process . Only buy genuine / reputable components or do not even attempt this.

Getting started with the flashing your ESP-01 module

1. Wire your FTDI programmer to the ESP module following the fritzing diagram below. Figure 1.0 – ESP flashing setup

Notice how the blue wire is floating? This wire is connected to the reset pin and will come in handy later on in the process 2. Make sure you have downloaded both the ESPFlash tool and a copy of the AT firmware (Electro Dragon website). At this point it’s a good idea to connect up your FTDI programmer to your computer via USB. Your configuration should look a little something like below. 3. Run the ESPFlashDownloadTool.exe software and click on "ESP8266 download" button. You should see a screen similar to below. Make sure you set the download config path to the location of your AT firmware and manually set the memory address to 0x00000. Figure 1.1 – Flashing tool setup NOTE: Select the correct COM port of your FTDI programmer  (should auto appear when you connect via USB) and default the baud rate to 115200. Also set the SPI mode to DIO. 4. Now it’s time to get flashing. Remember that floating blue wire in the first frizting diagram? Using your breadboard tap ground on your breadboard ground rail. This will reset the ESP module and prep it for flashing. 5. Now just click start and it should take a good few seconds to complete the process. HINT: If that flash sits in idle and nothing happens. Just hit the reset (ground rail) a few more times until it kicks in (it’s weird and seems a little flaky I know, but it works) Done and dusted. Your ESP module should be flashed with the latest AT firmware from AI thinker.

Now that you’re at this point lets give it a little test

1.  Remove the GPI01 wire from ground. This will take the ESP module out of programming mode. 2. Run ESPlorer and open a connection to your ESP module using AT commands. Make sure you connect to it on baud rate 115200. 3. Again with the reset pin! Tap the ground to reset the unit and it should now be seeing some output in your console. Type AT+GMR in the command window and hit send. Your console should now be provided with the firmware version, that will signal that the module is now responding. See the example below: Figure 1.2- ESPlorer AT command test Perfect! You’ve now successfully completed your first ESP module flash. Give yourself a pat on the back and break out the stogie.

Now it’s time for the fun stuff! Let’s get started on your first IOT device.

1. Repeat the configuration shown in figure 1.0 (although if you’re following this tutorial you should already have the correct wiring) TIP: The Arduino is used as a rather expensive power source. You can swap this out for another 5v source if needed. 2. Open your Arduino IDE > Preferences and add the following line. http://arduino.esp8266.com/stable/package_esp8266com_index.json . See example below: Figure 1.3 – Arduino IDE additional boards 3. Create a new project file and paste the code below :

#include < ESP8266WiFi.h > 
// your wifi network credentials   

const char * ssid = "********";  

const char * password = "************"; 

// global variables  

WiFiServer wifiServer(80);  

int ledPin = 2;  

void setup() { // set pin modes   

    pinMode(ledPin, OUTPUT);  

    digitalWrite(ledPin, LOW); 
   // connect to wifi server  

    Serial.begin(9600);  

    Serial.print("Establishing connection:");  

    WiFi.begin(ssid, password);  

    while (WiFi.status() != WL_CONNECTED) {  

        delay(1000);  

    }  

    Serial.println("Now Connected"); 

  // Start Wifi server  

    wifiServer.begin();  

    Serial.print("URL: ");  

    Serial.print("http://");  

    Serial.print(WiFi.localIP());  

    Serial.println("/");  

}  

void loop() { 
    // Get parameters from URL  
    int x = LOW; // Wait for client connection  
    WiFiClient remoteClient = wifiServer.available();  
    if (!remoteClient) { 
    // no connection yet; continue listening  
        return;  
    } 
   // Client now connected  
    while (!remoteClient.available()) {  

        delay(10);  
    } // Get requested URL  

    String clientRequest = remoteClient.readStringUntil('\r'); 

    remoteClient.flush();  

    if (clientRequest.indexOf("/LEDPIN=OFF") != -1) {  
        digitalWrite(ledPin, LOW);  
        x = LOW;  
    }  

    if (clientRequest.indexOf("/LEDPIN=ON") != -1) {  

        digitalWrite(ledPin, HIGH);  

x = HIGH;  

    } // Return the response  

    remoteClient.println("HTTP/1.1 200 OK");  

    remoteClient.println("Content-Type: text/html");  

    remoteClient.println("");  

    remoteClient.println("<!DOCTYPE HTML>");  

    remoteClient.println("<html>");  

    remoteClient.println("<a href=\"/LEDPIN=ON\">GPIO2 PIN ON</a> | ");  

    remoteClient.println("<a href=\"/LEDPIN=OFF\">GPIO2 OFF</a>");  

    remoteClient.println("</html>");  

    delay(50);  

}  

3. Now select the correct board by clicking on tools > board as shown below:


Figure 1.4 – Programming ESP module 4. Because you are now programming the ESP module again, you will need to ground the GPI01 wire. 5. Compile the code by clicking “Verify” and wait for the process to complete. 6. Click the "upload" button to program the ESP module. You may need to reset the ESP module a few times in order to get this working correctly. But usually it works first go for me. 7. Open the serial monitor under tools and set the baud rate to 9600. 8. Again.. Hit the reset button. The module should then restart and provided everything went well, it will give you a URL to connect to. 9. Copy and paste that URL into your browser and click go. You should see two buttons. One to turn the GPIO LED on and off. Try clicking and powering the LED light bulb Congratulations! You’re done. Welcome to the world of IOT.

One last thing before you go.

If you want to run the program independently of the FTDI programmer and I’m assuming you will. You will need to configure the circuit below. Keep in mind that I am using the Arduino as the power source and you can use another power if you have one available. Your ESP module cannot exceed 3.3v or you will run the risk of damaging or destroying it. You will also notice I am using a LD117V33 voltage regular, this will bring down the Arduino’s 5 volts output to 3.3 volts safely. NOTE: At this point you can pull the TX and RX pins. Figure 1.5 - Independant Circuit   Anyway. I hope this helps cut your teeth and you’re well on your way to building your own projects. I’d love to hear your comments.


Posted by: Shaun Case
Position: Technical Consultant
Date: 26/07/2017

Share On: