Monitoring MH Internal Temperature and Sending Email Alert - Mk 2

Discussion in 'Computers' started by DBK, Sep 1, 2018.

  1. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    11,879
    Location:
    Plympton, Devon
    Ratings:
    +26,939
    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:

    DSC_0527.JPG

    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.

    DSC_0528.JPG

    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.

    DSC_0530.JPG

    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.

    Screenshot_20180901-154210.png

    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​

    def read_temp():
    lines = temp_raw()
    while 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
    # Main loop
    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​

    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​

    # 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​

    # 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: Sep 1, 2018
    • Like it Like it x 8
    • Informative Informative x 2
    • Nice One! Nice One! x 2
  2. The Dudess

    The Dudess Funster

    Joined:
    Jan 10, 2013
    Messages:
    4,121
    Location:
    Near Uttoxeter and Crete
    Ratings:
    +5,351
    I don't understand a word of it ......................... but I think you're a very clever man. (y)(y)(y)
     
    • Like it Like it x 1
    • Thanks Thanks x 1
  3. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    11,879
    Location:
    Plympton, Devon
    Ratings:
    +26,939
    Not clever really, all the code etc is available on the interweb thingy. It is just a matter of adapting it and sticking it together so the joins are not too obvious. :)
     
    • Like it Like it x 1
    • Nice One! Nice One! x 1
  4. autorouter

    autorouter Funster

    Joined:
    Apr 27, 2016
    Messages:
    503
    Location:
    Manchester
    Ratings:
    +439
    You can buy one of these with the 40 pin header already soldered on. It's called the Raspberry Pi Zero WH. The "H" indicates the the header is soldered on.
     
    • Thanks Thanks x 1
    • Informative Informative x 1
  5. JackieP

    JackieP Funster

    Joined:
    Mar 10, 2010
    Messages:
    655
    Location:
    Isle of Man
    Ratings:
    +1,938
    What an amazing thing to put together. Well done you and thanks for the steps along the way. Not sure I could do it either but I have huge admiration for someone who can.

    I hardly ever leave the dog in the van but if I do then I do find they I get stressed worrying about the build up of heat while I’m away. I often rush back only to find it perfectly comfortable and Dugal fast asleep in his bed.
     
    • Thanks Thanks x 1
  6. The Dotties

    The Dotties Funster

    Joined:
    Jan 31, 2015
    Messages:
    891
    Location:
    Gloucester
    Ratings:
    +2,290
    Two words spring to mind.
    Dragons Den.
    Brilliant.
    I know that’s 3, but ‘Brilliant ‘ doesn’t count.
    Bugger. Now what have I done.:)
     
    • Thanks Thanks x 1
  7. Sportsnapper

    Sportsnapper Funster

    Joined:
    Sep 3, 2016
    Messages:
    57
    Location:
    Reading
    Ratings:
    +37
    Nice - though I feel even a Pi zero is a little overkill for temp monitoring. I've been wondering what I can add to ours in terms of automation and monitoring but using esp8266/NodeMcu devices. I'd hooked them up to a NodeRed server running on a Pi3. But I'm too bust atm getting some of the bigger & perhaps more 'important' things sorted out (more lights etc.)
     
    • Thanks Thanks x 1

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice