electronut Programming & Embedded Systems - by Mahesh Venkitachalam

A Simple IoT Project with the ESP8266 WiFi module

ESP8266 v01


This is one of my older ESP8266 articles. Please check out my more recent ESP8266 projects below:

The ESP8266 WiFi Module

The ESP8266 is a WiFi module that costs less than 5 USD. This makes putting your sensors on the net actually feasible. (Hooking up the $75 Arduino Yun to each of your sensors - not no feasible.) There’s a lot of excitement about this sensor on the Internet currently, and people have done an amazing job deciphering the obscure command structure of this device that comes from China. There seems to be three ways of using this module, in order of increasing complexity:

  1. Sending it AT commands from a computer via an USB to serial adapter. This is mostly useful for testing and setup.
  2. Interfacing with an Arduino or any other microcontoller and using this board as a peripheral.
  3. Programming the module directly and use its GPIO pins to talk to your sensors, eliminating the need for a second controller.

I’ve explored options #1 and #2 above, and that’s what I’ll be talking about here. If you are new to this whole thing, I recommend that you watch this video from Great Scott Labs first.

Setting up the ESP8266

The first thing you want to do with ESP8266 (as with any aliens) is to establish communication. For this, you hook up a USB to TTL adapter to the module, and talk to it using a serial port terminal application like CoolTerm. One thing to be careful about when you hook up this module is to remember that this module operates at 3.3 V - even the serial lines should not exceed this voltage. So here is how I hooked up my ESP8266:

ESP8266 circuit

In the above circuit, you can see that I used a 3.3 V regulator to power the board, and a resistor dividor on the RX line to keep the voltages on spec. The sessions below use CoolTerm. My board worked with a baud rate of 9600, since its firmware was already upgraded to You may need to try other baud rates - 115200, for instance.

[System Ready, Vendor:www.ai-thinker.com]

Now, you can send it AT commands. You can see the full command reference here. First let’s send “AT”:



Now let’s get the firmware version:




Now, let’s get the operation mode.




3 implies that we are in Standalone + Access Point mode. That’s fine. Now, let’s do something fun. Let’s list all the available WiFi access points.


+CWLAP:(4,"Deepak Mullick",-93,"80:a1:d7:77:a1:e4",6)


Now let’s connect to my WiFi network.



Now, let’s check if we actually got an IP address:



Whoohoo! We are on the network. We’ll actually make use of the Internet in the next section. Also, the above settings are now actually stored in the module. Even if you power it on and off, it will connect automatically to this network.

At this point, you can upgrade the firmware if you want. I used this Python script. You can read about the process at the electrodragon web page.

LM35 Temperature Plot using an Arduino

Now that we have put the module on the network, let’s make use of it by creating a small IoT (Internet of Things) device. I had written a previous post on plotting sensor data on ThingSpeak. This time, I’ll use an LM35 temperature sensor. I’ll talk to the ESP8266 using an Arduino Mini Pro clone. Here’s what the LM35 looks like:


In the code below, I use the SoftwareSerial library to talk to the ESP8266. I use the hardware Serial for debugging. (You could try the opposite.) I also assume that the ESP8266 is already setup to connect to the WiFi network.

// esp8266_test.ino
// Plot LM35 data on thingspeak.com using an Arduino and an ESP8266 WiFi
// module.
// Author: Mahesh Venkitachalam
// Website: electronut.in

#include <SoftwareSerial.h>
#include <stdlib.h>

// LED
int ledPin = 13;
// LM35 analog input
int lm35Pin = 0;

// replace with your channel's thingspeak API key
String apiKey = "T2RJXWQAVXG4ZV39";

// connect 10 to TX of Serial USB
// connect 11 to RX of serial USB
SoftwareSerial ser(10, 11); // RX, TX

// this runs once
void setup() {                
  // initialize the digital pin as an output.
  pinMode(ledPin, OUTPUT);    

  // enable debug serial
  // enable software serial

  // reset ESP8266

// the loop
void loop() {

  // blink LED on board
  digitalWrite(ledPin, HIGH);   
  digitalWrite(ledPin, LOW);

  // read the value from LM35.
  // read 10 values for averaging.
  int val = 0;
  for(int i = 0; i < 10; i++) {
      val += analogRead(lm35Pin);   

  // convert to temp:
  // temp value is in 0-1023 range
  // LM35 outputs 10mV/degree C. ie, 1 Volt => 100 degrees C
  // So Temp = (avg_val/1023)*5 Volts * 100 degrees/Volt
  float temp = val*50.0f/1023.0f;

  // convert to string
  char buf[16];
  String strTemp = dtostrf(temp, 4, 1, buf);


  // TCP connection
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += ""; // api.thingspeak.com
  cmd += "\",80";

    Serial.println("AT+CIPSTART error");

  // prepare GET string
  String getStr = "GET /update?api_key=";
  getStr += apiKey;
  getStr +="&field1=";
  getStr += String(strTemp);
  getStr += "\r\n\r\n";

  // send data length
  cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());

    // alert user

  // thingspeak needs 15 sec delay between updates

In the code above, I read the analog pin, compute the temperature, and send that information to a ThingSpeak channel via a GET request.

Here is how the circuit is hooked up. I am using a cheap Arduino Pro Mini clone for this experiment, and the battery is a rechargeable one meant for charging cell phones.

ESP8266 Arduino LM35

And here is the plot at thingspeak.com:

LM35 ThingSpeak Plot


I think this module is great value for the money. A very practical way of putting your sensors on the Internet. Although this module can be programmed directly, I’ll probably just use it as a peripheral due to the ease of using it that way.


  1. A nice introductory video from Great Scott Labs about the ESP8266.
  2. Seeed Studio article on Getting Started with ESP8266.
  3. electrodragon’s web page on the ESP8266.
  4. ThingSpeak website.
  5. My small project on using ThingsSpeak to plot sensor data.
  6. esptool.py - a Python script to update firmware.
  7. CoolTerm - nice serial terminal software.

If you liked this article, please consider supporting my efforts by purchasing my book.

Python Playground, published by No Starch Press, USA, is a collection of imaginative programming projects that will inspire you to use Python to make art and music, build simulations of real-world phenomena, and interact with hardware like the Arduino and Raspberry Pi. Do check it out!