ESP8266 ESP-01 Module With Ubuntu

Here are a few notes on working with the uber-cheap ESP-01 WiFi microcontroller module with Ubuntu.

Firmware Options

Standard ‘AT Command’ Firmware

This is fine if you want to use the ESP-01 as a gateway to the rest of the world via WiFi, e.g. from your Arduino. As-delivered your ESP-01 probably came with this pre-loaded. You can connect up a serial line @ 3.3V signal level and issue AT commands at 9600 baud. In case you want to restore this firmware download it here. There’s a good list of the AT commands supported at the end of this page.

NodeMCU Lua

This firmware lets you program the ESP-01’s microcontroller in the Lua programming language. Get the firmware here.

Arduino IDE With ESP8266 Extension

This is a slightly different approach. You can use Arduino-style ‘C'(++-ish) language to program the ESP-01. Here’s the programming reference.

Programming & Run-Time Connection

Super-cheap USB-serial converters don’t have hardware handshake lines that could be used to put the ESP-01 into its receptive ‘programming mode’. Instead I use a momentary-action push switch to tug GPIO0 down to ground while plugging the USB cable in (powering and and therefore resetting the ESP-01). This now works with both the Arduino IDE + ESP8266 plugin as well as the command-line esptool.py programmer (with tweaks as below).

Caveat: you should use a 3.3V USB-serial converter that also has a 3.3V-out connection. These ESP-01 modules suck quite some current – enough to swamp the 3.3V regulator built in to most USB-serial converters. You’ll need a separate 3.3V regulator when it comes to runtime to supply at least 250mA @ 3.3V.

esptool.py

I had a problem with this whereby it would get around 7% through and then fail with ‘invalid head of packet’. Some Googling around revealed that I should edit the esptool.py to set
ESP_RAM_BLOCK = 0x180
ESP_FLASH_BLOCK = 0x40

Putting It All Together

Let’s make a IoT thing. Wire up an ESP-01 to connect to a server out there on the Internet to update the server according to the high/low voltage on the ESP-01 input pin (GPIO2). We’ll program using the Arduino IDE because that’s definitely the easiest way at the time of writing. Here’s the code:

////////////////////////////////////////////////////////////////////////////////
//
// Simple ESP8266 Sensor Sending Input Value To Internet Host Via HTTP/REST
//
////////////////////////////////////////////////////////////////////////////////

#include <ESP8266WiFi.h>

const char *wifiSSID = "yourwifinetworkname";
const char *wifiPassword = "yourexcellentwifipassword";
const char *serverName = "your.internetserver.com";
const char *serverPath = "/yourRestServer.php";

#define PIN_LED (BUILTIN_LED)
#define PIN_INPUT 2

////////////////////////////////////////////////////////////////////////////////
void setup()
{
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_INPUT, INPUT);
wifiConnect();
}

////////////////////////////////////////////////////////////////////////////////
void loop()
{
static int lastInputValue = -1;

int inputValue = digitalRead(PIN_INPUT);

if (lastInputValue != inputValue)
{
sendInputValueToServer(inputValue);
lastInputValue = inputValue;
}

digitalWrite(PIN_LED, LOW); // Internal LED lights when pin is low
delay(100);
digitalWrite(PIN_LED, HIGH);
delay(2000);
}

////////////////////////////////////////////////////////////////////////////////
void sendInputValueToServer(int inputValue)
{
WiFiClient client;
if (client.connect(serverName, 80))
{
client.print(String("GET ") + serverPath + "?value=" + inputValue +
" HTTP/1.1rn" +
"Host: " + serverName + "rn" +
"Connection: closernrn");
delay(10);
while (client.available())
{
String line = client.readStringUntil('r');
}
}
}

////////////////////////////////////////////////////////////////////////////////
void wifiConnect()
{
WiFi.begin(wifiSSID, wifiPassword);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
}
}

Bootnote: ESP-07

Anybody trying the ESP-07 which has an external antenna and lots of other pins might being thinking their module is dead – it blinks the blue LED on power-on but then nothing. Well, if you probe the TXD pin and decode at 76800 baud (weird, I know) you will probably see the startup message. I had to tie GPIO15 down to ground as well as RST and CH_PD to 3.3V and then communicate with the AT command set at 115200 baud. Who knew!?!