DBK
LIFE MEMBER
In a recent thread I described my efforts to build a Raspberry Pi temperature sensor which would monitor the internal temperature of the MH and send me an email if it rose too high. The purpose was to know how hot it was in the vehicle if we had to leave our dog in it.
The system worked but the hardware was more expensive than necessary so I set out to make a cheaper version using a Raspberry Pi Zero W:
These cost about £9.50 but you do have to buy and solder on the 40 pin header. It is quite small and you can judge the size by the Micro SD card you can see on the left in the photograph above. The "W" in the name stands for "Wireless" which together with Bluetooth is built into the device. The original version which is still available does not have wireless or Bluetooth.
To measure temperature I bought a waterproof version of a DS18B20 so called "one wire" sensor. The name is a bit of a misnomer as it has three wires coming out of it, but two are for power and only one wire is used for the data. A pack of 5 sensors cost £6.00.
I attached the sensor and the 4.7KΩ resistor onto a small circuit board above the Pi Zero.
Please ignore the quality of the soldering - I made a mistake and had to unsolder everything and put things back in the correct place.
The program is set to run in the background as soon as the device is powered up, sending me an email immediately and then every 30 minutes or at any interval I choose. At the moment I've set the warning temperature to 20C so I can see what it happening but by setting the warning limit higher it wont send an email unless the limit is exceeded.
I fitted it all into a purpose made case (which cost more than the basic Pi Zero ) and here it is sitting in the MH.
If you don't want to lash out on a proper case a bit of Tupperware could be adapted or you could make your own using something like foam board.
The emails look like this - these show them coming frequently but that was because I was fiddling with it and turning it on and off.
The total cost of everything was about £20 but it is worth getting a Pi Zero kit at a little under £30 initially as it will come with the various adaptors needed to connect it to a keyboard and TV screen.
This device uses the MiFi in the MH to connect but if there is a Mk 3 version later it will probably use a GSM/GPRS module so it make its own calls using a cheap PAYG sim card as @scotjimland suggested.
The Python code I used is as follows. The email settings are for Gmail and will be different for other providers. The "28-01131e6729ff" number you can see is the unique identifier for the temperature sensor. Different sensors will have different numbers.
#!/usr/bin/python
import time
import sys
import os
import smtplib
import subprocess
# Set warning temperature
warningtemp = 20
# Set interval in minutes between warning emails. 28 works out every half an hour
warninginterval = 28
# initialize counter and email warning flag
counter = 0
warning = 0
# Set up the temperature sensor and read the temperature
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
temp_sensor = '/sys/bus/w1/devices/28-01131e6729ff/w1_slave'
def temp_raw():
def read_temp():
while True:
# read the temperature
# Test if over-temp and an email has not been recently sent
# Send email
# Increment counter
# Test if it is time to send another warning email
# If so then reset everything
# Stop counter getting too big and potentially generating an error.
# Delay everything for a minute
The system worked but the hardware was more expensive than necessary so I set out to make a cheaper version using a Raspberry Pi Zero W:
These cost about £9.50 but you do have to buy and solder on the 40 pin header. It is quite small and you can judge the size by the Micro SD card you can see on the left in the photograph above. The "W" in the name stands for "Wireless" which together with Bluetooth is built into the device. The original version which is still available does not have wireless or Bluetooth.
To measure temperature I bought a waterproof version of a DS18B20 so called "one wire" sensor. The name is a bit of a misnomer as it has three wires coming out of it, but two are for power and only one wire is used for the data. A pack of 5 sensors cost £6.00.
I attached the sensor and the 4.7KΩ resistor onto a small circuit board above the Pi Zero.
Please ignore the quality of the soldering - I made a mistake and had to unsolder everything and put things back in the correct place.
The program is set to run in the background as soon as the device is powered up, sending me an email immediately and then every 30 minutes or at any interval I choose. At the moment I've set the warning temperature to 20C so I can see what it happening but by setting the warning limit higher it wont send an email unless the limit is exceeded.
I fitted it all into a purpose made case (which cost more than the basic Pi Zero ) and here it is sitting in the MH.
If you don't want to lash out on a proper case a bit of Tupperware could be adapted or you could make your own using something like foam board.
The emails look like this - these show them coming frequently but that was because I was fiddling with it and turning it on and off.
The total cost of everything was about £20 but it is worth getting a Pi Zero kit at a little under £30 initially as it will come with the various adaptors needed to connect it to a keyboard and TV screen.
This device uses the MiFi in the MH to connect but if there is a Mk 3 version later it will probably use a GSM/GPRS module so it make its own calls using a cheap PAYG sim card as @scotjimland suggested.
The Python code I used is as follows. The email settings are for Gmail and will be different for other providers. The "28-01131e6729ff" number you can see is the unique identifier for the temperature sensor. Different sensors will have different numbers.
#!/usr/bin/python
import time
import sys
import os
import smtplib
import subprocess
# Set warning temperature
warningtemp = 20
# Set interval in minutes between warning emails. 28 works out every half an hour
warninginterval = 28
# initialize counter and email warning flag
counter = 0
warning = 0
# Set up the temperature sensor and read the temperature
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
temp_sensor = '/sys/bus/w1/devices/28-01131e6729ff/w1_slave'
def temp_raw():
f = open(temp_sensor, 'r')
lines = f.readlines()
f.close()
return lines
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = temp_raw()
while lines[0].strip()[-3] != 'YES':
# Main loopwhile lines[0].strip()[-3] != 'YES':
time.sleep(0.2)
lines = temp_raw()
temp_output = lines[1].find('t=')
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
lines = temp_raw()
temp_output = lines[1].find('t=')
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
while True:
# read the temperature
temp_string = str(read_temp())
# Test if over-temp and an email has not been recently sent
if temp_string > warningtemp and warning == 0:
print ("temperature: " , temp_string)
# Send email
from email.mime.text import MIMEText
USERNAME = "myemail"at"gmail.com"
PASSWORD = "email-pwd"
MAILTO = "myemail "at" gmail.com"
msg = MIMEText('Internal temperature = '+str(temp_string)+'C')
msg['Subject'] = 'From Murvi'
msg['From'] = USERNAME
msg['To'] = MAILTO
PASSWORD = "email-pwd"
MAILTO = "myemail "at" gmail.com"
msg = MIMEText('Internal temperature = '+str(temp_string)+'C')
msg['Subject'] = 'From Murvi'
msg['From'] = USERNAME
msg['To'] = MAILTO
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo_or_helo_if_needed()
server.starttls()
server.ehlo_or_helo_if_needed()
server.login(USERNAME,PASSWORD)
server.sendmail(USERNAME, MAILTO, msg.as_string())
server.quit()
# Set warning flag to show an email has been sent
warning=1
server.ehlo_or_helo_if_needed()
server.starttls()
server.ehlo_or_helo_if_needed()
server.login(USERNAME,PASSWORD)
server.sendmail(USERNAME, MAILTO, msg.as_string())
server.quit()
# Set warning flag to show an email has been sent
warning=1
# Increment counter
counter += 1
# Test if it is time to send another warning email
if counter > warninginterval and warning == 1:
# If so then reset everything
counter = 0
warning = 0
warning = 0
# Stop counter getting too big and potentially generating an error.
if counter > 1000:
counter = 0
# Delay everything for a minute
time.sleep(60)
Last edited: