TSwarm pt 2 – ESP12 yellow board hardware and micropython basics

After playing a little bit with all the junk mentioned in previous post, especially ESP12 board itself, I have decided to put together some of my initial findings in single place – just because compiling all of it took some time and a little bit of exploration, and maybe will save you some time while doing own projects.

ESP12 yellow board - pinout

Connecting to UART

Connecting yellow board to UART USB interface I’ve bought was fairly simple. Just use some wires to connect RXD, GND and TXD pins from the board, with corresponding ones on UART. You will find three dedicated pins on board that are best to use.

For power source, I’m just using three AA batteries. It is possible to power this thing from USB through UART interface, but ESP can be picky when i comes to power source, and I’ve red somewhere that it can even burn your USB port. I’ve decided to not try this one, mainly because I don’t trust UART dongle I’ve bought. Batteries are cheap, laptops are not. And it’s generally good idea to work with thing, and test it in target configuration – so my sensor swarm will be powered by IKEA AA batteries during both, development and real-life usage.

Flashing the thing

After connecting board to my laptop, I’ve followed manual on micropython page (make sure that flash jumper is on board). Long story short – you will have to:

  • install esptool
pip install esptool
  • erase flash
esptool.py --port /dev/ttyUSB0 erase_flash
  • grab your micropython version from the download page and write it to board flash (for my yellow boards I need to use -fm dio parameter)
  • esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect -fm dio 0 esp8266-20170108-v1.8.7.bin

    Give it some time to flash. After it’s finished, you can remove flash pin, and follow manual from micropython page to get REPL prompt. I won’t be copy-pasting more from the oficial manual manual, so…

Let’s cut to the chase

Here is some general stuff that might be useful when playing with the board, sensor and screen.

  • I have checked few boards of mine, and all of them have following GPIO pins connected to RED LEDS (order from bottom of the image to top): 2, 0, 4, 5, 14, 16
  • RGB led is controlled by three GPIO PINs: 12, 13, 15. Here are the colors:
  • p12 p13 p15
     0   0   0   - off
     1   0   0   - green
     0   1   0   - blue
     0   0   1   - red
     1   1   0   - light-blue
     0   1   1   - violet
     1   0   1   - lime-green (yellowish)
     1   1   1   - white(ish)
  • Using DHT (11 or 22):
  • import dht
    import machine
    
    d = dht.DHT11(machine.Pin(2))
    d.measure()
    d.temperature() # eg. 23 (°C)
    d.humidity() # eg. 41 (% RH)
  • Example use of OLED screen:
  • import ssd1306
    i2c = machine.I2C(-1, machine.Pin(4), machine.Pin(5))
    oled = ssd1306.SSD1306_I2C(128, 64, i2c) # width, height, screen
    
    oled.contrast(contrast=155) #default 255
    
    oled.fill(0) # 1 fill white, 0 fill black = off
    oled.text('temp: ' + str(d.temperature()), 0, 0) # posX, posY
    oled.text('humi: ' + str(d.humidity()), 0, 10)   # posX, posY
    oled.invert(True) # invert screen
    oled.show() # display screen

    Until you call oled.show() nothing will be printed

  • Connecting GPIO16 with REST allows you to use ESP deepsleep function (to be precise, it allows you to wake up from deepsleep). When board wakes it will run all of the scripts that it would during normal powerup (so boot.py and main.py). According to the ESP specs, deepsleep drains around 10uA.
  • import esp
    esp.deepsleep(seconds * 1000000)
  • There is no way to programmatically turn off PW LED (power indicator led), so if you need low power solution (cause this led will drain most of your battery power), just use knife or something sharp – it should pop easily without damaging the board

Summary

For now I’m really impressed by this tiny thing. For me this project is much more fun than playing with RaspberyPi (which is, in my opinion, just overkill in projects like this). I have done some initial coding, mainly for ‘slave’ units that will connect to the network, get temperature and humidity, send this data to dango-based kinda-webservice and then go to sleep for five minutes. Now I’m trying to tackle the security, so I would like to use SSL, do some authentication that will send CRSF token from django to the unit, re-send this token while posting the data. That’s the idea for now.

 

Leave a Reply

Your email address will not be published. Required fields are marked *