All,
I’m hoping some of you can help with some code issues I’m having on my Raspberry Pi.
-
I’ve got the example code (Example-01-SendData.py) working and sending data. So one would think everything is in place for it all to work.
-
I have another Python file / sketch, which connects to a Silicon Labs Thunderboard Sense and gathers multiple sensor data from it. (http://community.silabs.com/t5/Projects/Project-Completed-and-Working-a-Treat-TB-Sense-and-Pi/m-p/207689#M570)
-
I’m now trying to combine both sketches. So the overall idea would be :
- Start sketch on RPi boot.
- Connect via BLE to Thunderboard Sense.
- Collect data every 5-10 minutes.
- Send this data into Cayenne and view the graphs etc.
- Have a beer on the couch and watch the data come in.
I’ve not yet got this to work based on some strange errors which have come up. Not sure why. I don’t know whether it has to do with versions of Python? Should I be using the Example-01-SendData sketch or the Example-03-CayenneClient sketch?
I’ve installed all the correct libraries and updated the RPi, so it shouldn’t be that.
Any ideas?
Please find an example of the combined code below; let me know what anyones thoughts are.
from __future__ import division
import sys
from bluepy.btle import *
import struct
import thread
from time import sleep
#import http.client
import urllib2
from urllib2 import URLError
import cayenne.client
import time
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME = "xxx"
MQTT_PASSWORD = "xxx"
MQTT_CLIENT_ID = "xxx"
client = cayenne.client.CayenneMQTTClient()
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
temperature_data_value=0
humidity_data_value=0
Sound_data_value=0
TVOC_data_value=0
eCO2_data_value=0
Pressure_data_value=0
ambient_data_value=0
#Old ThingSpeak authentication information.
#PRIVATE_KEY = 'abc'
# Base URL of Thingspeak
#baseURL = 'https://api.thingspeak.com/update?api_key='
def vReadSENSE():
scanner = Scanner(0)
devices = scanner.scan(2)
for dev in devices:
print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi)
for (adtype, desc, value) in dev.getScanData():
print " %s = %s" % (desc, value)
num_ble = len(devices)
print num_ble
if num_ble == 0:
return None
ble_service = []
char_sensor = 0
non_sensor = 0
TVOC_char = Characteristic
eCO2_char = Characteristic
Pressure_char = Characteristic
Sound_char = Characteristic
temperature_char = Characteristic
humidity_char = Characteristic
ambient = Characteristic
count = 15
for i in range(num_ble):
try:
devices[i].getScanData()
ble_service.append(Peripheral())
ble_service[char_sensor].connect('00:0b:57:36:63:ff',devices[i].addrType)
#ble_service[char_sensor].connect(devices[i].addr, devices[i].addrType)
char_sensor = char_sensor + 1
print "Connected %s device with addr %s " % (char_sensor, devices[i].addr)
except:
non_sensor = non_sensor + 1
try:
for i in range(char_sensor):
services = ble_service[i].getServices()
characteristics = ble_service[i].getCharacteristics()
for k in characteristics:
print k
if k.uuid == "efd658ae-c401-ef33-76e7-91b00019103b":
print "eCO2 Level"
eCO2_char = k
if k.uuid == "efd658ae-c402-ef33-76e7-91b00019103b":
print "TVOC Level"
TVOC_char = k
if k.uuid == "2a6d":
print "Pressure Level"
Pressure_char = k
if k.uuid == "c8546913-bf02-45eb-8dde-9f8754f4a32e":
print "Sound Level"
Sound_char = k
if k.uuid == "2a6e":
print "Temperature"
temperature_char = k
if k.uuid == "2a6f":
print "Humidity"
humidity_char = k
if k.uuid == "c8546913-bfd9-45eb-8dde-9f8754f4a32e":
print "Ambient Light"
ambient = k
except:
return None
while True:
# units in ppb
#The Total Volatile Organic Compound (TVOC) output range for
#CCS811 is from 0ppb to 1187ppb. Values outside this range are clipped.
TVOC_data = TVOC_char.read()
#TVOC_data_value = (ord(TVOC_data[1]) << 8) + ord(TVOC_data[0])
#float_TVOC_data_value = (TVOC_data_value / 100)
TVOC_data_value = struct.unpack('<h', TVOC_data)
TVOC_data_value = TVOC_data_value[0]
#units in ppm
#The equivalent CO2 (eCO2) output range for CCS811 is from
#400ppm to 8192ppm. Values outside this range are clipped.
eCO2_data = eCO2_char.read()
#eCO2_data_value = (ord(eCO2_data[1]) << 8) + ord(eCO2_data[0])
#eCO2_data_value = ord(eCO2_data[0])
eCO2_data_value = struct.unpack('<h', eCO2_data)
eCO2_data_value = eCO2_data_value[0]
# pressure is in units of mbar / hPa
#Reads actual pressure from uncompensated pressure and returns the value in Pascal(Pa).
#E.g.: output value of "96386" equals 96386 Pa = 963.86 hPa = 963.86 millibar
Pressure_data = Pressure_char.read()
#Pressure_data_value = ((ord(Pressure_data[1]) << 8) + ord(Pressure_data[0]) * 10)
#Pressure_data_value = (Pressure_data * 10)
Pressure_data_value = struct.unpack('<L', Pressure_data)
Pressure_data_value = Pressure_data_value[0]/1000
# units in dB
Sound_data = Sound_char.read()
#Sound_data_value = ((ord(Sound_data[1]) << 8) + ord(Sound_data[0])/1000)
#Sound_data_value = (Sound_data * 100)
Sound_data_value = struct.unpack('<h', Sound_data)
Sound_data_value = Sound_data_value[0]/100
#bat_data = bat_char.read()
#bat_data_value = ord(bat_data[0])
#units in celcius
temperature_data = temperature_char.read()
#temperature_data_value = (ord(temperature_data[1]) << 8) + ord(temperature_data[0])
#float_temperature_data_value = (temperature_data_value / 100)
temperature_data_value = struct.unpack('<H', temperature_data)
temperature_data_value = temperature_data_value[0]/100
#humidity in % relative
humidity_data = humidity_char.read()
#humidity_data_value = (((ord(humidity_data[1]) << 8) + ord(humidity_data[0])) / 100)
#humidity_data_value =(ord(humidity_data[1])< 14:
humidity_data_value = struct.unpack('<H', humidity_data)
humidity_data_value = humidity_data_value[0]/100
#ambient light measured in lux
ambient_data = ambient.read()
ambient_data_value = struct.unpack ('<L', ambient_data)
ambient_data_value = ambient_data_value[0]/100
print "TVOC: ", TVOC_data_value
print "eCO2: ", eCO2_data_value
print "Pressure: ", Pressure_data_value
print "Sound: ", Sound_data_value
print "Temperature: ", temperature_data_value
print "Humidity: ", humidity_data_value
print "Ambient Light :", ambient_data_value
#if count > 14:
# try:
# f = urllib2.urlopen(baseURL + PRIVATE_KEY + "&field1=%s&field2=%s&field3=%s&field4=%s&field5=%s&field6=%s&field7=%s" % (TVOC_data_value, eCO2_data_value, Pressure_data_value, Sound_data_value, temperature_data_value, humidity_data_value, ambient_data_value))
# except (urllib2.URLError, http.client.HTTPException, socket.timeout) as e:
# print e.reason
# pass
#except (URLError, http.client.BadStatusLine, http.client.HTTPException) as e:
# print e.reason
# pass
# print f.read()
# f.close()
# count = 0
# count = count + 1
# sleep(30)
#while True:
#vReadSENSE()
while True:
client.loop()
client.celsiusWrite(1, temperature_data_value)
client.luxWrite(2, ambient_data_value)
client.virtualWrite(3, TVOC_data_value)
client.virtualWrite(4, eCO2_data_value)
client.hectoPascalWrite(5, Pressure_data_value)
client.virtualWrite(6, Sound_data_value)
time.sleep(5)