{"action":"created","issue":{"url":"https://api.github.com/repos/theyosh/TerrariumPI/issues/124","repository_url":"https://api.github.com/repos/theyosh/TerrariumPI","labels_url":"https://api.github.com/repos/theyosh/TerrariumPI/issues/124/labels{/name}","comments_url":"https://api.github.com/repos/theyosh/TerrariumPI/issues/124/comments","events_url":"https://api.github.com/repos/theyosh/TerrariumPI/issues/124/events","html_url":"https://github.com/theyosh/TerrariumPI/issues/124","id":306245519,"number":124,"title":"Adding Sensor Results from Serial (Arduino)","user":{"login":"rrgrow","id":37473193,"avatar_url":"https://avatars3.githubusercontent.com/u/37473193?v=4","gravatar_id":"","url":"https://api.github.com/users/rrgrow","html_url":"https://github.com/rrgrow","followers_url":"https://api.github.com/users/rrgrow/followers","following_url":"https://api.github.com/users/rrgrow/following{/other_user}","gists_url":"https://api.github.com/users/rrgrow/gists{/gist_id}","starred_url":"https://api.github.com/users/rrgrow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rrgrow/subscriptions","organizations_url":"https://api.github.com/users/rrgrow/orgs","repos_url":"https://api.github.com/users/rrgrow/repos","events_url":"https://api.github.com/users/rrgrow/events{/privacy}","received_events_url":"https://api.github.com/users/rrgrow/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-03-18T14:35:15Z","updated_at":"2018-03-20T21:16:32Z","closed_at":"2018-03-20T21:16:32Z","author_association":"NONE","body":"<p><span style=\"color: #ff0000;\"><em><strong>Note... </strong></em><em><strong>Not an Issue just a question..</strong></em></span></p><p>I've managed put this py script together from another project&nbsp;(probably porly) to receive data from the arduino mega through serial with multiple sensors readings split into pieces on one line then received by the Raspberry PI 3 on usb port x and insert into a sql database. I would run this script once a minute through crobtab.</p><h2>Hardware used for 2 water tanks:</h2><ol><li><strong>Arduino Mega&nbsp; x1</strong></li><li><strong>HC-SR04&nbsp; &nbsp;(DISTANCE) x2</strong></li><li><strong>DHT22&nbsp; &nbsp; &nbsp; &nbsp;(TEMP,HUM FOR HELPING DISTANCE CALCULATIONS) x2</strong></li><li><strong>DS18B20&nbsp; &nbsp;(Water Temperature Sensor) x2</strong></li></ol><h2>Results from running Script in terminal:</h2><h2>1&nbsp; &nbsp; 46&nbsp; &nbsp;40.70&nbsp; &nbsp;24.50&nbsp; &nbsp;17.56&nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp;47&nbsp; &nbsp; 41.60&nbsp; &nbsp;24.00&nbsp; &nbsp;19.50</h2><h2>Tank 1</h2><table width=\"559\"><tbody><tr><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td></tr><tr><td>1</td><td>46</td><td><p>40.70</p></td><td>24.50</td><td>17.56</td></tr><tr><td>Sensor_1</td><td>Distance_1</td><td><p>&nbsp; Humidity_Distance_1</p></td><td>Temp_Distance_1</td><td>Water_Temp_1</td></tr></tbody></table><p>&nbsp;</p><h2>Tank 2</h2><table width=\"524\"><tbody><tr><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td></tr><tr><td>2</td><td>47</td><td><p>41.60</p></td><td>24.00</td><td>19.50</td></tr><tr><td>Sensor_2</td><td>Distance_2</td><td><p>&nbsp; Humidity_Distance_2</p></td><td>Temp_Distance_2</td><td>Water_Temp_2</td></tr></tbody></table><p>&nbsp;</p><p><strong>&nbsp;</strong></p><h2>Python Script:</h2><p>#!/usr/bin/env python<br />import datetime<br />import serial<br />import time<br />import MySQLdb as mdb</p><p>arduino= serial.Serial(\"/dev/ttyUSB0\")<br />arduino.baudrate=9600</p><p>data = arduino.readline()<br />pieces = data.split(\"\\t\")<br />now = datetime.datetime.now()<br />con= mdb.connect('localhost','root','XXXXXX','water');</p><p>with con:<br /> cursor=con.cursor()<br /> cursor.execute(\"INSERT INTO level (date_time,sensor,level,hum,temp) VALUES (%s,%s,%s,%s,%s)\",(now,pieces[0],pieces[1],pieces[2],pieces[3])) <br /> cursor.execute(\"INSERT INTO level (date_time,sensor,level,hum,temp) VALUES (%s,%s,%s,%s,%s)\",(now,pieces[5],pieces[6],pieces[7],pieces[8])) <br /> cursor.execute(\"INSERT INTO temp (date_time,sensor,temp) VALUES (%s,%s,%s)\",(now,pieces[0],pieces[4])) <br /> cursor.execute(\"INSERT INTO temp (date_time,sensor,temp) VALUES (%s,%s,%s)\",(now,pieces[5],pieces[9])) <br /> con.commit()<br /> cursor.close()<br />print pieces[0],pieces[1],pieces[2],pieces[3],pieces[4],pieces[5],pieces[6],pieces[7],pieces[8],pieces[9]</p><p>&nbsp;</p><p>&nbsp;</p><h2>Arduino Sketch:</h2><p>// Include DHT Libraries from Adafruit<br />// Dependant upon Adafruit_Sensors Library<br />#include \"DHT.h\"</p><p>// Include NewPing Library<br />#include \"NewPing.h\"</p><p>// DS Library<br />#include &lt;OneWire.h&gt;<br />#include &lt;DallasTemperature.h&gt;</p><p><br />// Define Constants<br />#define DHTPIN_1 12 // DHT-22 Output Pin connection<br />#define DHTPIN_2 13 // DHT-22 Output Pin connection<br />#define DHTTYPE DHT22 // DHT Type is DHT 22 (AM2302)<br />#define TRIGGER_PIN_1 2<br />#define ECHO_PIN_1 3<br />#define TRIGGER_PIN_2 4<br />#define ECHO_PIN_2 5<br />#define MAX_DISTANCE 400<br />#define WATER_TEMP_1 6<br />#define WATER_TEMP_2 7</p><p><br />NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);<br />NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);<br />OneWire oneWire1(WATER_TEMP_1);<br />DallasTemperature watertemp1(&amp;oneWire1);<br />OneWire oneWire2(WATER_TEMP_2);<br />DallasTemperature watertemp2(&amp;oneWire2);</p><p>// Define Variables</p><p>float hum1; // Stores humidity value in percent<br />float temp1; // Stores temperature value in Celcius<br />float hum2; // Stores humidity value in percent<br />float temp2; // Stores temperature value in Celcius<br />float duration1; // Stores First HC-SR04 pulse duration value<br />float duration2; // Stores Second HC-SR04 pulse duration value<br />float distance1; // Stores calculated distance in cm for First Sensor<br />float distance2; // Stores calculated distance in cm for Second Sensor<br />int iterations = 5;<br />float Celcius1=0;<br />float Celcius2=0;</p><p>// Initialize DHT sensor for normal 16mhz Arduino</p><p>DHT dht1(DHTPIN_1, DHTTYPE); <br />DHT dht2(DHTPIN_2, DHTTYPE);</p><p>void setup() {<br /> Serial.begin (9600);<br /> dht1.begin();<br /> dht2.begin();<br /> watertemp1.begin();<br /> watertemp2.begin();<br /> <br />}</p><p>void loop()<br />{<br /> <br /> // Measure duration for first sensor<br /> <br /> duration1 = sonar1.ping_median(iterations);<br /> <br /> // Add a delay between sensor readings<br /> <br /> delay(10);<br /> <br /> // Measure duration for first sensor<br /> <br /> duration2 = sonar2.ping_median(iterations);<br /> <br /> delay(10); // Delay so DHT-22 sensor can stabalize<br /> <br /> hum1 = dht1.readHumidity(); // Get Humidity value<br /> temp1= dht1.readTemperature(); // Get Temperature value<br /> hum2 = dht2.readHumidity(); // Get Humidity value<br /> temp2= dht2.readTemperature(); // Get Temperature value<br /> watertemp1.requestTemperatures(); <br /> Celcius1= watertemp1.getTempCByIndex(0);<br /> watertemp2.requestTemperatures(); <br /> Celcius2= watertemp2.getTempCByIndex(0);<br /> // Calculate the distances for both sensors</p><p>long t1 = 0, h1 = 0, hp1 = 0, t2 = 0, h2 = 0, hp2 = 0, hpL1 = 0, hpL2 = 0;</p><p>// Waiting for pulse<br /> t1 = duration1 + (0.606 * temp1) + (0.0124 * hum1);<br /> t2 = duration2 + (0.606 * temp2) + (0.0124 * hum2);<br /> <br /> // Calculating distance 1<br /> h1 = t1 / 10;<br /> h1 = h1 - 50; // offset correction<br /> h1 = 30 - h1; // water height, 0 - 50 cm<br /> hp1 = 2 * h1; // distance in %, 0-100 %<br /> hpL1 = hp1*100/70/2; // Letre based on 70 litres at 100%<br /> <br /> // Calculating distance 2<br /> h2 = t2 / 10;<br /> h2 = h2 - 50; // offset correction<br /> h2 = 30 - h2; // water height, 0 - 50 cm<br /> hp2 = 2 * h2; // distance in %, 0-100 %<br /> hpL2 = hp2*100/70/2; // Letre based on 70 litres at 100%</p><p>// Sending to computer<br /> Serial.print(\"1\");<br /> Serial.print(\"\\t\"); // for splitting<br /> Serial.print(hpL1);<br /> Serial.print(\"\\t\"); // for splitting<br /> Serial.print(hum1);<br /> Serial.print(\"\\t\"); // for splitting<br />Serial.print(temp1);<br /> Serial.print(\"\\t\"); // for splitting<br /> Serial.print(Celcius1);<br /> Serial.print(\"\\t\"); // for splitting<br />Serial.print(\"2\");<br /> Serial.print(\"\\t\"); // for splitting<br /> Serial.print(hpL2);<br /> Serial.print(\"\\t\"); // for splitting<br /> Serial.print(hum2);<br /> Serial.print(\"\\t\"); // for splitting<br /> Serial.print(temp2);<br /> Serial.print(\"\\t\"); // for splitting<br /> Serial.print(Celcius2);<br /> Serial.println(\" \");<br /> <br /> delay(1000);<br />}</p><h2><strong>My question:</strong></h2><p>I would like to be able to add a sensor from sensor setting page and select serial port(number), piece [number] with current setting already in place. I think this would really help add more hardware to this project.&nbsp;&nbsp;</p><p>ie..</p><div class=\"x_content\"><div class=\"col-md-2 col-sm-2 col-xs-12 form-group\"><label for=\"sensor_[nr]_address\">Address<span class=\"required\">&nbsp;* = Serial</span></label></div><div class=\"col-md-2 col-sm-2 col-xs-12 form-group\"><label for=\"sensor_[nr]_type\">Type<span class=\"required\">&nbsp;* = Piece</span></label></div><div class=\"col-md-2 col-sm-2 col-xs-12 form-group\">Piece Number = 9 for example (Water Temp 2)</div><div class=\"col-md-2 col-sm-2 col-xs-12 form-group\"><label for=\"sensor_[nr]_name\">Name<span class=\"required\">&nbsp;* = Water Temp 2</span></label></div><div class=\"col-md-1 col-sm-1 col-xs-12 form-group\"><label for=\"sensor_[nr]_alarm_min\">Alarm min<span class=\"required\">&nbsp;* =&nbsp;</span></label></div><div class=\"col-md-1 col-sm-1 col-xs-12 form-group\"><label for=\"sensor_[nr]_alarm_max\">Alarm max<span class=\"required\">&nbsp;* =</span></label></div><div class=\"col-md-1 col-sm-1 col-xs-12 form-group\"><label for=\"sensor_[nr]_min\">Limit min&nbsp;<span class=\"required\">* =</span></label></div><div class=\"col-md-1 col-sm-1 col-xs-12 form-group\"><label for=\"sensor_[nr]_max\">Limit max&nbsp;<span class=\"required\">* =</span></label></div></div>"},"comment":{"url":"https://api.github.com/repos/theyosh/TerrariumPI/issues/comments/374759676","html_url":"https://github.com/theyosh/TerrariumPI/issues/124#issuecomment-374759676","issue_url":"https://api.github.com/repos/theyosh/TerrariumPI/issues/124","id":374759676,"user":{"login":"rrgrow","id":37473193,"avatar_url":"https://avatars3.githubusercontent.com/u/37473193?v=4","gravatar_id":"","url":"https://api.github.com/users/rrgrow","html_url":"https://github.com/rrgrow","followers_url":"https://api.github.com/users/rrgrow/followers","following_url":"https://api.github.com/users/rrgrow/following{/other_user}","gists_url":"https://api.github.com/users/rrgrow/gists{/gist_id}","starred_url":"https://api.github.com/users/rrgrow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rrgrow/subscriptions","organizations_url":"https://api.github.com/users/rrgrow/orgs","repos_url":"https://api.github.com/users/rrgrow/repos","events_url":"https://api.github.com/users/rrgrow/events{/privacy}","received_events_url":"https://api.github.com/users/rrgrow/received_events","type":"User","site_admin":false},"created_at":"2018-03-20T21:16:32Z","updated_at":"2018-03-20T21:16:32Z","author_association":"NONE","body":"Great work!\r\n\r\nGot that all sorted and working like a charm. Thanks for that I thought it would be a nightmare but it was quite easy.\r\n\r\nI was wondering if its easy enough to add more tanks,light etc in System environment? one more tank  and that shows the level/Litre by default    as well as / instead of    when the sensor option is applied. I have a timer set for my pump to feed 15 on 15 off (works well but without Water Level/Distance indicator ) I have no refill needs in the way of automation not as yet anyway.\r\nBe nice to have feature in System environment if possible or just a simple copy and paste and change names to suite etc.."}}
{"actor":{"display_login":"rrgrow"}}