Getting started in the Internet of Things (IoT) with the NodeMCU platform
If you have been wanting to get into the Internet of Things (IoT) but are not sure where to start, or you are enthusiastic about IoT and want to bring others into the field, I am going to describe how to get started with some easy-to-use hardware and software for as little as $20.
Above: a sophisticated IoT project still using only 4 components costing under $20.
This article is is targeted at those who have some knowledge of computer programming (at least that it exists) but little or no experience with digital electronics. I aim to introduce you to the tools that you will need and allow you to get started experimenting with The Internet Of Things.
If you are interested in conducting a group "Hackathon" at work or
school, this should be all you need to get your teams started.
If you're a corporation looking to conduct a team building
hackathon consult me.
The basics of IoT platforms
You're probably reading this on a computer that consists of a central processor, some memory chips, some file storage (magnetic drive or more memory chips), a keyboard or touch-panel, and an LCD screen. These items are the basics of all computing: processing, volatile (temporary) storage, nonvolatile storage, input devices and output devices.
IoT devices are just the same, except everything is much smaller; they are typically powered by a microcontroller which is a term for devices that combine all the traditional parts of a computer onto a single chip (or an integrated multi-chip module).
Just how small can vary. The range of scale and power available for IoT devices encompasses:
- 8-bit microcontroller devices (such as the well known Arduino family), size around 5cm, costing under $5
- 32-bit microcontroller devices, size around 5-10cm, costing
- Small form-factor PCs (such as Raspberry Pi, and various similar devices), size under 10cm, costing under $100
I am going to talk about the middle class of 32-bit microcontroller devices in this article. At the low-end, the Arduino and similar are excellent but are not powerful enough to connect to the internet on their own. At the high end, there's a place for the more powerful and expensive devices, but I'll talk about how I use those boards another time.
In the microcontroller realm you typically have the following resources:
- For processing you have a processor that runs between 1 and 100 million instructions per second (MIPS).
- For memory, you typically have a frustratingly small amount of RAM, from 1kB to a few hundred kB.
- For file storage, you have somewhere from 8k to a few megabytes of FLASH memory.
- For input and output you have from two to twenty digital "pins", and some communication interfaces such as Asynchronous serial, Inter-IC communications Bus (I2C), Bluetooth, or WiFi. Under the control of your programs, the IO pins can be used singly or in groups to control buttons, lights, sensors and screens.
Above: Classes of embedded computers: L-R DigiSpark, Arduino Nano, NodeMCU, Raspberry Pi
A modern 32-bit microcontroller is about as powerful as a home computer from the 1990s, but shrunk to the size of a postage stamp.
These devices can be programmed in assembly language or low-level languages such as C and C++, but I won't go into those here. Beginners will be much more comfortable with higher-level languages such as Lua and Python.
To develop program code for a microcontroller you write your programs on a normal sized computer and then "upload" the program to the microcontroller. This can be done with a wired connection or sometimes over wireless links.
For the rest of this article I am going to talk about one particular combination of hardware and software that I think makes an economical and beginner-friendly platform for IoT experimentation, the NodeMCU platform.
The hardware - Espressif ESP8266 Wifi Modules
The ESP8266 is a tiny microcontroller chip invented by Espressif in China. It is intended for very low cost smart devices such as internet controlled light bulbs and power switches. There are a number of "compute modules" that combine the ESP8266 chip with some flash memory and power components to provide a postage-stamp-sized module that can be incorporated into larger circuits. Sometimes these modules are then mounted on a carrier board which adds more power circuitry and some switches. One of the most beginner-friendly is the reference design from the NodeMCU project. The "NodeMCU Developers Kit" (DevKit) is an open-source design that is available from a number of manufacturers for under $5 (more about this below).
Above: Two different ESP modules (left) and a NodeMCU DevKit board consisting of module plus extra circuitry (right)
The software - NodeMCU
The NodeMCU project provides a firmware distribution for the ESP8266 modules. The term "firmware" is used to describe software that you program into hardware just once (get it? It's in between hardware and software. Engineers didn't get out much before computers became cool). You can think of the NodeMCU firmware as a very simple operating system for the module environment; you combine the standard firmware with your custom-written application software to solve some particular problem.
The NodeMCU firmware is written in C, but provides an environment for you to upload and run application programs written in Lua (an easy to learn scripting language). So all the hard C programming has been done, meaning you (the beginning IoT programmer) can work in the much more friendly Lua language. To use NodeMCU, you must install the NodeMCU firmware on your device just once, after that you can simply uload programs written in Lua into the device's flash memory over a USB cable, to make the device do whatever is desired.
The NodeMCU project also provides the reference hardware design for their Development Kit board, which is ideal for running the firmware, but the firmware itself will run on almost any ESP8266 module, not just the ones based on the reference design.
The WeMos D1 and D1 Mini boards are another design that is ideal for use with NodeMCU.
The NodeMCU development kit board gives you around 10 general purpose IO pins, WiFi wireless ethernet, asynchronous serial port, plus a USB socket for power and for uploading software to the device. Other ESP8266 modules may have more or fewer IO pins. You can add bluetooth if desired by connecting a bluetooth module to the asynchronous serial pins. You can also store files in whatever whatever space in your module's flash memory is left-over after the firmware and your application software are uploaded. The NodeMCU DevKit board has 4MB of flash memory, of which around 3MB remains available after installing the firmware.
The NodeMCU DevKit board has a blue light-emitting-diode (LED) connected to pin D4, and a push button (labelled "Flash") connected to pin D3, so if you want to dip your toe into IoT for the least amount of money, you could buy the DevKit board and nothing else, powering it by USB cable from your computer or from a spare USB charger.
Above: The NodeMCU DevKit reference design is small enough to fit into the lid of a mint tin, but powerful enough to connect to the Internet.
Writing and uploading code
NodeMCU has some example code on their website, and there is a comprehensive documentation site for the NodeMCU Lua programming environment. I'll be posting my example projects here soon, also.
There are a number of software IDEs (Integrated Development Environments) available for editing and uploading Lua code. The quality of IDEs in the embedded world might be a little less than for mainstream computers, however I have found the ESPlorer application is serviceable, if a little quirky. It's compatible with Mac, Windows and Linux.
Most ESP modules ship with a firmware that is intended to work as a "WiFi Modem" (the so-called AT Firmware). The first thing you'll need to do is wipe this and install the NodeMCU firmware by following these instructions.
You can obtain the latest NodeMCU firmware from this service. I recommend you start with the default modules plus HTTP, I2C, MQTT, PWM and SPI. If you buy an LCD screen add the UCG module.
Once your board is running the NodeMCU firmware, there are three main ways to get your Lua application program code onto your module:
- Use a USB-to-Serial converter between your PC's USB port and the NodeMCU board's asynchronous serial interface. The NodeMCU DevKit has a built-in USB converter, so you just need a USB cable.
- Connect a Bluetooth-to-Serial module to the NodeMCU board's asynchronous serial interface, and pair the bluetooth module with your PC.
- Use one of the above methods to install a program on your board that lets you send further programs to it over WiFi.
We'll cover the first two of these methods in the next article in this series. From the point of view of your PC, they are the same, except that in the first case you are connecting to a virtual serial port implemented by the USB converter, and in the second case you are connecting to a virtual serial port implemented by the bluetooth module. USB is slightly cheaper and easier to work with, but do consider Bluetooth if you have a Mac, especially those with limited USB type-A ports.
Connecting your module to the outside world
The NodeMCU programming environment provides a number of library functions grouped into modules, and has programming modules that provide high level abstractions for many common input and output devices. Where there is no high level abstraction, the low level GPIO module provides direct access to the input output pins and allows youto write your own programs to interface with almost anything.
The NodeMCU firmware provides a number of core software modules to perform various tasks (such as read particular models of sensors, and control particular models of display), and there are even more modules availble as open source contributions that you can add to your base firmware. There are web services that will construct a new firmware image for you containing the set of modules that you select, or you may upload the NodeMCU source code, import some extra modules, and compile a firmware image that contains all the modules you desire.
The bare basics of digital electronics
Computers do all their thinking with binary logic (ones and zeros), and represent binary logic in the physical world using voltage. A logical one is high voltage, a logical zero is no voltage. When a computer talks to another computer or a peripheral device, there's one or more wires which are either at high voltage or low voltage. That's basically all there is to it. Skip the rest of this section if you like.
For a long time, most computers used 5 Volts as the standard high voltage. As computers became faster and used more power, the industry shifted toward 3.3 Volts as the standard voltage.
The compute-modules we will be using follow the 3.3 Volt convention, but you should be aware that a lot of stuff still uses 5 Volts, and some 3.3 Volt equipment will be damaged (possibly fried) if given 5 Volts where it expects 3.3 Volts. The ESP chips are claimed to be "5 Volt Tolerant" but there have been reports of chips failing when exposed to 5 Volts, so lets stick to 3.3 volt devices in our projects. There are inexpensive conversion circuits that you can use where you need to connect 5 Volt signals to devices that may be harmed by voltages above 3.3 Volts
Solderless breadboading and peripheral modules
Commercial electronic devices are made of components joined together using solder, which is an alloy of various metals that has the particular quality of melting at a relatively low temperature (around 200 degrees). (Don't panic, you will not need to do this.) In soldered circuits, components are placed in contact with other components, wires, or flat panel "circuit boards", and then joined permanently by flowing molten solder onto each electrical connection using a hand-held soldering iron or more complex tools. Once the molten solder cools and solidifies, components are permanently joined.
The prospect of using molten metal to construct circuits is a bit daunting for beginners, so the good news is you can get a very long way indeed without needing to do any soldering. The projects I teach can be built without any use of a soldering-iron. Soldering is an inexpensive, compact and robust way to permanently connect components, and should be considered once you have your design perfected and want to make lots of a particular circuit, but to start out we will use other techniques. The trade-off is that the things we make will be a little bit larger, and more delicate (they might come apart if you drop them).
To make relatively reliable but inexpensive projects, we will use these tools:
- Solder-less breadboards.
- These boards have rows of spring-loaded sockets that are connected together electrically. A wire placed in a socket will be connected to the other sockets in the same row.
- We'll use inexpensive pre-cut lengths of wire fitted at each end with either metal pins suitable for plugging into breadboards, or with sockets suitable for accepting pins. You can even cut your own jumpers from a roll if you use the right kind of wire.
- Peripheral modules
- We will interface our compute module to various kinds of Peripheral Input/Output (IO) modules, beginning with a starter kit of handy modules
Above: various types of solderless breadboards, the smallest are repositionable on a Lego-like baseboard. Each socket is electrically connected to the other four sockets in the same row of five.
Above: Jumper wires for inter-connecting non-adjacent sockets, and for connecting to off-board modules
Above: Several kinds of peripheral input and output modules
Imagine the most cliched 1960s computer (as seen in old movies) that you can, the above techniques of modules and interconnecting wires are exactly the technologies that began the computer revolution. To recap, we will have a compute module, installed on a breadboard, and connected by jumper-wires to peripheral modules which might be on the same breadboard, on an adjacent board, or mounted on a panel.
Above: Mid-1960s IBM computer component showing panel-mounted I/O and plug-board wiring.
Above: NodeMCU DevKit board mounted on a solderless breadboard and connected to module consisting of three full-colour Light Emitting Diodes. On these breadboards, each socket is electrically connected to the other four sockets in its row.
Simple outputs (e.g. for Lights)
To control lights, or motor modules the gpio firmware-module can be used to set a pin to a high or low voltage using the gpio.write function. The pins on the ESP and most microcontrollers can act as either inputs ("reading" the voltage presented to the pin) or outputs ("writing" a voltage to the pin). We must set the mode of a pin with the gpio.mode function to use it as an output.
Microcontrollers don't output very much power on their IO pins, typically only just enough to light up a small light-emitting-diode (LED), so if you are controlling higher powered devices like relays or motors, you will need to use a power boosting circuit. Generally modules for relays and motors include this boost circuit so can be connected directly to your output pins.
When you use a light-emitting diode (LED) you generally need another component called a 'resistor' to set the brightness by limiting how much power may pass through the LED. Without a resistor the LED may consume too much power and burn-out. Pre-built LED modules will often include all the extra power-limiting circuitry you
Above: A Light-Emitting Diode (LED) module connected so that it can light up red or green under control of the NodeMCU DevKit
Simple inputs (eg. for Buttons)
If you configure a pin as input by calling gpio.mode then it does not output any voltage, instead any voltage applied from outside will be reported as a logical one or a zero when gpio.read is called. When the NodeMCU environment starts up, all the pins are in input mode unless you reconfigure them in your code.
The simplest way to connect a switch is to have a switch between the pin and ground (zero volts) and to have other circuitry that presents high voltage when the switch is not pressed. The extra circuitry can be part of a switch module, or provided by the "internal pull up" circuit inside the microcontroller. Again, pre-built switch modules should include any extra circuitry that is needed.
A digital input pin is going to convert whatever voltage it sees into one of two values. Typically anything from 0 Volts up to about half of the high level will read as a zero, and anything higher than that will read as a one. Some microcontrollers can read "analog" voltage where the actual voltage is reported as a number proportional to the voltage (say converting a voltage between zero and 3.3 volts into a number between 0 and 255). We'll stick to digital inputs only here.
Above: A button module connected to a NodeMCU DevKit
Once we have a Thing with buttons and lights, we are ready to join the Internet Of Things, typically via wireless ethernet (WiFi)
There are four modules in NodeMCU for talking to the Internet:
For configuring the wifi hardware (as an access point, client, or both) and scanning and joining networks
For making network connections or implementing servers
For making HTTP requests or implementing servers
For publishing messages to the Message Queue Telemetry Transport, or subscribing to topics on a message server
Screens and more
So far we have discussed only the simplest input and output, switches and lights are from the point-of-view of a microcontroller communicating only a single binary digit --- one 'bit' --- of information, something is either on or off. More complex information can be communicated in two ways: by using multiple pins, or by using one pin (or a few pins) as a serial communication bus.
There are a couple of different ways of making a serial bus, from most to least complex these are:
- USB (Universal Serial Bus) is very popular but generally too complicated for most micro-controllers and modules
- TTL Serial (Asynchronous serial link) is an ancient protocol once commonly used for modems and printers, and still used to program many microcontrollers
- I2C (Inter Integrated-Circuit bus) uses two pins (a clock and a bi-directional data pin). This is often used to talk to sensors and simple displays.
- SPI (serial peripheral interface) uses one pin in each direction plus some clock signaling and device identification pins. It's often used to talk to graphical display modules, sensors and memory cards.
- OWB (One-wire bus) uses a single pin that acts as clock and bi-directional data; it's slow but cheap. A popular temperature sensor is the best known use.
- Various simple proprietary signalling mechanisms
Most microcontrollers have hardware and software to communicate with several of these technologies. As long as you know which protocol a particular device module expects, you should be able to talk to it.
The good news is that the NodeMCU programming environment has built in firmware modules to talk to many different kinds of serial IO devices such as sensors, addressable strings of LEDs, and various kinds of displays. It also has lower level modules for each kind of serial bus, so that if you have a hardware module that is not directly supported you can write your own interfacing code.
As an example, one could construct an adjustable thermostat consisting of a temperature and humidity sensor, a graphical liquid crystal display, a rotary dial switch and a push-button switch entirely using off-the-shelf modules supplied as part of the NodeMCU environment.
Above: A sophisticated but easy to assemble IoT project consisting of a rotary dial switch, an LCD Screen, and an InfraRed proximity detector.
See Ideas below for some more project examples.
Here is what to buy to get started. I will present three lists - A basic starter set, a comprehensive but inexpensive set of modules, and some stretch options.
As I mentioned above, for the very budget-conscious, you can buy just the NodeMCU DevKit board itself and experiment with its on-board button and LED.
The item listings below are at AliExpress.com which is a Chinese electronics and consumer goods bazaar. Sometimes items go out of stock or change price unexpectedly, I'm happy to advise if you are unsure about anything.
Basic starter set
The bare minimum starter kit currently costs around US$12 if you order the recommended items below (I have no commercial interest in these recommendations):
- NodeMCU development board- $3.18
- CPU with wifi and USB interface. This has one light and one button built in.
- A 400-hole breadboard- $1.00
- This breadboard will accomodate your NodeMCU board and leave space for plugging in other modules if you so desire
- 10cm Jumper wires- $1.55
- A basic set of multi coloured jumper wires can be peeled apart singly, or in twos and threes, as needed.
- Button module- $0.95
- You probably want one or more buttons so that you can send messages to the internet. Look at the sensor kit in the next section for more input options
- Light-emitting-diode (LED) module- $0.70
- This module actually has three LEDs - one red, one green, one blue, in the same package. You can use just one colour, or mix colours.
- A USB battery - $3.91
- A rechargeable power bank is the best way to power your projects. An old phone charger works too. Consider buying locally as some countries will not accept batteries by airmail. If you don't already have a micro-USB cable, they cost around one dollar.
Above: all the items in the $12 basic IoT starter kit.
If you are buying for a team hackathon, consider searching for the same item plus "10pcs" or "100pcs" you will often find bulk items at an even lower cost.
A comprehensive set of Input/Output modules
Once you have a basic kit, you might like to experiment with things other than buttons and lights. A kit containing one each of practically every kind of sensor costs a little over US$10.00:
Above: A $10 sample-bag of peripheral input and output modules
So for around $20 total outlay you can be up and running with the ability to build many kinds of IoT projects.
You might like to consider these optional extras:
- Bluetooth-to-serial interface - $2.78
- If you find plugging and unplugging USB to be annoying, or have an ultralight notebook with limited USB, you can use Bluetooth to upload to your board. You will need to buy or borrow a USB-to-Serial converter, below, to set up this module. I'll cover how to use this module in another post.
- USB-to-serial interface - $1.50
- If you have trouble finding drivers for the USB chip built into your NodeMCU module, or if you are using an ESP board that lacks a built in USB interface, the FTDI USB-to-serial converter has the best supported drivers.
- Longer jumper wires - $2.55
- Jumper cables are commonly available in 10cm, 20cm or 30cm lengths. While the shorter ones are most useful, a set of longer cables is handy to have.
- Shorter jumper wires - $1.68
- If you need to join nearby rows on a breadboard, a short wire that is exactly the right length can be neater than a jumper cable
- 128x160 LCD Screen - $3.15
- A small video screen lets you show text, graphics and menus. This screen uses the ST7735 chip that is supported by NodeMCU's ucg module.
- 320x240 LCD Touch screen - $7.62
- A larger LCD screen with touch panel, which can take the place of a whole lot of buttons. This screen uses the ILI9341 chip that is supported by NodeMCU's ucg module.
- Mini breadboards - $3.81 for 6
- These small breadboards clip together or can be stuck to a surface. Handy for modules that are too wide for a single board.
- Small breadboards and a baseboard - $3.55 and $2.82
- These boards can be snapped onto a Lego-like baseboard (not Lego compatible) which lets you arrange them exactly how you need them
Enclosing your projects
Often a project sited on a breadboard or two needs no box. If you want to box it up, here are some suggestions:
- Use plastic food containers from the supermarket
- Pegboard and zipties
- Buy project enclosures from your hardware store or AliExpress
- Make custom enclosures if you have access to a CNC Mill or Laser cutter (try your local Makerspace)
Above: suggestions for enclosing your projects.
What can I build
Future articles will go into the practicality of of setting up your board, and some some example projects, but here are some possibilities for IoT in the home and workplace to get you thinking:
- A doorbell that sends a notification to your phone wherever you are
- A sensor that alerts you when the toaster has popped up
- Measure the temperature outside, and in each individual room inside
- Send a notification when an item is placed in the mailbox
- Display real-time status of your projects or software builds with coloured lights
- Display the time of the next booked meeting in a meeting room
- Send an alert if it starts to rain
- Report the occupancy of the visitor carparks in your building
- Answer the question "Oh, no, did I forget to close the front door?"
Above: temperature and humidity sensor built on a small breadboard. Settle once-and-for-all whether Jane's office is really colder than Bob's!
Above: NodeMCU module wired up to modules for combined dial/push switch, infrared object sensor and a small LCD screen. This project sends you a reminder alert when your toast is ready.
Above: Wemos D1 module with bluetooth-to-serial converter and an interface board for controlling an illuminated pushbutton switch. Intended as a "team meeting alarm".
I found your blog entitle "Make The Future" article "Program Arduino and ESP 8266 projects over Bluetooth from Mac" (http://blog.unixbigot.id.au/2016_09_01_archive.html). and you show that the 1.8" TFT 128x160 can be used with NodeMCU. I also trying to do something like you but have some problem. Could you please advice me.
I use the WeMos D1 mini board to connect the 1.8 TFT 128x160 display. I also flash the new current version 188.8.131.52 of NodeMCU firmware and test with the lua examples code. However, it just show full white screen. I don't know what am I doing wrong. Could you please guide me or point me the main point or any related resources.
Best regards, Thank you.
Sorry, for my duplicate post.
I have written some instructions for how to connect an LCD screen here
Thank you very much.
Post a Comment