{"id":263,"date":"2017-03-11T21:46:01","date_gmt":"2017-03-11T21:46:01","guid":{"rendered":"http:\/\/www.adienicholls.co.uk\/blogs\/?p=263"},"modified":"2018-02-11T20:09:17","modified_gmt":"2018-02-11T20:09:17","slug":"raspberry-pi-thermal-probe","status":"publish","type":"post","link":"http:\/\/www.adienicholls.co.uk\/blog\/raspberry-pi-thermal-probe\/","title":{"rendered":"Raspberry Pi Thermal Probe"},"content":{"rendered":"<h1>How to read a temperature probe, save the value to a MySQL database and then show the value on a webpage or just show the value with no database.<\/h1>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/www.modmypi.com\/blog\/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi\" target=\"_blank\" rel=\"noopener\">See this link <\/a>for how to setup a DS18B20+ thermal probe with a Raspberry Pi, these instructions\u00a0 are the best that i have come across for this and will cover the bullet points below :<\/p>\n<ul>\n<li>Enabling the 1 wire features in the Raspbian OS<\/li>\n<li>Physically connecting the probe via a resistor to the RPi<\/li>\n<li>Instructions on how to read the probe and a script example<\/li>\n<\/ul>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.modmypi.com\/blog\/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi\" target=\"_blank\" rel=\"noopener\">https:\/\/www.modmypi.com\/blog\/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi<\/a><\/p>\n<h3>Once you have followed the above steps you should have a working temperature probe, <strong>that&#8217;s great.. but what can you do with it now!?<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-449\" src=\"http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2017\/03\/tempdemo.png\" alt=\"\" width=\"568\" height=\"72\" \/><\/p>\n<p>For the piTank\/piGrow projects,\u00a0 I started off by just using the script provide in the instructions above, modifying it slightly and using that to display the temperature value on the piTank web-page. Using some JavaScript you can make the box&#8217;s refresh every x seconds, running the probe read script and showing a new value without the need for a page reload.\u00a0 I call this a &#8216;live&#8217; reading.<\/p>\n<p><!--more--><\/p>\n<h3>This is how a live reading is taken:<\/h3>\n<p>Create a PHP file and copy the below code into it.\u00a0 I saved this as temp1.php.<\/p>\n<pre>&lt;?php\r\n# Program:\u00a0\u00a0 \u00a0piGrow\r\n# Version:\u00a0\u00a0 \u00a01.0\r\n# Probe:\u00a0\u00a0 \u00a0P1 - Canopy\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/File to read\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$file = '\/sys\/bus\/w1\/devices\/*DEVICE-ID*\/w1_slave';\r\n\u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/Read the file line by line\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$lines = file($file);\r\n\u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/Get the temp from second line \r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$temp = explode('=', $lines[1]);\r\n\u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/Setup some nice formatting (i.e. 21,3)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$temp = number_format($temp[1] \/ 1000, 1, '.', '');\r\n\u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/And echo that temp\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0echo \"Canopy Temp &lt;br&gt;\";\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0echo $temp . \"C\";\r\n?&gt;<\/pre>\n<p>You will need to replace *DEVICE-ID*\u00a0 with the device ID\/serial number of the probe that you have.<\/p>\n<p>Upload the file to your web server and allow the relevant permissions.<\/p>\n<p>Use the &lt;?php include(&#8220;temp1.php&#8221;);?&gt; syntax to display the probes value in your webpage (index.php).<\/p>\n<p>To get this value to update every 60000\u00a0 milliseconds (60 seconds) without reloading the page, Place the following Java Script inside the head of the page and ensure the &lt;?php include (&#8221; &#8220;);?&gt; has an object ID.<\/p>\n<pre>var auto_refresh = setInterval(function()\r\n{ \r\n$('<em>#temp1<\/em>').load('<em>temp1.php<\/em>').fadeIn(\"slow\");\r\n}, 60000);<\/pre>\n<p>Basically the JS says reload <em>this ID<\/em> with this <em>PHP script<\/em> every minute.<\/p>\n<pre>&lt;div class=\"tempbox\" id=\"temp1\"&gt;&lt;?php include(\"temp1.php\");?&gt;&lt;\/div&gt;<\/pre>\n<p>That was great, but I now wanted to log the temperature values to a database.\u00a0 With the values logged to a DB I can then query the DB and use the latest or average values in different action scripts ,turning heaters\/pumps\/air\/filters on and off. This value I call a stored reading.<\/p>\n<h3>This is how I take a stored reading<\/h3>\n<p><strong>This presumes you already have a MySQL instance setup andrunning, with a username\/password and DB ready.<\/strong><\/p>\n<p>Create a bash script to read the temp value and and hold it for the next step of the script, this next step sends the live value to the MySQL DB creating a stored reading:<\/p>\n<p>Basically, import some bits for the script to run, grab a value, connect and send to DB along with the date and time, everything goes right end as normal or if something goes wrong, send a close signal to\u00a0 the DB and error out.\u00a0 Note, some excess code is in there to aid debugging. Have a play!<\/p>\n<p>You will need to\u00a0 update the Device ID of the thermal probe, DB connection details, Table details and check everything over yourself.<\/p>\n<pre>#!\/usr\/bin\/env python\r\n# Program:\u00a0\u00a0 \u00a0piGrow\r\n# Version:\u00a0\u00a0 \u00a01.0\r\n# Probe:\u00a0\u00a0 \u00a01 \r\n\r\nprint \"Script : temp1log.py : Canopy : Start\"\r\n\r\nimport os\r\nimport time\r\nimport datetime\r\nimport glob\r\nimport MySQLdb\r\nimport sys\r\nfrom time import strftime\r\n\r\nos.system('modprobe w1-gpio')\r\nos.system('modprobe w1-therm')\r\ntemp_sensor = '\/sys\/bus\/w1\/devices\/**DEVICE ID*\/w1_slave'\r\n\r\ndb = MySQLdb.connect(\"localhost\",\"USERNAME\",\"PASSWORD\",\"DATABSE\" )\r\ncur = db.cursor()\r\n\r\ndef temp_raw():\r\n\u00a0\u00a0\u00a0 f = open(temp_sensor, 'r')\r\n\u00a0\u00a0\u00a0 lines = f.readlines()\r\n\u00a0\u00a0\u00a0 f.close()\r\n\u00a0\u00a0\u00a0 return lines\r\n\u00a0\u00a0 \u00a0\r\ndef read_temp():\r\n\u00a0\u00a0\u00a0 lines = temp_raw()\r\n\u00a0\u00a0\u00a0 while lines[0].strip()[-3:] != 'YES':\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 time.sleep(1)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lines = temp_raw()\r\n\u00a0\u00a0\u00a0 temp_output = lines[1].find('t=')\r\n\u00a0\u00a0\u00a0 if temp_output != -1:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 temp_string = lines[1].strip()[temp_output+2:]\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 temp_c = float(temp_string) \/ 1000.0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return temp_c\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\nwhile True:\r\n\u00a0\u00a0\u00a0 temp = read_temp()\r\n\u00a0\u00a0\u00a0 print temp\r\n\u00a0\u00a0\u00a0 datetimeWrite = (time.strftime(\"%Y-%m-%d \") + time.strftime(\"%H:%M:%S\"))\r\n\u00a0\u00a0\u00a0 print datetimeWrite\r\n\u00a0\u00a0\u00a0 sql = (\"INSERT INTO temp1 (datetime,temperature) VALUES (%s,%s)\",(datetimeWrite,temp))\r\n\u00a0\u00a0\u00a0 try:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print \"Writing to database...\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Execute the SQL command\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cur.execute(*sql)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Commit your changes in the database\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 db.commit()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print \"Write Complete\"\r\n\r\n\u00a0\u00a0\u00a0 except:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Rollback in case there is any error\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 db.rollback()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print \"Failed writing to database\"\r\n\r\n\u00a0\u00a0\u00a0 cur.close()\r\n\u00a0\u00a0\u00a0 db.close()\r\n\u00a0\u00a0\u00a0 break\r\n\u00a0\u00a0 \u00a0\r\nprint \"Script : temp1log.py : End\"\r\nsys.exit<\/pre>\n<p>Once the value has been saved you can display it on your webpage using the same method as the live reading, just the PHP file that is called has a different content:<\/p>\n<p>This PHP file will query the MySQL database for the last entered row of that table and then output the value.<\/p>\n<pre>&lt;?php\r\n# Program:\u00a0\u00a0 \u00a0piGrow\r\n# Version:\u00a0\u00a0 \u00a01.0\r\n# Probe:\u00a0\u00a0 \u00a01\r\n\r\n$mysqli = new mysqli(\"localhost\", \"USERNAME\", \"PASSWORD\", \"DATABASE\");\r\n\r\n\/* check connection *\/\r\nif ($mysqli-&gt;connect_errno) {\r\n\u00a0\u00a0\u00a0 printf(\"Connect failed: %s\\n\", $mysqli-&gt;connect_error);\r\n\u00a0\u00a0\u00a0 exit();\r\n}\r\n$query = \"SELECT temperature FROM temp1 ORDER BY id DESC LIMIT 0, 1\";\r\n$result = $mysqli-&gt;query($query);\r\n\/* numeric array *\/\r\n$row = $result-&gt;fetch_array(MYSQLI_NUM);\r\nprintf ($row[0]);\r\n\/* free result set *\/\r\n$result-&gt;free();\r\n\/* close connection *\/\r\n$mysqli-&gt;close();\r\n?&gt;\r\n<\/pre>\n<p>Then just call the PHP file using the same PHP include syntax as in the live reading<\/p>\n<p>Finally, set temp1log.py script to run every minuted in cron, all done!<\/p>\n<pre>pi@pigrow:~ $ sudo crontab -e<\/pre>\n<p>Add the following line to the bottom of the file.<\/p>\n<pre>*\/1 * * * * python \/home\/pi\/scripts\/temp1log.py &amp;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How to read a temperature probe, save the value to a MySQL database and then show the value on a webpage or just show the value with no database. &nbsp; See this link for how to setup a DS18B20+ thermal probe with a Raspberry Pi, these instructions\u00a0 are the best that i have come across &hellip; <a href=\"http:\/\/www.adienicholls.co.uk\/blog\/raspberry-pi-thermal-probe\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Raspberry Pi Thermal Probe<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[],"_links":{"self":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts\/263"}],"collection":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":7,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":464,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts\/263\/revisions\/464"}],"wp:attachment":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}