Life of a NodeMCU with Python!


The inception of IOT in an engineer's life is probably with the word ESP8266 or NodeMCU or terrible and incomprehensible AT commands or just the word HARD! I have seen so many people including myself wanting to learn IOT with a wifi device like ESP8266 or nodeMCU but struggling after the first code! Frankly because the AT commands doesn't make any sense to me! (didn't at that time)


When I wanted to start, I was given a bunch of codes in Arduino and a serial circuit connection from Arduino Uno to nodeMCU board and not surprisingly most of the code didn’t even make sense!





Seeing this beautiful little piece with me always made me think, wouldn’t it be awesome if I can code it in Arduino or c++ or python and just upload it like Arduino?
I mean it has GPIO pins, Serial, I2C and SPI pins and a micro usb port and on top of that it has flash just like Arduino(s).

Well it turns out that I was right and there are many methods to program a nodeMCU board except those satanic AT commands. I finally learned two beautiful ways to code it and one of them is with Python.


Python is my favourite language and I have always challenged the world that it can do anything in the world thereby eliminating the need to learn multiple languages. Even in memory constraint devices like microcontrollers and nodeMCU python showed itself in a beautiful form of microPython. 



Before I bore you all out with more of my words let's know a little about nodeMCU.


Here's two of nodeMCU board that I have:-

NodeMCU Esp32

Esp32- Wrover 


I have an UBUNTU system, if you have a windows then the cmd prompt does the task. Now to powerup the board we can use micro-USB and connect it to laptop. Once it is powered on we will need the firmware. Now, remember I was blabering earlier about using the memory on-board to install a python or Arduino or something? Well, it's like this... nodeMCU is based on the microcontroller board ESP8266 so whatever we want to do should be burned/ uploaded on the cip as a binary file, like when you upload on Arduino the binary file of your sketch is created and uploaded on the AVR128p microcontroller that is onboard. So here we will be downloading a finary file (called firmware) of micropython so that we can use python in it.
So, here we go:
  1. Go to http://micropython.org/download#esp8266
  2. Download the latest firmware for 1024kb modules and above.
  3. Now to upload it will use esptool, don't worry I will be explainig the process in detail:
    a)For Windows: go to cmd prompt, type: pip install esptool
    b)For ubuntu/linux: go to terminal and type: pip install esptool
  4. Now Using esptool.py you can erase the flash with the command:
    a)For windows: esptool.py --port COMX erase_flash (replace X with your port, e.g COM3, an easy way to find out your port is through Arduino IDE)
    b)For Ubuntu: esptool.py --port /dev/ttyUSB0 erase_flash
    i. For esp32: esptool.py --chip esp32 -p /dev/tty.SLAB_USBtoUART erase_flash
  5. And then deploy the new firmware using:
    esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20170XXX.bin
    (needles to say, replace xxx with your version)
    For example, for me it is:
    For esp8266: esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 ./Downloads/esp8266-20171101-v1.9.3.bin
    For esp32: esptool.py --chip esp32 -p /dev/tty.SLAB_USBtoUART write_flash -z 0x1000 ~/Downloads/esp32-201

    Now remember the good old python console?
    Let us get that:- picocom /dev/ttyUSB0 -b115200
  6. Now have fun!!
Wait! Over? Now what? Yeah.. I can print("Hello World") but then...? I mean...like..?? :D
Well that's just begining! Now we are ready to scratch the bottom of the board and make every single bit of it to our use.
So, these are the sections and order in which we will be mastering the nodeMCU board:-
  • Knowing your esp board
  • General board control (using the GPIO pins)
  • Networking basics (connecting to wifi and creating wifi hotspot)
  • Networking Advanced (socket programming, https requests and mqtt)
  • Wired Connections (Serial,I2C,SPI)
  • Some specific drivers (OneWire, NeoPixel and DHT)
  • Creating your own libraries and space management.
So, buckle up, get some water, snack and drinks and let's get started as it is going to be a long, long journey! See you at the next post.

Comments

Popular posts from this blog

How to write your own Library in Arduino?

Let's Begin Now!!