DIY air temperature monitor using raspberry pi

Rather then pull @fruitnut’s thread on soil monitoring off topic, I decided to create a new thread to answer @horna in case others may find it useful.

I’ll start with the caveat that I’m not claiming this is better than any commercially available weather station, but for anyone who likes tinkering with electronics, or has a spare raspberry pi they want to put to good use, this might be an alternative that gives you complete control over the data you are collecting.

Materials required

  • Any raspberry pi (the Pi Zero W works well)
  • the ADT7410 i2c temperature chip
  • three small lengths of jumper wire with female connectors on one end and the other end matching your pi GPIO header (some raspberry pi models take male connectors, some take female connectors, some have no headers at all and will need either one soldered on first)
  • a soldering iron & beginner-level soldering skills
  • a short section of PVC pipe with an internal diameter wide enough to fit the ADT7410 chip inside
  • optional: scraps of mylar or similar foil insulation to cover the PVC
  • for outdoors: a scrap of hard plastic or metal sheeting to divert rain from entering the top of the PVC pipe

Setup steps

First, follow the “assembly” instructions for soldering the chip header.

Next, follow the instructions for installing the Python libraries on your pi and connecting the chip.

The end of those instructions include the sample python code for printing the temperature to the screen every 30 seconds. Once you’ve run that and confirmed it works, you can tweak that code to instead save the reading to either a local database or to send it to a script running on a web server either on the local network or the internet (as I have done). My script uploads three fields via the POST method: temperature, time, and a sensor ID (to associate the temperature with the correct monitor).

Here’s a slightly simplified version of my own code to upload and log any errors (this code does NOT have a backup option to save locally if the website is unavailable):

#!/usr/bin/python3

import time
import board
import busio
import adafruit_adt7410
import requests
from datetime import datetime

i2c_bus = busio.I2C(board.SCL, board.SDA)
adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
adt.high_resolution = True
webKey = 'password for your website'
webURL = 'https://yourwebsite-script-address-goes-here'

curTime = datetime.now()
newTemp = {'temp': adt.temperature, 'sensor': 'zero1', 'time': curTime, 'key': webKey}
rez = requests.post(webURL, data = newTemp, headers={"Accept":"text/html", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/5
37.36"})

if rez.text != 'OK':
     with open('/home/pi/gh/log', 'a') as f:
          print(str(curTime) + " " + rez.text, file=f)

You can then create a crontab entry to run the script every minute:

* * * * * /home/pi/monitor.py

For my greenhouse temperature monitor, I have it placed in the center of the rafters, a few feet below the peak of the roof and around the highest part of the eventual tree canopy, while for the outdoor monitor, I have it mounted on the outside north wall of my greenhouse, with the wire running through the wall to the raspberry pi mounted inside. Photo was posted here:

To display that data, I pull it from the mySQL database using PHP and generate a chart using the Chart.js library. Here’s what that page looks like:

You can view the page source here for an idea of how to use Chart.js similarly:

Temperatures

I’m sure I left out something important, so I’m happy to answer any questions or help troubleshoot if anyone else tries to follow this and runs into problems.

11 Likes

Respect, though I’m not sure if the general audience here know of crontab and sql, or what a .py or .js is.

There maybe other off the shelf to get this data in, perhaps through wunderground.com?

1 Like

Yeah, I figured only a small subset of the people on here would be into this kind of tinkering, but I know a few people on here have expressed interest in it, so I figured I’d write it assuming only people with that kind of hobby would read past the first few sentences :joy:.

Without some kind of sensor, none of that data available based on other weather stations will be accurate, unless there happens to be a public station right next to your house already.

There are 3 stations within about a mile or two of my house, two on the ridge nearby and one down by the beach, but I’m at the bottom of a valley with poor cold air drainage. My low on a clear, calm, cold night is usually 5+ degrees below any of the other stations, even though on cloudy nights they are similar.

But you can easily buy an off-the-shelf weather station that will be easy to set up and will collect other kinds of data too. That’s the best option for someone who isn’t already into doing this kind of stuff.

1 Like

I see, maybe you’re running a honeypot to catch coders.

Re: wunderground, I was assuming wunderground supports uploading data into their website from off the shelf temperature sensors and that some sensors could take temperature from probes being inserted into ground. I could be wrong.

1 Like

Oh I’m sorry, I misunderstood. Yes, there are a number of different weather station networks you can connect an off-the-shelf weather station to, if it is supported by the weather station’s software.

really nice project!

i especially like how you spend effort on the outside temp sensor not being hit by the sun. 16 bit resolution is not worth much with an improperly installed sensor.

if been thinking about something similar. But first i got to build a greenhouse.

Have you considered also installing a sensor for relative humidity?
And some hardware to automatically vent/close the greenhouse depending on temp.

i have messed around with Arduino like microcontrollers a bit. But not much with IoT. Was it hard to setup your (local) website? Do you pay a monthly fee for that?

I was thinking of sending the temp data via email. Would circumvent the need for a cloud website or need for your pc to always be on.

the adafruit board seems really nice. (although i think the 16 bit oversampling resolution is way overkill) but a bit pricy if you wanted to install 10+ sensors. (to measure at different soil depths or points in the canopy for example.)

you could go with an TMP1075DR
https://eu.mouser.com/ProductDetail/Texas-Instruments/TMP1075DR?qs=byeeYqUIh0MQFff6VZgmIA%3D%3D
https://www2.mouser.com/ProductDetail/Texas-Instruments/TMP1075DR?qs=byeeYqUIh0MQFff6VZgmIA%3D%3D
to save some money. (and loose a tiny bit of accuracy)

o i am curious. The raw data from your sensor at 16 bit. is there a lot of noise? Have you tried it out in a glass of water with some ice-cubes in it? (simple way to get a super stable temperature for testing/calibration)

2 Likes

I do have another chip that measures both temperature and RH, but it uses a python library that is less supported and I had trouble getting the sensor to work right. Could’ve just been a defective chip? Until I have a dehumidifier set up, it’s a low priority for me, but I’ll be doing that next winter and will probably try to get a sensor for RH working by then.

I’m not confident enough that I’ve foreseen all possible bugs in my code, and even if I were, I’d worry about the possibility of a corrupt SD card causing my climate controls to fail. I do have a switch that the greenhouse pi can control for a small power strip, but I decided against using it for anything more critical than supplemental lighting, because the solid state/analog options are less risky. For monitoring or lighting it’s not the end of the world if the system crashes and needs to be fixed, but for heating and cooling that could be lethal for the greenhouse inhabitants.

For cooling, I plug my exhaust fan into a thermocube TC-21, which isn’t perfect (it runs too much in shoulder seasons), but it is reliable so far, and my solution for spring and fall is to simply unplug the fan unless the forecast looks sunny and warm enough to need it.

For heating, I use a 1.5kW “Dr. Heater” with the knob on the lowest setting, but that knob is poorly calibrated and the heater still runs too much. Next winter I’ll instead turn the knob to the maximum, but plug it into a thermocube TC-3 (described at the same link as the TC-21, above).

The sensor itself is not waterproof, so I have only put three of them side-by-side in the same room, and they were all within 0.2°C of each other, so that was enough for me to be confident in their accuracy for my purposes. There is very little noise at the resolution that I care about, as you can see with the line for my greenhouse since midnight today:

However, there are occasionally (once every few days) “chirps” where for a single reading it jumps up or down by a few tenths of a °C, and I assume that’s a hardware issue rather than a pocket of warm/cool air hitting it suddenly. There’s a lot more jitter for the outside sensor, and it’s the same for the greenhouse when the exhaust fan runs, but I think that reflects actual variations in air temperature. Here’s the outside sensor for the same period today:

And here’s a typical summer day, July 31 of last year, where you can see how smooth the greenhouse line looks until the fan turns on:

1 Like

thanks for the graphs and information :slight_smile:

it seems like the fan turning on might interfere with the readings due to power use fluctuations? Or do you think the spikes in the readings during fan on time, are not “error’s” but actually variations due to turbulence or airflow?

if had good luck with some epoxy varnish or even 2 component epoxy glue to waterproof electronics.

silicone sealant also works. But can some times not stick wel to the wire coating.

1 Like

I think it’s the latter, because I see a similar effect when I just open the doors on a slightly breezy day.

1 Like

I connected some DS18B20 temperature sensors (waterproof BTW) to a Pi 3W a few years ago, fun project. I have them taped to some water lines to measure the temp of the water flowing through. Should work similarly if just exposed to the air or inserted into soil I’d imagine.

FYI, the Weatherflow Tempest measures temp, wind, humidity, lightning strikes, solar radiation, etc… That last one is important, if it measures a really high temp but knows there’s X amount of solar radiation hitting the unit, it adjusts the temp down to what it would be without that. Important for measuring mid-day temps on a sunny day when the sensor is out in the bright sun… Not as much in the wee hours when you’re wanting to know how cold it got.

Speaking of such, last night we saw a low of 30.0F and were at or below 32F for slightly over 3 hours. I clicked “-” once to be less granular:

2 Likes

Spotted one of these blips/chirps in the data this morning in case you’re curious to see what they look like @oscar:

1 Like

The DS18B20’s I’m using occasionally return a totally anomalous value. I’m reading and converting that data via a cron-fired BASH script and just added in a bit of logic to sanity check and re-fetch a value if it was obviously wrong. Before pushing into the database…

2 Likes