Monday, May 2, 2016

Getting started with the ESP8266, NodeMCU and Wemos D1 Mini

After a long, work induced, hiatus away from electronics I have started up again with the ESP8266 serial of WiFi connected microprocessors. In the past I had been experimenting with sensor platforms based on an Arduino UNO and Ethernet shield. These were fine as far as they went but being tied to a wired ethernet cable made them less that portable.

I made the decision to switch platforms after trying WiFi solutions based on the CC3000 chipset tied to the Arduino UNO and being very disappointed by their reliability. I noticed that the Arduino development environment had started to support the ESP8266 chipset and initial reports seemed to indicate they ran for long periods of time without issues.

With more and more 3.3 Volts parts being made available on breakout boards, it also seemed like the right time to make the shift to a 3.3V microcontroller.

Initially I started with the NodeMCU development board which ran a Lua interpreter. This appeared to work fairly well but I am used to the Arduino development and programming with its implementation of C so I switched to the Arduino IDE. In addition there are many more hardware support libraries available in the Arduino environment, the vast majority of which run without modification of the ESP8266. The NodeMCU boards fit neatly into breadboards and are well suited for experimentation.

Fortunately I have side-stepped any issues with drivers for the on-board serial to USB chips by using Linux as a development environment. Unfortunately I can't offer any suggestions for other operating systems such as Windows or Mac OS/X.

My first project was a temperature and humidity sensor using the DHT11 sensor. Using the WiFi connection, through my home router, the sensor information was sent out to ThingSpeak every 10 minutes. After sending the MCU was put into deep sleep mode until another ten minutes had passed, at which point the MCU was reset and the cycle started again. The code I used for this is available for download here: thingspeak_data-logger.ino (Arduino 1.6.7)

Since I ultimately wanted to run the sensor from a rechargeable battery I needed to be able to read the voltage being supplied to the ESP8266. This function is built into the ESP8266 but it took some time to figure out the right incantation to get it to work. For the record this is documented below:

In the initial header of your project you need to specify "ADC_MODE(ADC_VCC);" to enable analog 0 (A0) to read VCC.
Once that is complete, in the main body of the code, you can read VCC using "float vcc = ESP.getVcc();" to read the current VCC*1024. Dividing by 1024 should yield a figure representing VCC in volts.

Using the NodeMCU with deepsleep I was able to power the sensor from a USB power-pack for approximately 20 days before the powerpack shutdown to protect the Lithium-ion batteries.

Later, cruising around the online Chinese supplyhouse AliExpress, I discovered a smaller form factor version of the ESP8266 breakout board that had a healthy collection of shields containing most of the electronics I was interested in. The Wemos D1 Mini is about the size of a postage stamp but still contains all the support electronics including serial-to-USB and 5V to 3.3V regulator.

When using the WiFi sensor code on the Wemos D1 Mini I have been able to run for at 15 days on the same USB battery pack before it showed any sign of discharging (three out of four battery level lights lit) so I'm assuming that the D1 Mini draws much less power in deep sleep and is therefore a better choice for battery powered projects.

For my own reference as much as anything I made a list of the currently available D1 Mini shields and the pins they use:

Shield Name Device Type Pin Used Pin Definition
DHT Shield DHT11 Pin D4 Data Out
DHT ProShield DHT22 Pin D4 Data Out
OLED Shield OLED using SSD1306 driver Pin D1, D2 SCL, SDA
1-Button Shield Momentary push button Pin D3 Button
Relay Shield V2 5VDC Relay Pin D1 Relay Enable
Micro SD Shield Micro SD adapter Pin D5,D6,D7,D8 CLK,MISO,MOSI,CS
ProtoBoard Shield Proto Area All Pins Proto Area
Dual Base for D1 mini Stack D1, shields side-by-side Pins are duplicated

Important notes: You will need to use a modified library for the OLED display which is available here: SparkFun_Micro_OLED_Arduino_Library If you use the standard Sparkfun library you will not be able to display text.

I have an example of using the OLED shield to display sensor readings from the DHT22 available here: wemos_temp_display.ino (Using DHT sensor library from Adafruit V1.2.3)

I'm hoping that the Wemos D1 Mini and its accompanying shields will enable the quick prototyping of handheld sensor platforms that include wifi connectivity. My next project will involve configuring WiFi SSID, WEP keys and ThingSpeak API keys without having to connect to a computer with the Arduino IDE. Hopefully through the use of a captive portal and switching the ESP8266 into Access Point mode to allow configuration via a tablet of smartphone.

With any luck this information will help you make progress quicker that I did. This hardware is well worth investigating: A full set, including Wemos D1 Mini and all shields, is only going to set you back perhaps $30-$40 if you already have a computer to run the Arduino IDE.

One last thing ... Shipping from China to Texas has typically taken around 20-30 days when ordering from AliExpress. So, order everything you think you might need at once otherwise you are not likely to enjoy the wait while the additional shields you need take, literally, the slow boat from China.

No comments: