Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Sensor Results from Serial (Arduino) #124

Closed
RRCRT opened this issue Mar 18, 2018 · 16 comments
Closed

Adding Sensor Results from Serial (Arduino) #124

RRCRT opened this issue Mar 18, 2018 · 16 comments

Comments

@RRCRT
Copy link

RRCRT commented Mar 18, 2018

Note... Not an Issue just a question..

I've managed put this py script together from another project (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.

Hardware used for 2 water tanks:

  1. Arduino Mega  x1
  2. HC-SR04   (DISTANCE) x2
  3. DHT22       (TEMP,HUM FOR HELPING DISTANCE CALCULATIONS) x2
  4. DS18B20   (Water Temperature Sensor) x2

Results from running Script in terminal:

1    46   40.70   24.50   17.56     2     47    41.60   24.00   19.50

Tank 1

01234
146

40.70

24.5017.56
Sensor_1Distance_1

  Humidity_Distance_1

Temp_Distance_1Water_Temp_1

 

Tank 2

56789
247

41.60

24.0019.50
Sensor_2Distance_2

  Humidity_Distance_2

Temp_Distance_2Water_Temp_2

 

 

Python Script:

#!/usr/bin/env python
import datetime
import serial
import time
import MySQLdb as mdb

arduino= serial.Serial("/dev/ttyUSB0")
arduino.baudrate=9600

data = arduino.readline()
pieces = data.split("\t")
now = datetime.datetime.now()
con= mdb.connect('localhost','root','XXXXXX','water');

with con:
cursor=con.cursor()
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]))
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]))
cursor.execute("INSERT INTO temp (date_time,sensor,temp) VALUES (%s,%s,%s)",(now,pieces[0],pieces[4]))
cursor.execute("INSERT INTO temp (date_time,sensor,temp) VALUES (%s,%s,%s)",(now,pieces[5],pieces[9]))
con.commit()
cursor.close()
print pieces[0],pieces[1],pieces[2],pieces[3],pieces[4],pieces[5],pieces[6],pieces[7],pieces[8],pieces[9]

 

 

Arduino Sketch:

// Include DHT Libraries from Adafruit
// Dependant upon Adafruit_Sensors Library
#include "DHT.h"

// Include NewPing Library
#include "NewPing.h"

// DS Library
#include <OneWire.h>
#include <DallasTemperature.h>


// Define Constants
#define DHTPIN_1 12 // DHT-22 Output Pin connection
#define DHTPIN_2 13 // DHT-22 Output Pin connection
#define DHTTYPE DHT22 // DHT Type is DHT 22 (AM2302)
#define TRIGGER_PIN_1 2
#define ECHO_PIN_1 3
#define TRIGGER_PIN_2 4
#define ECHO_PIN_2 5
#define MAX_DISTANCE 400
#define WATER_TEMP_1 6
#define WATER_TEMP_2 7


NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);
OneWire oneWire1(WATER_TEMP_1);
DallasTemperature watertemp1(&oneWire1);
OneWire oneWire2(WATER_TEMP_2);
DallasTemperature watertemp2(&oneWire2);

// Define Variables

float hum1; // Stores humidity value in percent
float temp1; // Stores temperature value in Celcius
float hum2; // Stores humidity value in percent
float temp2; // Stores temperature value in Celcius
float duration1; // Stores First HC-SR04 pulse duration value
float duration2; // Stores Second HC-SR04 pulse duration value
float distance1; // Stores calculated distance in cm for First Sensor
float distance2; // Stores calculated distance in cm for Second Sensor
int iterations = 5;
float Celcius1=0;
float Celcius2=0;

// Initialize DHT sensor for normal 16mhz Arduino

DHT dht1(DHTPIN_1, DHTTYPE);
DHT dht2(DHTPIN_2, DHTTYPE);

void setup() {
Serial.begin (9600);
dht1.begin();
dht2.begin();
watertemp1.begin();
watertemp2.begin();

}

void loop()
{

// Measure duration for first sensor

duration1 = sonar1.ping_median(iterations);

// Add a delay between sensor readings

delay(10);

// Measure duration for first sensor

duration2 = sonar2.ping_median(iterations);

delay(10); // Delay so DHT-22 sensor can stabalize

hum1 = dht1.readHumidity(); // Get Humidity value
temp1= dht1.readTemperature(); // Get Temperature value
hum2 = dht2.readHumidity(); // Get Humidity value
temp2= dht2.readTemperature(); // Get Temperature value
watertemp1.requestTemperatures();
Celcius1= watertemp1.getTempCByIndex(0);
watertemp2.requestTemperatures();
Celcius2= watertemp2.getTempCByIndex(0);
// Calculate the distances for both sensors

long t1 = 0, h1 = 0, hp1 = 0, t2 = 0, h2 = 0, hp2 = 0, hpL1 = 0, hpL2 = 0;

// Waiting for pulse
t1 = duration1 + (0.606 * temp1) + (0.0124 * hum1);
t2 = duration2 + (0.606 * temp2) + (0.0124 * hum2);

// Calculating distance 1
h1 = t1 / 10;
h1 = h1 - 50; // offset correction
h1 = 30 - h1; // water height, 0 - 50 cm
hp1 = 2 * h1; // distance in %, 0-100 %
hpL1 = hp1*100/70/2; // Letre based on 70 litres at 100%

// Calculating distance 2
h2 = t2 / 10;
h2 = h2 - 50; // offset correction
h2 = 30 - h2; // water height, 0 - 50 cm
hp2 = 2 * h2; // distance in %, 0-100 %
hpL2 = hp2*100/70/2; // Letre based on 70 litres at 100%

// Sending to computer
Serial.print("1");
Serial.print("\t"); // for splitting
Serial.print(hpL1);
Serial.print("\t"); // for splitting
Serial.print(hum1);
Serial.print("\t"); // for splitting
Serial.print(temp1);
Serial.print("\t"); // for splitting
Serial.print(Celcius1);
Serial.print("\t"); // for splitting
Serial.print("2");
Serial.print("\t"); // for splitting
Serial.print(hpL2);
Serial.print("\t"); // for splitting
Serial.print(hum2);
Serial.print("\t"); // for splitting
Serial.print(temp2);
Serial.print("\t"); // for splitting
Serial.print(Celcius2);
Serial.println(" ");

delay(1000);
}

My question:

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.  

ie..

Address * = Serial
Type * = Piece
Piece Number = 9 for example (Water Temp 2)
Name * = Water Temp 2
Alarm min * = 
Alarm max * =
Limit min * =
Limit max * =
@theyosh
Copy link
Owner

theyosh commented Mar 18, 2018

Euh, is the cronjob that is gathering the data once a minute on the same Pi as where my software is running... then there is a very easy way to do it.

The following code could do it:

with open(“/home/pi/TerrariumPI/static/sensors.json”) as sensordata: sensordata.write('{"temperature1": %s, "distance": %s}' % (pieces[0],pieces[1],pieces[2]))
Extend it with all the data you measure. This will end up in a single file at the location:
/home/pi/TerrariumPI/static/sensors.json. Update it once a minute with your cron job.

Then you are able to add an external sensor according to: https://github.com/theyosh/TerrariumPI/wiki/Remote-data

Use in this example the following url: http://[Pi_IP:8090]/static/sensors.json#temperature1 for reading out one temperature sensor.
Repeat this for every sensor like: http://[Pi_IP:8090]/static/sensors.json#distance

I hope it is clear what I try to explain and helps you out

@RRCRT
Copy link
Author

RRCRT commented Mar 18, 2018

Many Thanks I understand and follow completely.
The cron is running on the same PI as your software yes.

I tested the py script with the added lines in terminal and get:

SyntaxError: Non-ASCII character '\x93' in file water.py on line 12, but no enco ding declared; see http://python.org/dev/peps/pep-0263/ for details

some missing code or i'm placing the code in the wrong file.
Thanks again

@RRCRT
Copy link
Author

RRCRT commented Mar 19, 2018

Works with edit below:

with open("/home/pi/TerrariumPI/static/sensors.json","r+") as sensordata: sensordata.write('{"temperature1": %s, "distance": %s}' % (pieces[4],pieces[6]))

@theyosh
Copy link
Owner

theyosh commented Mar 19, 2018

Cool!

my code was just to give an idea. So you should extend it so it contains all your sensors. And I think that is not that hard to do.

@RRCRT
Copy link
Author

RRCRT commented Mar 20, 2018

Great work!

Got that all sorted and working like a charm. Thanks for that I thought it would be a nightmare but it was quite easy.

I 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.
Be nice to have this feature in System environment if possible or just a simple copy and paste in files changing names and numbers etc to suite..

@RRCRT RRCRT closed this as completed Mar 20, 2018
@theyosh
Copy link
Owner

theyosh commented Mar 21, 2018

No, at the moment my software is for a single terrarium system. Sorry, but it will be a lot more complicated when you want to control more devices. So for now, it will not be.

But cool that adding data from an external analog device is working

@RRCRT
Copy link
Author

RRCRT commented Mar 22, 2018

Thats fine I can deal with that..
other question:
would it be easy enough to add 'door' to the type section in add sensors or 'remote' in add door sensor section.

@theyosh
Copy link
Owner

theyosh commented Mar 22, 2018

Just did...

  • git pull (on master branch)
  • restart software
  • clear browser cache

And it should be there... use 0 for closed and 1 for open

@RRCRT
Copy link
Author

RRCRT commented Mar 24, 2018

Great work I did try myself to do the same thing but with no joy. it works fine just a little feed back for ya:
after you have submitted/added a new door then land back on door settings at anytime the hardware type displays GPIO not Remote as I'm not submitting any changes this doesn't matter but if I had for example 2-3 more doors listed and would like to add another I would have to change all hardware from GPIO back to Remote on all doors listed on page before submitting again. At a guess read status isn't there like in sensor settings.

“Great minds think alike, though fools seldom differ.”
top
bottom

I'm looking to get pH sensor(s) soon and have some sort of pH up/down dispenser system into the tank(s) to automate pH levels. I may have to work in simple python and crontab for this.
Check pH level if to high then add 1 ml of pH down turn on circulation pump in tank(s) wait/sleep 10-20 mins then take pH reading again and so on.
Thanks again for your help I'm very happy with my system now. I have no doubt I'll be quizzing you again soon enough lol.

@RRCRT
Copy link
Author

RRCRT commented Mar 24, 2018

I have made a quick and simple edit to profile.tpl adding a calendar widget from google using the url below and pasting it in just before footer:
https://calendar.google.com/calendar/embedhelper

calendar

calendar2

My Code:

<iframe src="https://calendar.google.com/calendar/embed?showTitle=0&showPrint=0&showCalendars=0&showTz=0&height=600&wkst=2&hl=en_GB&bgcolor=%23FFFFFF&src=YOURCALENDARNAME%40gmail.com&color=%2329527A&ctz=Europe%2FLondon" style="border-width:0" width="100%" height="600" frameborder="0" scrolling="no"></iframe>




YOURCALENDARNAME=(for privacy reasons I've taken mine out but this should disply yours if selected in gen on page)

I changed the width to 100% to fit into browser window. Works really well and I can now set tasks/events/schedules etc.. Just have to remember to select the calendar you want to see in the setting page from google otherwise your tasks/events/schedules will not show up.
School boy error from myself.
also on mobile portrait doesn't resize but landscape does well. This is something I can live with.

Just wanted to share this with you as it was just so easy to do and you may like to use it.

theyosh added a commit that referenced this issue Mar 26, 2018
theyosh added a commit that referenced this issue Mar 26, 2018
@RRCRT
Copy link
Author

RRCRT commented Mar 29, 2018

calendar4

Nice.. You can see what issue I have with the sizing. I did try and change the containers size in css file but had no luck? Thanks for the update 👍

@theyosh
Copy link
Owner

theyosh commented Mar 29, 2018

Clear browser cache... you have an old css file now loaded. In CSS the frame is 100% width and 400px height

@RRCRT
Copy link
Author

RRCRT commented Mar 29, 2018

Thanks that worked and looks fab. I'll remember to do that if I ever edit css scripts again school boy error again lol.

webcam

If you could help me please. I had this same issue since I installed the software about a month ago where the webcam dir gets full of jpg files and only shows a still shot on the front end. I have removed all files out the dir and refreshed to make sure the dir is still clear for a 1 min or so then refreshed / landed back on the webcam page. the dir is full up again to the exact file name ( de2b8d7f52f0d204cc7667a23b2a7902_tile_3_7_7.jpg ). it like the permissions are wrong and it cant stream some how?

@theyosh
Copy link
Owner

theyosh commented Mar 29, 2018

Euh... this is working like expected. The webcam is just a still image with zoom levels like google maps. That is all. There is no streaming. The webcam is update once a minute.

Those files are correct, and will be refreshed every minute. I had explained before. Streaming is not an option due to needed CPU power. That will not be stable with all the sensors and timings

Also, please do not start different issues here.. This issue is closed. Please open a new one when you have a problem. Also, try to see if there is not a closed issue that explains your problem.

@nke69
Copy link
Contributor

nke69 commented Apr 4, 2018

hello, I followed the procedure here above, I get my datas (Arduino) in a json from my python script, T °, distance, etc.

By cons when I insert the path of the json as remote temperature sensor I have this error:

2018-04-04 22: 07: 59,000 - INFO - terrariumWrapper - Starting TerrariumPI server at location: http://192.168.1.61:8090 ...
HTTPConnectionPool (host = '192.168.1.61', port = 8090): Max retries exceeded with url: /static/extern/sensors.json (Caused by NewConnectionError ('<requests.packages.urllib3.connection.HTTPConnection object at 0x6c366a90>: Failed to establish a new connection: [Errno 111] Connection refused ',))

An idea ?

capture

the results of json is ok
capture2

@theyosh
Copy link
Owner

theyosh commented Apr 4, 2018

Yes! This happens only during startup. The TerrariumPI webserver is the last part that is started. So the sensors are loaded before the webserver is loaded and cannot read the 'remote' data . And therefore, it will give a warning when you start the software, but in 30 seconds it should update normally and no errors should be given.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants