ESP8266

From Lars' Site
Jump to: navigation, search

The ESP8266 is a microcontroller from Chinese manufacturer Espressif that includes Wi-Fi capability.

This page (and all the other pages in the ESP8266 Category) contains my brief research into this device.

Introduction

When this device was first introduced it gained a bit of traction as a simple and cheap WiFi module for use with other SOC's in embedded systems. The device has got an UART and by using a modem-like set of AT commands it was (is) possible to communicate using TCP/IP over WiFi. Personally I didn't pay much attention at the time as I was primarily working on Bright Thing's uThing - which had plenty of WiFi capability. One drawback of the uThing though is that it is virtually impossible to run it on battery power. The device will suck about 1 watt no matter how idle it is.

Not long ago someone worked out how to program the ESP8266 using the Arduino GUI (meaning ultimately GCC) and that raised my eyebrows considerably. All of a sudden it was/is possible to make a battery driven, WiFi enabled sensor node for less than $3 and _that_ is quite impressive.

First Attempt

Wiring up Hardware

The ESP-12 module that I had purchased through Alibaba had a 2 mm pitch on the connectors, so I had to solder a bunch of short wires on those connectors in order to fit them into a breadboard. First lesson learned was that the ESP8266 is essentially unprotected so it is extremely easy to fry them. I managed to fry my first attempt by accidently connecting it to 5 V rather than 3.3 V. Second attempt I included an LM1117 3.3 V regulator.

Wrapping it all up on a breadboard, it ended up looking like this:

ESP-12 Test Setup.jpg

Not pretty I know, but it should do the job. The wire hanging off the lower left corner is GPIO 0. That needs to be left hanging (or hooked to VCC) for normal operation, but it needs to be shortened to GND during programming. The USB-Serial "dongle" that I am using can be switched between +5 V and +3.3 V levels. It is - as mentioned before - extremely important to keep it at 3.3 V.

Installing Arduino ESP8266 support

Using the latest Arduino IDE, version 1.6.5, makes installing ESP8266 support almost ridiculously easy. In File/Preferences there is a field to enter additional board manager URL's:

Arduino Preferences w ESP8266 support.png

Once the link http://arduino.esp8266.com/stable/package_esp8266com_index.json has been entered, the following shows up in Tools/Board/Boards Manager dialog:

Board Manager.png

The above screenshot was made after installation. If it is not installed, simply click the Install button.

First Sketch

First step was to create a simple sketch. Unlike the standard Arduino, I did not have a LED hooked up to a GPIO pin, so rather than the usual "blink" sketch, I did a simple serial print loop:

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.println("Hello ESP8266!");
  delay(1000);
}

That is about as simple as it gets. Next step was to adjust the flash settings:

Arduino ESP8266 Flash Settings.png

Finally I connected the black wire to GND and hit the upload button. The result was:

Sketch uses 207,312 bytes (47%) of program storage space. Maximum is 434,160 bytes.
Global variables use 44,828 bytes (54%) of dynamic memory, leaving 37,092 bytes for local variables. Maximum is 81,920 bytes.
/home/lth/.arduino15/packages/esp8266/tools/esptool/0.4.5/esptool -vv -cd ck -cb 115200 -cp /dev/ttyUSB0 -ca 0x00000 -cf /tmp/build7814067049661485291.tmp/Test_Print.cpp.bin 
esptool v0.4.5 - (c) 2014 Ch. Klippel <ck@atelier-klippel.de>
	setting board to ck
	setting baudrate from 115200 to 115200
	setting port from /dev/ttyUSB0 to /dev/ttyUSB0
	setting address from 0x00000000 to 0x00000000
	espcomm_upload_file
	stat /tmp/build7814067049661485291.tmp/Test_Print.cpp.bin success
opening port /dev/ttyUSB0 at 115200
	tcgetattr
	tcsetattr
	serial open
opening bootloader
resetting board
trying to connect
	setting character timeout 0
	done
	setting character timeout 1
	done
	espcomm_send_command: sending command header
	espcomm_send_command: sending command payload
trying to connect
	setting character timeout 0
	done
	setting character timeout 1
	done
	espcomm_send_command: sending command header
	espcomm_send_command: sending command payload
	espcomm_send_command: receiving 2 bytes of data
	espcomm_send_command: receiving 2 bytes of data
	espcomm_send_command: receiving 2 bytes of data
	espcomm_send_command: receiving 2 bytes of data
	espcomm_send_command: receiving 2 bytes of data
	espcomm_send_command: receiving 2 bytes of data
	espcomm_send_command: receiving 2 bytes of data
	espcomm_send_command: receiving 2 bytes of data
	espcomm_open
Uploading 211456 bytes from /tmp/build7814067049661485291.tmp/Test_Print.cpp.bin to flash at 0x00000000
	erasing flash
	size: 033a00 address: 000000
	first_sector_index: 0
	total_sector_count: 52
	head_sector_count: 16
	adjusted_sector_count: 36
	adjusted_size: 024000
	espcomm_send_command: sending command header
	espcomm_send_command: sending command payload
	setting timeout 10000
	setting character timeout 100
	done
	setting timeout 1
	setting character timeout 1
	done
	espcomm_send_command: receiving 2 bytes of data
	writing flash
...............................................................................................................................................................................................................
starting app without reboot
	espcomm_send_command: sending command header
	espcomm_send_command: sending command payload
	espcomm_send_command: receiving 2 bytes of data
closing bootloader

After flashing, I removed the black GPIO 0 -> GND wire and fired up a terminal:

lth@ncpws04:~$ microcom -p /dev/ttyUSB0 
connected to /dev/ttyUSB0
Escape character: Ctrl-\
Type the escape character followed by c to get to the menu or q to quit
Hello ESP8266!
Hello ESP8266!
Hello ESP8266!
....

Deep Sleep

The latest alpha release of the Arduino ESP8266 software includes a ESP.deepSleep method. It requires external GPIO 16 shorted to the device reset pin and takes an argument in microseconds. It appears to work fine, but I was a little bit baffled when I ran the following sketch:

void setup() {
  Serial.begin(115200);
  Serial.println("Done setup");
}

void loop() {

  Serial.println("Loop start");
  
  Serial.println("Hello ESP8266!");
  Serial.print("ChipID is: ");
  Serial.println(ESP.getChipId());
  
  ESP.deepSleep(1 * 1000000, WAKE_RF_DISABLED);

  Serial.println("Loop End");

}

The serial output from that was:


r▒l��r��#�n�
            ▒�
              ��p�<����8��ǒ���p
                               ▒
                                �nn��;�nĒ��

                                           b�$�r▒r▒p�n���
                                                         ��l����
                                                                �
                                                                 b�n��n�$����
                                                                             b��>~�n����l`��#�n��r▒nr���;��
                           ?��r▒r�ےn�����l`��n�b�Done setup
Loop start
Hello ESP8266!
ChipID is: 15858539

A bit of searching revealed that the bootloader print out some stuff at 74800 bps. Now, I was unable to get my usual terminal to run at that speed and couldn't be arsed figuring out how to do it, so I don't really know yet what the bootloader is actually printing. However, it is worth noticing that the Serial.println("Loop End") statement is never reached. In other words, going into deep sleep is "final" and require a complete restart of the chip after the sleep timeout.

Power Consumption

Notice! Measured with a rather crappy multimeter of Chinese origin:

State Current Power
Reflash 53.8 mA 178 mW
delay() - wifi connected 77 mA 231 mW
delay() - no wifi 12.84 mA 42.3 mW
Deep sleep 0.02 mA 0.07 mW

Assuming constant deep sleep that would give up towards half a year running of a single CR2032 cell or when actually doing some work, a month on a button cell is not outside the realm of possibility.


Flash Storage

Apparently, from doing a few Google searches, while all ESP8266 based modules uses the same SOC, they differ wildly in terms of available flash storage. Since I blew the first device I connected, I thought I better rip the RF shield of and see what exactly was under the hood:

20150927 175729.JPG

So the flash chip is a BG25Q32A, which I searched for but while it does indeed exist I couldn't find a datasheet. Assuming however that it is roughly identical to the Winbond W25Q32BV[1] the chip is 32 Mbit - or 4 MB in size.

Having the RF shield removed also leads to a few other interesting observations. Now, I know that they produce hundreds of thousands of these devices, but still. I bought this device for USD 2 as a single item (I bought 10 but that was the same price per piece). The absolute cheapest I could find a 25Q32 chip was USD 0.48[2]. Let's for a moment assume that the SOC is at least more expensive than the flash memory - let's put that at USD 0.50, the crystal they might be able to get for $ 0.10 and let's throw another $ 0.10 after the resistors, capacitors and the PCB. In other words - the cost of this device is probably in bulk around $ 1.20 or thereabouts + overhead (staff, expensive production equipment etc.) They really are cutting the margin's paper thin.

Notes

  1. Datasheet here: https://www.winbond.com/resource-files/w25q32bv_revi_100413_wo_automotive.pdf
  2. http://www.digikey.com/product-detail/en/W25Q32FVSSIG%20TR/W25Q32FVSSIG%20TR-ND/4037405

Gallery

Other Pages