Search
  • Medium One

Build a Home Weather Station Using Raspberry Pi and Sense Hat

Updated: Jun 15

In this tutorial, you will make a simple weather station by connecting a Raspberry Pi with an attached Sense Hat to the Medium One cloud. The Sense Hat includes temperature, humidity, and barometric pressure sensors.


Simple workflows created on Medium One will periodically request temperature, humidity, and barometric pressure measurements from the Pi. You will use Medium One dashboard widgets to visualize the data.


Communication between the Raspberry Pi and the Medium One cloud is established via MQTT. Medium One has a comprehensive MQTT library.


Note that the bulk of the work in this tutorial is spent installing an MQTT library on the Raspberry Pi and configuring it as a Medium One MQTT client. Once that is done, we’ll demonstrate simple workflows to request data from the Pi.  Much more elaborate data analysis is possible, but this tutorial will get you started.


This tutorial assumes you have properly set up and updated your Pi, and your Pi is connected to the Internet. Instructions for doing this can be found at the Raspberry Pi website: www.raspberrypi.org.  The Raspberry Pi 3 is strongly recommended due to its speed and built in wifi, but a Pi 2 or B+ should also work.  


This tutorial assumes you are using Python 3.


In this tutorial, you will:

  1. Connect the Raspberry Pi to Medium One

  2. Build workflows to receive and process weather data

  3. Visualize weather data on the Dashboard

Here’s what you need to get started:

  1. Medium One Prototyping Sandbox + Raspberry Pi 2 or 3 + Sense Hat [Buy]

  2. USB keyboard/mouse

  3. Monitor screen with HDMI connector and HDMI cable to connect to Raspberry Pi

  4. Small Philips head screwdriver (for attaching the Sense Hat)

  5. Ethernet cable


Step 1. Connect the Sense Hat to the Raspberry Pi

The hardware installation is straightforward. Here are detailed instructions from the Raspberry Pi website: https://www.raspberrypi.org/learning/astro-pi-guide/assemble.md

Step 2. Install the Python Sense-Hat package

This python package allows you to access all of the hardware on the Sense Hat including the LED matrix and the temperature, humidity, and barometric pressure sensors, which we’ll use in this tutorial. Type the following commands in a terminal window:


sudo apt-get update

sudo apt-get install sense-hat

sudo reboot 

Step 3. Install the paho-mqtt Package

The paho-mqtt package allows you to set the Raspberry Pi up as an MQTT client so it can communicate with the Medium One cloud.  In a terminal window, type the following:

sudo pip3 install paho-mqtt 


Step 4. Configure the Raspberry Pi as an MQTT Client

Open Python 3 (IDLE) and paste the following code into an empty file:

https://github.com/Medium-One/raspberry-pi-sense-hat/blob/master/medium_one_client.py


After making sure the paste into IDLE didn’t create any indentation problems, save the file.


We need to replace the variables at the beginning of the file with the credentials for your Medium One Account.


  1. m1_user, m1_password, m1_userMQTT_id. In your Medium One account, go to Setup > Manage Users. Click "Add New User". The username and password you enter are your m1_user and m1_password. Click "Save". You should see the user in the table, the value in the MQTT ID column is the m1_userMQTT_id.

  2. m1_apikey. Go to Setup > Manage API Keys. Create a new API key. This is your m1_apikey.

  3. m1_project_id. Go to Setup > MQTT. The Project MQTT ID is your m1_project_id.


Save the file.

Step 5. Connect the Raspberry Pi to the Cloud

First run the Pi client code that you just created. To run the code in Python 3, go to terminal and enter:

python3 medium_one_client.py 

NOTE: The file medium_one_client.py uses a forever function to ensure ongoing communication between the Raspberry Pi and Medium One’s cloud. To stop the program from running, use CTRL+C (for PC) to end it. To run it again, you need to execute the above line of code.


When you run the Medium One client above, it first attempts to connect to the Medium One cloud over MQTT using the credentials you’ll obtain soon.

Upon successful connection, the client then subscribes to the Medium One topic which will be used to transmit requests for information from the cloud to the Pi.

Once that is done, the client loops in a separate thread waiting for messages from Medium One published to the subscribed topic. These might include “Temp_Request” or “Pressure_Request” etc. The client then responds by performing the requested action.


The following picture shows Raspberry Pi connected to Medium One.


Log into Medium One. Click 'Data Viewer' > 'Data Streams' > 'raw'.

If the Raspberry Pi connected successfully there will be at least one event in the raw stream.


Expand the most recent event, and it will be a ‘Connected:True’ event.

In Medium One dashboard, go to 'Config' > 'Data Sreams' and click the ‘Edit’ icon of the raw stream.

Activate the “Connected” tag by clicking the checkmark under Active. Then click “Save Data Stream”.

Step 7. Build Workflow 1: Initialize Weather Station

Stop and restart the client program on the Raspberry Pi.

Since the raw:Connected tag is now active, it will be detected and added as an option in the Workflow Studio.


In the Workflow Studio, create a new workflow titled ‘Initialize Weather Station’.  Drag a Base Python module onto the screen.

Next, drag a raw:Connected trigger onto the screen from the Tags & Triggers section and connect it to the base python module.

The final workflow looks like this:

Double click on the Base Python module and replace the default code with the code here.


Click Save and Activate.

Step 8. Build Workflow 2: Request Weather Data

Before creating this workflow, restart the Raspberry Pi client code. That will result in the Pi transmitting a ‘raw:Initialized’ tag. Activate and save it as you did in step 6 for the ‘raw:Connected’ tag.


Now create a second workflow called ‘Request Weather Data’. This workflow requires a Base Python module, a raw:Initialized trigger, and a custom Scheduler trigger that runs every 30 minutes.


To add the second input to the Base Python module, double click the module, click on Inputs, and click "Add Input"


Here’s the final workflow.

For the 'Custom' Scheduler trigger, be sure to set the ‘Minutely Interval’ to 30 minutes.


Double click on the Base Python module and replace the default code with the code here.


Click Save and Activate to complete the workflow.

Step 9. Build Workflow 3: Process Temperature

Restart the Raspberry Pi client code again.


This time, it will transmit raw:Temperature, raw:Pressure, and raw:Humidity tags which you must activate just like the previous tags.


When activating these three tags, make sure you set the Extended Type to ‘Number – Time Series.’  After activating them, run the Pi code one more time.


Create a new workflow titled ‘Process Temperature’. The Pi transmits temperature data in Celsius. This workflow converts Celsius to Fahrenheit and creates a processed stream which can be used directly or as a trigger for other workflows.


You will need a Base Python module and a raw:Temperature trigger. You will also need to add a "Processed Stream - Single" module from the Outputs category.


Here’s the final workflow.

Replace the default code in the Base Python module with the code here.


This time, when you restart the Pi client code, a ‘processed:Temp_Proc’ tag will be created by this workflow.


You must activate this final tag and re-run the Pi client code one last time.

Step 10. Visualize the Weather Data using Medium One Widgets

Now that the weather station is running, we need a way to access the temperature, humidity, and barometric pressure data. This could be emailed, pushed to a mobile app, or analyzed using any number of tools. In this case, we’ll make use of the Medium One single user line chart widget to display data as trends over the past day.


In the ‘Dashboard’ view, select the Single User Line Chart Widget.

To configure the first widget, select the user (‘device’ in this case), and the processed:Temp_Proc tag.

Finally, set the time period to ‘Today.’


Add two more single user line chart widgets. Both need to be configured to the correct user ('device' in this example) and the time period should be set for ‘today.’  One should be based on the raw:Humidity tag and the other on raw:Pressure.


Save this dashboard configuration by clicking on the ‘Save As’ (floppy disk) icon and naming it ‘Weather Station.’  It will now be readily accessible every time you log onto your Medium One account.

Here’s an example of a barometric pressure trend.


Congratulations! You've completed the tutorial.


Want to learn more about what Medium One can do?

Check out our documentation and tutorials & get started on your next IoT project!

© 2018 Medium One   All Rights Reserved

4633 Old Ironsides Dr. #280, Santa Clara, CA  95054                

info@mediumone.com

|

  • YouTube - White Circle