Adding WiFi to Your Hardware Projects Using The ESP8266

Having built several Arduino based projects over the last year, the next step for me was to add WiFi connectivity to open up the possiblity for buidling things that can be controlled wirlessly, things that might actually be usfeul as opposed to just fun projects.

Looking at the options, the easiest way to add Wifi to your project might be a shield such as the official Arduino WiFi Shield, or a CC3000 Based Shield, which will set you back in the region of £15 - £20. The advantage of using a shield is that they snap straight onto the Arduino board, and don't require a messay array of jumper wires. However, a bit more searching lead me to some interesting alternatives. In particular the ESP8266 module stands out as a very cheap (~£1.70 shipped from China) and surprisingly performant option.

The ESP8266 Module

The module is essentially a System-on-a-Chip (SoC) that contains a 32-bit CPU clocked to 80 MHz, a WiFi transmitter/reciever, some internal RAM and ROM, plus an external flash memory chip. All in all it's a pretty nifty little thing, and provides a really simple way of adding WiFi connectivity to your hardware projects.

Wireless Serial Port

The default firmware lets you treat the ESP8266 as a wireless serial port, ie - you can send data in the same way as you would print to a serial USB sconnection. Simply connect up the Tx and Rx ports of the ESP8266 to the Rx and Tx ports of your Arduino or similar device, and then tell it what to do by executing commands using Serial.write(command). The commands are pretty straightforward, you can reset the module, set it up as an access point, connect to an access point, send data to connected clients, etc. The full list is available here.

I wrote a very basic server example which runs on an Arduino connected to the ESP8266. It repeatedly sends the text 'Hello world' to clients connected on port 23. To find the IP address of your module, you can run the command AT+CIFSR on it. When the server is running you should then be able to connect to this IP on port 23 using a network client terminal such as telnet or PuTTY, and see 'Hello world' repeatedly being printed to your terminal. Here's the code:

void setup() {
  //set baud rate to match module
  Serial.begin(115200);
  //reset the module
  Serial.println("AT+RST");
  //give it time to reboot
  delay(5000);
  
  //time-out a Serial.find after 5 seconds
  Serial.setTimeout(5000);

  //set module into access point and client mode
  sendCommandToESP("AT+CWMODE=3");  
  //connect to my home wifi
  sendCommandToESP("AT+CWJAP=\"WIFI_NETWORK_NAME\",\"PASSWORD\"");
  //allow multiple connections
  sendCommandToESP("AT+CIPMUX=1");  
  //run server on port 23
  sendCommandToESP("AT+CIPSERVER=1,23"); 
}

void sendCommandToESP(String command) {
  Serial.println(command);
  Serial.find("OK");
}

//send data to connected device every 4 seconds
void loop() {
  //send 11 bytes on connection '0'
  Serial.println("AT+CIPSEND=0,11");  
  delay(1000);
  //send the data
  Serial.println("Hello world");
  delay(3000);
}

To play around with the AT commands, it's worth getting a USB to Serial TTL converter, and running the commands directly from your computer using a serial port terminal such as CoolTerm. For wiring everything up, I followed this tutorial from the ESP8266 community wiki.

Self-Contained Application Platform

The integrated processor is used for running the TCP/IP stack as well as all of the additional networking software, separating the low-level network implementation details from your application and hardware by providing an easy to use serial interface.

Impressively, it is also possible to load and run your own application code on the device. There is an active community of developers and hobbyists using the ESP8266, who have collectively released several SDKs targetting the ESP8266 as a WiFi application platform. There is even an Arduino port for the ESP8266, allowing you to run your Arduino code directly on the module itself, all via the Arduino IDE!

I've been looking at using the Sming Framework for writing efficient native applications for the ESP8266 in C++. I've had a few ideas related to clustering these modules (seeing as they're so cheap) and Sming looks like a pretty good option for implementing them. The framework has a similar API to the Arduino SDK, and in addition is compatible with most Arduino libraries, which is a huge plus. I'll post an update if aything interesting comes of it :)