Boca
Posts: 14
Joined: Sat Feb 24, 2018 1:25 pm

Re: Pi as heating/elec failure alarm

Fri Mar 23, 2018 7:15 am

Hi RDS,

the crontab entry which sends the hourly email is in brackets in my earlier post. The W1_slave content is below with the last 5 digits being the temperature (*1000).

Code: Select all

[email protected]:~ $ cat /sys/bus/w1/devices/28-051702709dff/w1_slave
1b 01 4b 46 7f ff 0c 10 4e : crc=4e YES
1b 01 4b 46 7f ff 0c 10 4e t=17687
Thanks, Tony

Boca
Posts: 14
Joined: Sat Feb 24, 2018 1:25 pm

Re: Pi as heating/elec failure alarm

Fri Mar 23, 2018 12:56 pm

Hi RDS,

The python to read the device is shown below and is from https://www.raspberrypi-spy.co.uk/2013/ ... er-sensor/

Code: Select all

#!/usr/bin/python
#--------------------------------------
#
#              ds18b20.py
#  Read DS18B20 1-wire temperature sensor
#
# Author : Matt Hawkins
# Date   : 10/02/2015
#
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------

def gettemp(id):
  try:
    mytemp = ''
    filename = 'w1_slave'
    f = open('/sys/bus/w1/devices/' + id + '/' + filename, 'r')
    line = f.readline() # read 1st line
    crc = line.rsplit(' ',1)
    crc = crc[1].replace('\n', '')
    if crc=='YES':
      line = f.readline() # read 2nd line
      mytemp = line.rsplit('t=',1)
    else:
      mytemp = 99999
    f.close()

    return int(mytemp[1])

  except:
    return 99999

if __name__ == '__main__':

  # Script has been called directly
  id = '28-00000482b243'
  print "Temp : " + '{:.3f}'.format(gettemp(id)/float(1000))
Thanks, Tony

RDS
Posts: 643
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Pi as heating/elec failure alarm

Fri Mar 23, 2018 5:15 pm

Hi Tony
Your code is completely different to mine and as I am still learning myself, I cannot suggest what needs to change.

However, I have shown below the code I use in my Python 3 program.
It takes the form of a couple of statements to install the email, a procedure (def send_email) which requires 2 strings: the subject and the message and then I then call up that procedure with the information I need to send, as shown in the third code listing.

Code: Select all

import smtplib
from email.mime.text import MIMEText

Code: Select all

def send_email(subjectt,messagee):
    USERNAME = "[email protected]"
    PASSWORD = "xxxxxxxxxxx"
    MAILTO  = "[email protected]"

    msg = MIMEText(messagee)
    msg['Subject'] = subjectt
    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()

Code: Select all

if detect % email_threshold == 0: # e.g. if threshold = 1000 it send the email every time detect increases by 1000
       send_email(str(detect) + " units today","Current Rate " + str(kwhr) + "kw/hr" + '\n' + "Last Rate" + '\n' + "Previous Rate")
The values of 'detect' and 'kwhr' are calculated in my program and are sent in the email from the pi, either in the title, or in the subject. I also send some text in the message area of the email. Originally I had intended to add more data here but I never got around to adding it!

Sorry I am not able to modify your code but the above code has been working successfully in my program for over 2 years.

As a test, if you use the combined code here completely in a separate Python 3 program and add a couple more lines at the top, to define some values for detect, email_threshold and kwhr (suggest 5000, 1000 and 3.45 respectively) and add your own email addresses and password, it will send the email with the values you entered. I always write my programs in small bits like this and get each bit working before combining them. (Just my way of doing it)

I hope this helps you. (I have edited this post a few times since originally posting it)

Return to “Beginners”