How do you improve a Raspberry Pi? By adding real-world sensors – here's how to get started with the Sense Hat

build a greenhouse monitor with a raspberry pi

Shutterstock

The Raspberry Pi Sense Hat is a versatile add-on, with sensors for temperature, humidity, pressure, orientation and direction, and there are numerous examples of how it can be used online. The European Space Agency has even sent two of them to the International Space Station for conducting experiments in weightless environments.

Here at IT Pro, we're a little more down to earth. To showcase the Sense Hat's capabilities, we'll focus on three of those sensors – temperature, humidity and pressure – to build a greenhouse-monitoring system. With winter on the way and gardeners moving vulnerable plants under cover, it makes sense to have a way of keeping an eye on conditions without having to go out and check a thermometer.

We'll therefore take a reading every 15 minutes and upload the data to a web server so it can be checked from the comfort of a centrally heated home. We'll also make use of the LED matrix mounted on the Sense Hat's top surface to give visual feedback should temperatures stray outside of a defined range. That way, if you mount the Pi so it's visible from the house, you can see at a glance if you need to go out and turn on a heater or open a window, even if you haven't checked the server.

Naturally, you need a power source wherever you position your Pi plus Sense Hat. If you're going to use it in an outbuilding, you should also make sure it isn't going to get wet. You don't need Wi-Fi if you only want to record your measurements onto the SD card, but you will if you want to upload them to the server, so make sure your network signal carries far enough, or use a booster. If you don't have a greenhouse, you could use the same technique to monitor the temperature of, say, a nursery, a vivarium housing reptiles or even just your office.

Install the Hat and set up your Pi

The Sense Hat works with Raspberry Pi 2 Model B and later, so if you've upgraded and have an old Pi hanging around, this is a great way to give it a useful second life. For this project, we're using a Raspberry Pi Zero WH, with built-in Bluetooth and Wi-Fi, and a pre-soldered GPIO header. We won't be using Bluetooth, but Wi-Fi and GPIO are essential; if you're using a Zero without pins, either buy a header and solder it on or, if you don't have a soldering iron, check out Pimoroni's solderless header.

You'll need the “Male + Female + Installation Jig”, which, at £7, is pricier than the male pins alone (to which the Sense Hat attaches), but is still cheaper than an iron, flux and just the pins you need. We've used it ourselves and, although the first few taps with a hammer are a breath-holding experience, it's easier than you might imagine.

Sense Hat ships with four standoffs. Having unplugged your Pi from the mains, screw the standoffs into the holes on the corners of your Pi (if you're using a Pi Zero, hold your Pi so the side with all of the chips on it is facing you and the GPIO pins are at the top. Screw the standoffs to the top left and both of the right holes, leaving the bottom-left hole vacant). Now mount the Sense Hat by pressing it onto the GPIO pins, with the LED matrix facing you and the female GPIO sockets again at the top.

When it's properly seated, use the remaining four screws (two on the Zero) to secure the Sense Hat to the standoffs. On the Zero, you'll notice that only the top two standoffs align with holes on the Sense Hat, so screw these into place. The third standoff you attached to the Pi will sit beneath the Hat, supporting it without being physically attached.

Download the Pi imager from this link, then insert a microSD card into your PC and click “Choose OS”. Select “Raspberry Pi OS (32-bit)”, click “Choose SD Card” and select the card you've inserted. Finally, click “Write”. This wipes the microSD card and installs the OS. When it's finished, open Windows Notepad and enter the following, replacing the parts in

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdevupdate_config=1
country=

network={ ssid=”” psk=””
}

Save this file in the root of the microSD card as wpa_supplicant.conf (be careful: unless you change the “Save as file type” dropdown to “All files (*.*)” on the Notepad save dialog, it will add .txt to the filename and your Pi won't be able to connect to your network. Close the file, create a text file with no contents and save it as ssh. Make sure it has no extension by again choosing “All files (*.*)” from the “Save as file type” dropdown.

Eject your microSD card, insert it into the Pi, connect to power and wait for it to boot. Its first boot may take a little longer than subsequent boots, so leave it a couple of minutes, then check your router's DHCP table to see when it's connected to the network. Make a note of its IP address, then open a Windows command prompt and type:

ssh [email protected]

If your Pi's IP address isn't 192.168.1.88, adapt the command as appropriate. When asked if you want to authorise the connection, type yes, press Enter and, when asked for a password, type raspberry and press Enter again.

Your first job is to change the default password, so type passwd and press Enter, then type in the existing password and your new password when requested. Make sure your Pi is running the most up-to-date software by entering these two commands, pressing Enter after each one.

sudo apt-get update

sudo apt-get upgrade

Finally, it's time to install the Sense Hat software with:

sudo apt-get install sense-hat

Next, to simplify the process of logging in remotely, set a meaningful hostname, which you can use in place of the IP address, so it won't matter if your router issues a different address in the future. Type the following and press Enter:

sudo raspi-config

Key down to option 2 and press Enter, then select Hostname. Choose any name you like. We're running nine Pis on our network, each of which we've given a name beginning pi to differentiate them from PCs and Macs, so we've used pisense for this device. In future, we can connect to it from Windows command prompt with:

ssh [email protected] 

You don't need to add .local when setting it up, but you'll need to use it with the ssh command.

Finally, let's enable VNC so we can interact with the GUI remotely, rather than working solely in the text-based environment. Return to the config homescreen and this time key down to Interfacing Options (the fifth entry). Press Enter, key down to VNC and press Enter again. Confirm that you want to enable it. When it finishes installing the VNC extensions, quit the config routine and let the Pi reboot.

While it's doing so, download VNC Viewer on your PC. When the Pi has finished booting, launch VNC Viewer and, in the address bar, type “pisense.local” (swapping “pisense” for whatever hostname you specified) and press Enter. Enter “pi” as the username, and whatever password you specified during the setup process to log in.

Interacting with Sense Hat

We're going to control the Sense Hat and read its data using Python, and we'll use cron to automate the collection at regular intervals and upload it to the server.

Click the raspberry on the taskbar and launch Geany Programmer's Editor from the Programming submenu. Enter the following code, which we'll explain in detail below:

#!/usr/bin/env python3

import time

from datetime import datetime

from sense_hat import SenseHatsense = SenseHat()
sense.clear()

now = datetime.now()thedate = now.strftime(“%Y-%m-%d”)
thetime = now.strftime(“%H:%M:%S”)

temp = sense.get_temperature()pressure = sense.get_pressure()
humidity = sense.get_humidity()

print(temp)print(pressure)
print(humidity)

if temp 30:r = 255g = 0b = 0
sense.clear((r, g, b))

import csvthefile = “/home/pi/sense.csv”thefields = [thedate, thetime, temp, pressure, humidity]with open(thefile, “a”) as fp:wr = csv.writer(fp, dialect=”excel”)
wr.writerow(thefields)

It's important that you maintain left margin indents in the code as Python uses these to define subroutines. For example, the indented section between if temp and elif temp runs only if the condition specified on the if temp line is true, and Python knows that the point where the indent stops, at the elif line, is the end of that subroutine.

The code explained

The first few lines of code set up the operating environment. We import the time and date, which we'll write out with the data later on so that we know when each measurement was taken, we then connect to the Sense Hat itself with the following lines:

from sense_hat import SenseHat

sense = SenseHat()

The line that immediately follows this clears the matrix display so that if it's currently showing anything (later in the code, we'll turn it red for high temperatures and blue for low), it will be switched off until we can verify that the conditions remain true.

We then write the date and time to the variables thedate and thetime. Notice that we've formatted each variable in the brackets at the end of the line, and we've chosen to use year-month-date for the date as this makes the data easier to sort should we choose to import it into a spreadsheet. If you want to format the date differently, you can choose from a wide range of variables:

%a truncated week day Mon, Tue, Wed
%A full week day Monday, Tuesday, Wednesday
%d date to two digits 01, 02, 03… 29, 30, 31
%m month to two digits 01, 02, 03… 10, 11, 12
%b truncated month Jan, Feb, Mar
%B full month January, February, March
%y year to two digits 20, 21, 22
%Y year to four digits 2020, 2021, 2022

The same is true of recording the time. We have chosen to use the 24-hour clock, but you could swap our %H for %I to use the 12 hour clock, and append %p at the end of the string to add AM or PM as appropriate. For a full list of variables, check out W3 Schools' comprehensive explanation.

In the next section, we start interacting with the Sense Hat by reading data from its sensors. We're interested in temperature, pressure and humidity, each of which we will write to an appropriately named variable. The format in each case is very similar:

temp = sense.get_temperature()pressure = sense.get_pressure()
humidity = sense.get_humidity()

We'll then print the variables onscreen. After all, there's not much point gathering them if we're not going to display them.

print(temp)

print(pressure)

print(humidity)

Now that the recordings are stored in variables, we can perform tests against them. What we're most interested in is the temperature value, so in the next section we check, first, whether the temperature is below 15˚C and, in the section below that, if it's above 3o˚C.

if temp 30:r = 255g = 0b = 0
sense.clear((r, g, b))

If either case is found to be true, we again set some variables. In the case of temperatures below 15˚C, we set the variable for b to 255 and zero out the variables r and g. If the temperature is above 30˚C, we set r to 255, and zero out g and b. It doesn't take a genius to work out that we're setting values for red, green and blue here, and that 255 relates to the intensity of each tone. Thus, if the temperature is below our minimum (15°C) we set full-on blue and, if it's above our maximum (30°C), we pick full red.

Now that we've specified our colours, we'll return to a command that we've already employed once in the code: sense.clear() . When we included it at the start of the code, we hadn't specified any colours within the brackets, so the effect was to turn off the LED array. Yet, by including the variables for r, g and b within the braces when we call it at this point, we'll instead illuminate the array with full blue if it's cold, and full red if it's hot. If the temperature sits between our specified minimum and maximum, neither of our test conditions will have been met, so no change will be made to the LED array.

As we turned it off at the start of the code, therefore, it will remain off if neither of the temperature tests results in a positive response. In this way, if the temperature was cold enough to have turned the array blue and we remedied it by turning on a heater in the greenhouse, we'd be able to see from inside the house that the temperature had risen sufficiently to protect our plants when the array extinguished itself.

Recording your observations

If all you want is a temperature-based traffic light system, you could stop at that point, but isn't it more interesting to keep a record of your readings so you can refer back? At present, the file we want to write to doesn't exist, so click the raspberry icon and pick Text Editor from the Accessories submenu. Without adding anything to the new file this creates, press Ctrl+S for the Save dialog, click Home in the sidebar and save the file as sense.csv. Close the sense.csv file and return to Geany Programmer's Editor.

The next section of code writes them to a comma-separated values (CSV) file, which we'll upload to a server using a separate routine.

import csvthefile = “/home/pi/sense.csv”thefields = [thedate, thetime, temp, pressure, humidity]with open(thefile, “a”) as fp:wr = csv.writer(fp, dialect=”excel”)
wr.writerow(thefields)

We start by telling Python that we're going to work with a CSV file, and specify that the file in question is called sense.csv, which is stored in our home directory. Having specified the name and location of the file we want to write to, we list the variables we want to write. As you can see, these are the date, time, temperature, pressure and humidity readings. Each variable will be separated by a comma, resulting in a file that can be opened and read just as easily in a plain text editor as it can in Excel.

The fourth line opens the file whose name is stored in the variable thetime, and the a switch tells Python that we want to append our data to the end of what already exists. The next two lines are indented as they tell the Pi what to do with the open file.

That's the end of the code. Press Ctrl+S to save it and call it sense.py. To run it manually, open a Terminal window by clicking the Terminal icon on the Pi's taskbar (or select Terminal from the Accessories menu), type python3 sense.py and hit Enter. You'll see the current temperature, pressure and humidity and, if either of the tests for low or high temperatures is satisfied, the LED array will light up blue or red. Behind the scenes, the measurements, plus the current date and time, will be added to the sense.csv file. You can check that this has happened by right-clicking the file and picking Text Editor from the menu to open it.

Upload and automation

The next step is to upload the CSV file to a web server so you can more easily interpret the readings. Luckily, this requires only two lines of code, so long as you have the FTP login details for your server. In Geany Programmer's Editor, create a new file and enter the following, replacing the sections in angled brackets as appropriate, while keeping everything that follows curl on a single line:

#!/bin/bash

curl -T /home/pi/sense.csv ftp:///sense.csv –user :

Save the file as upload.sh, again in the Home folder where it will sit alongside your existing sense.py and sense.csv files. If you want to check that you've got your FTP server address, username and password correct before relying on the code, type the curl line at the command prompt first and make sure the transfer completes successfully.

Of course, you don't want to have to manually invoke these two scripts every time you take a reading. Doing so would require you to log in to the Pi through the day and night using either ssh or VNC and, unless you accurately anticipated an extreme temperature, the traffic light system would be useless. So we'll use cron to automate the process. Shut Geany Programmer's Editor and return to the Terminal. Type the following and press Enter:

sudo crontab -e

When asked which editor you'd like to use, press 1 for Nano. Key down to the bottom of the file, press Enter to move to a new line and type the following two lines of code:

*/15 * * * * python3 /home/pi/sense.py

2-59/15 * * * * sudo /home/pi/upload.sh

Press Ctrl+O to save the file (the “O” stands for “write out”, not open), then Ctrl+X to exit Nano.

The first of these two lines runs your sense.py Python routine every 15 minutes – on the hour, at quarter past, half past and quarter to. The four stars that follow the */15 mean every hour, every day, every month and every day of the week. If you only wanted to run it every 15 minutes in December, you would instead use */15 * * 12 *. If you only wanted to run it on the first of the month, it would be */15 * 1 * *. If you wanted to run it every minute of every hour, regardless of the date, use * * * * *.

On the second line, we're again invoking something every 15 minutes. In this case, it's the script that uploads the CSV file to the server. However, you'll notice that the first part of the equation is more complicated. While we ran sense.py every 15 minutes starting from minute zero of each hour, we're running upload.sh at the same interval, but offset by two minutes – so, we're telling cron to start counting at two minutes past the hour and upload at two, 17, 32 and 47 minutes past the hour. Why? That way we can be sure the data collection routine has finished running before the upload routine kicks in.

At the server end

When you upload a CSV file to a web server and try to open it in a browser, most browsers will download the file rather than displaying it, which isn't ideal as you then need to open it in a spreadsheet. It's more convenient to render the results in a browser by incorporating them into a web page. Here, there's no point reinventing the wheel – and doing so would require several further pages of explanation – so credit to the Stack Overflow contributors who have already solved this problem.

The answer here reads a specified CSV file and drops the contents into a table. Ensure you change the filename on the second line, which begins $file, to sense.csv and, if necessary, include its path relative to the domain root. You could also edit the code to use tags rather than table cells, and perhaps import Bootstrap within your header or footer to make it mobile-responsive.

Over time, your CSV will grow considerably (at present, we're adding four new lines every hour so it will grow by almost 100 lines a day). You might, therefore, want to periodically open the file in the Pi text editor and delete the contents, or use PHP to only display, say, the five most recent entries when parsing it on the server. In the latter instance, another Stack Overflow post is likely to provide the help you need.

Keyword: Build a greenhouse monitor with a Raspberry Pi

CAR'S NEWS RELATED

Best cars for a 'Love Bug' remake

Tennessee’s Yard Art The Thorndyke Special The Hot Rod The Hippie Van Jim’s Lamborghini Growing up, it’s safe to say my absolute favorite movie was Disney’s “The Love Bug.” As a kid living in a world before Pixar’s “Cars,” it was pretty much the best car movie. I loved ...

View more: Best cars for a 'Love Bug' remake

Audi timing belt tensioner prompts Utah bomb squad visit

An aftermarket timing belt tensioner found in a Salt Lake City apartment prompted a visit from the bomb squad this week after it was mistaken for a potential explosive device. Police partially evacuated the apartment complex in which it was found “out of an abundance of caution” and called ...

View more: Audi timing belt tensioner prompts Utah bomb squad visit

2023 GMC Sierra HD 2500 and 3500 will get more expensive

GMC Sierra HD pickups are about to get more expensive again after recently becoming more expensive, according to GM Authority. In July, GMC and Buick raised the prices on certain vehicles in their lineups anywhere from $975 to $1,675 by making the previously optional OnStar a standard feature. Next ...

View more: 2023 GMC Sierra HD 2500 and 3500 will get more expensive

Our week with EVs: Recapping the diverse collection of electric cars we tested

Mercedes-Benz EQB First Drive Review: Next electric Benz is a little different 2022 Chevy Bolt EV Road Test Review: Time to play EV, gas or airplane 2023 Genesis GV60 Road Test Review: The third of Hyundai Group’s E-GMP EVs is the fastest, but softest It may be hard to ...

View more: Our week with EVs: Recapping the diverse collection of electric cars we tested

Europe car sales lowest since 1996 after 12-month decline

BERLIN – Europe registered the lowest number of new passenger cars in the month of June this year since 1996 at just over 1.06 million vehicles, with some carmakers seeing sales drop by nearly 50%, data from Europe’s automobile association showed on Friday. Volkswagen Group was the hardest-hit major ...

View more: Europe car sales lowest since 1996 after 12-month decline

More Than Just A Carmaker: Toyota Motor PH Launches Toyota Mobility Solutions

Automotive industry leader, Toyota Motor Philippines Corporation (TMP) inaugurated today Toyota Mobility Solutions Philippines, Inc. (TMSPH), a wholly owned subsidiary that will be a provider of mobility-related services. TMSPH ventures into the development and offering of a range of ‘new mobility solutions’ that will enhance Toyota as a brand ...

View more: More Than Just A Carmaker: Toyota Motor PH Launches Toyota Mobility Solutions

'F1 22' feels fast and familiar | Gaming Roundup

‘F1 22’ impressions ‘Construction Simulator’ is launching Sept. 20 Autoblog may receive a share from purchases made via links on this page. Pricing and availability are subject to change. This week in racing game news: ‘F1 22’ impressions At its core, “F1 22,” the latest installment in the F1 ...

View more: 'F1 22' feels fast and familiar | Gaming Roundup

Watch a single-engine plane crash-land on 91 Freeway in California

A single-engine Piper Cherokee airplane was forced to crash land on the 91 Freeway in California after losing power about 45 minutes outside of Los Angeles. Yes, that means this was a very busy highway, practically bursting at the seams with traffic. No, amazingly nobody died, either in the ...

View more: Watch a single-engine plane crash-land on 91 Freeway in California

Ford files 'Mustang Dark Horse' trademark application

Make adventures more comfortable with high-quality Jeep grab handles

New Lucid Air variant to debut & Stealth Look to be on display during Monetary Car Week

Chevy offers incentives to prevent Corvette Z06 flipping

Average U.S. gas price falls below $4/gallon

German court: SUV driver must pay more than a car for running red light

Australian vehicle crash tests to include underwater performance

Toyota C-HR rumored to get revamp next year and an EV version

Geely's electric Zeekr 009 is an EV Alphard we never had; 700 km, 542 PS, top speed 190 km/h

A Newey, Brawn and Anderson row that shows F1 doesn’t change

Sorry, EV buyers, you won't get to pick your own pedestrian safety sounds

There won't be enough copper to meet climate goals, study indicates

OTHER CAR NEWS

; Top List in the World https://www.pinterest.com/newstopcar/pins/
Top Best Sushi Restaurants in SeoulTop Best Caribbean HoneymoonsTop Most Beautiful Islands in PeruTop Best Outdoor Grill BrandsTop Best Global Seafood RestaurantsTop Foods to Boost Your Immune SystemTop Best Foods to Fight HemorrhoidsTop Foods That Pack More Potassium Than a BananaTop Best Healthy Foods to Gain Weight FastTop Best Cosmetic Brands in the U.STop Best Destinations for Food Lovers in EuropeTop Best Foods High in Vitamin ATop Best Foods to Lower Your Blood SugarTop Best Things to Do in LouisianaTop Best Cities to Visit in New YorkTop Best Makeup Addresses In PennsylvaniaTop Reasons to Visit NorwayTop Most Beautiful Islands In The WorldTop Best Law Universities in the WorldTop Richest Sportsmen In The WorldTop Biggest Aquariums In The WorldTop Best Peruvian Restaurants In MiamiTop Best Road Trips From MiamiTop Best Places to Visit in MarylandTop Best Places to Visit in North CarolinaTop Best Electric Cars For KidsTop Best Swedish Brands in The USTop Best Skincare Brands in AmericaTop Best American Lipstick BrandsTop Michelin-starred Restaurants in MiamiTop Best Secluded Getaways From MiamiTop Best Things To Do On A Rainy Day In MiamiTop Most Instagrammable Places In MiamiTop Interesting Facts about FlorenceTop Facts About The First Roman Emperor - AugustusTop Best Japanese FoodsTop Most Beautiful Historical Sites in IsraelTop Best Places To Visit In Holy SeeTop Best Hawaiian IslandsTop Reasons to Visit PortugalTop Best Hotels In L.A. With Free Wi-FiTop Best Scenic Drives in MiamiTop Best Vegan Restaurants in BerlinTop Most Interesting Attractions In WalesTop Health Benefits of a Vegan DietTop Best Thai Restaurant in Las VegasTop Most Beautiful Forests in SwitzerlandTop Best Global Universities in GermanyTop Most Beautiful Lakes in GuyanaTop Best Things To Do in IdahoTop Things to Know Before Traveling to North MacedoniaTop Best German Sunglasses BrandsTop Highest Mountains In FranceTop Biggest Hydroelectric Plants in AmericaTop Best Spa Hotels in NYCTop The World's Scariest BridgeTop Largest Hotels In AmericaTop Most Famous Festivals in JordanTop Best European Restaurants in MunichTop Best Japanese Hiking Boot BrandsTop Best Universities in PolandTop Best Tips for Surfing the Web Safely and AnonymouslyTop Most Valuable Football Clubs in EuropeTop Highest Mountains In ColombiaTop Real-Life Characters of Texas RisingTop Best Beaches in GuatelamaTop Things About DR Congo You Should KnowTop Best Korean Reality & Variety ShowsTop Best RockstarsTop Most Beautiful Waterfalls in GermanyTop Best Fountain Pen Ink BrandsTop Best European Restaurants in ChicagoTop Best Fighter Jets in the WorldTop Best Three-Wheel MotorcyclesTop Most Beautiful Lakes in ManitobaTop Best Dive Sites in VenezuelaTop Best Websites For Art StudentsTop Best Japanese Instant Noodle BrandsTop Best Comedy Manhwa (Webtoons)Top Best Japanese Sunglasses BrandsTop Most Expensive Air Jordan SneakersTop Health Benefits of CucumberTop Famous Universities in SwedenTop Most Popular Films Starring Jo Jung-sukTop Interesting Facts about CougarsTop Best Hospitals for Hip Replacement in the USATop Most Expensive DefendersTop Health Benefits of GooseberriesTop Health Benefits of ParsnipsTop Best Foods and Drinks in LondonTop Health Benefits of Rosehip TeaTop Best Air Fryers for Low-fat CookingTop Most Asked Teacher Interview Questions with AnswersTop Best Shopping Malls in ZurichTop The Most Beautiful Botanical Gardens In L.A.Top Best Mexican Restaurants in Miami for Carb-loading rightTop Best Energy Companies in GermanyTop Best Garage HeatersTop Largest Banks in IrelandTop Leading Provider - Audit and Assurance In The USTop Best Jewelry Brands in IndiaTop Prettiest Streets in the UKTop Best Lakes to Visit in TunisiaTop Highest Mountains in Israel