Thermometer DS18B20 with Beaglebone Black

This thermometer is useful for this summer, it will send me an alert when the temperature reaches 27 degrees. As I am in Australia in summer. I am doing this as my AC unit is not working properly. Also, I did it because I can.

First, install the DS18B20 termometer following the in instruction in   DS18B20 here.

Then. connect the DS18B20 output  to  “P9_22”,  GND to  “P9_46”, and Vdd to “P9_3”

 

Code in python

 

#!/usr/bin/python

# Libraries for simple operations and threading
import threading
import math
import time
import Adafruit_BBIO.GPIO as GPIO
# Libraries for sending emails
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import Encoders

# Library for checking internet Connection 
import urllib2

# Email adresses to use, sender and receiver
# Sender should be changed and tested before deployment
fromaddr = "sender_email"
toaddr = "receiver_email"

# Library and parameters for identifying USB 
import os,glob

# taking time to print date
from datetime import datetime

# Taking value from one-wire temperature sensor
# The number starting in 28 varies for each sensor 
w1="/sys/bus/w1/devices/28-0000052196d6/w1_slave"

#global connection_busy
connection_busy = 0
in_flag = 0 
con_flag = 0
setpoint_alarm = 28


GPIO.setup("P9_42",GPIO.OUT)

# Function for checking internet connection
def internet_com(): # Thread for chcking the internet connection. 
 
 while True:
 # Checking connection evry 5 seconds
 current_seconds = datetime.now().strftime('%S')
 if int(float(current_seconds))% 5 ==0 : 
 global in_flag
 in_flag=1
 try:
 response = urllib2.urlopen('http://74.125.228.100',timeout=3)
 con_flag1 = 1
 except : #urllib2.URLError as err:
 con_flag1=0
 #else : 
 #con_flag1 = 1
 global con_flag
 con_flag = con_flag1
 

# Thread for saivng data from weather station and temp sensor. 
def sd_saving ():

 
 while True : 
 # Saving values in sd card
 try: 
 time.sleep(1)
 raw_serial = open(w1, "r").read()
 data_cow = datetime.now().strftime('%d/%m/%y %H%M%S') + "," + str(float(raw_serial.split("t=")[-1])/1000)+ "\n"
 data_name= "/mnt/usb/"+"data_"+datetime.now().strftime('%d%m%y') + ".csv"
 filesaving_3 = open(data_name,"a") # Open File to save values
 filesaving_3.write(data_cow)
 filesaving_3.close()
 except : 
 print "Variables not ready"

 

def email_comm ():
 # Thread for the email communication
 # File attachment has been disabled
 while True: 
 raw_email = open(w1, "r").read()
 
 if (float(raw_email.split("t=")[-1])/1000) > setpoint_alarm : # Checking Temperature Value, to triggger Alarm
 
 internet_flag = con_flag 
 if internet_flag==1 :# and connection_busy==0 : # Only when there is an internet connection
 try: 
 
 server = smtplib.SMTP('smtp.gmail.com',587)
 # Login to the server
 server.ehlo()
 server.starttls()
 server.ehlo()
 server.login("your_email", "your_password")
 # Creating Email. The Body of the email 
 messagge_email = ("The T is " + str(float(raw_email.split("t=")[-1])/1000) + " " + datetime.now().strftime('%d/%m/%y %H:%M:%S') + " " 
 " " + str(setpoint_alarm) + " \n ")
 msg = MIMEMultipart()
 #msg = MIMEText(message)
 msg['From'] = fromaddr
 msg['To'] = toaddr
 msg['Subject'] = "Python Degrees"
 msg.attach( MIMEText(messagge_email) )
 
 # For sending attachment file. 
 #file = "/mnt/usb/"+datetime.now().strftime('%d%m%y') + ".csv"
 #part = MIMEBase('application', "octet-stream")
 #part.set_payload( open(file,"rb").read() )
 #Encoders.encode_base64(part)
 #part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
 #msg.attach(part)

 text = msg.as_string()
 server.sendmail(fromaddr, toaddr, text)
 server.close()
 except: 
 print "error email" 


def status_led(): 
 # Thread that controls two LEDs to show the status of connection 
 old_min = 0
 while True: 
 current_min = datetime.now().strftime('%S')
 if int(float(current_min))%3==0 : # Check the LEDs every 3 seconds 
 
 old_min = datetime.now().strftime('%M')
 status = con_flag
 if status == 1 : 
 GPIO.output("P9_42",GPIO.HIGH) 

 if status == 0 : 
 while status == 0: # No connection, inside the while until is connected again
 GPIO.output("P9_42",GPIO.LOW)
 time.sleep(0.4)
 GPIO.output("P9_42",GPIO.HIGH) 
 time.sleep(0.4)
 status = con_flag
 


# Start the threads, 
p = threading.Thread (target=email_comm) # Email Alarm Thread
u = threading.Thread (target=status_led) # Status LED Thread
z = threading.Thread (target=internet_com) # Internet Connection Thread 
y = threading.Thread (target=sd_saving) # SD saving Thread

# Starting the threads
z.start()
p.start()
u.start()
y.start()


# Start main routine
while True:
 
 raw = open(w1, "r").read() # Reading temperature from sensor
 message_1 = ("T="+str(float(raw.split("t=")[-1])/1000) + " C")
 print message_1