Tuesday, June 23, 2015

Building a Weather Sensor using NodeMCU with a REST API
[PART1 - FLASHING ESP8266]

Project Introduction

A couple of weeks ago, I received my newly ordered ESP8266 SoC. In some way related with my thesis research, I got really interested in the idea of building a small and very cheap energy sensor.

Also the main reason why I choose ESP8266 over an Arduino, Raspberry-PI or other alternatives its because, they consume too much power, have too many resources for an embedded device—most Arduinos found out there have Dual or Quad core processors, — and they are very expensive for the end goal of a simple sensor. Anyway, it would be a very long post to compare between ESP8266 and alternatives in a fair and measured way, so I will save it for a future post.

So, to warm up I decided to build a simple weather sensor, just to get used about how to flash new firmware, access GPIOs, access the MCU via USB and so on. Hopefully, this post will show you how to get started on programming an ESP8266 with NodeMCU.

Fig.1 ESP8266 - ESP12E
Fig.2 ESP8266 on Amica R2 DevBoard v1.0

Required Equipment
  1. ESP8266 with DevBoard (NodeMCU LUA Amica R2 | ESP8266 Wifi Board)
  2. Breadboard
  3. Wires
  4. USB to MicroUSB cable
  5. DHT11 sensor
Some notes on the equipment, for a production setup you just need the ESP8266 (Fig.1) which costs about 2-3 US$, the DHT11 sensor, the wires and some soldering material. But since we do not want to build permanently soldered prototypes we use a breadboard and a ESP8266 mounted on a DevBoard that can be plugged on our breadboard. That's why we need the equipment on Fig.2, which its just a normal ESP8266 mounted on a DevBoard, with a convenient MicroUSB connector so that you can plug it onto your computer and start hacking!

Additionally, if you buy an NodeMCU Amica R2 | ESP8266, the seller will send you an already flashed ESP8266 with NodeMCU firmware, where you can run Lua scripts.

Where to buy? http://www.electrodragon.com (trustworthy, cheap and quality material).

Our final prototype will be something like this, accessible through your browser, returning a JSON response back to you with the sensor readings! Great isn't it!




Getting Started

The following instructions are targeted for OSX users, and they may work for Linux environments. Windows users will not need wine, just install the drivers and you are good to go.

Installing the drivers

 

If you didn't notice the DevBoard MicroUSB connector is just a simple UART Bridge (Universal Asynchronous Receiver/Transmitter), so in order for your OS to recognize it, you will need to install the drivers for that UART Bridge, which is an CP2102 VCP.

The drivers for any OS can be found here, for OSX users just download your driver from the list.

Flashing ESP8266

 

To flash your ESP8266 with a new firmware you can use 1 of 2 tools, that I recommend:

  1. Esptool, to install it jump on your terminal and git this https://github.com/themadinventor/esptool. Follow their tutorial on git to setup the tool.

  2. Espressif Flasher Tool (the makers of ESP8266), just download the flashing tool from here. Its a forum of the Espressif Systems, where they post new versions of their flashing tool. Pay attention to the file name, it should be something like:
    "FLASH_DOWNLOAD_TOOLS_v[VERSION]_[RELEASE_DATE].rar".

After choosing your tool, you will need to download the latest NodeMCU firmware, you can found it here:
https://github.com/nodemcu/nodemcu-firmware/raw/master/pre_build/latest/nodemcu_latest.bin.


Assuming that you downloaded the latest firmware to your "~/Downloads" folder, and that the file is named "nodemcu_latest.bin", you can flash the ESP8266.

Before flashing the device, you will need to do the following:

  • Connect the ESP to your computer

  • Check the port where it is connect. On windows you can find this on the Device Manager. On OSX you just type "ls /dev/" on the terminal and check if you have something like "cu.SLAB_USBtoUART" or "tty.SLAB_USBtoUART" in your /dev directory.

  • If you succeed finding the port name, consider this tag [ESP8266_PORT_NAME] as the port name you just found. I will use this tag further on this post.

Flash Mode:
You will need to press the FLASH button and then the RST button while pressing the FLASH button. This will put the device in flash mode. After doing this release both any button you are pressing. Run the flashing procedure. After completion press the RST button again, and after that unplug the device from your computer. Plug it again to use your newly flashed device.

Option 1: Using the Esptool

Put your device in Flash Mode, then run the following command on your terminal (you can write the entire command on just one line):
python esptool.py --port /dev/[ESP8266_PORT_NAME] \
    write_flash 0x0 ~/Downloads/nodemcu_latest.bin -fs 32m -fm dio -ff 40m
Complete the Flashing Mode procedure and you have flashed your device with success hopefully.

Option 2: Using the Espressif Flasher Tool
  • Install Homebrew on you OSX

  • Run the command "brew install wine" to install wine. This will enable you to run the Flasher Tool Windows executable from your OSX.
  • After installing wine you need to make you your device visible within wine.
    cd ~/.wine /dosdevices
    ln -s /dev/tty.[ESP8266_PORT_NAME] com1
    
  • The previous point will make your device visible on the COM1.
  • Now start the flasher tool running the command bellow from your terminal:
    wine flash_download_tool_v[VERSION]_[RELEASE_DATE].exe 
    
  • Select your image and adjust the settings according to this screenshot:




  • Now connect the ESP8266 device using USB and perform first the Flash Mode procedure.
  • Click START in the flasher tool and wait until it is finished. Resume the last part of the Flash Mode procedure and you have flashed your device with success.

This is my 1st post of a series of 3 posts about Building a Weather Sensor using NodeMCU with a REST API. I hope you liked it, stay tuned I'll be posting the 2nd and 3rd very soon.

Thanks for reading!