Tutorial: Making the AT Command ESP8266 WiFi Work with Arduino
We have recently started a new kickstarter for a Grove connector based Lightning Detector Board based on the AS3935 lightning detection chip. Since we have now finished the code getting our Mini Pro LP Arduino board to work with the Grove Serial ESP8266 WiFi, we felt we should explain how we got one of the hardest parts of the design to work.
The Thunder Board Kickstarter
[callout size=”col-12″ title=”Checkout the ThunderBoard Kickstarter” button_title=”Go to Kickstarter” button_link=”https://www.kickstarter.com/projects/sunair/the-thunder-board-iot-lightning-detection-for-make?ref=5i86ii” button_size=”normal” button_rounded=”true” button_color=”red”]
The AT Command Set on the ESP8266
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 2017. A version of the AT command set is used by the ESP8266 to control the WiFi functions on the chip.
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.
Getting the ESP8266 WiFi to work with an Arduino
The Arduino ThunderBoard IOT project is lightning detector project designed to both provide a local display giving alerts about Lightning while also using MQTT to send data up to an Internet Dashboard at PubNub/Freeboard. More on how to do that in a later posting.
We are using an ESP8266 WiFi board with a Grove connector in the ThunderBoard IOT project.
The processor in the Arduino ThunderBoard IOT project is a Mini Pro LP Arduino which contains a 16MHz ATMega328p AVR processor (same processor as on the Arduino Uno R3 board, but with a low power mode and Grove connectors).
The ThunderBoard Lightning Detector came up pretty quickly, however connecting the WiFi to our local access point and sending the MQTT JSON data across the Internet was another story. The Software Serial connection to the ESP8266 at 115200 baud (the default for the Wifi unit) was very unreliable and just wouldn’t connect in any kind of a consistent fashion. After quite a bit of experimenting we determined the software serial port just wouldn’t connect well at any baud rate above about 34000. Looking at the problem, we realized that having a hardware serial port (the debug and programming port) running at 115200 baud and the software serial port running at 115200 baud was just to fast for the Arduino 328P to make a solid connection. The hardware serial port puts a minimal load on the CPU, but the software serial port uses a lot of CPU cycles because you are basically bit banging the bits in and out with timing loops.
We solved this after more experimenting by changing the baud rate into the ESP8266 to 19200 and then the connection was solid. We spent a lot of time looking at different ways of programming the baud rate (some that would brick the ESP8266) and finally settled upon the code “AT+UART_DEF=19200,8,1,0,0”. After resetting the Software Serial port to 19200, we were ready to go.
The Example Code
Here is the sample code from our Arduino ThunderBoard IOT project that shows how we do it.
The library we used for the main connection was WiFiEsp and can be found on GitHub.
void initWiFiConnection() { status = WL_IDLE_STATUS; // initialize ESP module WiFi.init(&SoftSerial); delay(3000); // attempt to connect to WiFi network while ( status != WL_CONNECTED) { WiFi.reset(); // Connect to WPA/WPA2 network Serial.print(F("ssid=")); Serial.println(ssid); status = WiFi.begin(ssid, pass); Serial.println(F("after WiFi.begin----------------------->")); } } and in setup() // Setup WiFI // initialize serial for ESP module SoftSerial.begin(115200); SoftSerial.println("AT+RST"); Serial.println("AT+RST"); char value; while (SoftSerial.available()) { value = SoftSerial.read(); Serial.println(value); } SoftSerial.println(F("AT")); Serial.println(F("AT")); delay(1000); while (SoftSerial.available()) { value = SoftSerial.read(); Serial.println(value); } // Baud rates above about 38,000 do not work reliably on the 328p (Pro Mini) Serial.println(F("AT+UART_DEF=19200,8,1,0,0")); SoftSerial.println(F("AT+UART_DEF=19200,8,1,0,0")); delay(1000); while (SoftSerial.available()) { value = SoftSerial.read(); Serial.println(value); } // Restart SoftwareSerial for the slower baud rate for the WiFi SoftSerial.end(); SoftSerial.begin(19200); initWiFiConnection();
Is it possible to determine how far away the thunder and lighting is from a fixed point ?
Yes, but not with a great deal of accuracy. 0 to 30km.
John