Monitoring Internal MH Temperature and Sending Alarm Email

Discussion in 'Computers' started by DBK, Aug 24, 2018.

  1. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    11,879
    Location:
    Plympton, Devon
    Ratings:
    +26,939
    In this thread: https://www.motorhomefun.co.uk/forum/threads/what-can-you-see.181922/
    I reported some early progress monitoring the internal temperature of the MH and displaying the information on a website which could be accessed from a mobile phone.

    The work has moved on a bit and changed. The graphs were interesting and if I wanted to set up a remote weather station (which was the original idea :)) then this method would be fine. But what I want to do first is monitor internal temperature to ensure our dog doesn't cook if we have to leave him in there and if the temperature rises too high for the system to warn me. Of course, we shouldn't put our dog in that position in the first place but if we can at least be sure the temperatures are not too high in the MH it will give a degree of reassurance.

    I tried first a method where it sent me a tweet if the temperature went above a set threshold. This sort of worked but the tweet didn't arrive for hours so that was not a lot of use!

    The ideal would be for it to send me an SMS text message and Vodafone used to have a service where you could send an email and they would re-send it as an SMS message. Sadly they discontinued this service some time ago although some providers, and I think T-Mobile are one of them, still offer it.

    But I'm with Vodafone so I will have to stay with emails.

    The current hardware looks like this:

    DSC_0515.JPG

    The white thing sticking out of the side is the wireless dongle which allows the Pi to connect with the MiFi. Later Pi models have wireless (and bluetooth) built in.

    The thing with the array of white squares sitting on top of the Raspberry Pi is the SenseHat which for this job is way over the top. It has numerous sensors including a magnetometer, gyroscope, accelerometers as well as the pressure, humidity and temperature sensors I used. It is an interesting bit of kit but suffers from a major weakness - being mounted above the Pi it sits in the warm air rising up from the CPU and other components so the accuracy of the temperature and relative humidity readings are wrong.

    There are ways to reduce the effect and in my code I've used one of them which is to measure the CPU temperature and use it to compensate. The result isn't perfect but it is within a couple of degrees or so in my testing which is good enough as the threshold temperature at which it issues an email can be set to take this into account.

    As it stands now the device measures the temperature every minute and if it goes above a temperature set by me, say 35C it will send me an email then another after thirty minutes and so on until I return and switch it off. The program can be set to run automatically when the USB cable powering the Pi is plugged into a USB socket.

    The email I get looks like this:

    From: Murvi (or whatever your MH is called :))

    To: my email address


    Subject: High temperature warning! Temperature = 26.8C

    To get the email above I set the temperature threshold down to 25C so it would generate the message.

    So it works, but it isn't cheap so I have started on a Mk 2 version which will use a Raspberry Pi Zero W which costs only around £10 and to sense the temperature I will use a digital temperature sensor like this, which I've used before with the Pi and cost less than a fiver. I might get it finished before we go away in a week and if so I'll report back.

    [​IMG]

    For the geeks the Python code I'm using is as follows. The code following WHILE & IF statements should be indented but this doesn't seem to work on the Forum although indents are essential in Python. It is also set up for Gmail, the settings for other providers will be different. To get it to run I had to alter my Gmail settings to allow less secure apps to run. Without this the Gmail server won't accept the email.

    The lines starting with "print" are not required, they just print the result in the terminal window and were useful for me to see what was happening when debugging it. These and of course the humidity and pressure lines are also not required when this is run purely as an alarm. I've left them in just for completeness. :)

    An option would be to get the program to send an email every, say, thirty minutes which simply showed the internal temperature. The emails could be in two formats, an information one and a second one with the warning of high temperatures. I will look at this for the Mk 2. :)

    For better security it is probably best to create a new email account for this because if the device was ever stolen my email password could be found on it. I'll have a look at this in slower time too.

    #!/usr/bin/python

    from sense_hat import SenseHat
    import time
    import sys
    import os
    import smtplib
    import subprocess

    # Set warning temperature
    warningtemp = 35

    # Set interval in minutes between warning emails
    warninginterval = 30

    # initialize counter and email warning flag

    counter = 0
    warning = 0

    # Read SenseHat Data
    sense = SenseHat()
    sense.clear()

    while True:
    output = subprocess.check_output("cat /sys/class/thermal/thermal_zone0/temp", shell=True)
    cpu = int(output)/1000 # Get CPU temperature
    cpu = round(cpu, 1)
    tempraw = sense.get_temperature() # Get temp from SenseHat
    tempraw = round(tempraw, 1)
    temp = (tempraw - (cpu - tempraw)) #Modify the temperature to compensate for the CPU temp
    temp = round(temp, 1)
    print "Temperature: " + str(temp)+"C" # Not required
    humidity = sense.get_humidity() # Not required
    humidity = round(humidity, 1) # Not required
    print("Humidity :",humidity) # Not required
    pressure = sense.get_pressure() # Not required
    pressure = round(pressure, 1) # Not required
    print("Pressure:",pressure) # Not required

    # Test if over-temp

    if temp > warningtemp and warning == 0:

    # Set warning flag

    warning = 1

    from email.mime.text import MIMEText

    USERNAME = "my email address"
    PASSWORD = "my email password"
    MAILTO = "my email address"
    msg = MIMEText('High temperature warning! Temperature = '+str(temp)+'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()

    # 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 > 100:

    counter = 0

    # Delay everything for a minute

    time.sleep(60)
     
    Last edited: Aug 24, 2018
    • Like it Like it x 2
    • Informative Informative x 1
    • Nice One! Nice One! x 1
  2. munkey_bwy

    munkey_bwy Funster

    Joined:
    May 17, 2018
    Messages:
    47
    Location:
    Holbeach
    Ratings:
    +61
    Very nerdy and nice. Can you not more the piehat away from the pi with a GPIO extension cable? I'm sure that they are available.
     
    • Thanks Thanks x 1
  3. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    11,879
    Location:
    Plympton, Devon
    Ratings:
    +26,939
    Yes that would work and if I want to try this as the basis for a weather station it would be essential but it then becomes a bit unwieldy and impractical. Ideally I want to have a one small box which I can stick on the MH interior wall which will have a small sensor sticking out of it. Hopefully, this is what the Mk 2 version will look like. :)
     
  4. Richard and Ann

    Richard and Ann Funster

    Joined:
    Jan 19, 2014
    Messages:
    1,642
    Location:
    Derbyshire
    Ratings:
    +3,183
    Ok if you enjoy doing it, which I suspect you do (y). If we had an animal, I think I'd leave the roof open and a fan on....

    Or... A more modern solution is leave it fastened up all day outside barking :cool: sorted
     
    • Funny Funny x 2
    • Like it Like it x 1
  5. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    11,879
    Location:
    Plympton, Devon
    Ratings:
    +26,939
    A good point of course, ideally the dog shouldn't be left in the MH and it doesn't happen often. But we do sometimes have lunch out, which is the hottest time of the day.
    And yes, this is just a hobby thing!
     
  6. eddievanbitz

    eddievanbitz Trader - Funster

    Joined:
    Oct 4, 2007
    Messages:
    4,750
    Location:
    Taunton Somerset
    Ratings:
    +21,432
    • Thanks Thanks x 1
    • Nice One! Nice One! x 1
  7. TCG

    TCG Funster

    Joined:
    Jul 6, 2017
    Messages:
    353
    Location:
    Salford Priors
    Ratings:
    +484
    As we do. But we just leave the air con on (y) or better still, if in France just take the dog with us
     
    • Thanks Thanks x 1
  8. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    11,879
    Location:
    Plympton, Devon
    Ratings:
    +26,939
    A text is something which on my phone is very obvious when it arrives. So a text is still a very good idea and better than an email I think but I got lost looking for free providers who would turn an email into a text. As I mentioned above Vodafone don't do this anymore.

    In theory it ought to be possible to use the Huawei MiFi the Pi is connecting to to send a text, it can using the app, but I couldn't find anything understandable by me how to do this from the command line.
     
  9. The Dotties

    The Dotties Funster

    Joined:
    Jan 31, 2015
    Messages:
    891
    Location:
    Gloucester
    Ratings:
    +2,290
    @DBK .

    I can do this a much easier way.
    Wait for you to test it out, then ask when you are going into production :LOL:
     
    • Funny Funny x 1
  10. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    11,879
    Location:
    Plympton, Devon
    Ratings:
    +26,939
    I've worked out how to do indents on the forum, so here is a simplified version of the code above. It is what will be used on the alarm only Mk 2 version. :)

    #!/usr/bin/python

    from sense_hat import SenseHat
    import time
    import sys
    import os
    import smtplib

    # Set warning temperature
    warningtemp = 35

    # Set interval in minutes between warning emails
    warninginterval = 30

    # initialize counter and email warning flag

    counter = 0
    warning = 0

    # Read SenseHat Data
    sense = SenseHat()
    sense.clear()

    while True:

    temp = sense.get_temperature()
    temp = round(temp, 1)​

    # Test if over-temp

    if temp > warningtemp and warning == 0:​

    # Send warning email

    from email.mime.text import MIMEText​

    USERNAME = "my email address"
    PASSWORD = "my email password"
    MAILTO = "my email address"
    msg = MIMEText('High temperature warning! Temperature = '+str(temp)+'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 flag to record 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)​
     
    • Like it Like it x 1
  11. Jenka

    Jenka Funster

    Joined:
    Mar 28, 2017
    Messages:
    20
    Location:
    Tamworth
    Ratings:
    +19
    Just a thought Air Con!
     
    • Thanks Thanks x 1
  12. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    11,879
    Location:
    Plympton, Devon
    Ratings:
    +26,939
    Indeed, and when the power fails and it stops?

    But this is just a hobby thing as of course my lash up could also fail. :)

    However, the concept is sound. The sensor I've used could also detect movement and either report the dog bouncing around or someone nicking the vehicle. :) Add an audio sensor and it could even report the dog barking - or a baby crying if you needed to monitor that. :)
     
  13. Jenka

    Jenka Funster

    Joined:
    Mar 28, 2017
    Messages:
    20
    Location:
    Tamworth
    Ratings:
    +19
    Ah makes sense now good idea.:dogthanks:
     
    • Thanks Thanks x 1
  14. hilldweller

    hilldweller Funster Life Member

    Joined:
    Dec 5, 2008
    Messages:
    30,224
    Location:
    Macclesfield
    Ratings:
    +39,411
    It's so easy and cheap, look up SIM900.

    I got fed up with the ipaddress problems for plain data/control and went the text way. The SIM900 modules are just so easy to use and using giffgaff for free texts cheap. And of course it works world wide wherever you can receive a text.



    void sendStatus()
    {
    lcd.setCursor(0,1);
    // 0123456789012345
    lcd.print("STR");

    Serial.println("AT + CMGS = \"" + destinationNumberBrian + "\"");
    delay(1000);
    Serial.print("Stat:");

    if ( digitalRead( intruderPin ) == LOW ) Serial.print("Intruder ");

    if ( reported ) Serial.print("Reported ");
    else Serial.print("Not Reported ");

    if ( digitalRead( setPin ) == LOW ) Serial.print("Alarm Set ");
    else Serial.print("Alarm UnSet ");

    Serial.write((char)26); // ctrl+z
    delay(1000);

    Serial.println("AT+CMGD=1,4"); // delete all SMS, returns OK
    reported = false; // clear report lockout

    delay(5000); // time to read STR
    lcd.setCursor(0,1);
    // 0123456789012345
    lcd.print(" ");
    }
     
    • Thanks Thanks x 1
  15. DBK

    DBK Funster

    Joined:
    Jan 9, 2013
    Messages:
    11,879
    Location:
    Plympton, Devon
    Ratings:
    +26,939
    Many thanks for that Brian, the module is essentially a mobile phone so it makes it's own connection and therefore can send texts. That is certainly a very good alternative. :)

    There is one for the Raspberry Pi too, though this one I found first is a SIM800 but would do the same job I expect. I found this description which covers it: https://learnaddict.com/2018/02/17/...sages-with-raspberry-pi-and-sim800-gsm-board/
    One option would be I could send text messages to it to alter things like the threshold temperature it issues an alert at. :)

    [​IMG]
     
  16. hilldweller

    hilldweller Funster Life Member

    Joined:
    Dec 5, 2008
    Messages:
    30,224
    Location:
    Macclesfield
    Ratings:
    +39,411
    The world is your oyster. You could even turn on a fan to cool dog. Add a wire or two and you have an alarm that will text you. Add a GPS module and you have a tracker.

    I don't know the difference with the 800 I just assumed 900 was better.

    As you may have guessed the code I posted is Arduino C++. I've done some work with the Pi but for simple control the Arduino is so very cheap and a joy to use and I know C better than any other language so it suits me. Your project to me:
    £5 Arduino Uno, £5 i2c LCD, £3 DS3231 Clock/Temp, £20 SIM900, from UK stock. £33.
    Add GPS £6 and for £40 you have dog control, alarm, GPS tracker. Eat yer heart out Eddie.
     
    • Thanks Thanks x 1
    • Love it Love it x 1
  17. Lenny HB

    Lenny HB Funster

    Joined:
    Oct 18, 2007
    Messages:
    13,663
    Location:
    On the coast in West Sussex
    Ratings:
    +21,452
    Interesting thread guys, thanks.
     
    • Thanks Thanks x 1
  18. Borderland

    Borderland Funster

    Joined:
    Nov 5, 2014
    Messages:
    657
    Location:
    Borders
    Ratings:
    +3,525
    I have used a cheap GPS tracker and temperature monitor with alarm output. I have fed the temp alarm to the tracker alarm input. The tracker has only one alarm input but you feed many different types of alarm using diodes. If an alarm goes off you don't know what the alarm is until you go back to the vehicle but any alarm would need investigating so therefore no need to know when you receive the alert.
    The tracker sends a warning to my mobile and total cost for tracker and temp monitor was approx £40.
     
    • Like it Like it x 1
    • 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