Inkluderar MQTT
This commit is contained in:
@@ -14,6 +14,7 @@ import signal
|
||||
import traceback
|
||||
import math
|
||||
import logging
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
@dataclass
|
||||
class Measurement:
|
||||
@@ -94,16 +95,22 @@ def thread_SendingData():
|
||||
params += " " + str(mea.rssi)
|
||||
params += " " + str(mea.timestamp)
|
||||
fmt +=",timestamp"
|
||||
cmd = path + "/" + args.callback + " " + fmt + " " + params
|
||||
print(cmd)
|
||||
ret = os.system(cmd)
|
||||
if (ret != 0):
|
||||
measurements.appendleft(mea) #put the measurement back
|
||||
print ("Data couln't be send to Callback, retrying...")
|
||||
time.sleep(5) #wait before trying again
|
||||
else: #data was sent
|
||||
previousMeasurement=Measurement(mea.temperature,mea.humidity,mea.voltage,mea.calibratedHumidity,mea.battery,0) #using copy or deepcopy requires implementation in the class definition
|
||||
print ("MQTT publishing")
|
||||
mqttclient.publish(("MiTemperature2/%s/temperature" % mea.sensorname), mea.temperature)
|
||||
mqttclient.publish(("MiTemperature2/%s/humidity" % mea.sensorname), mea.humidity)
|
||||
mqttclient.publish(("MiTemperature2/%s/battery" % mea.sensorname), mea.battery)
|
||||
print ("MQTT done")
|
||||
|
||||
#""" cmd = path + "/" + args.callback + " " + fmt + " " + params
|
||||
# print(cmd)
|
||||
# ret = os.system(cmd)./L
|
||||
# if (ret != 0):
|
||||
# measurements.appendleft(mea) #put the measurement back
|
||||
# print ("Data couln't be send to Callback, retrying...")
|
||||
# time.sleep(5) #wait before trying again
|
||||
# else: #data was sent
|
||||
# previousMeasurement=Measurement(mea.temperature,mea.humidity,mea.voltage,mea.calibratedHumidity,mea.battery,0) #using copy or deepcopy requires implementation in the class definition
|
||||
#"""
|
||||
except IndexError:
|
||||
#print("Keine Daten")
|
||||
time.sleep(1)
|
||||
@@ -294,6 +301,9 @@ if not args.name:
|
||||
args.name = args.device
|
||||
|
||||
if args.callback:
|
||||
mqttserver = "192.168.0.114"
|
||||
mqttclient = mqtt.Client("koksfonstret")
|
||||
mqttclient.connect(mqttserver)
|
||||
dataThread = threading.Thread(target=thread_SendingData)
|
||||
dataThread.start()
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
|
||||
tmux new-session -d -s sensorer '/home/pi/LYWSD03MMC.py -a -wdt 5 --devicelistfile sensorer.ini --callback sendToMQTT.sh'
|
||||
tmux new-session -d -s sensorer '/home/pi/LYWSD03MMC.py -a -wdt 5 --devicelistfile sensorer.ini --callback yes --battery'
|
||||
tmux detach -s sensorer
|
||||
|
||||
Reference in New Issue
Block a user