ESP8266 IoT programming

ESP8266 WiFi module

The ESP8266 WiFi module converts TCP/IP data to UART. This comes in handy if you want to connect a microcontroller to the internet. This module only costs a few euro’s and has a lot of functionallity. If you are interested in this module then i hope this blog will be usefull and will save you some time setting up the module. I’m planning to create an Android application for my phone to control different devices like lamps, computer, media set, etc, in my house. To get to this I already created a lot of software for the microcontroller (see my other blogs) so now it’s time to explore the ESP8266 and get this module working.

The ESP8266 hardware

There are a few things to keep in mind when you connect the ESP8266 to your computer.

  1. The module needs a lot of current specially when booting up. Use a power supply that can deliver at least 400mA. Symptoms of to less current is a blinking blue led, but you are able to communicate with the module or the unit keeps rebooting.
  2. The ESP8266 requires 3.3V for the rx and tx lines and your computer with rs232 uses about 12V for rx and tx. So make sure you level to the correct voltage, see example below. If you use more then 3.3V on the module it will burn!
  3. The module needs a 3.3V power supply and the microcontroller (Atmegaa328) needs 5V. A voltage regulator is needed at least for the 3.3V power supply and must be able to deliver at least 400mA.
ESP8266 setup
ESP8266 connected to a computer schematic

 

Above is the schematic how to connect the ESP8266 to a computer. Left is the module itself. To get the 3.3V power for the unit I used this  voltage regulator. It can provide 800mA with cooling. In this project no cooling is needed, because the module consumes less then 400mA.  The middle module is a level converter to convert from 3.3V to 5V and vice versa. It is needed for the Atmega328 microcontroller, but for testing and configuring the module on a computer only it is not needed yet and can be by-passed. For later testing it is nice to connect the microcontroller and a computer at the same time to see what is going on, but feel free to leave the logic level converter out for now and connect the ESP8266 directly to the rs232 logic converter. As I don’t have an rs232 port on my laptop I used an USB to rs232 converter.

Communicating with the ESP8266

The ESP8266 comes with different default baudrate settings. The best way to figure out what is the correct baudrate is trying them all… I used TeraTerm client to control and test the module and in my case the baudrate is default 115200 bps. The port number can be found at the device settings so in my case it’s com9.

Select the correct com port.

 

The module requires an “enter” after each AT command. This can be set in the terminal setup by changing the transmit new-line to CR+LF.

Tera term settings
Tera term port settings.

 

There is a complete list with commands for the module available here. For now I will connect the unit to my home WiFi modem and save the settings. After this it will always connect automatically and I don’t have to program the microcontroller to this. Another thing todo is to configure the module as a server and specify a port to listen to, but this will be done my the micrcontroller too so it’s only for testing now.

Commands to set up a permanent WiFi connection

 

AT+GMR – show the current firmware version. Just to check if the communication is working.
AT+CWMODE=3 – set’s the module to “SoftAP+Station mode”.
AT-CWLAP – gives a list with WiFi AP’s in the neighbourhood. I blacked it out a bit for security reasons.
AT+CWJAP_DEF=”TP-LINK_A6A8″,”password” – connects the module to the WiFi and saves the settings in the flash memory.

If a different baud rate is required then use the command “AT+UART_DEF=115200,8,1,0,0” to make it definite and get it saved in the flash memory.

The following configures the module as a server, all though this will be done by the micro controller later on.

Setting up the server.

 

AT-CIPMUX=1 – Enables multiple connections instead of just 1 connection at a time with the module.
AT+CIPSERVER=1,80 – The ‘1’ creates a server and listens to port 80.
AT+CIFSR – shows the ip the module received.

Open a browser and connect to the correct ip (in my case 192.168.0.101)  The module received the text send by the browser so the module works fine.  In the next blog I will connect the esp8266 to a microcontroller and make it possible to control devices via the web browser. Thanks for reading!