This tutorial demonstrates collecting sensor data in Python on the Raspberry Pi and storing the data in FairCom DB utilizing MQTT (Message Queuing Telemetry Transport).
The Eclipse Paho project provides open-source client implementations of MQTT messaging protocols. In this tutorial, we will use the Paho MQTT Publisher to communicate with FairCom Edge on the Pi. The example uses Python 3 to read a sensor and publish the value via MQTT to FairCom Edge. For more information about MQTT, see MQTT Broker later in this document.
You will need to install and start FairCom Edge as described in Download and Installation.
To use MQTT for messaging from Python to FairCom Edge on a Pi, follow these steps:
For example, to use a DS18b20 sensor, the install command is:
sudo apt-get install python3-w1thermsensor
https://pypi.org/project/paho-mqtt/
pip3 install paho-mqtt
import time
import paho.mqtt.client as mqtt
from w1thermsensor import W1ThermSensor
sensor = W1ThermSensor()
client = mqtt.Client(client_id="connectTest")
client.connect("localhost", 1883, 60)
while True:
temp = sensor.get_temperature(W1ThermSensor.DEGREES_F)
jsonMsg = "{\"temperature_F\":%f}" % (temp)
client.publish("Sensor1", jsonMsg)
print("published %s" %(jsonMsg))
time.sleep(10)
Now let's check to see if the messages are getting to the FairCom Edge MQTT Broker:
Note: MQTT must be configured as described in Setting up MQTT Persistence on the Edge.
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client(client_id="connectTest")
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
client.subscribe("Sensor1")
client.loop_forever()
import paho.mqtt.client as mqtt
client = mqtt.Client(client_id="connectTest")
client.connect("localhost", 1883, 60)
jsonMsg = "{\"operation\": \"CreatePersistenceTopic\", \"persistenceTopic\": \"Sensor1\", \"databaseConnectionString\": \"FAIRCOMS@localhost\", \"tableName\": \"Sensor1Table\", \"tableAutoTimeStamp\": true, \"tableAutoTimeStampIndex\": true, \"mapOfPropertiesToFields\": [ { \"jsonPropertyPath\": \"temperature_F\", \"fieldName\": \"temperature\", \"fieldType\": \"DOUBLE\"}, {\"jsonPropertyPath\": \"humidity\", \"fieldName\": \"humidity\", \"fieldType\": \"DOUBLE\"}, {\"jsonPropertyPath\": \"pressure_Hg\", \"fieldName\": \"pressure\", \"fieldType\": \"DOUBLE\"}]}"
client.publish("ctreeAdministration", jsonMsg)
print("published %s" %(jsonMsg))
The above JSON message is shown below formatted for readability:
jsonMsg =
"{
"operation": "CreatePersistenceTopic",
"persistenceTopic": "Sensor1",
"databaseConnectionString": "FAIRCOMS@localhost",
"tableName": "Sensor1Table",
"tableAutoTimeStamp": true,
"tableAutoTimeStampIndex": true,
"mapOfPropertiesToFields":
[
{
"jsonPropertyPath": "temperature_F",
"fieldName": "temperature",
"fieldType": "DOUBLE"
},
{
"jsonPropertyPath": "humidity",
"fieldName": "humidity",
"fieldType": "DOUBLE"
},
{
"jsonPropertyPath": "pressure_Hg",
"fieldName": "pressure",
"fieldType": "DOUBLE"
}
]
}"
Select * from Sensor1Table where temperature > 72.0