Friday, 14 April 2017

Just starting with NodeMCU

I have just received a couple of NodeMCU's which I plan to use for some security devices.


These are tiny circuit boards about 50mm long x 25mm wide. The chip is a computer processor with WiFi built in attached to a board with pins on for easy development of a prototype. They are designed to be low cost for use as Internet of Things (IoT) processors. UK stock cost me a massive £8 each but from China they are less than £3 each, including shipping!

They are used by the same people that like to make small gadgets using the tiny Raspberry Pi computer and the Arduino processor boards.

Before the NodeMCU's arrived I'd been searching the Internet to find out how to get started. The trouble is that it gets confusing. There is lots of outdated information, the NodeMCU is also referred to as ESP8266, there are multiple development environments and so on.

This is a list of what has confused me and the conclusions I have made, so far.

NodeMCU or ESP8266
The ESP8266 is the processor which has built in Wifi. This is used as an add-on board for Arduino as well as a stand alone product called NodeMCU.

NodeMCU
This uses the ESP8266 wifi enabled processor and includes a serial to USB adapter on board.
It is that latter bit that is significant. You don't need any extra hardware to get going with the NodeMCU. Just power.
As far as I can tell the NodeMCU includes a firmware implementation of the LUA environment. Not just the hardware.
If you use one of the other development environments LUA will be replaced and so arguably it is no longer a NodeMCU board! Very confusing.
I will refer to NodeMCU meaning the hardware in the configuration expected by the NodeMCU implementation of the ESP8266.


There are several different manufacturers but as far as I can tell there are only minor differences between them and the pin outs are the same. There is a smaller and larger board. I prefer the pin outs on the larger board.

The boards I received are branded 'Amica.'

How to power the NodeMCU
The simplest is to just use the USB port. Plug in a USB power adapter or a cable from the USB port of a PC.
The preferred method for a wired in power source is to use the Vin pin. This connects to a voltage regulator on the board so you can connect any DC power source between +5V and +10V. The regulator can handle up to 800mA which is enough for most small circuits.
There are also several 3.3V pins which, as far as I understand, are in and out so they provide 3.3V for external devices or can be used with a regulated power supply or to plug batteries in to. The ESP8266 chip will apparently work with anything between 2.8V and 3.5V, so 2x AA batteries should do it. I have not tested this yet.
There's some more detailed explanations on the following page:
http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/powering-the-esp-12e-nodemcu-development-board/

Development environments

This is where it gets even more confusing.

NodeMCU firmware
The firmware is the programme running on the ESP8266 processor. You can change that firmware. Exactly what that is and how you change it depends on what development environment you use:
- LUA comes as standard.
- Python needs to be built and deployed to the NodeMCU.
- Arduino IDE compiles the code and installs it as part of the upload process to the NodeMCU.


- The standard for a NodeMCU is LUA.
This is a high level language needing very few skills to get going with.
It's disadvantage is that it uses up a lot of the in-built memory for the interpreter!
Despite it being the original environment, I ruled this out fairly early on and have not tried it:
http://nodemcu.com/index_en.html
Documentation: https://github.com/nodemcu

- Python
There is a whole community developing this:
https://docs.micropython.org/en/v1.8.6/esp8266/esp8266_contents.html
I've not looked in great detail at this but it looks like a fully implemented solution with some good features. I've done some Python programming before but I'm not a fan of the lax structure of the Python language. Others however do like it and it is very popular.

- Arduino IDE
This is the one I have started to use. It uses C++ syntax and libraries and appears to be the most common choice. The code is complied before being uploaded to the ESP8266 so uses the least amount of memory.
You need the Arduino IDE:
https://www.arduino.cc/en/Main/Software
Then add the extension for the IDE to support ESP8266 based boards. This is available as a package that is easy to install. You just include this one line in the Arduino IDE preferences:
http://arduino.esp8266.com/stable/package_esp8266com_index.json

See the following tutorial:
http://www.instructables.com/id/Quick-Start-to-Nodemcu-ESP8266-on-Arduino-IDE/


The instruction imply that the ESP8266 boards install automatically. They do not. They get added to the Board Manager and you need to browse the list in Board Manager, select the ESP8266 based boards and hit install.


Once that is installed you need to select the NodeMCU board from the list of boards.

Finally before being able to upload your programme to the NodeMCU you need to find out what COM port your NodeMCU appears as on your computer. The COM port is assigned when you plug in the NodeMCU to the USB port.


Serial USB Connection
The back of my board says to use a CP2102 driver at only 9,600 baud. The default in the Arduino IDE is much faster at 115,200 baud which worked fine for me.


I had installed the Arduino IDE prior to connecting the NodeMCU. The Arduino IDE also installs some serial drivers. I don't know where the driver came from but when I plugged in the board to the PC USB port it installed a CP210x driver and displayed the COM port it was assigned to. In my case COM 4 but that will depending on which USB port it is connected to and what else uses COM ports on the machine.



You can lookup the COM ports used in Device Manager or watch the driver install, which shows the COM port.
I'm using Windows 10 but this was similar in Windows 7 and 8.1.

Don't get misled by the references to an FTDI cable or serial adapter that you might come across in other articles. That adapter cable is not necessary for a NodeMCU. The serial to USB is built in, that is what the CP2102 chip does. The adapter cable is only needed for the add-on shield type boards typically used with Arduino's that do not include that extra chip.

Hello World
When learning a programming language the first thing you typically do is to display 'Hello World' on the screen. Something easy to do that checks that all the basics are working. The electronics equivalent of that, in this context, is to blink an LED.


I copied in the sketch, as the code is referred to, pressed the button in the Arduino IDE to upload it to the NodeMCU and waited for a minute or two as it compiled and uploaded. It worked perfectly.

==

Useful sites:

ESP8266 using Arduino IDE, code library documentation:
https://github.com/esp8266/Arduino/blob/master/doc/esp8266wifi/readme.md

Arduino language reference:
https://www.arduino.cc/en/Reference/HomePage
Arduino standard and add-on libraries. Not directly useful for ESP8266 but might be handy:
https://www.arduino.cc/en/reference/libraries


==

Update:
I found out by accident that the Vin pin will also supply the output voltage from the USB port. I don't know how reliable this is but it powered the 5V ultrasonic sensor I was using.

==

I purchased the same make NodeMCU from two different suppliers:
UK for speed - http://www.ebay.co.uk/itm/231983263238 - alictronix
China for very low cost - http://www.ebay.co.uk/itm/222176238324 - worldchips
Both suppliers have been very good and I would re-order from them.

==

9 comments :

Sridhar said...
This comment has been removed by the author.
Sridhar said...

John,
Accidentally stepped on to this blog. I exactly used the same. Amica nodeMCU board, Arduino IDE for development. I echo your thoughts and made similar notes about VIN ( in built regulator and its operating voltage), powering sources possibilities etc. Thanks for the blog especially as I dont need to write one again now as it exactly is what I wanted to write..I just started 2 weeks back and built the following: 1) sending Temp & Humidity data to MQTT cloud, Thingspeak 2) LDR sensor 3) PIR sensor.
What I am struggling to achieve and working on now :
1) Power the nodeMCU with battery source 2) Trying to understand the use of relays and ways to use it 3) I see a lot of transistors/diodes & LMS 2596 regulator as items needed in many blogs/articles. Trying to understand the relevance and the need of using them. 4) Ultimately want to make my home automated ( controlling lights and fans) using wireless board and mobile

Let me know if you can help me in the above understanding.

John C Brown said...

1. Is easy with the NodeMCU because of the built in regulator. 3x or 4x AA batteries in series will power the NodeMCU. Just connect + to Vin android - to any ground pin.

John C Brown said...

2. Using relays is best looked up elsewhere. Watch out though most relays need 5v and that takes me to your point 3.

John C Brown said...

3. The external regulator and transistors etc. are needed for some circuits because the external components need more current or a higher voltage than the nodeMCU can power.

John C Brown said...

I'm building alarm bits and have plans for a wireless remote control. I'm not sure when I'll get time to work on it all. Loads of people use the NodeMCUs for home automation so you should be able to find enough information on other blogs and forums. Good luck with your projects.

John C Brown said...

That was AND not android as my phone's autocorrect decided!

Sridhar said...

Thank you John ! I just got the hello world of Relay interaction with ESP8266 working.

John C Brown said...

Excellent. :-)