I was waiting for the new sensehat to reach me before writing this post, but the recent updates suggests that I may not get it before the deadline. So I decided to go on with the faulty one I have. Although the code will work regardless of the sensehat condition, the output I showing here will b faulty because of my hat.
In this post, I'll getting data from sense hat and publish it to MQTT broker. Later this data is displayed as a freeboard dash with MQTT plugin.
Hardware Setup
This post will be using the SenseHat I got as a part of the challenge kit. SenseHat for raspberry pi is a addon board which houses
- Humidity Sensor
- Barometric Pressure Sensor
- Temperature Sensor (?)
- Magnetometer
- Accelerometer
- Gyroscope
- 8x8 Color LED Matrix
- 5-button joy stick
For more details about SenseHat, visit Raspberry Pi Sense HAT or https://www.raspberrypi.org/products/sense-hat/
The sensehat can be mounted on the raspberry pi (here I use Pi3) with the screws provided with the sensehat. It will look like:
Next is to install the libraries for sensehat. To install them:
$ sudo apt-get update $ sudo apt-get install sense-hat
This will install the c/c++ and python libraries for using sense hat. Now you need to restart the system for the changes to take effect;
$ sudo reboot
Now you should be able to use SenseHat.
Software Setup
For this post, I'll using a python script which will read the values from environmental sensors and publish it using MQTT to topic 'usr/vish/sensehat'. Each packet will a JSON object like:
{"pressure":"1010.0576171875","temperature":"-422.7525634765625","humidity":"-39.87132263183594"}For this, we'll be using Paho MQTT python client library. Installation of the library is described in [PiIoT#06]: Ambient monitoring with Enocean sensors.
Once the library is installed, we are ready to go.
To get the sensor values the script is roughly like this:
## Init Sensehat
import sense_hat as sHat
sh = sHat.SenseHat();
# Function to read the sensor and send data
def senseNsend( client ):
dataPacket = {}
# Get environmental sensors data from sense hat
dataPacket['humidity'] = sh.get_humidity()
dataPacket['pressure'] = sh.get_pressure()
dataPacket['temperature'] = sh.get_temperature()
mqttPacket = "{" + ','.join( '"%s":"%r"' %(k,v) for (k,v) in dataPacket.iteritems() ) + "}"
#Now send 'mqttPacket' to broker
# End of FunctionMore documentation on using Environmental sensors on SenseHat can be obtained from https://pythonhosted.org/sense-hat/api/#environmental-sensors
Next this is to create a MQTT broker connection and send the actual packet.
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
basePath = 'usr/vish/'
appPath = 'sensehat'
# MQTT Broker Params
mqtt_broker = "192.168.1.54"
mqtt_port = 1883
## Define MQTT callbacks
def onConnect( client, userData, retCode ):
print "Connected to broker."
client.publish( basePath+'devices/sensehat', '{"name":"sensehat","desc":"Sensehat to MQTT bridge"}' )
client = mqtt.Client( client_id = "sh2mqtt_bridge",
clean_session = True )
client.on_connect = onConnect
client.connect( mqtt_broker, mqtt_port, 60 )
client.loop_start()This will create a client named 'sh2mqtt_bridge' and connect it to the IP at mqtt_broker. Now we can publish a packet to the broker with:
client.publish( basePath+appPath, mqttPacket )
Here I have put the topic to publish as basePath+appPath = 'usr/vish/sensehat'
The complete code is attached to your reference.
You can use one of the MQTT debugging tools like MQTTSpy to monitor the messages in topic 'usr/vish/sensehat'.
Designing the dashboard
Now we will be using freeboard to design the dashboard for viewing data. I have already explained how to host freeboard with nodejs in [PiIoT#01] : Designing a dash board and Freeboard MQTT plugin in [PiIot#04]: Freeboarding with MQTT. Follow the instructions and start your freeboard sensor. Follow the instructions to create the dashboard:
- Start your Sensehat to MQTT python script
- Load freeboard page fromyou nodeserve in a browser
- Configure your MQTT broker as a source using Paho MQTT Client plugin mentioned in PiIoT#04
- Create a pane with name 'SenseHat'
- Create a text widget inside the pane with name as 'Pressure' and souce as 'datasources["mercury-sensehat"]["usr/vish/sensehat"]["pressure"]'. You will be able to select this data source, if your python script is running. Enable spacklines to get a line graph of values.
- Create similar text Widgets for Temperature and Humidity
Finally you dashboard will be looking like this:
You will be able to view the values send by your sensehat( Note that here the values are faulty because of my Sensehat)
Now you can save this to 'www/freeboard/dashboards' directory of you nodejs script as 'senseHat.json'. File is attached below.
To view the dashboard later, goto http://<freeboard host IP>:8080/#source=dashboards/senseHat.json.
Sense Your Environment
For demo, I have modified the update interval to 3 sec. Below is a video of demo where I'm using my android phone's chrome to view the sensehat data.
Happy Coding,
vish


Top Comments