![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
temperature, process the data and publish a message to temperature analyzed whenever it’s relevant. This is where InfluxDB comes into play; the backend application needs to store the data somewhere to be able to get the daily temperature average or a temperature forecast that is more relevant to the end user than single-point measurements.
The most popular use cases of IoT sensors are asset tracking (location of trucks and goods around the world), remote-area monitoring (temperature and humidity in a farm, for instance), resource optimization (energy and water consumption) and location/workplace analytics (pollution, noise and air quality).
In this article, you’ll build an example based on the remote-area monitoring use case by creating a fake smart temperature sensor.
temperaturetopic. Then you set up InfluxDB, and lastly, you create a backend application that consumes messages from the temperature topic and stores them in the database.
You can follow along with this tutorial using this GitHub repository.
paho-mqtt library:
pip install paho-mqtt
pip install Faker
temperature.
Create a new Python file called smart_sensor.py and use the following code:
"""
MQTT Smart temperature Sensor
"""
import time.
import paho.mqtt.client as mqtt
from faker import Faker
# connect to the MQTT broker
MQTT_BROKER_URL = "mqtt.eclipseprojects.io"
MQTT_PUBLISH_TOPIC = "temperature"
mqttc = mqtt.Client()
mqttc.connect(MQTT_BROKER_URL)
# Init Faker, our fake data provider
fake = Faker()
# Infinite loop of fake data sent to the broker
while True:
temperature = fake.random_int(min=0, max=30)
mqttc.publish(MQTT_PUBLISH_TOPIC, temperature)
print(f"Published new temperature measurement: {temperature}")
time.sleep(1)
python smart_sensor.py
temperature topic and store data in the database.
docker-compose.yml file that defines the following configuration:
version: '3.3'
services:
influxdb:
image: influxdb:2.0.7
environment:
DOCKER_INFLUXDB_INIT_MODE: setup
DOCKER_INFLUXDB_INIT_USERNAME: ${INFLUXDB_USERNAME}
DOCKER_INFLUXDB_INIT_PASSWORD: ${INFLUXDB_PASSWORD}
DOCKER_INFLUXDB_INIT_ORG: ${INFLUXDB_ORG}
DOCKER_INFLUXDB_INIT_BUCKET: ${INFLUXDB_BUCKET}
DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: ${INFLUXDB_TOKEN}
ports:
- "8086:8086"
.env file to define the environment variables required in this docker-compose.yml:
INFLUXDB_USERNAME=admin INFLUXDB_PASSWORD=admin1234 INFLUXDB_TOKEN=F-QFQpmCL9UkR3qyoXnLkzWj03s6m4eCvYgDl1ePfHBf9ph7yxaSgQ6WN0i9giNgRTfONwVMK1f977r_g71oNQ== INFLUXDB_URL="http://localhost:8086" INFLUXDB_ORG=iot INFLUXDB_BUCKET=temperature
--env-file in your docker-compose command to force Docker to take .env into consideration:
docker-compose --env-file .env up
http://localhost:8086 with InfluxDB running locally, and you should land on the InfluxDB UI. You’ll find the credentials in your .env file. As you can see, InfluxDB is more than a database; it’s an ecosystem that helps manage and visualize your data. You’ll learn more about this later.
Now it’s time to create an MQTT consumer that receives your temperature measurements and stores them in InfluxDB.
influxdb-client:
pip install 'influxdb-client[ciso]'
It’s also a good idea to keep all your constants in one place. It prevents you from repeating yourself and making mistakes. Since you already stored the most important one in .env, you will need dotenv to load them in your script:
pip install python-dotenv
Now you need to start with the MQTT logic. Subscribing to a topic requires two callback functions: on_connect and on_message.
on_connect is called when your application successfully connects to the broker. You’ll use this function to subscribe to the topic temperature. As a result, whenever your smart sensor publishes a message on that topic, the on_message function will be called. You’ll use the on_message callback to send the temperature measurement to InfluxDB.
Use the code below to create a new Python file called influxdb_consumer.py:
MQTT subscriber - Listen to a topic and sends data to InfluxDB
import os
from dotenv import load_dotenv
import paho.mqtt.client as mqtt
load_dotenv() # take environment variables from .env.
# InfluxDB config
# TODO
# MQTT broker config
MQTT_BROKER_URL = "mqtt.eclipseprojects.io"
MQTT_PUBLISH_TOPIC = "temperature"
mqttc = mqtt.Client()
mqttc.connect(MQTT_BROKER_URL)
def on_connect(client, userdata, flags, rc):
""" The callback for when the client connects to the broker."""
print("Connected with result code "+str(rc))
# Subscribe to a topic
client.subscribe(MQTT_PUBLISH_TOPIC)
def on_message(client, userdata, msg):
""" The callback for when a PUBLISH message is received from the server."""
print(msg.topic+" "+str(msg.payload))
## InfluxDB logic
# TODO
## MQTT logic - Register callbacks and start MQTT client
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.loop_forever()
smart_sensor.py is still running and then run influxdb_consumer.py:
python influxdb_consumer.py
You should see temperature measurements coming in:
Now that you’re able to receive messages from the broker, store them in InfluxDB.
Next, you need to configure the InfluxDB client. You’ll use the preconfigured INFLUXDB_TOKEN, which is convenient for test purposes, but you can also create a new token via the UI. Your instance of InfluxDB should still be running.
Go back to the UI (http://localhost:8086) and generate a new authentication token; then click Data. After that, in Client Libraries, you need to select Python. This section lets you create an authentication token:
import os
from dotenv import load_dotenv
from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.write_api import ASYNCHRONOUS
import paho.mqtt.client as mqtt
# take environment variables from .env
load_dotenv()
# InfluxDB config
BUCKET = os.getenv('INFLUXDB_BUCKET')
client = InfluxDBClient(url=os.getenv('INFLUXDB_URL'),
token=os.getenv('INFLUXDB_TOKEN'), org=os.getenv('INFLUXDB_ORG'))
write_api = client.write_api()
field is the measurement and tag could be filtered data, for instance, by location. The other key concepts of InfluxDB are defined on this page.
Now update the on_message callback to a code that writes the measurement to InfluxDB:
def on_message(client, userdata, msg):
""" The callback for when a PUBLISH message is received from the server."""
print(msg.topic+" "+str(msg.payload))
# We received bytes we need to convert into something usable
measurement = int(msg.payload)
# InfluxDB logic
point = Point(MQTT_PUBLISH_TOPIC).tag("location", "New York").field("temperature", measurement )
write_api.write(bucket=BUCKET, record=point)
influxdb_consumer.py and smart_sensor.py are still running. If they are, the new data point should be added to the database every second so you can visualize your data in InfluxDB UI.
http://localhost:8086 and click on Data; then select Buckets. You should see your temperature bucket:
Select the temperature bucket. Now you should have landed on Data Explorer, where you can query your data and visualize it:
A nice feature here is that once you create a visualization that matches your need, you can click “Save As” and add it to a dashboard of your choice:
Thanks to the UI, you don’t need any third-party visualization tools and dashboarding for your application.