Secondary Menu

IoT Made Ease: ESP-MicroPython-MQTT-ThingSpeak

About: Engineer, writer and forever student. Passionate to share knowledge of electronics with focus on IoT and robotics.
More About mjrovai »

In my previous tutorial, MicroPython on ESP using Jupyter, we learned how to install and run MicroPython on an ESP device. Using Jupyter Notebook as our development environment, we also learned how to read from sensors (Temperature, Humidity, and Luminosity), We use several communication protocols and methods, Analog, Digital, 1-Wire and I2C, this last one to display our captured data on an OLED display.

Now, on this tutorial using an MQTT protocol, we will get all captured data, sending them to an IoT service, ThingSpeak.com and to a mobile App (Thingsview), where we can log and play with data.

Step 3: Micropython, REPL, Jupyter

You must have a Micropython interpreter loaded on your ESP device. Once loaded, you should program your ESP using any of available ways/IDEs available, like:

REPL

Jupyter Notebook

Mu

ESPCut (Windows only)

... etc

On my tutorial, Micropython on ESP Using Jupyter, I detailed how to download and install MicroPython interpreter, ESPTool to manage ESP devices and how to use Jupyter Notebook as a Development Environment. Feel free to use what is more comfortable for you.

I usually make all development on Jupyter Notebook, and once I get the final code, I copy them to Geany and load it on my ESP using Ampy.

A simple function: adc.read() can be used to read the ADC value. But remember that the internal ADC will convert voltages between 0 and 3.3V in correspondent digital values, varying from 0 to 1023. Once we are interested in "Luminosity", we will consider Max light as the maximum captured value from the sensor (in my case 900) and minimum light that in my case is 40. Having those values we can "map" the value from 40 to 900 in 0 to 100% of luminosity. For that, we will create a new function:

As an option, I will also include the LED to be ON when we start reading sensors, going OFF after that data is displayed. Doing this will help to confirm that the program is working when we have the ESP disconnected from PC and running automatically.

As a response, we will get boot.py, that is the first file that will run in the system.

Now, let's use Ampy to load our python Script LocalData.py as /main.py, so the script will run just after boot:

ampy put localData.py /main/py

If we use the command amp ls now, you will see 2 files inside the ESP.: boot.py and main.py

Resetting your ESP, will make the program localData.py run automatically, displaying the sensor data on display.

The above Terminal print screen shows what we have done.

With above code, the display will be shown only once, but we can define a loop on main() function, that will show data on every defined time interval (PUB_TIME_SEC), and for example, until we press the button:

The variable PUB_TIME_SEC must be declared by the time that you want your samples.

To enhance more our code, would be good to inform that we will go out from the loop, for that we will define 2 new general functions, one for clear the display and another to blink the LED on a certain number of times.

Step 7: Connecting the ESP to Local WiFi

The network module is used to configure the WiFi connection. There are two WiFi interfaces, one for the station (when the ESP8266 connects to a router) and one for the access point (for other devices to connect to the ESP8266). Here, our ESP will be connected to local network. Let's call the library and define our network credentials:

import network
WiFi_SSID = "YOUR SSID"
WiFi_PASS = "YOUR PASSWORD"

The function below can be used to connect the ESP to your local network:

Step 8: The ThingSpeak

At this point, we learned how to capture data from all sensors, displaying them on our OLED. Now, it is time to see how to send those data to an IoT platform, the ThingSpeak.

Let's begin!

First, you must have an account at ThinkSpeak.com. Next, follow the instructions to create a Channel and take note of your Channel ID and Write API Key.

Above you can see the 5 fields that will be used on our Channel.

Step 9: MQTT Protocol and ThingSpeak Connection

MQTT is a publish/subscribe architecture that is developed primarily to connect bandwidth and power-constrained devices over wireless networks. It is a simple and lightweight protocol that runs over TCP/IP sockets or WebSockets. MQTT over WebSockets can be secured with SSL. The publish/subscribe architecture enables messages to be pushed to the client devices without the device needing to continuously poll the server.

The MQTT broker is the central point of communication, and it is in charge of dispatching all messages between the senders and the rightful receivers. A client is any device that connects to the broker and can publish or subscribe to topics to access the information. A topic contains the routing information for the broker. Each client that wants to send messages publishes them to a certain topic, and each client that wants to receive messages subscribes to a certain topic. The broker delivers all messages with the matching topic to the appropriate clients.

ThingSpeak™ has an MQTT broker at the URL mqtt.thingspeak.com and port 1883. The ThingSpeak broker supports both MQTT publish and MQTT subscribe.

In our case, we will use: MQTT Publish

The figure describes the topic structure. The Write API Key is required to publish. The broker acknowledges a correct CONNECTrequest with CONNACK.

The MQTT protocol is supported in a built-in library in the Micropython binaries -- this protocol can be used send data from your ESP8266, over WIFI, to a free cloud database.

And that's it! We are ready to send data to ThinsSpeak, simply using the 3 lines of code below:

client.connect()
client.publish(topic, payload)
client.disconnect()

Now, if you go to your channel page (as mine above) you will see that each one of the 5 fields will have data related to your sensors.

Step 10: Sensor Data Logger

Now, that we know that with only a few lines of code it is possible to upload data to an IoT service, let's create a loop function to do it automatically at a regular interval of time (similar to what we have done with "Local data").

Using the same variable (PUB_TIME_SEC), declared before, a simple main function to continuously capture data, logging them on our channel would be:

Note that only the "payload" must be updated, once "topic" is related to our channel credential and will not change.

Looking for your ThingSpeak channel page, you will observe that the data will be loading continuously to each field. You can cover the LDR, put your hand on temp/hum sensors, press the button, etc. and see how the channel will be automatically "log" those data for future analysis.

Usually, for Data Logging, we should try to use as less power as possible, so, we would not use the LED or display locally. Also, it is common with ESP devices, put them on "deep sleep", where the microprocessor will be on its state of minimum energy until it is time to capture data and send them to the IoT platform.

But, once here the idea is learning, let's also include the display and LED as we did before. Doing that, our "logger" function will be:

And press the ESP - reset button. You will have the ESP capturing data and logging them on ThingSpeak.com until the bottom is kept pressed (wait for the LED to blink 3 times and the OLED turn off).

Step 11: The ThingView App

The data logged can be view directly on ThingSpeak.com site or via an APP, for example, ThingsView!

ThingView is an APP developed by CINETICA, that enables you to visualize your ThingSpeak channels in an easy way, just enter the channel ID and you are ready to go.

For public channels, the application will respect your windows settings: color, timescale, chart type and the number of results. The current version supports line and column charts, the spline charts are displayed as line charts.

For private channels, the data will be displayed using the default settings, as there is no way to read the private windows settings with the API key only.