I am working on a project using a RPi Version 3 Model B V1.2 device and the Bring Your Own Thing set up. The RPi is pulling some data from sensors (Temperature and Soil Moisture) via BTLE and pushing the data using the Python CayenneMQTTClient. I don’t seem to get any historical data. I get instantaneous data and can view the latest data in the widgets.
Here is my set up:
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
while True:
client.loop()
sleep(30)
Here are the two writes:
client.celsiusWrite(addr + " TEMP", float(temp)/float(1000))
client.virtualWrite(addr + " MOISTURE", (float(soil)/float(4096) * 100), “Soil Moisture”, “%”)
The following image is my dashboard no data in graphs but data in widgets:
From the Data tab using Live data query:
Switching to the Minute query:
Any help would be greatly appreciated.
If device is conected moments ago, it takes time to gather statistic.
jcruz
March 5, 2018, 4:51pm
3
Hi @ryan.j.mortenson ,
Would you mind sharing the code you’re using?
Thanks.
-Jesse
I left the device running overnight collecting data approximately every 30 seconds.
Requested Code:
import argparse
import logging
import sys
import threading
import cayenne.client
import traceback
from bluepy import btle
from time import sleep
NAME_DESCRIPTOR = "Complete Local Name"
NAME = "Empty Example"
MQTT_USERNAME = ""
MQTT_PASSWORD = ""
MQTT_CLIENT_ID = ""
CHAR_TEMP_UUID = ""
CHAR_SOIL_MOISTURE_UUID = ""
log = None
client = None
def handle_device_connection(addr):
log.info("Attempting to connect to: {}".format(addr))
try:
dev = btle.Peripheral(addr)
except Exception as e:
log.error("Could not connect to device: {}".format(addr))
return
try:
temp_char = dev.getCharacteristics(uuid=CHAR_TEMP_UUID)[0]
soil_char = dev.getCharacteristics(uuid=CHAR_SOIL_MOISTURE_UUID)[0]
temp = int(temp_char.read()[-1::-1].encode("hex"), 16)
soil = int(soil_char.read()[-1::-1].encode("hex"), 16)
client.celsiusWrite(addr + " TEMP", float(temp)/float(1000))
client.virtualWrite(addr + " MOISTURE", (float(soil)/float(4096) * 100), "Soil Moisture", "%")
log.info(temp)
log.info(soil)
dev.disconnect()
except Exception as e:
log.error("Lost connection")
log.error(traceback.format_exc())
class ScanPrint(btle.DefaultDelegate):
def __init__(self):
btle.DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
scan_data = dev.getScanData()
for (sdid, desc, val) in scan_data:
if desc == NAME_DESCRIPTOR and val == NAME:
log.info("Found device {} reading values".format(dev.addr))
t = threading.Thread(target=handle_device_connection, args=(dev.addr, ))
t.start()
def on_message(message):
print("message received: " + str(message))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--verbosity", "-v", type=str, choices=["DEBUG", "INFO"],
default="INFO", help="Level of verbosity")
args = parser.parse_args()
FORMAT = '[%(asctime)s %(filename)15s line: %(lineno)5s] %(message)s'
logging.basicConfig(stream=sys.stdout, format=FORMAT, level=getattr(logging, args.verbosity))
log = logging.getLogger("ble_test")
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
while True:
client.loop()
log.info("Starting Scan...")
scanner = btle.Scanner(0).withDelegate(ScanPrint())
devices = scanner.scan(30)
log.info("Scan Complete, sleeping for 30 seconds")
sleep(30)
ryan.j.mortenson:
celsiusWrite
Hi @ryan.j.mortenson ,
client.celsiusWrite(CHANNEL, VALUE) <— CHANNEL must be an integer.
1 Like
Worked like a charm, thank you.
2 Likes