A Guide for Getting Started with ESP8266 and Arduino IDE


What is ESP8266?

The ESP8266 is a low-cost Wi-Fi microchip with full TCP/IP stack and microcontroller capability produced by Shanghai-based Chinese manufacturer, Espressif Systems. The chip first came to the attention with the ESP-01 module, made by a third-party manufacturer, Ai-Thinker.

This small module allows microcontrollers to connect to a Wi-Fi network and make simple TCP/IP connections using Hayes-style commands. It comes with default firmware which supports AT commands. But rather than default firmware you can upload your own program  or firmware with Arduino IDE or any other IDE. If you upload a new code the original firmware will be erased.

It costs less than 5 USD and become one of the best choice for IoT projects in very short span of time.

Note: The ESP32 is the ESP8266 successor. It adds a CPU core, faster Wi-Fi, more GPIOs, and supports Bluetooth 4.2 and Bluetooth low energy. Additionally, the ESP32 comes with touch sensitive pins, and built-in hall effect sensor and temperature sensor.

Development Boards

ESP8266 is just the name of the chip, many companies have designed their own boards that use this chip, so there are many different ESP8266 boards on the market. If you don't know the difference between all these different models, you might have a hard time deciding on what board to buy.
Some boards have all kinds of features on-board to help developing ESP8266 hardware and software: for example, a USB to Serial converter for programming, a 3.3V regulator for power, on-board LEDs for debugging, a voltage divider to scale the analog input. If you're a beginner, I would definitely recommend a development board. It's easier to get started if you don't have to worry about all these things.

1. Bare-bones boards and boards without a USB interface

If you want to add an ESP8266 to a small project, or if you want a cheaper  board, you might want to buy a board that doesn't have features like a USB to Serial converter for programming, a 3.3V regulator for power, on-board LEDs etc. In that case, you can buy one of the many ESP-## modules developed by AI Thinker. They contain just the ESP8266 and the necessary components to run it.

The Bare-bone category has 2 sub-categories: boards with a 3.3V regulator on-board, and boards with just the ESP8266 and a flash memory chip, without 3.3V regulator.

If your board doesn't have a 5V to 3.3V regulator, buy one separately. You could use an LM1117-3.3 for example. The on-board 3.3V regulator of most Arduino boards is not powerful enough to power the ESP.

To program the ESP-## modules developed by AI Thinker, you'll need a USB-to-Serial converter. The FTDI FT232RL is quite popular, because it can switch between 5V and 3.3V. It is essential that the USB-to-Serial converter you buy operates at 3.3V. If you buy a 5V model, you will damage the ESP8266. Other than FTDI FT232RL, Arduino Uno is second most popular way to program ESP-##.

2. Development boards with a USB interface

If you want ESP8266 with extra features then choose NodeMCU, SparkFun ESP8266 Thing - Dev Board, SparkFun Blynk Board, Adafruit Feather HUZZAH with ESP8266 Wi-Fi etc. These boards will show up in Device manager (Windows) or in lsusb (Linux) as soon as you plug them in. They have a 3.3V regulator on-board, and can be programmed over USB directly, so you don't need any external components to get it working. The only thing you may need to do, is solder on some headers.

Getting Started with Cheaper Boards (ESP8266 ESP-01 developed by AI Thinker)

The ESP8266 ESP-01 boards come with following specifications:

  • 8Mbit external QSPI flash memory (1MByte)
  • 32-bit Tensilica Xtensa LX106 CPU running 80MHz
  • 3.3V supply (current can spike 300mA+, depending on mode)
  • PCB-trace antenna
  • 2 x 4 dual-in-line pinout
  • 14.3 x 24.8mm
  • 1.5g


Coming to the pin configuration, the ESP-01 module consists of 8 pins and these pins are VCC, GND, TX, RX, RST, CH_PD, GPIO0 and GPIO2.

The following image shows the pin diagram of the ESP-01 Module.


Pin Description

VCC: It is the power pin through which 3.3V is supplied.
GND: It is the ground pin.
TX: This pin is used to transmit serial data to other devices.
RX: The RX pin is used to receive serial data from other devices.
RST: It is the Reset Pin and it is an active LOW Pin. (ESP8266 will reset if the RST pin receives LOW signal).
CH_PD: This is the chip enable pin and it is an active HIGH Pin. It is usually connected to 3.3V.
GPIO0: The GPIO0 (General Purpose I/O) Pin has dual functions – one for normal GPIO Operation and other for changing the Mode (Programming/Normal). Start in Programming mode if receive LOW signal.
GPIO2: This is GPIO Pin.

ESP8266 ESP-01 Boot Mode Selection

ESP8266 WiFi Module has two modes of operation: Programming Mode and Normal Mode.

Programming Mode: In Programming Mode, you can upload the program or firmware to the ESP8266 Module. In order to enable the Programming Mode, the GPIO0 pin must be connected to GND.

Normal Mode: Leave GPIO0 pin as it to run in normal mode. In Normal Mode, the uploaded program or firmware will run normally.

Also, the RST (Reset) will play an important role in enabling Programming Mode. The RST pin is an active LOW pin and hence, it is connected to GND through a Push Button. So, whenever the button is pressed, the ESP8266 Module will reset.

Testing Default Firmware (Official AT Firmware) Code on ESP8266 ESP-01

To test default AT firmware on ESP8266, you need A USB to Serial TTY adapter to connect with ESP8266,  FTDI FT232RL may work but if you didn’t get the wiring right you will fried the ESP8266. One of the best and safe option is to use the Arduino Uno as a Bridge, bypassing everything in the uno and talking to the ESP8266 directly.

Arduino Uno have an on-board USB-to-Serial converter, you can use that. So heres how this works. You can connect the reset pin on the Uno to GND to bypass the Arduino boot loader (If you Arduino Uno is not genuine, It may work without connecting reset pin to GND), then connect TX to TX, RX to RX, GND to GND, VCC and CH_PD to 3.3v, and leave the GPIO0, GPIO2, RST pin as it (Normal Mode run the default firmware code inside ESP8266).

VCC to 3.3V
GND to GND
TX to TX of Arduino UNO
RX to RX of Arduino UNO
CH_PD to 3.3V


1. Accessing ESP8266 from Arduino Serial Monitor in the Arduino IDE

Once you have it wired up, you should be able to use the Arduino Serial Monitor in the Arduino IDE to access the ESP8266. Remember you’ll connect to the port the Arduino is on.
As TX is connected to TX and RX is connected to RX, it is just like extending the wires for TX and RX and Serial Monitor in the Arduino IDE will access the ESP8266 directly.



Next make sure you choose “Both NL & CL” then adjust the Baud rate. My module was talking on 115200 baud but I’ve heard other models on 9600.
At this point you should be able to send an “AT” command and get an “OK” response. WoHoo! you’re talking to the ESP8266.

Now send "AT+GMR" to retrieve the Firmware version and keep it for future reference.

To connect wifi use AT+CWJAP="SSID", "Password" For more commands please refer


2. Accessing ESP8266 from Arduino Uno code

Remember one thing here, to access ESP8266 from arduino you need to connect RX pin of arduino to TX pin of ESP8266 and TX pin of arduino to RX pin of ESP8266 as here we are not just extending the wire but arduino is directly communicating to ESP8266. Also the RST pin of arduino should not be connected to GND, it should be left as it.

To access ESP8266 from code the wiring will have the TX of the Uno connected to the RX on the ESP8266 and the RX on the Uno talking to the TX on the ESP8266, but don’t do it yet. PROBLEM theres only one serial on the Uno but we need it for connecting the IDE to the UNO since it works over the USB Serial on the same pins. We can’t use one Serial for both. Thankfully we can use some code called SoftwareSerial to make a Virtual Serial port on the Arduino. This means that instead of connecting to the TX/RX we’ll connect to another pin set and have SoftwareSerial send commands to those pins instead.



Ok lets use pin 10 as RX and pin 11 as TX of Arduino. That can be achieved by using SoftwareSerial library of Arduino.

VCC to 3.3V
GND to GND
TX to 10 of Arduino UNO
RX to 11 of Arduino UNO
CH_PD to 3.3V

First we need to setup the virtual serial for the module. We’ll include the library and define the variable

#include <SoftwareSerial.h>
#define RX 10
#define TX 11
SoftwareSerial esp8266(RX,TX);

From here on we’ll talk to the ESP8266 through “esp8266” and we’ll talk to the IDE through “Serial”. Let’s try it in our setup function

void setup() {
 Serial.begin(9600);
 esp8266.begin(115200);
 }

Note that we set the Serial connection to a 9600 baud rate and the esp8266 connection to a 115200 baud rate. Make sure to set your IDE Serial Monitor to the right baud and set the esp8266.begin value to the baud rate of the module you confirmed earlier.

OK ready for the whole code base? here is the code Basically there’s a helper function called “sendCommand” that takes a command, a timeout value and the expected result as input.

#include <SoftwareSerial.h>

#define RX 10
#define TX 11

int countTrueCommand;
int countTimeCommand;
boolean found = false;
SoftwareSerial esp8266(RX,TX);

void setup() {

  Serial.begin(9600);
  esp8266.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
}

void loop() {
   sendCommand("AT+CWLAP",20,"OK");
}

void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);//at+cipsend
    if(esp8266.find(readReplay))//ok
    {
      found = true;
      break;
    }
    countTimeCommand++;
  }

  if(found == true)
  {
    Serial.println("Success");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  found = false;
 }

Flashing Custom Program on ESP8266 ESP-01

ESP8266 ESP-01 is itself have a microcontroller with 2 GPIO pins, We can install our custom code or micro OS in this. The EspressIf itself provide various framework for ESP8266. Let's flash custom program first then we will check other options.

Even though ESP8266 WiFi Module is manufactured by Espressif Systems, the SoC is used by many third party manufacturers to implement their own custom modules. In my case, I have the ESP8266 ESP-01 Module by AI-Thinker.

Before proceeding into the process of custom program flash, it is important to configure the ESP8266 WiFi Module in Programming Mode and Arduino UNO as the USB to Serial Interface so GPIO0 pin should be connected to GND, RX to RX and TX to TX.

VCC to 3.3V
GND to GND
TX to TX of Arduino UNO
RX to RX of Arduino UNO
GPIO0 to GND
RST to GND through Push Button
CH_PD to 3.3V

Once the ESP8266 Module is powered ON, Push the RST button and open the Arduino IDE. In the Board options (Tools –> Board), select the “Generic ESP8266” Board. Select the appropriate port number in the IDE. Now, open new Sketch paste the code below, connect led on pin 2. Here, 2 means GPIO2 pin of the ESP8266 Module. Before you hit the upload make sure that GPIO0 is connected to GND first and then press the RST button.

void setup()
{
  pinMode(2, OUTPUT);
}

void loop()
{
  digitalWrite(2, HIGH);
  delay(1000);
  digitalWrite(2, LOW);
  delay(1000);
}

Now hit the upload button and the code will take a while to compile and upload. You can see the progress at the bottom of the IDE. Once the program is successfully uploaded, you can remove the GPIO0 from GND. The LED connected to GPIO2 will blink.

Updating/Flashing Official AT Firmware on ESP8266 ESP-01

The Official AT Firmware can be installed for two reasons: If you want to update the ESP8266 Firmware to its latest version or to completely install the new firmware (in case it was overwritten by any other program).

We will flash the official Espressif Systems’ firmware for ESP8266 Module using the official ESP8266 Flasher Tool provided by Espressif Systems which is the ESP8266 FLASH DOWNLOAD TOOL and Arduino UNO as the USB to Serial Interface.

Please note down the current Firmware version that you have retrieved by "AT+GMR" and make the circuit configuration same as during custom program upload.

1. Downloading the Latest ESP8266 Firmware

There are two types of SDK or Software Development Kits for ESP8266: Non-OS SDK and RTOS SDK. The Non-OS SDK, as the name suggests, isn’t based on any OS. Using this SDK, you can compile IOT_Demo and AT Commands. The RTOS SDK on the other hand is based on FreeRTOS. I’ll be using the Non-OS SDK (well, technically, the Firmware files based on the Non-OS SDK). The ESP8266 Firmware files are in the form of Binary Files i.e. .bin files.

Now since I will be using only the AT Command Set of the ESP8266, I will download only the Bin Files associated with the AT Commands. In order to download the AT Commands Firmware for ESP8266, visit the official link here or directly download from this link. You can download other version as well as per your choice.

2. Downloading the ESP8266 Flash Download Tool

You can directly download ESP8266 Flash Download Tool from this link.

3. Installing the Firmware in ESP8266

After opening the ESP8266 Flash Download Tool, you need to select the firmware files that we need to install. You need to upload four files at four different addresses. The following table will give you the list of files and their corresponding addresses.

NOTE: This list is for Firmware over the Air (FOTA) Flash Memory Mapping for ESP8266 Modules with 1MB of Flash. For other flash sizes, please refer to the ESP8266 Getting Started Guide from this link.

File
Address in Flash Memory
boot_v1.7.bin
0x00000
user1.1024.new.2.bin
0x10000
esp_init_data_default.bin
0xFC000
blank.bin
0xFE000


In the ESP8266 Download Tool, in the SPI Download Tab, select the necessary files in the “Download Path Config” option by click on the (…) button. Also, add the corresponding addresses in the space provided.

After selecting the four files and filling in the Addresses, set the crystal frequency to 26MHz (it is usually 26MHz but in case your ESP8266 board has a different crystal, enter that value). Next select the flash size. Since I have a 1MB flash on-board, I have selected the 8Mbit (which is equal to 1MB) flash size option.

Finally, select the correct COM Port and set the baud rate to 115200. Make sure that all the serial terminal are closed. Now click on START button. The flashing of the firmware in ESP8266 should begin. If you notice in the ESP8266 Firmware Flash Tool, the moment you click on START, you will get two MAC Address as AP and STA and also information about the Module in the DETECTED INFO section.

If everything goes well, the ESP8266 Module must be updated with new firmware. If there is any error, disconnect the USB cable, connect it once again, and after clicking on START, push the Reset button of the ESP8266.

You will get a FINISH message on the Tool and you can close the tool after this. Disconnect the GPIO0 from GND and reset the device. Open the serial monitor of Arduino once again and check for new firmware.


Comments and Questions welcome! Thanks for reading.
Read More

Footprint of IoT Powered and Filter Free Air Purifiers

The presence of dust in homes, offices, and other human environments are unavoidable. In fact, according to the Environmental Protection Agency, indoor air can be 2 to 5 times more polluted than outdoor air. This airborne pollution contributes to minor annoyances such as itchy eyes, sneezing, and headaches to human beings. Worst still, it can be a major contributing factor to severe allergies, life-threatening asthma.
To solve this problem, a working air purifier machine well address this issue. there are various IoT based air purifiers available in market, Xiaomi beat all of them with low price and advance feature.

IoTMania - IoT Based Air Purifirs

Xiaomi launched their IoT based air purifier. It can clean the air in a 23-square-metre room in 10 minutes.
The Mi Air Purifier can be controlled by a smartphone and uses a brush-less DC motor for better performance and less friction. It delivers clean air at the rate of 330 m3/h. Xiaomi promises that it can clean 99.7 percent of small PM 2.5 particles in a room.
The air purifier, which is WiFi linked, has different modes that can be changed through an app for the device from your phone, tablet or desktop. It can also detect your presence and can switch off when you leave the house. It also learns your sleeping patterns and changes the modes accordingly.
Coway’s innovative technology, which combines its core competitiveness “care,” Big Data and the Internet of Things (IoT), is also attracting attention from the global and domestic markets.
Coway’s innovative technology, which combines its core competitiveness “care,” Big Data and the Internet of Things (IoT) with air purifiers, is also attracting attention from the global and domestic markets.
Coway Dual Power Air Purifier IoCare checks the levels of fine dust, ultra-fine particles, carbon dioxide and gas, which are the major factors that affect the air quality of households, in real time. It then offers customized solutions, such as ventilation and air cleaning, via smartphones in accordance with real time household air quality. Instead of changing filters all at once, the product also provides personalized filters, including yellow dust filters, old house filters and new house filters, according to pollutant types and concentration levels, which vary with the lifestyles of households.

What's Not So Cool?

The filter is the major component on the device and that will be an additional cost that the consumer will have to bear with every three to 4 months. The filter costs Rs 2,499 and this will be a recurring cost for the consumers.

What's Next?

Technologies like electrostatic precipitator and electrostatic ionization have potential to remove the filter dependency completely. Many companies trying to make such air purifiers. Gurgaon based Bitspi is in the way to launch one of kind intelligent, fully connected indoor working air monitoring, control and purification system. The new air cleaning system is potentially capable of detecting and combating airborne germ, chemical, smoke and other particle attacks, and also no dependency on filters.
Read More

JavaScript microcontroller you can program wirelessly - Puck.js

Many companies have designed their own Bluetooth beacon mostly to find objects such as your keys or pets. Puck.js is also a Bluetooth 4.2 LE beacon, but has many more features including compatibility with the upcoming Bluetooth 5.0 standard, and some GPIOs programmable with JavaScript (Espruino implementation), and corresponding visual programming editor.

Puck.js beacon supports Google’s Eddystone and Apple’s iBeacon to advertise itself to other devices, as well as Web Bluetooth currently supported by Chrome, but with other browser soon to follow. The beacon also supports both slave and master modes. You can program the device by going to Espruino website with Chrome browser, connecting your Puck.js, and start programming by dragging blocks around, or directly using JavaScript, using one of ~100 example projects or from scratch.

Puck.js is a Nordic nRF52832 SoC beacon that can be programmed via Javascript. The Open Source JavaScript interpreter Espruino is installed in the beacon that allows the user to program what ends up in the advertising data based on the board’s NFC, temperature and magnetometer sensors. The 4 leds and 6 GPIO can also be controlled. Being advertising-data programmable it will support Eddystone, iBeacon or any other custom protocol you might wish to invent.

The hardware, software, libraries and documentation are all Open Source that future-proofs any ideas based on this beacon should the developer be no longer be able to support the product or if your design needs to move outside the original hardware and software scope.puckprogramming
Read More

IoT - Business opportunities in India

The Internet of Things enables new business opportunities by connecting your devices, sensors, cloud services and data to create the Internet of your things. The market value of IoT is massive as the market value is directly proportional to the number of devices that is 1.9 billion today and 9 billion by 2018.

Here are the top way to wealth creation by IoT.

Connected advertising and marketing:

The digital advertising industry is already undergoing something of a transformation. While facing an existential threat from the ascendant ad blocker, advertisers are leading the charge to create ever more tailored advertising experiences. 
The proliferation of connected devices under the internet of things (IoT) promises to disrupt the industry further by increasing the number of opportunities for advertisers to learn about and sell to consumers. British Airway’s smart billboard ad in Piccadilly Circus, London wowed passersby in 2012 as a small boy reacted to overhead planes in real time, promoting the flights and their current destinations and Lexus Australia caused a stir down under with its campaign to attract car drivers with personalized messages on an overhead billboard while one US retailer Forever 21 used an ingenious thread screen to turn customer selfies into threaded pictures in store.

Intelligent traffic management systems:

The traffic experts at INRIX report that British drivers spend 30 hours per year in traffic jams. For German drivers it’s 35 hours. In the USA, the San Francisco Municipal Transportation Agency says that 30 percent of the city’s traffic congestion is caused by drivers looking for a place to park. And the Texas Transportation Institute estimates the annual cost of traffic congestion in the United States alone adds up to $87.2 billion in wasted fuel and lost productivity. Much of this congestion could be addressed with smart Internet of Things technologies that already exist, such as intelligent traffic systems. 
Machina research, in a paper prepared for the GSM Association, sees $100 billion in revenue by 2020 for applications such as toll-taking and congestion penalties. A related revenue source will be smart parking-space management, expected to drive $30 billion in revenue.

Waste management systems: 

As a major application field of IoT, waste management has become one such issue. The absence of efficient waste management has caused serious environmental problems and cost issues. Therefore, in this paper, an IoT-based smart garbage system (SGS) is proposed to reduce the amount of food waste. In an SGS, battery-based smart garbage bins (SGBs) exchange information with each other using wireless mesh networks, and a router and server collect and analyze the information for service provisioning. Furthermore, the SGS includes various IoT techniques considering user convenience and increases the battery lifetime through two types of energy-efficient operations of the SGBs: stand-alone operation and cooperation-based operation. 
In Cincinnati, residential waste volume fell 17% and recycling volume grew by 49% through use of a "pay as you throw" program that used IoT technology to monitor those who exceed waste limits.

Industrial uses: 

It including Internet-managed assembly lines, connected factories, and warehouses, etc, sees a huge revenue by 2020. 

Read More