prepare usage of mqtt
parent
34d2c7d737
commit
8c12ee4063
|
@ -21,6 +21,19 @@ for n in parameter:
|
|||
if n in log_conf:
|
||||
parameter[n]=log_conf[n]
|
||||
|
||||
bmqtt=False
|
||||
if "mqtt" in log_conf:
|
||||
bmqtt=True
|
||||
lcmq=log_conf['mqtt']
|
||||
mbroker=""
|
||||
if 'broker' in lcmq:
|
||||
mbroker=lcmq['broker']
|
||||
else:
|
||||
bmqtt=False
|
||||
mport=1883
|
||||
if 'port' in lcmq:
|
||||
mport=lcmq['port']
|
||||
|
||||
if "sqlserver" in log_conf:
|
||||
hostname="banana"
|
||||
if "host" in log_conf['sqlserver']:
|
||||
|
@ -82,6 +95,8 @@ if "ads1x15" in log_conf:
|
|||
cnadc="a"+str(adc_count)+"_"+str(j)
|
||||
channel_names.append(cnadc)
|
||||
channel_info[cnadc]={"meas_data": meas_data(var_name=cnadc,multiplicator=1,mean_count=parameter['cycle'],store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="ads"+str(adc_type),i2c=adc_address,store_file="/home/pi/log/data_ads",sigma=tsl_sigma)}
|
||||
if bmqtt and ("mqtt" in log_conf['ads1x15']):
|
||||
channel_info[cnadc]['meas_data'].set_mqtt(host=mbroker,port=mport)
|
||||
if "sqlserver" in log_conf:
|
||||
channel_info[cnadc]['meas_data'].set_sql(host=hostname,port=port)
|
||||
adc_count=adc_count+1
|
||||
|
|
|
@ -18,6 +18,19 @@ for n in parameter:
|
|||
if n in log_conf:
|
||||
parameter[n]=log_conf[n]
|
||||
|
||||
bmqtt=False
|
||||
if "mqtt" in log_conf:
|
||||
bmqtt=True
|
||||
lcmq=log_conf['mqtt']
|
||||
mbroker=""
|
||||
if 'broker' in lcmq:
|
||||
mbroker=lcmq['broker']
|
||||
else:
|
||||
bmqtt=False
|
||||
mport=1883
|
||||
if 'port' in lcmq:
|
||||
mport=lcmq['port']
|
||||
|
||||
# config of bme280 sensor
|
||||
# use pip install RPi.bme280 for library
|
||||
meas_types=["temperature","humidity","pressure"]
|
||||
|
@ -55,6 +68,10 @@ if "bme280" in log_conf:
|
|||
bme_temp=meas_data(var_name="temperature",multiplicator=1000,mean_count=bme_cycle,store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="BME280",i2c=bme_add,store_file="/home/pi/log/data_temperature",sigma=bme_sigma)
|
||||
bme_hum=meas_data(var_name="humidity",multiplicator=1000,mean_count=bme_cycle,store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="BME280",i2c=bme_add,store_file="/home/pi/log/data_humidity",sigma=bme_sigma)
|
||||
bme_press=meas_data(var_name="pressure",multiplicator=1000,mean_count=bme_cycle,store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="BME280",i2c=bme_add,store_file="/home/pi/log/data_pressure",sigma=bme_sigma)
|
||||
if ("mqtt" in log_conf['bme280']) and bmqtt:
|
||||
bme_temp.set_mqtt(broker=mbroker,port=mport)
|
||||
bme_hum.set_mqtt(broker=mbroker,port=mport)
|
||||
bme_press.set_mqtt(broker=mbroker,port=mport)
|
||||
if "sqlserver" in log_conf:
|
||||
hostname="banana"
|
||||
if "host" in log_conf['sqlserver']:
|
||||
|
|
|
@ -14,6 +14,19 @@ for n in parameter:
|
|||
if n in log_conf:
|
||||
parameter[n]=log_conf[n]
|
||||
|
||||
bmqtt=False
|
||||
if "mqtt" in log_conf:
|
||||
bmqtt=True
|
||||
lcmq=log_conf['mqtt']
|
||||
mbroker=""
|
||||
if 'broker' in lcmq:
|
||||
mbroker=lcmq['broker']
|
||||
else:
|
||||
bmqtt=False
|
||||
mport=1883
|
||||
if 'port' in lcmq:
|
||||
mport=lcmq['port']
|
||||
|
||||
# config of bme680 sensor
|
||||
# use pip install bme680 for library
|
||||
meas_types=["temperature","humidity","pressure","voc"]
|
||||
|
@ -55,6 +68,10 @@ if "bme680" in log_conf:
|
|||
bme_hum=meas_data(var_name="humidity",multiplicator=1000,mean_count=parameter['cycle'],store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="BME680",i2c=bme_add,store_file="/home/pi/log/data_humidity",sigma=bme_sigma)
|
||||
bme_press=meas_data(var_name="pressure",multiplicator=1000,mean_count=parameter['cycle'],store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="BME680",i2c=bme_add,store_file="/home/pi/log/data_pressure",sigma=bme_sigma)
|
||||
bme_voc=meas_data(var_name="voc",multiplicator=1000,mean_count=parameter['cycle'],store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="BME680",i2c=bme_add,store_file="/home/pi/log/data_pressure",sigma=bme_sigma)
|
||||
if ("mqtt" in log_conf['bme680']) and bmqtt:
|
||||
bme_temp.set_mqtt(broker=mbroker,port=mport)
|
||||
bme_hum.set_mqtt(broker=mbroker,port=mport)
|
||||
bme_press.set_mqtt(broker=mbroker,port=mport)
|
||||
if "sqlserver" in log_conf:
|
||||
hostname="banana"
|
||||
if "host" in log_conf['sqlserver']:
|
||||
|
|
15
log_cpu.py
15
log_cpu.py
|
@ -30,6 +30,21 @@ if "sqlserver" in log_conf:
|
|||
port=int(log_conf['sqlserver']['port'])
|
||||
cpu_meas.set_sql(host=hostname,port=port)
|
||||
|
||||
bmqtt=False
|
||||
if "mqtt" in log_conf:
|
||||
bmqtt=True
|
||||
lcmq=log_conf['mqtt']
|
||||
mbroker=""
|
||||
if 'broker' in lcmq:
|
||||
mbroker=lcmq['broker']
|
||||
else:
|
||||
bmqtt=False
|
||||
mport=1883
|
||||
if 'port' in lcmq:
|
||||
mport=lcmq['port']
|
||||
if bmqtt:
|
||||
cpu_meas.set_mqtt(host=mbroker,port=mport)
|
||||
|
||||
a = 2
|
||||
|
||||
|
||||
|
|
|
@ -53,6 +53,20 @@ if "mcp9808" in log_conf:
|
|||
if "sensors" in osm:
|
||||
if "temperature" in osm['sensors']:
|
||||
mcp_data.set_sensebox(sid,osm['sensors']['temperature'])
|
||||
bmqtt=False
|
||||
if "mqtt" in log_conf:
|
||||
bmqtt=True
|
||||
lcmq=log_conf['mqtt']
|
||||
mbroker=""
|
||||
if 'broker' in lcmq:
|
||||
mbroker=lcmq['broker']
|
||||
else:
|
||||
bmqtt=False
|
||||
mport=1883
|
||||
if 'port' in lcmq:
|
||||
mport=lcmq['port']
|
||||
if bmqtt:
|
||||
mcp_data.set_mqtt(host=mbroker,port=mport)
|
||||
|
||||
|
||||
if bmcp9808:
|
||||
|
|
15
log_sds.py
15
log_sds.py
|
@ -42,6 +42,19 @@ pm=[0,0]
|
|||
pm_count=0
|
||||
ser.open()
|
||||
ser.flushInput()
|
||||
bmqtt=False
|
||||
if "mqtt" in log_conf:
|
||||
bmqtt=True
|
||||
lcmq=log_conf['mqtt']
|
||||
mbroker=""
|
||||
if 'broker' in lcmq:
|
||||
mbroker=lcmq['broker']
|
||||
else:
|
||||
bmqtt=False
|
||||
mport=1883
|
||||
if 'port' in lcmq:
|
||||
mport=lcmq['port']
|
||||
|
||||
|
||||
|
||||
def dump_data(d):
|
||||
|
@ -159,6 +172,8 @@ def main(args):
|
|||
|
||||
pm25=meas_data(var_name="pm25",multiplicator=10,ring_length=60,mean_count=5,store_each_cycle=True,device=parameter['device'],sensor="sds",i2c=0,store_file="/home/pi/log/data_sds",sigma=2)
|
||||
pm10=meas_data(var_name="pm10",multiplicator=10,ring_length=60,mean_count=5,store_each_cycle=True,device=parameter['device'],sensor="sds",i2c=0,store_file="/home/pi/log/data_sds",sigma=2)
|
||||
pm25.set_mqtt(broker="banana",port=1883)
|
||||
pm10.set_mqtt(broker="banana",port=1883)
|
||||
pm25.set_sql(host=hostname,port=port)
|
||||
pm10.set_sql(host=hostname,port=port)
|
||||
sensebox_id="5cf8a49707460b001b43a245"
|
||||
|
|
|
@ -21,6 +21,19 @@ for n in parameter:
|
|||
if n in log_conf:
|
||||
parameter[n]=log_conf[n]
|
||||
|
||||
bmqtt=False
|
||||
if "mqtt" in log_conf:
|
||||
bmqtt=True
|
||||
lcmq=log_conf['mqtt']
|
||||
mbroker=""
|
||||
if 'broker' in lcmq:
|
||||
mbroker=lcmq['broker']
|
||||
else:
|
||||
bmqtt=False
|
||||
mport=1883
|
||||
if 'port' in lcmq:
|
||||
mport=lcmq['port']
|
||||
|
||||
if "sqlserver" in log_conf:
|
||||
hostname="banana"
|
||||
if "host" in log_conf['sqlserver']:
|
||||
|
@ -61,6 +74,8 @@ if "tristar" in log_conf:
|
|||
tsl_sigma=1
|
||||
for i in ["volt_scale","amp_scale","volt_bat_term","volt_bat_sens","volt_arr","amp_bat","amp_arr","temp_heatsink","temp_bat","ah_res","ah_tot","kwh_res","kwh_tot","watt_in","watt_out","hour_tot","state","volt_sweep_mp","volt_sweep_oc"]:
|
||||
channel_info[i]={"meas_data": meas_data(var_name=i,multiplicator=1,mean_count=parameter['cycle'],store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="tristar",i2c=0,store_file="/home/pi/log/data_ve",sigma=tsl_sigma)}
|
||||
if bmqtt and ("mqtt" in log_conf['ads1x15']):
|
||||
channel_info[i]['meas_data'].set_mqtt(host=mbroker,port=mport)
|
||||
if "sqlserver" in log_conf:
|
||||
channel_info[i]['meas_data'].set_sql(host=hostname,port=port)
|
||||
|
||||
|
|
|
@ -43,6 +43,20 @@ if "tsl2591" in log_conf:
|
|||
if tsl_sigma < 1:
|
||||
tsl_sigma=1
|
||||
tsl_data=meas_data(var_name="lux",multiplicator=1000,mean_count=parameter['cycle'],store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="TSL2591",i2c=tsl_add,store_file="/home/pi/log/data_lux",sigma=tsl_sigma)
|
||||
bmqtt=False
|
||||
if "mqtt" in log_conf:
|
||||
bmqtt=True
|
||||
lcmq=log_conf['mqtt']
|
||||
mbroker=""
|
||||
if 'broker' in lcmq:
|
||||
mbroker=lcmq['broker']
|
||||
else:
|
||||
bmqtt=False
|
||||
mport=1883
|
||||
if 'port' in lcmq:
|
||||
mport=lcmq['port']
|
||||
if bmqtt:
|
||||
tsl_data.set_mqtt(host=mbroker,port=mport)
|
||||
if "sqlserver" in log_conf:
|
||||
hostname="banana"
|
||||
if "host" in log_conf['sqlserver']:
|
||||
|
|
|
@ -21,6 +21,19 @@ for n in parameter:
|
|||
if n in log_conf:
|
||||
parameter[n]=log_conf[n]
|
||||
|
||||
bmqtt=False
|
||||
if "mqtt" in log_conf:
|
||||
bmqtt=True
|
||||
lcmq=log_conf['mqtt']
|
||||
mbroker=""
|
||||
if 'broker' in lcmq:
|
||||
mbroker=lcmq['broker']
|
||||
else:
|
||||
bmqtt=False
|
||||
mport=1883
|
||||
if 'port' in lcmq:
|
||||
mport=lcmq['port']
|
||||
|
||||
if "sqlserver" in log_conf:
|
||||
hostname="banana"
|
||||
if "host" in log_conf['sqlserver']:
|
||||
|
@ -55,6 +68,8 @@ if "vedirect" in log_conf:
|
|||
varnames={"volt_bat_ve":"V","volt_arr_ve":"VPV","amp_ve":"I","watt_ve":"PPV","days_ve":"HSDS"}
|
||||
for i in varnames:
|
||||
channel_info[i]={"meas_data": meas_data(var_name=i,ring_length=parameter['ring_length'],device=parameter['device'],sensor="VEdirect",i2c=0,store_file="/home/pi/log/data_ve",sigma=tsl_sigma)}
|
||||
if bmqtt and ("mqtt" in log_conf['vedirect']):
|
||||
channel_info[i]['meas_data'].set_mqtt(host=mbroker,port=mport)
|
||||
if "sqlserver" in log_conf:
|
||||
channel_info[i]['meas_data'].set_sql(host=hostname,port=port)
|
||||
|
||||
|
|
|
@ -40,6 +40,20 @@ if "veml6070" in log_conf:
|
|||
if tsl_sigma < 1:
|
||||
tsl_sigma=1
|
||||
tsl_data=meas_data(var_name="uv",multiplicator=1000,mean_count=parameter['cycle'],store_each_cycle=True,ring_length=parameter['ring_length'],device=parameter['device'],sensor="VEML6070",i2c=veml_port,store_file="/home/pi/log/data_uv",sigma=tsl_sigma)
|
||||
bmqtt=False
|
||||
if "mqtt" in log_conf:
|
||||
bmqtt=True
|
||||
lcmq=log_conf['mqtt']
|
||||
mbroker=""
|
||||
if 'broker' in lcmq:
|
||||
mbroker=lcmq['broker']
|
||||
else:
|
||||
bmqtt=False
|
||||
mport=1883
|
||||
if 'port' in lcmq:
|
||||
mport=lcmq['port']
|
||||
if bmqtt:
|
||||
tsl_data.set_mqtt(host=mbroker,port=mport)
|
||||
if "sqlserver" in log_conf:
|
||||
hostname="banana"
|
||||
if "host" in log_conf['sqlserver']:
|
||||
|
|
31
meas_data.py
31
meas_data.py
|
@ -1,4 +1,6 @@
|
|||
import socket,numpy,time,thread,json,requests
|
||||
import paho.mqtt.client as mqtt
|
||||
import paho.mqtt.publish as publish
|
||||
class meas_data:
|
||||
def __init__(self,var_name,ring_length=100,sigma=2,device=socket.gethostname(),sensor="CPU",i2c=0,store_file="/home/pi/data",mean_count=5,store_each_cycle=True,multiplicator=1):
|
||||
self.set_varname(var_name)
|
||||
|
@ -31,6 +33,10 @@ class meas_data:
|
|||
self.i2c=i2c
|
||||
self.set_multiplicator(multiplicator)
|
||||
self.bstoreeach=store_each_cycle
|
||||
self.mqtt_broker=""
|
||||
self.mqtt_port=1883
|
||||
self.mqtt_topic=""
|
||||
self.mqtt_bool=False
|
||||
self.json_out={"time":0,"device":device,"payload":{var_name:{"sensor":sensor,"value":0,"i2c":i2c}}}
|
||||
def set_varname(self,var_name):
|
||||
tvn=var_name.translate(None,"/:;\\{}(){]%&")
|
||||
|
@ -69,6 +75,25 @@ class meas_data:
|
|||
self.sqlhost=th
|
||||
self.sqlport=int(port)
|
||||
self.bsql=True
|
||||
def set_mqtt(self,broker="banana",port=1883,topic="")
|
||||
mt=topic
|
||||
if len(mt)==0:
|
||||
mt=self.device+"/"+self.sensor+"/"+self.var_name
|
||||
else:
|
||||
mt=mt.translate(None,":;\\{}(){]%&")
|
||||
mb=broker.translate(None,"/:;\\{}(){]%&")
|
||||
mport=int(port)
|
||||
if mport < 1024:
|
||||
mport=1883
|
||||
try:
|
||||
mqtt.connect(host=mb,port=mport)
|
||||
except:
|
||||
print("no connection to broker")
|
||||
else:
|
||||
self.mqtt_broker=mb
|
||||
self.mqtt_port=mport
|
||||
self.mqtt_topic=mt
|
||||
self.mqtt_bool=True
|
||||
def set_file_log(self,store_file="/home/pi/data"):
|
||||
if len(store_file)>1:
|
||||
self.store_file=store_file+"_{:d}.txt"
|
||||
|
@ -119,6 +144,8 @@ class meas_data:
|
|||
thread.start_new_thread(self.send_file,(0,))
|
||||
if self.bsense:
|
||||
thread.start_new_thread(self.upload_osm,(0,))
|
||||
if self.mqtt_bool:
|
||||
thread.start_new_thread(self.send_mqtt,(0,))
|
||||
self.mval=[]
|
||||
if len(self.value)>=self.ring_length:
|
||||
self.value=self.value[((-1)*self.ring_length):]
|
||||
|
@ -158,7 +185,9 @@ class meas_data:
|
|||
print("Error %d: %s" % (r.status_code,r.text))
|
||||
else:
|
||||
self.sense_last_time=self.act_time
|
||||
|
||||
def send_mqtt(self,trigger=0):
|
||||
if (len(self.mqtt_topic)>0) and self.mqtt_bool:
|
||||
publish.single(self.mqtt_topic,self.value,hostname=self.mqtt_broker,port=self.mqtt_port)
|
||||
|
||||
|
||||
#test=meas_data("temp",5)
|
||||
|
|
Loading…
Reference in New Issue