By: Jonathan Envall (je223rk) for 1dt305 'Tillämpad IoT' @ Linneaus University.
A DHT11, temperature and humidity sensor, is used to gather information about air humidity and temperature. The sensor is connected using a LoPy4 and the data is sent over WiFi to be displayed with the help of the pybytes dashboard. The time frame for this project is a few hours, less if one has previous experience with micropython and Pycom devices/pybytes.
My original project idea was to monitor the soil moisture level on a specifically cherished plant at my home. But having ordered two sensors of the type "Capacitive Soil Moisture Sensor v1.2" and not managing to get either of them to function properly, I have instead gone with a simpler project where I gather air temperature and humidity data seeing as I already had purchased this sensor.
The soil moisture sensor behaved very differently on different input pins of the expansion board. This is expected since the different inputs have different characteristics, such as impedance, however even when the sensor seemed to be giving a reasonable value, said value never changed even though the sensor was put into a glass of water. And having already spent a decent amount of money and the course coming to an end, I decided not to try and order a different one.
Fig 1. The capacitive moisture sensor that wouldn't work.Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
This tutorial instead serves as a guide for a basic IoT project using the LoPy4. I monitor air temperature and humidity over 16 hours using a DHT11 sensor. It is a simple project but one that does the two arguably most important things to an IoT project, collect data and then upload said data to a server to be saved and monitored. I don't expect anyone who has taken the course to get any more insights since the project is the same as one of the examples given as a lecture, but it contains the necessary steps to get an IoT project up and running (on a mac computer)!
List of materials with links to the sites I purchased them on.
I have used the LoPy4 together with the expansion board V3.0 to connect my DHT11 sensor. This is done using the jumber cables.
The sensor was from a bundle of sensors which were bought together with the pycom devices for a total of 1178 SEK through electrokit.se.
Fig 2. The DHT11 sensor (Source)Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
The device was programmed on a mac computer using Atom (Atom.io) together with the pymakr plugin provided by Pycom (Pymakr installation guide).
Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Fig 3. The pymakr plugin is installed in Atom by going to the 'Install' tab in Settings and searching for pymakr
To monitor the data I have used Pycom's 'pybytes' service. In order for this to work, the LoPy4 must be flashed with the pybytes firmware. This was done using 'Pycom Firmware Updater' for Mac which can be found at docs.pycom.io.
The LoPy4 is programmed using micropython. With the pymakr plugin, it is possible to run micropython code directly through a REPL (Read Evaluate Print Loop) interface in order to do quick tests and experiment. When the code was completed, it was uploaded to the device via the upload feature of the plugin, marked in red below:
Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Fig 5. Circuit diagram with the connections to the sensor.
Wires are: Ground (Black), 3.3V (Red), Data (White).
The sensor was connected according to the circuit diagram above. I have used Pin 23 for the data connection. Since the sensor has a built in resistor, we do not need to add any extra resistance to the circuit and can simply plug the sensor into the LoPy4. Since i don't have access to a proper battery, the usb port of the expansion board was used for power. Usb can also be used to send data but here it was merely the power source.
The pybytes platform was used for data collection. This platform is very easy to use with the LoPy4 and connectivity was never a problem. Adding a LoPy4 device to pybytes is very simple, as you simply paste the activation token into the firmware updater tool used to flash the device with. The steps do to this can be found at docs.pycom.io/pybytes/connect/.
Pybytes provides a dashboard where signals and devices can be monitored, and which is very intuitive to use. One can even create appropriate plots to display the data in a meaningful way, these are shown further down this tutorial.
main.py
import time
import pycom
from machine import Pin
from dht import DHT
pycom.heartbeat(False)
th = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0)
time.sleep(1)
while True:
pycom.rgbled(0x000f00) #Set LED green, to show that something is happening..
#Read value
result = th.read()
while not result.is_valid():
time.sleep(.5)
result = th.read()
#Print the values in the console:
print('Temp:', result.temperature)
print('RH:', result.humidity)
#Send to pybytes
pybytes.send_signal(1,result.temperature)
pybytes.send_signal(2,result.humidity)
time.sleep(1)
pycom.rgbled(0x0f0f00) #LED is yellow, we are waiting..
time.sleep(59)
A DHT sensor library from JurassicPork @ Github was used (https://github.com/JurassicPork/DHT_PyCom). This library is loaded with the from dht import DHT
command, and is located in a /lib folder which is uploaded to the device along with main.
The data is transmitted once every minute over my home WiFi network using the Pybytes library API, spcifically the pybytes.send_signal(pin, value)
function is used (docs.pycom.io). When the device is connected to pybytes, a config file pybytes_config.json
is created containing the necessary information about the network, information which is entered during device activation (see the Platform chapter or docs.pycom.io).
Pybytes uses an MQTT protocol transmit data to and from the device, although in this project we only send data from the device to pybytes, and not vice versa. The MQTT protocol works as a message broker between the server (pybytes) and the device (in this case the LoPy4).
A dashboard was created on the pybytes platform using the tool 'Create new display' to make plots of the data coming from the two signals.
Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
The plots were made visible on the device dashboard and so the data could be monitored:
Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
The data is automatically saved in the database every time the device performs the send_signal()
call, which is once every minute.
The project turned out OK. I definitely could have improved the quality and made it more interesting by getting some more interesting air quality sensors such as ones measuring CO2 or volatile particles and then possibly measuring more parameters at once! However the project turned out to be a good way of getting used to the pybytes platform and micropython, among with the firmware updater tool.
Fig 8. The physical setup.Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
To gather some relevant data I let the sensor run for roughly 16 hours and recorded the following data on air temperature and humidity (between around 00:00 and 16.30):