Skip to content

Instantly share code, notes, and snippets.

@niektemme
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niektemme/80f94c4bb60e0f444541 to your computer and use it in GitHub Desktop.
Save niektemme/80f94c4bb60e0f444541 to your computer and use it in GitHub Desktop.
Smart Thermostat Raspberry PI - part upload to HBase
...
import happybase
...
def xinsert():
"""
Receives and sends data from and to the XBee.
Sending and receiving combined in one thread because only one tread can use GPIO port.
Recieving is done as fast as possible.
Sinding only at 1 second interval.
Runs as a sperate thread.
"""
ficurtime = 0
fivepoch = 0
fiprevepcoh = 0
ficursource = '40b5af01' #device id of raspberry pi only used for logging
DEST_ADDR_LONG = "\x00\x13\xA2\x00@\xB5\xAF\x00" #destination adress currently fixed value
while True:
logging.debug("insert started")
logging.debug(datetime.datetime.utcnow())
try:
#start with receive part
ficurtime = time.time()
fivepoch = int(ficurtime)
fimintime = fivepoch - 5
response = xbee.wait_read_frame()
logging.debug(response)
vid = (response['id'])
if vid != 'tx_status':
vsource = (codecs.decode(codecs.encode(response['source_addr_long'],'hex'),'utf-8')[8:])
logging.debug(vsource)
logging.debug(vid)
vdata = codecs.decode(response['rf_data'],'utf-8')
logging.debug(vdata)
if vid == 'rx': #special case if port is rx. Assumes arduino is sending data.
vid = 'rx'+(vdata[0:3].zfill(6)) #first part of payload is sendor ID
vdata = vdata[4:]
vdatas = vdata.split(',') #assumes array of values
for vdatasv in vdatas:
vdatasv = int(vdatasv)
fins(time.time(),vsource,vid,vdatasv) #use fins() function to actuall insert data in database
else: #case of normal xbee payload
vid = vid.zfill(8)
vdata = int(vdata)
fins(time.time(),vsource,vid,vdata) #use fins() function to actuall insert data in database
#send at 1 second interval
if fivepoch > fiprevepcoh:
fiprevepcoh = fivepoch
dbd11 = dbc.cursor()
dbd11.execute("SELECT vsendmes from sendmes where vepoch > %i order by vepoch desc,vsub desc LIMIT 1" % fimintime)
rows = dbd11.fetchall()
for row in rows:
fipayload = row[0]
xbee.tx(dest_addr='\x00\x00',dest_addr_long=DEST_ADDR_LONG,data=str(fipayload),frame_id='\x01') #send trought XBee
fins(ficurtime,ficursource,'rx000B04',int('9'+str(fipayload))) #logging of send message in hsensvals table
dbd11.close()
except Exception:
logging.exception("fxinsert")
time.sleep(0.001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment