Sensical Raspberry Pi Sense Hat Fun with Azure IoT Hub
Hey All! So, I wanted to share some more fun to be had with Raspberry Pi that I was experimenting with during the holiday break!
Recently, I sent away in the mail for a Raspberry Pi Sense Hat. These really cool devices are used as part of the AstroPi mission in Space for education! You can read more about the program here.
https://astro-pi.org/
There are a lot of way cool features included on these little add on circuit boards for your Raspberry Pi. Really impressed!
For starters, everybody cares about the weather!
Sense Hat comes on-board with weather environment measurement capabilities like temperature, pressure, and humidity sensors.
You should also care about where you are in space and time! Relatively speaking!
Sense Hat contains an accelerometer, gyroscope, and magnetometer (compass)!
For physical input controls, Sense Hat has a small joystick on the circuit board as well.
Lastly, and probably the most excellent feature, it contains a 8x8 multi-color bright LED display for truly radical output of what's going on. Wow!
That display you can use to do all kinds of cool things like display retro video game alien sprites like I did below!
When you unbox the product and install the Sense Hat via the 40-pin connector it should look pretty similar to my Sense Hat pictured here. I have mine installed on my Raspberry Pi 4.
Also, I provided the box carton picture for reference on how the product was shipped. Reverse unboxing! Gotta love it!
If you are running the latest version of Raspbian OS, you should already have all the software you need which is the Python3 programming language and Sense Hat for Python3. If you are running another Linux based OS distro, you should be able to download the appropriate packages from your package manager.
For example, the Sense Hat package can be installed by using the following command if needed.
sudo apt-get install sense-hat
There is ready to go out-of-the-box example code you can try out as well built in the sense-hat installation.
You can access that example directory here. There are plenty of examples to try out and customize on your own! Really cool!
/usr/src/sense-hat/examples/python-sense-hat
The first thing I wanted to try out is interact with the 8x8 multicolored LED display on the product. This is where you can display text, colors, and pictures! Really cool!
I first opened up the example text_scroll.py project first in nano editor to see what it would do.
Looks like that its setting the text scroll rotation at 180 degrees across the 8x8 LED display, setting the text color to Red in Hex, and displaying "One small step for Pi!"
Let's go ahead and close the nano editor and execute the Python program!
python3 text_scroll.py
It's working! It's working! Insert pod racing meme here! :)
Ok, So, the next thing I wanted to do before leaving you (besides drawing aliens on the screen again) was to get the current temperature and humidity data displayed from the sensors on the LED display. Also, I wanted to send that same sensor data to an Azure IoT Hub. That's useful, right?
First thing I did was setup an IoT Hub in Microsoft Azure. Here you can see it deploying! I used the Free Tier option! Free makes sense to me!
Once deployed, I went ahead and added a device to get a connection string to the IoT Hub. I called it SenseHat appropriately.
Ok! So, now that is in order. I went on the Raspberry Pi and I used the official Microsoft IoT quick start for Python. This was used to create a sample IoT device and add the connection string to my IoT Hub. That project is referenced below.
https://github.com/Azure-Samples/azure-iot-samples-python/archive/master.zip
Here you can see me installing the pre-requisite azure-iot-device Python package utilizing pip on the pi! :) Pip on the pi! Say that three times fast! I couldn't do it!
Here I am customizing the example code to formulate sensor data received from the Sense Hat. I am using Red and Blue colors for Temp and Humidity.
I can gather and receive sensor information from the Sense Hat in real-time by adding the following code at the beginning of the python project.
# added Sense Hat import from sense_hat import SenseHat sense = SenseHat()
Ok, I am now going to hold my breath and execute the program!
Here it is running! I have the temperature showing in Red and the humidity showing in Blue as programmed on the display. This is shown below for comparison of what it looks like on the display.
Here we can also see that same data being successfully sent while the program is executing. It is receiving data from the sensors from the Sense Hat and sending onward and upward to the Azure IoT Hub in real time!
Let's verify the data got there by checking the Azure IoT Hub!
Yep! It did! We can see the registered device and messages are actively being received at this time!
Oh, what fun!
So, I hoped you enjoyed learning and checking out some of the personal experiments I did. This was really cool experimenting with the Raspberry Pi, the Raspberry Pi Sense Hat, Python, and Azure IoT Hub with Azure IoT examples!
Please stay safe and we will see you next time! I am going to draw more alien sprites now on the screen!
References pages:
Official Raspberry Pi Sense Hat Project Page.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat
Official documentation for the Raspberry Pi Sense Hat.
https://www.raspberrypi.org/documentation/hardware/sense-hat/
Language and API documentation for the Sense Hat
https://pythonhosted.org/sense-hat/api/
Microsoft IoT Hub Sample Quickstart for Python
https://github.com/Azure-Samples/azure-iot-samples-python/archive/master.zip
AstroPi Mission
https://astro-pi.org/