element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Pi IoT
  • Challenges & Projects
  • Design Challenges
  • Pi IoT
  • More
  • Cancel
Pi IoT
Blog [PiIoT#08]: Sensing with SenseHat
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: vish
  • Date Created: 28 Aug 2016 2:12 AM Date Created
  • Views 1345 views
  • Likes 2 likes
  • Comments 2 comments
  • mqtt
  • python
  • raspberry pi 3 projects
  • vish_piiot
  • raspbbery pi
  • piiot
  • eclipse-paho
  • pi3
  • raspberry pi projects
  • freeborad
Related
Recommended

[PiIoT#08]: Sensing with SenseHat

vish
vish
28 Aug 2016

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:

image

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 Function

More 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:

  1. Start your Sensehat to MQTT python script
  2. Load freeboard page fromyou nodeserve in a browser
  3. Configure your MQTT broker as a source using Paho MQTT Client plugin mentioned in PiIoT#04
  4. Create a pane with name 'SenseHat'
  5. 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.
  6. 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)

image

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.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

 

Happy Coding,

vish

<< Prev | Index | Next >>

Attachments:
sh2mqtt.py.txt.zip
senseHat.json.txt.zip
  • Sign in to reply

Top Comments

  • fvan
    fvan over 9 years ago +2
    You can correct the temperature by taking it from the pressure sensor rather than from the faulty humidity sensor. At least that will give you two out of three correct values temp = sense.get_temperature_from_pressure…
  • volly
    volly over 9 years ago

    @fvan right you are...Thank you for pointing this out....

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • fvan
    fvan over 9 years ago

    You can correct the temperature by taking it from the pressure sensor rather than from the faulty humidity sensor. At least that will give you two out of three correct values image

     

    temp = sense.get_temperature_from_pressure()

     

    See https://pythonhosted.org/sense-hat/api/#sense-hat-api-reference

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube