Connect with us

How To

How to Set up Raspberry Pi Weather Station Using the Sense HAT?

Published

on

Raspberry Pi Weather Station

Setting up the Raspberry Pi Weather Station using the Sense HAT is more hardware-oriented. In this article, we are going to show you how to set up the Raspberry Pi weather station which can be accessed over the internet. The things you need for this project is Sense HAT, a Raspberry Pi add-on board which was made especially for the Astro Pi mission. To record all the environmental information, the Sense HAT was designed with a variety of sensors which can detect things like temperature. With the Raspberry Pi and the Sense HAT working together, you can put this information online, which gives your home a new smart feature.

Raspberry Pi Weather Station
Raspberry Pi Weather Station

The primary thing you need for this project is a web server so that the Raspberry Pi Weather Station can be accessed over the internet. Here we are going to use Flask, which is a Python microframework which can be used to build web servers and create web applications.

Related: How to Setup Your Raspberry Pi as a VPN Router?

How to set up a Raspberry Pi weather station using the Sense HAT

Step 1: Install Raspbian and check for updates. You can refer this guide to know How to install the Raspbian OS. So the first thing you have to make sure is that your Raspberry Pi is up to date:

sudo apt-get update
sudo apt-get upgrade

Step 2: Install the needed packages, the packages you need should be installed by default, but it’s better to run these commands and make sure.

sudo apt-get install python3-flask
sudo apt-get install sense-hat

You can get a message like this after the first command:

python3-flask is already the newest version.

And, similarly, after the second:

sense-hat is already the newest version.

If so, great – it means we are good to go.

Raspberry Pi Weather Station
Raspberry Pi Weather Station

Step 3: Build the web app and web server, In this step, you have to build a Python-powered web app and Python-powered web server using Flask. This seems like a little bit difficult, but it is very easy.

First, simply create the new Python file and add some lines in it. The following command creates the file:

nano weather.py

You will be in your new file now. But there’s nothing in it yet, You can fill it up with the following text:

from flask import Flask, render_template
from sense_hat import SenseHat

app = Flask(__name__)

@app.route(‘/’)

def index():
sense = SenseHat()

celcius = round(sense.get_temperature(), 1)
fahrenheit = round(1.8 * celcius + 32, 1)

return render_template(‘weather.html’, celcius=celcius, fahrenheit=fahrenheit)

if __name__ == ‘__main__’:
app.run(debug=True, host=’0.0.0.0′)

Just hit Ctrl+X to exit a file, then hit Y to agree to save it and hit Enter to keep same filename, which you just gave it.

Now, you have to create the simple HTML template for a Python file, which we have created. Let’s create a folder and then a new .html file.

mkdir templates
nano templates/weather.html

Here is the contents of the template we used:

<!doctype html>

<html>
<head>
<title>Raspberry Pi Weather Station</title>
</head>

<body>
<h1>Raspberry Pi Weather Station</h1>

<h2>Temperature</h2>
<p><strong>Celcius:</strong> {{ celcius }}</p>
<p><strong>Fahrenheit:</strong> {{ fahrenheit }}</p>
</body>
</html>

you can tweak yours to give it a different look, of course.

Related: Raspberry Pi SSH

Now, we can launch our web server and app by typing:

python3 weather.py

Now that is running, you can type 127.0.0.1 (the localhost IP) into the address bar in the Raspberry Pi’s browser and then see your site. Here’s what it will look like:

Raspberry Pi Weather Station
Raspberry Pi Weather Station

Step 4: Add humidity and air pressure, If you are happy with just temperature, You are all set here. But the Sense HAT can give you more information, including the humidity and air pressure. Now you can modify the weather station to display them as well. The first thing, you have to do is exit your application by pressing the Ctrl+C

Raspberry Pi Weather Station
Raspberry Pi Weather Station

Now you are out, then type the following line:

nano weather.py

This is your Python file from Step 3.

Below the line fahrenheit = round(1.8 * celcius + 32, 1), add the following lines:

humidity = round(sense.get_humidity(), 1)
pressure = round(sense.get_pressure(), 1)

Then, edit the return line like this:

return render_template(‘weather.html’, celcius=celcius, fahrenheit=fahrenheit, humidity=humidity, pressure=pressure)

Finally, You need to modify the HTML template. So go back to that file:

nano templates/weather.html

and make it look like this:

<!doctype html>

<html>
<head>
<title>Raspberry Pi Weather Station</title>
</head>

<body>
<h1>Raspberry Pi Weather Station</h1>

<h2>Temperature</h2>
<p><strong>Celcius:</strong> {{ celcius }}</p>
<p><strong>Fahrenheit:</strong> {{ fahrenheit }}</p>

<h2>Humidity</h2>
<p>{{ humidity }}</p>

<h2>Pressure</h2>
<p>{{ pressure }}</p>
</body>
</html>

Again, feel free to modify the look of your personal site.

Related: How to Use Raspberry Pi as a Chromecast Alternative?

Now put 127.0.0.1 into the address bar in the Raspberry Pi’s web browser again and enjoy your new and improved site! which may look like this:

Raspberry Pi Weather Station
Raspberry Pi Weather Station

Step 5: Set up port forwarding, Now the weather station is working, but it is only available from within your home network. For example, if your smartphone is connected to the same network than the Raspberry Pi, you can browse to your Raspberry Pi’s IP address followed by 5000, and see the same page (you can obtain your Pi’s IP address by typing hostname –I in Terminal).

Raspberry Pi Weather Station
Raspberry Pi Weather Station

Related: How to Create A Raspberry Pi Clock Using the Sense HAT?

However, you cannot open the page from outside the home network – yet. To be able to do so, you will need to set up port forwarding in your router’s admin interface. Access your router’s admin interface by typing in your router’s IP address and entering any necessary login information (this information is often printed on a sticker on your router). Then set up your port forwarding settings like so:

Service Port: 80
Internal Port: 5000
IP Address: [your Pi’s IP address]
Protocol: TCP

Yours may be presented in a different format or a different order, but you need the same values in each spot.

Now that you have made this, enter into a device which is outside the network and enter your network’s public IP address (you can obtain your public IP address by Googling “what’s my IP address”). You will be redirected to the weather station site. Congratulations, you can set up a Raspberry Pi weather station using the Sense HAT!

Thank you for reading this post.

Facebook

Trending