# Read from serial with data coming from ARDUINO (Temperature, Pressure and LIght)
#Change Log
# Version 1
# fixed serial synch
import serial, sys, string
import time, datetime, httplib, urllib
from socket import gaierror, timeout, error
from httplib import BadStatusLine
# Domain you want to post to: localhost would be an emoncms installation on your own laptop
# this could be changed to emoncms.org to post to emoncms.org
domain = "emoncms.org"
# Location of emoncms in your server, the standard setup is to place it in a folder called emoncms
# To post to emoncms.org change this to blank: ""
emoncmspath = ""
# Write apikey of emoncms account
apikey = "49884feb7e4e807d1e3e208d09926bc4"
# Set this to the serial port of your emontx and baud rate, 9600 is standard emontx baud rate
ser = serial.Serial('/dev/ttyACM0', 9600)
# ser.write("4b\n")
# ser.write("210g\n")
print "Arduino to Python to Eemoncms cloud serial link"
conn = httplib.HTTPConnection(domain, timeout=60)
print "Conn HTTP created"
def feedval(feedid):
data_url="http://emoncms.org/feed/value.json?apikey=" + apikey + "&id=" + str(feedid)
# read in the data from emoncms
try:
sock = urllib.urlopen(data_url)
data_str = sock.read()
sock.close
onoff = data_str.replace('"', '')
return onoff
except Exception, detail:
print "Error ", detail
light = feedval(123960)
# TELLS ARDUINO SENSORS PROGRAM TO STRART SENDING DATA IN THE FORMAT OK 1 23,8 123.8 456,9
ser.write("START")
# WAIT 3 Seconds
time.sleep(3)
ser.flushInput()
synch = 0
# Read in line of readings from Arduino serial
linestr = ser.readline()
#print "READ from serial"
while synch == 0:
# Remove the new line at the end
linestr = linestr.rstrip()
print "DATA RX:"+linestr
# Split the line at the whitespaces
values = linestr.split(' ')
if values[0]=="OK":
try:
nodeid = int(values[1])
nameid = 1
except ValueError:
linestr = ser.readline()
print "OK BUT FALSE:"+linestr
pass # it was an int.
synch = 1
else:
synch = 0
print "NOT OK RECEIVED!:"+linestr
print "SYNCH OK "+linestr
ser.write("v1")
time.sleep(5)
ser.write("v0")
print "HO ACCESO E SPENTO"
while 1:
try:
#print "TRY to read from serial"
# Read in line of readings from Arduino serial
linestr = ser.readline()
#print "READ from serial"
# Remove the new line at the end
linestr = linestr.rstrip()
#print "DATA RX:"+linestr
# Split the line at the whitespaces
values = linestr.split(' ')
if values[0]=="OK":
nodeid = int(values[1])
nameid = 1
#datacsv = []
#for i in range(2,(len(values)-1),1):
## Get 16-bit integer
## value = int(values[i]) + int(values[i+1])*256
## if value>32768: value -= 65536
#value = float(values[i])
#datacsv.append(str(value))
#req = "node="+str(nodeid)+"&csv="+','.join(datacsv)
#req = "node="+str(nodeid)+"&csv="+(datacsv)
#req = "node="+str(nodeid)+"&csv="+ str(values[2])
req = "node="+str(nodeid)+"&csv="+ str(float(values[2]))
print time.ctime(), " ", req # 'Mon Oct 18 13:35:29 2010'
#print req
# Send to emoncms
conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&"+req)
response = conn.getresponse()
print response.read()
print "Luce : ", feedval(123960)
except KeyError:
conn.close()
sys.exit()
except gaierror:
#except socket.gaierror:
print "Error with request - re-opening connection"
conn.close()
print "gai Error with request - close connector"
conn = httplib.HTTPConnection(domain, timeout=60)
print "gai Error with request - recycle"
#conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&"+req)
#response = conn.getresponse()
#print response.read()
except timeout:
#except socket.timeout:
print "Timeout Error with request - re-opening connection"
conn.close()
print "Timeout Error with request - close connector"
conn = httplib.HTTPConnection(domain, timeout=60)
print "Timeout Error with request - recycle"
#conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&"+req)
#response = conn.getresponse()
#print response.read()
except error:
#except socket.timeout:
print "Err 113 Host not found with request - re-opening connection"
conn.close()
print "Err 113 Host not found with request - close connector"
conn = httplib.HTTPConnection(domain, timeout=60)
print "Err 113 Host not found with request - recycle"
#conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&"+req)
#response = conn.getresponse()
#print response.read()
except BadStatusLine:
#except httplib.BadStatusLine:
print "Err httplib.BadStatusLine with response - re-opening connection and retry"
conn.close()
print "Err httplib.BadStatusLine with response - closed connector"
conn = httplib.HTTPConnection(domain, timeout=60)
print "Err httplib.BadStatusLine with response - recycle"
#conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&"+req)
#response = conn.getresponse()
#print response.read()
if light == feedval(123960):
print feedval(123960)
else:
if feedval(123960) == "0":
ser.flushOutput()
ser.write("v0")
print feedval(123960)
light = feedval(123960)
time.sleep(3)
if feedval(123960) == "1":
ser.flushOutput()
ser.write("v1")
print feedval(123960)
light = feedval(123960)
time.sleep(3)
Comments