IOT ESP8266 Tutorial – AT Command Set Firmware

IOT ESP8266 Tutorial – AT Command Set Firmware

This is the fourth of a multi-part posting on the ESP8266.   In this posting we are showing how to use the AT Command set firmwareIMG_6187 with the ESP8266.  The board to the left is a new SwitchDoc Labs project for pulse rate measuring for an upcoming IOT Project Cookbook.

Part 1  IOT ESP8266 Tutorial – Using nodeMCU/LUA

Part 2 IOT ESP8266 Timer Tutorial – Arduino IDE

Part 3 IOT ESP8266 Tutorial – Using the Arduino IDE

Part 4 IOT ESP8266 Tutorial – AT Command Set Firmware

Part 5 IOT ESP8266 Tutorial – Connect to the IBM Bluemix Internet of Things

Part 6 IOT ESP8266 Tutorial – Sending ESP8266 Data to the IBM Bluemix IOT

Part 7 IOT ESP8266 Tutorial – The IOT Application and Source Code

Part 8 IOT ESP8266 Tutorial – Displaying the data on the IBM Bluemix IOT

We are using the Adafruit ESP8266 Huzzah breakout board for these postings.

As you will see below, the AT firmware experience was not good.

 

Update:   Ways of making the ESP8266 operate more reliably in the AT Command Mode.

The ESP8266

The ESP8266 is made by a privately held company in China called Espressif.   They are a fabless semiconductor company that just came out of nowhere and shook up the whole industry.   Now all the major players are working on inexpensive versions of an IOT chip with WiFi connectivity.  And they are all struggling to make it as inexpensive as the ESP8266

Figure3

The Adafruit ESP8266 Huzzah

The Adafruit ESP8266 Huzzah board is a great  breakout for the ESP8266.  It makes it much easier to use with the Raspberry Pi than the really cheap modules.

Most of the low cost modules are not breadboard friendly, don’t have an onboard 3.3V regulator or level shifting for signals.  The Huzzah has all of those features.  For more on the ESP8266 Huzzah board see this posting.

 

There are three main types of firmware and development systems available for the ESP8266.  They are:

In this posting we examine the AT Command Set Firmware.

The AT Command Set Firmware

AT commands go way back.  The AT command set is a specific command language originally developed by Dennis Hayes for the Hayes Smartmodem (300 baud) in 1981!   The command set consists of a series of short text strings which combine together to control complex behavior.   The vast majority of dial up modems used this command set or similar extensions.

Forward to 2015.  A version of the AT command set is used by the ESP8266 to control the WiFi functions on the chip.  This is only one of three major options for firmware, and as you will see, the least reliable at this time.

Hint:  One thing to remember is that the AT command set requires both a return and a ctrl-j (\r\n) to complete a command.

Here are two equally useful references to the AT command set used by the ESP8266: room 15 and pridopia.

Here is an example:

 

How to Install the AT Command Firmware

It was tricky to find the AT command firmware and install it on the ESP8266 Huzzah board.

The drivers for the Huzzah (AI-Thinker module) are here.

We are using 0.9.5.2 firmware from this site.  First you should get rid of the spaces in the file name.

Secondly download and install the esptool.py package to get ready to flash the ESP8266  You can install the software with the command:

git clone https://github.com/themadinventor/esptool.git

and then use the following esptool.py command further down the page.  Note that your USB port name may be different.  If you are on the Raspberry Pi using an FTDI cable, it will probably be /dev/ttyUSB0/

SwitchDoc Labs has an inexpensive FTDI cable available here.

You also need to install a serial terminal  such as minimum  or better yet,  picocomm (my favorite) using the command below:

apt-get install picocom

Use the following esptool.py command:

python ./esptool.py  --port /dev/tty.usbserial-A50482MX write_flash 0x00000 v0.9.5.2ATFirmware.bin

Now you have the AT command set firmware in the ESP8266.  Type the following command to test it.

picocom  --baud 115200 /dev/ttyUSB0

type “AT (return then ctrl-j) and you will get an OK

Testing the AT Command Setup

Put the following code in a python file (such as  testSerialESP8266.py)

import serial, sys
import time

# Modified from https://www.instructables.com/id/Easy-ESP8266-WiFi-Debugging-with-Python/step2/Software/


def enum(**enums):
    return type('Enum', (), enums)

Status = enum(ERR='ERROR', OK=['OK', 'ready', 'no change'], BUSY='busy')

def send_cmd( sCmd, waitTm=1, retry=5 ):
        lp = 0
        ret = ""

        print"Sending command: %s" % sCmd

        for i in range(retry):
                ser.flushInput()
                ser.write( sCmd + "\r\n" )
                ret = ser.readline()    # Eat echo of command.
                time.sleep( 0.2 )
                while( lp < waitTm or 'busy' in ret):
                        while( ser.inWaiting() ):
                                ret = ser.readline().strip( "\r\n" )

                                lp = 0
                        if( ret in Status.OK ): break
                        #if( ret == 'ready' ): break
                        if( ret == Status.ERR ): break
                        time.sleep( 1 )
                        lp += 1

                time.sleep(1)
                if( ret in Status.OK ): break

        print( "Command result: %s" % ret )
        return ret



port = "/dev/ttyUSB0"

speed = 115200
#ssid = "yourSSID"
#pwd = "yourPASSWORD"

ser = serial.Serial(port,speed)
if ser.isOpen():
    ser.close()
ser.open()
ser.isOpen()

send_cmd(“AT")


Here is the command to run the program and the results:

pi@LightSwarm ~/esp8266 $ sudo python testSerialESP8266.py
Sending command: AT
Command result: OK
pi@LightSwarm ~/esp8266 $

Webserver Code for the AT Command Firmware

The simplest web server code we found for the Raspberry Pi (in python) is located on this GitHub site.  There are a number of other versions out on the web, but they all basically had the same issues and problems.   On the upside, using the AT command set is a simple way of getting a connection to a TCP socket or serving a website.  The downside on using the AT Command set on an ESP8266 is further below.  Here is example code for connecting to a WiFi access point using AT commands from the above source:

 

                send_cmd( "AT+CWMODE=1" ) # set device mode (1=client, 2=AP, 3=both)
                send_cmd( "AT+CWLAP", 30) # scan for WiFi hotspots
                send_cmd( "AT+CWJAP=\""+ssid+"\",\""+pwd+"\"", 10 ) # connect
                send_cmd( "AT+CIFSR", 5) # check IP address
                send_cmd( "AT+CIPMUX=1" ) # multiple connection mode

And the results from the terminal window:

Sending command: AT+CWMODE=1
Command result: 
Command result: OK
Sending command: AT+CWLAP
Command result: +CWLAP:(3,"HP-Print-55-Officejet 6700",-69,"c8:cb:b8:45:92:55",1)
Command result: +CWLAP:(4,"myqwest2705",-93,"00:24:7b:2d:f8:62",1)
Command result: +CWLAP:(3,"HP-Print-C6-Officejet Pro 8620",-83,"6c:c2:17:1d:2f:c6",1)
Command result: +CWLAP:(4,"HOME-F1B2",-91,"00:ac:e0:ec:f1:b0",1)
Command result: +CWLAP:(4,"HOME-F183-2.4",-73,"0c:54:a5:cd:25:28",6)
Command result: +CWLAP:(4,"HOME-9618",-92,"44:32:c8:c1:96:18",6)
Command result: +CWLAP:(0,"xfinitywifi",-90,"46:32:c8:c1:96:1a",6)
Command result: +CWLAP:(0,"xfinitywifi",-72,"0c:54:a5:cd:25:2a",6)
Command result: +CWLAP:(0,"xfinitywifi",-92,"06:ac:e0:ec:f1:b0",1)
Command result: +CWLAP:(4,"SharonsKitchen",-88,"cc:03:fa:c1:60:72",8)
Command result: +CWLAP:(4,"HOME-0CA3-2.4",-80,"0c:54:a5:7b:85:f8",11)
Command result: +CWLAP:(0,"xfinitywifi",-80,"0c:54:a5:7b:85:fa",11)
Command result: 
Command result: OK
Sending command: AT+CWJAP="ssidxxx","passwordxxx"
Command result: 
Command result: OK
Sending command: AT+CIFSR
Command result: +CIFSR:STAIP,"192.168.1.127"
Command result: +CIFSR:STAMAC,"18:fe:34:fd:92:xx"
Command result: 
Command result: OK
Sending command: AT+CIPMUX=1
Command result: 
Command result: OK
Sending command: AT
Command result: 
Command result: OK
Sending command: AT+CIPSERVER=1,80

Really pretty simple to implement.  The whole webserver is only 136 lines of code.  However, the ESP8266 is flakey no matter what version of firmware we used for the AT command set.
The reason we were so interested in the AT Command set is that it is easy to implement and send / receive across a serial connection from the Raspberry Pi or Arduino computer to the ESP8266. We were quite disappointed that it just doesn’t seem to be reliable. We were planning to use the ESP8266 as a backup WiFi connection on the SunRover robot connected to the Arduino using the AT command set.  Now we will have to implement a more complex protocol using either NodeMCU or the Arduino IDE solutions below.

Firmware Comments

One word fits.  Frustration.  While the code for doing a web server for using the AT Command set firmware on the ESP8266 was fairly straightforward, the ESP8266 was not reliable.  We tried a number of different versions and after a webpage access or two, the ESP8266 would lock reporting a “busy p” to any additional AT commands.   After looking on the web, we see this is a very extensive problem and most people handle the “busy p” problem by resetting the ESP8266 with the external RESET pin.  It takes about 30 seconds for the ESP8266 to come back up, join the wireless network and be ready to serve pages.   That just doesn’t work for most applications, nor does it work for ours.

One last note.  After looking at the websites, and understanding peoples problems with this firmware, it may eventually get fixed.  But right now, it is not ready for prime time.

2 Comments

  1. I didn’t try it, but it looks like there’s something missing in the python at the top, send_cmd never gets called by the main code. Did I miss something?

5 Trackbacks / Pingbacks

  1. IOT ESP8266 Tutorial - Using the Arduino IDE - SwitchDoc Labs
  2. IOT ESP8266 Timer Tutorial - Arduino IDE - SwitchDoc Labs
  3. IOT ESP8266 Tutorial - Using nodeMCU/LUA - SwitchDoc Labs
  4. IOT ESP8266 Tutorial - Connect to the IBM Bluemix Internet of Things - SwitchDoc Labs
  5. IOT ESP8266 Tutorial - The IOT Application and Source Code - SwitchDoc Labs

Comments are closed.