{"id":187,"date":"2014-11-26T13:20:22","date_gmt":"2014-11-26T13:20:22","guid":{"rendered":"http:\/\/guceret.com\/wp\/?p=187"},"modified":"2014-12-02T05:18:13","modified_gmt":"2014-12-02T05:18:13","slug":"thermometer-by-email","status":"publish","type":"post","link":"https:\/\/guceret.com\/wp\/?p=187","title":{"rendered":"Thermometer DS18B20 with Beaglebone Black"},"content":{"rendered":"<p>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.<\/p>\n<p><!--more--><\/p>\n<p>First, install the DS18B20 termometer following the in instruction in \u00a0\u00a0<a title=\"DS18B20\" href=\"http:\/\/robogoby.blogspot.com.au\/2014\/05\/beaglebone-and-ds18b20-temp-sensor.html\" target=\"_blank\">DS18B20 here<\/a>.<\/p>\n<p>Then. connect the DS18B20 output \u00a0to\u00a0\u00a0&#8220;P9_22&#8221;, \u00a0GND to \u00a0&#8220;P9_46&#8221;, and Vdd to &#8220;P9_3&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>Code in python<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">#!\/usr\/bin\/python\r\n\r\n# Libraries for simple operations and threading\r\nimport threading\r\nimport math\r\nimport time\r\nimport Adafruit_BBIO.GPIO as GPIO\r\n# Libraries for sending emails\r\nimport smtplib\r\nfrom email.MIMEMultipart import MIMEMultipart\r\nfrom email.MIMEText import MIMEText\r\nfrom email.MIMEBase import MIMEBase\r\nfrom email import Encoders\r\n\r\n# Library for checking internet Connection \r\nimport urllib2\r\n\r\n# Email adresses to use, sender and receiver\r\n# Sender should be changed and tested before deployment\r\nfromaddr = &quot;sender_email&quot;\r\ntoaddr = &quot;receiver_email&quot;\r\n\r\n# Library and parameters for identifying USB \r\nimport os,glob\r\n\r\n# taking time to print date\r\nfrom datetime import datetime\r\n\r\n# Taking value from one-wire temperature sensor\r\n# The number starting in 28 varies for each sensor \r\nw1=&quot;\/sys\/bus\/w1\/devices\/28-0000052196d6\/w1_slave&quot;\r\n\r\n#global connection_busy\r\nconnection_busy = 0\r\nin_flag = 0 \r\ncon_flag = 0\r\nsetpoint_alarm = 28\r\n\r\n\r\nGPIO.setup(&quot;P9_42&quot;,GPIO.OUT)\r\n\r\n# Function for checking internet connection\r\ndef internet_com(): # Thread for chcking the internet connection. \r\n \r\n while True:\r\n # Checking connection evry 5 seconds\r\n current_seconds = datetime.now().strftime('%S')\r\n if int(float(current_seconds))% 5 ==0 : \r\n global in_flag\r\n in_flag=1\r\n try:\r\n response = urllib2.urlopen('http:\/\/74.125.228.100',timeout=3)\r\n con_flag1 = 1\r\n except : #urllib2.URLError as err:\r\n con_flag1=0\r\n #else : \r\n #con_flag1 = 1\r\n global con_flag\r\n con_flag = con_flag1\r\n \r\n\r\n# Thread for saivng data from weather station and temp sensor. \r\ndef sd_saving ():\r\n\r\n \r\n while True : \r\n # Saving values in sd card\r\n try: \r\n time.sleep(1)\r\n raw_serial = open(w1, &quot;r&quot;).read()\r\n data_cow = datetime.now().strftime('%d\/%m\/%y %H%M%S') + &quot;,&quot; + str(float(raw_serial.split(&quot;t=&quot;)&#x5B;-1])\/1000)+ &quot;\\n&quot;\r\n data_name= &quot;\/mnt\/usb\/&quot;+&quot;data_&quot;+datetime.now().strftime('%d%m%y') + &quot;.csv&quot;\r\n filesaving_3 = open(data_name,&quot;a&quot;) # Open File to save values\r\n filesaving_3.write(data_cow)\r\n filesaving_3.close()\r\n except : \r\n print &quot;Variables not ready&quot;\r\n\r\n \r\n\r\ndef email_comm ():\r\n # Thread for the email communication\r\n # File attachment has been disabled\r\n while True: \r\n raw_email = open(w1, &quot;r&quot;).read()\r\n \r\n if (float(raw_email.split(&quot;t=&quot;)&#x5B;-1])\/1000) &gt; setpoint_alarm : # Checking Temperature Value, to triggger Alarm\r\n \r\n internet_flag = con_flag \r\n if internet_flag==1 :# and connection_busy==0 : # Only when there is an internet connection\r\n try: \r\n \r\n server = smtplib.SMTP('smtp.gmail.com',587)\r\n # Login to the server\r\n server.ehlo()\r\n server.starttls()\r\n server.ehlo()\r\n server.login(&quot;your_email&quot;, &quot;your_password&quot;)\r\n # Creating Email. The Body of the email \r\n messagge_email = (&quot;The T is &quot; + str(float(raw_email.split(&quot;t=&quot;)&#x5B;-1])\/1000) + &quot; &quot; + datetime.now().strftime('%d\/%m\/%y %H:%M:%S') + &quot; &quot; \r\n &quot; &quot; + str(setpoint_alarm) + &quot; \\n &quot;)\r\n msg = MIMEMultipart()\r\n #msg = MIMEText(message)\r\n msg&#x5B;'From'] = fromaddr\r\n msg&#x5B;'To'] = toaddr\r\n msg&#x5B;'Subject'] = &quot;Python Degrees&quot;\r\n msg.attach( MIMEText(messagge_email) )\r\n \r\n # For sending attachment file. \r\n #file = &quot;\/mnt\/usb\/&quot;+datetime.now().strftime('%d%m%y') + &quot;.csv&quot;\r\n #part = MIMEBase('application', &quot;octet-stream&quot;)\r\n #part.set_payload( open(file,&quot;rb&quot;).read() )\r\n #Encoders.encode_base64(part)\r\n #part.add_header('Content-Disposition', 'attachment; filename=&quot;%s&quot;' % os.path.basename(file))\r\n #msg.attach(part)\r\n\r\n text = msg.as_string()\r\n server.sendmail(fromaddr, toaddr, text)\r\n server.close()\r\n except: \r\n print &quot;error email&quot; \r\n\r\n\r\ndef status_led(): \r\n # Thread that controls two LEDs to show the status of connection \r\n old_min = 0\r\n while True: \r\n current_min = datetime.now().strftime('%S')\r\n if int(float(current_min))%3==0 : # Check the LEDs every 3 seconds \r\n \r\n old_min = datetime.now().strftime('%M')\r\n status = con_flag\r\n if status == 1 : \r\n GPIO.output(&quot;P9_42&quot;,GPIO.HIGH) \r\n\r\n if status == 0 : \r\n while status == 0: # No connection, inside the while until is connected again\r\n GPIO.output(&quot;P9_42&quot;,GPIO.LOW)\r\n time.sleep(0.4)\r\n GPIO.output(&quot;P9_42&quot;,GPIO.HIGH) \r\n time.sleep(0.4)\r\n status = con_flag\r\n \r\n\r\n\r\n# Start the threads, \r\np = threading.Thread (target=email_comm) # Email Alarm Thread\r\nu = threading.Thread (target=status_led) # Status LED Thread\r\nz = threading.Thread (target=internet_com) # Internet Connection Thread \r\ny = threading.Thread (target=sd_saving) # SD saving Thread\r\n\r\n# Starting the threads\r\nz.start()\r\np.start()\r\nu.start()\r\ny.start()\r\n\r\n\r\n# Start main routine\r\nwhile True:\r\n \r\n raw = open(w1, &quot;r&quot;).read() # Reading temperature from sensor\r\n message_1 = (&quot;T=&quot;+str(float(raw.split(&quot;t=&quot;)&#x5B;-1])\/1000) + &quot; C&quot;)\r\n print message_1\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-187","post","type-post","status-publish","format-standard","hentry","category-beaglebone-black"],"_links":{"self":[{"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=187"}],"version-history":[{"count":13,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":219,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/187\/revisions\/219"}],"wp:attachment":[{"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guceret.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}