update sensor and device ID code

master
Cole Deck 2 years ago
parent 231d8c9bc7
commit 4b454d9d6f

@ -40,9 +40,9 @@ def get_frame():
async def test_coro():
C = MQTTClient()
print("START" + str(get_uptime()))
await C.connect('mqtt://100.79.112.65:1883')
await C.connect('mqtt://127.0.0.1:1883')
tasks = [
asyncio.ensure_future(C.publish('sensors/pi1/camera', encoder({"data": str(get_frame()), "sensorID": 0, "timestamp": time.time_ns()}), qos=QOS_2)),
asyncio.ensure_future(C.publish('sensors/pi1/camera', encoder({"data": str(get_frame()), "sensorID": 0, "timestamp": time.time_ns() }), qos=QOS_2)),
asyncio.ensure_future(C.publish('sensors/pi1/uptime', encoder({"data": get_uptime(), "sensorID": 0, "timestamp": time.time_ns()}), qos=QOS_2)),
]
await asyncio.wait(tasks)

@ -17,9 +17,10 @@ import cv2 as cv
sublist = []
sensorlist = []
devicelist = []
sensortypes = ["microphone", "camera", "humidity"]
showData = True
showData = True # debug print
async def broker_coro(brokerconfig):
broker = Broker(config=brokerconfig)
@ -27,7 +28,7 @@ async def broker_coro(brokerconfig):
async def uptime_coro():
C = MQTTClient()
await C.connect('mqtt://127.0.0.1:1883/')
await C.connect('mqtt://127.0.0.1:1883/') # connect to my own broker
# Subscribe to '$SYS/broker/uptime' with QOS=1
# Subscribe to '$SYS/broker/load/#' with QOS=2
#await C.subscribe([
@ -45,13 +46,8 @@ async def uptime_coro():
if packet.variable_header.topic_name.split("/")[-1] == "camera": # trigger image loading
print(len(dictout["data"]))
img = np.frombuffer(base64.b64decode(dictout["data"]), np.uint8)
#height, width = img.shape[:2]
#print(height, width)
#cv.imshow('frame', img)
cv.imwrite('./output.jpg', img)
#if cv.waitKey(1) == ord('q'):
# break
else:
publishData(str(dictout["data"]), str(dictout["sensorID"]), str(packet.variable_header.topic_name.split("/")[-1]), dictout["timestamp"])
#await C.unsubscribe(['a/b'])
@ -66,13 +62,16 @@ def publishData(data, sensorID, sensorType, dataTimestamp):
print("publishing network.bridge.sensor." + sensorType + " with metadata:", {"sensorID": sensorID, "sensorType": sensorType}, "and timestamp:", dataTimestamp, "and data:", data)
else:
print("publishing network.bridge.sensor." + sensorType + "with metadata", {"sensorID": sensorID, "sensorType": sensorType})
# send data to waggle:
plugin.publish("network.bridge.sensor." + sensorType, data, meta={"sensorID": str(sensorID), "sensorType": str(sensorType)}, timestamp=dataTimestamp)
#time.sleep(1)
print("DONE")
sensorcount = 0
devicecount = 0
def subdict(d, header):
# coordinate mqtt topic names and waggle topics / sensor names
global sensorcount
global devicecount
for k,v in d.items():
if isinstance(v, dict):
if header is not None:
@ -82,7 +81,13 @@ def subdict(d, header):
else:
for sensor in v:
sublist.append((header + k + "/" + sensor, QOS_2))
#print (header + k + "/" + sensor)
sensorlist.append((header + k + "/" + sensor, sensorcount))
sensorcount = sensorcount + 1
if (header + k + "/", devicecount) not in devicelist:
devicelist.append((header + k + "/", devicecount))
devicecount = devicecount + 1
#print (((header + k + "/" + sensor, QOS_2), sensorcount))
def decoder(data):
print (data)
@ -97,7 +102,9 @@ if __name__ == "__main__":
with open('subscribe.yaml', 'r') as fileread:
clientconfig = yaml.safe_load(fileread)
subdict(clientconfig, None)
print(sublist)
print("sublist:", sublist)
print("sensors:", sensorlist)
print("devices:", devicelist)
formatter = "[%(asctime)s] :: %(levelname)s :: %(name)s :: %(message)s"
logging.basicConfig(level=logging.ERROR, format=formatter)

@ -2,10 +2,13 @@ sensors:
pico1:
- "distance"
- "uptime"
pico2:
cameras:
- "1"
- "2"
pi1:
- "camera"
- "uptime"
subnet2:
pi2:
- "microphone"
- "uptime"
pico1:
- "soil"
- "uptime"

@ -19,3 +19,33 @@
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T15:33:23.011959212","value":"1098.75"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T15:34:44.917333986","value":"1180.65"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T15:34:53.142359741","value":"1188.88"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:11.443747983","value":"3247.18"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:14.465929434","value":"3250.2"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:24.438959594","value":"3260.17"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:32.625187148","value":"3268.36"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:40.726503997","value":"3276.46"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:48.881783996","value":"3284.62"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:09:57.108593092","value":"3292.84"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:04.981753079","value":"3300.72"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:12.597964062","value":"3308.33"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:20.662644273","value":"3316.4"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:28.753982972","value":"3324.49"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:37.557337680","value":"3333.29"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:45.428986457","value":"3341.16"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:10:53.654332320","value":"3349.39"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-02T16:11:02.256857301","value":"3357.99"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:28.485337540","value":"155228.65"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:33.640031336","value":"155233.8"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:34.126547740","value":"155234.29"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:44.140272441","value":"155244.3"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:44.640592407","value":"155244.8"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:49.720956112","value":"155249.89"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:38:59.638603072","value":"155259.8"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:04.890556821","value":"155265.05"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:10.141516037","value":"155270.31"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:15.391559847","value":"155275.56"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:20.641153525","value":"155280.81"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:25.891244907","value":"155286.06"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:31.140422116","value":"155291.3"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:36.641924478","value":"155296.81"}
{"meta":{"sensorID":"0","sensorType":"uptime"},"name":"network.bridge.sensor.uptime","timestamp":"2023-05-05T15:39:42.141766853","value":"155302.31"}

Loading…
Cancel
Save