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:
    12,242
    Location:
    Plympton, Devon
    Ratings:
    +28,025
    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 x 10
    • Nice One! x 4
    • Informative x 3
    • Thanks x 1
    • Useful x 1
  2. The Dudess

    The Dudess Funster

    Joined:
    Jan 10, 2013
    Messages:
    4,270
    Location:
    Near Uttoxeter and Crete
    Ratings:
    +5,585
    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:
    12,242
    Location:
    Plympton, Devon
    Ratings:
    +28,025
    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:
    582
    Location:
    Manchester
    Ratings:
    +505
    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:
    658
    Location:
    Isle of Man
    Ratings:
    +1,992
    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:
    951
    Location:
    Gloucester
    Ratings:
    +2,442
    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:
    107
    Location:
    Reading
    Ratings:
    +99
    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
  8. Guigsy

    Guigsy Funster

    Joined:
    Sep 17, 2017
    Messages:
    331
    Location:
    Birmingham, UK
    Ratings:
    +796
    I'm going to have a go at this. Got the Pi on order. Further ideas are:
    • Two temperature sensors using 1-wire bus. One inside, one outside.
    • Log the data in a simple database for graphing, etc.
    • Connect to my my solar setup to see if I can gather better data than the monitor I've got is currently providing. Apparently I just need a serial connection.
     
    • Thanks Thanks x 1
  9. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    12,242
    Location:
    Plympton, Devon
    Ratings:
    +28,025
    Best wishes, I'll be interested to hear how your solar monitoring goes and how you intend to do it. :)
     
  10. Sportsnapper

    Sportsnapper Funster

    Joined:
    Sep 3, 2016
    Messages:
    107
    Location:
    Reading
    Ratings:
    +99
    @Guigsy - rather than installing a DB, you could use a service - @DBK did some testing with Thinkspeak here https://www.motorhomefun.co.uk/forum/threads/what-can-you-see.181922/ - though he didn't say how he was updating it? I did a similar experiment a year or so ago, using the ESP8266 and the same sensor then updating Thingspeak via HTTP. here's an example - though I've not tried this, it's similar to code I've used in the past. https://randomnerdtutorials.com/esp8266-daily-task-publish-temperature-readings-to-thingspeak/
    Though you've whetted my appetite to try this again, I even think I've got a 12v to 5v converter spare
     
    • Thanks Thanks x 1
  11. The Wino

    The Wino Funster

    Joined:
    Mar 23, 2012
    Messages:
    2,488
    Location:
    leicester
    Ratings:
    +6,555
    Ive got a couple of Pi's sitting around not being used wonder if I could use them for this it would come in usefull. I was originally going to use them for a low power pc alternative for e-mails but that was 3 years ago they are just gathering dust atm
     
    • Thanks Thanks x 1
  12. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    12,242
    Location:
    Plympton, Devon
    Ratings:
    +28,025
    The Thinkspeak graphs worked but I realised for what I was trying to do a simple email was better. In theory you can get alerts from Thinkspeak but I couldnt get it to work. l think you can only get this if you pay for their subscription membership, which was around $1K a year!
     
  13. Guigsy

    Guigsy Funster

    Joined:
    Sep 17, 2017
    Messages:
    331
    Location:
    Birmingham, UK
    Ratings:
    +796
    I'm not decided if I want an onboard or cloud database yet.

    Advantages of going via the cloud is that I've got remote access. Also that the data and it's less likely to be corrupted when the Pi gets a power spike and nukes the SD card (unlikely, but possible). There are probably a few services out there that do most of what I need, probably meaning I can get it working very quickly.

    Disadvantage of sending to the cloud is that I'll always need an internet connection or the data will probably be lost. Also, as @DBK pointed out, the free ones never quite do all that you need.

    I've got 250w of solar panels and an MPPT charger, so I'll be interested to see if that's enough to keep the Pi, MiFi and tracker working all winter long. I know they'll only needs a few dozen watt-hours of juice per day but if the van is in the shade, in the depths of winter, will I get anything? At least I'll be able to monitor it.

    I'm powering the Pi via a Pimoroni Wide Input Shim, so it should cope with the massive fluctuations in voltage. I've just got to make sure I disconnect it if the input voltage is going critically low because it looks like it'll quite happily pancake the battery.

    I was also thinking about trying to find a 1-wire light sensor to monitor daylight. With my current solar setup, often I don't seem to be generating a lot of power. I'm never quite sure whether that's because the battery charge is already pretty high, or it's not as bright as my eyes think it is. It'd be useful extra metric to have.
     
    • Thanks Thanks x 1
  14. sonny

    sonny Funster

    Joined:
    Feb 10, 2013
    Messages:
    293
    Location:
    plymouth
    Ratings:
    +431
    Is this to tell you time to phone the fire brigade :)
     
    • Thanks Thanks x 1
  15. Sportsnapper

    Sportsnapper Funster

    Joined:
    Sep 3, 2016
    Messages:
    107
    Location:
    Reading
    Ratings:
    +99
    @Guigsy - that's a nice little board - not seen that before (but I'm not doing much with Pis atm). Wish I'd seen that before they had their 10% off day!
     
  16. The Dotties

    The Dotties Funster

    Joined:
    Jan 31, 2015
    Messages:
    951
    Location:
    Gloucester
    Ratings:
    +2,442
    My head hurts!
     
    • Funny Funny x 2
  17. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    12,242
    Location:
    Plympton, Devon
    Ratings:
    +28,025
    That had occurred to me! But it would be very easy to alter the code so it sent an immediate email if the temperature rose above say 50°C.

    Other options include a sensor to report if the vehicle moves. A camera could also be included to take a photo of the inside of the vehicle and email the image to me. This could be used as a security thing - taking a photo of any intruders. :)
     
  18. Sportsnapper

    Sportsnapper Funster

    Joined:
    Sep 3, 2016
    Messages:
    107
    Location:
    Reading
    Ratings:
    +99
    I was wondering about a GPS tracker. Not for security, but so that I know where I've been (don't laugh). Our first ever Garmin Quest used to save tracks, so we could look at our journeys and see how long it took us to get somewhere, actual routes etc. Couldn't see the screen it was so small, but a great device. Now I use Co-pilot on my phone, but it doesn't keep a track. This would be one way of doing it.
     
    • Thanks Thanks x 1
  19. Zains Pops

    Zains Pops Funster

    Joined:
    Sep 10, 2013
    Messages:
    983
    Location:
    Chelmsford Essex
    Ratings:
    +10,694
    That’s a guaranteed A* GCSE right there.

    Interesting stuff.
     
    • Thanks Thanks x 1
  20. hilldweller

    hilldweller Funster Life Member

    Joined:
    Dec 5, 2008
    Messages:
    30,586
    Location:
    Macclesfield
    Ratings:
    +40,407
    Maybe re-inventing the wheel. On a bike forum I watched a bike being ridden across the USA just using a mobile phone and app. Full on screen make all beautifully marked up.

    Logging GPS data is trivial, presenting it nicely takes more effort.
     
    • 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