def __init__(self): threading.Thread.__init__(self) self.sq = SensorQueueClient_write(QUEUE_INI) self.qv_kb_i_t = SensorValue("ID_21", "Temp KB indoor", SensorValue_Data.Types.Temp, "°C") self.qv_kb_i_h = SensorValue("ID_22", "Humi KB indoor", SensorValue_Data.Types.Humi, "% rF") self.qv_kb_p = SensorValue("ID_23", "Pressure KB", SensorValue_Data.Types.Pressure, "hPa") self.qv_kb_a_t = SensorValue("ID_24", "Temp KB outdoor", SensorValue_Data.Types.Temp, "°C") self.qv_kb_a_h = SensorValue("ID_25", "Humi KB outdoor", SensorValue_Data.Types.Humi, "% rF") self.qv_kb_k_t = SensorValue("ID_26", "Temp KB basement", SensorValue_Data.Types.Temp, "°C") self.qv_kb_k_h = SensorValue("ID_27", "Humi KB basement", SensorValue_Data.Types.Humi, "% rF") self.sq.register(self.qv_kb_i_t) self.sq.register(self.qv_kb_i_h) self.sq.register(self.qv_kb_p) self.sq.register(self.qv_kb_a_t) self.sq.register(self.qv_kb_a_h) self.sq.register(self.qv_kb_k_t) self.sq.register(self.qv_kb_k_h) self._running = True
def main(): """main part""" sq = SensorQueueClient_write("../../configs/weatherqueue.ini") qvalue_temp = SensorValue("ID_06", "TempKinderzimmer", SensorValue_Data.Types.Temp, "°C") qvalue_humi = SensorValue("ID_07", "HumiKinderzimmer", SensorValue_Data.Types.Humi, "% rF") sq.register(qvalue_temp) sq.register(qvalue_humi) temphumi = DHT22_AM2302(19, qvalue_temp, qvalue_humi) # BCM 19 = PIN 35 temp_cpu = CPU() measurements = {DS_TEMP1: Measurements(3), \ DS_TEMPCPU: Measurements(3), \ DS_TEMP2: Measurements(3), \ DS_HUMI: Measurements(3)} udp = Sender(CREDENTIALS) while (True): _temp, _humi = temphumi.read() measurements[DS_TEMP1].append(_temp) measurements[DS_HUMI].append(_humi) measurements[DS_TEMPCPU].append(temp_cpu.read_temperature()) measurements[DS_TEMP2].append(0) # empty, for later useage rrd_data = "N:{:.2f}".format(measurements[DS_TEMP1].last()) + \ ":{:.2f}".format(measurements[DS_TEMPCPU].last()) + \ ":{:.2f}".format(measurements[DS_TEMP2].last()) + \ ":{:.2f}".format(measurements[DS_HUMI].last()) # Log(rrd_data) udp.send(rrd_data) sleep(35)
class ToQueue(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.sq = SensorQueueClient_write(QUEUE_INI) self.qv_kb_i_t = SensorValue("ID_21", "Temp KB indoor", SensorValue_Data.Types.Temp, "°C") self.qv_kb_i_h = SensorValue("ID_22", "Humi KB indoor", SensorValue_Data.Types.Humi, "% rF") self.qv_kb_p = SensorValue("ID_23", "Pressure KB", SensorValue_Data.Types.Pressure, "hPa") self.qv_kb_a_t = SensorValue("ID_24", "Temp KB outdoor", SensorValue_Data.Types.Temp, "°C") self.qv_kb_a_h = SensorValue("ID_25", "Humi KB outdoor", SensorValue_Data.Types.Humi, "% rF") self.qv_kb_k_t = SensorValue("ID_26", "Temp KB basement", SensorValue_Data.Types.Temp, "°C") self.qv_kb_k_h = SensorValue("ID_27", "Humi KB basement", SensorValue_Data.Types.Humi, "% rF") self.sq.register(self.qv_kb_i_t) self.sq.register(self.qv_kb_i_h) self.sq.register(self.qv_kb_p) self.sq.register(self.qv_kb_a_t) self.sq.register(self.qv_kb_a_h) self.sq.register(self.qv_kb_k_t) self.sq.register(self.qv_kb_k_h) self._running = True def run(self): while self._running: if data[pik_i] is not None: self.qv_kb_i_t.value = "{:.1f}".format( float(data[pik_i].split(':')[1])) self.qv_kb_i_h.value = "{:.1f}".format( float(data[pik_i].split(':')[4])) self.qv_kb_p.value = "{:.1f}".format( float(data[pik_i].split(':')[5])) if data[pik_a] is not None: self.qv_kb_a_t.value = "{:.1f}".format( float(data[pik_a].split(':')[1])) self.qv_kb_a_h.value = "{:.1f}".format( float(data[pik_a].split(':')[4])) if data[pik_k] is not None: self.qv_kb_k_t.value = "{:.1f}".format( float(data[pik_k].split(':')[1])) self.qv_kb_k_h.value = "{:.1f}".format( float(data[pik_k].split(':')[4])) for _ in range(600): # interruptible sleep if not self._running: break time.sleep(0.1) def stop(self): self._running = False
def main(): """main part""" sq = SensorQueueClient_write("../../configs/weatherqueue.ini") qvalue_temp = SensorValue("ID_06", "TempKinderzimmer", SensorValue_Data.Types.Temp, "°C") qvalue_humi = SensorValue("ID_07", "HumiKinderzimmer", SensorValue_Data.Types.Humi, "% rF") sq.register(qvalue_temp) sq.register(qvalue_humi) temphumi = DHT22_AM2302(19, qvalue_temp, qvalue_humi) # BCM 19 = PIN 35 temp_cpu = CPU() measurements = {DS_TEMP1: Measurements(3), \ DS_TEMPCPU: Measurements(3), \ DS_TEMP2: Measurements(3), \ DS_HUMI: Measurements(3)} rrd_template = DS_TEMP1 + ":" + \ DS_TEMPCPU + ":" + \ DS_TEMP2 + ":" + \ DS_HUMI while (True): _temp, _humi = temphumi.read() measurements[DS_TEMP1].append(_temp) measurements[DS_HUMI].append(_humi) measurements[DS_TEMPCPU].append(temp_cpu.read_temperature()) measurements[DS_TEMP2].append(0) # empty, for later useage rrd_data = "N:{:.2f}".format(measurements[DS_TEMP1].last()) + \ ":{:.2f}".format(measurements[DS_TEMPCPU].last()) + \ ":{:.2f}".format(measurements[DS_TEMP2].last()) + \ ":{:.2f}".format(measurements[DS_HUMI].last()) # print(strftime("%H:%M:%S", localtime()), rrd_data) # rrdtool.update(RRDFILE, "--template", rrd_template, rrd_data) # python rrdtool seems not to work here; the pi needs a proper reinstall. # as a workaround, i just call the os for rrd update # rrd = "/usr/bin/rrdtool update {} --template {} {}".format(RRDFILE, rrd_template, rrd_data) rrd = [ "/usr/bin/rrdtool", "update", RRDFILE, "--template", rrd_template, rrd_data ] print(rrd) subprocess.call(rrd) sleep(35)
def __init__(self): threading.Thread.__init__(self) self.values = { 'temp': "n/a", 'tempcpu': "n/a", 'humi': "n/a", 'airpressure': "n/a", 'lightness': "n/a", 'airquality': "n/a" } self.qv_temp = SensorValue("ID_40", "TempKueche", SensorValue_Data.Types.Temp, "°C") self.qv_humi = SensorValue("ID_41", "HumiKueche", SensorValue_Data.Types.Humi, "% rF") self.qv_pressure = SensorValue("ID_42", "PressureKueche", SensorValue_Data.Types.Pressure, "hPa") self.qv_light = SensorValue("ID_43", "LightKueche", SensorValue_Data.Types.Light, "lux") self.qv_airquality = SensorValue("ID_44", "AirQualityKueche", SensorValue_Data.Types.AirQuality, "%") self.sq = SensorQueueClient_write("../../../configs/weatherqueue.ini") self.sq.register(self.qv_temp) self.sq.register(self.qv_humi) self.sq.register(self.qv_pressure) self.sq.register(self.qv_light) self.sq.register(self.qv_airquality) self.cpu = CPU() self.bme680 = BME680(i2c_addr=BME_680_BASEADDR, \ qv_temp=self.qv_temp, qv_humi=self.qv_humi, \ qv_pressure=self.qv_pressure, qv_airquality=self.qv_airquality) self.tsl2561 = TSL2561(qvalue=self.qv_light) self.rrd_template = DS_TEMP + ":" + \ DS_TEMPCPU + ":" + \ DS_HUMI + ":" + \ DS_AIRPRESSURE + ":" + \ DS_LIGHTNESS + ":" + \ DS_AIRQUALITY + ":" + \ DS_OPEN1 + ":" + \ DS_OPEN2 + ":" + \ DS_OPEN3 + ":" + \ DS_OPEN4 self._running = True
def __init__ (self): self.udp = UDP.Receiver(CREDENTIALS_HA) self.data = { p: None for p in PIs } self.sq = SensorQueueClient_write(QUEUE_INI) self.qv_kb_i_t = SensorValue("ID_21", "Temp KB indoor", SensorValue_Data.Types.Temp, "°C") self.qv_kb_i_h = SensorValue("ID_22", "Humi KB indoor", SensorValue_Data.Types.Humi, "% rF") self.qv_kb_p = SensorValue("ID_23", "Pressure KB", SensorValue_Data.Types.Pressure, "hPa") self.qv_kb_a_t = SensorValue("ID_24", "Temp KB outdoor", SensorValue_Data.Types.Temp, "°C") self.qv_kb_a_h = SensorValue("ID_25", "Humi KB outdoor", SensorValue_Data.Types.Humi, "% rF") self.qv_kb_k_t = SensorValue("ID_26", "Temp KB basement", SensorValue_Data.Types.Temp, "°C") self.qv_kb_k_h = SensorValue("ID_27", "Humi KB basement", SensorValue_Data.Types.Humi, "% rF") self.sq.register(self.qv_kb_i_t) self.sq.register(self.qv_kb_i_h) self.sq.register(self.qv_kb_p) self.sq.register(self.qv_kb_a_t) self.sq.register(self.qv_kb_a_h) self.sq.register(self.qv_kb_k_t) self.sq.register(self.qv_kb_k_h)
def __init__(self): threading.Thread.__init__(self) self.values = { 'temp': "n/a", 'tempcpu': "n/a", 'humi': "n/a", 'airpressure': "n/a", 'lightness': "n/a", 'airquality': "n/a" } self.qv_temp = SensorValue("ID_40", "TempKueche", SensorValue_Data.Types.Temp, "°C") self.qv_humi = SensorValue("ID_41", "HumiKueche", SensorValue_Data.Types.Humi, "% rF") self.qv_pressure = SensorValue("ID_42", "PressureKueche", SensorValue_Data.Types.Pressure, "hPa") self.qv_light = SensorValue("ID_43", "LightKueche", SensorValue_Data.Types.Light, "lux") self.qv_airquality = SensorValue("ID_44", "AirQualityKueche", SensorValue_Data.Types.AirQuality, "%") self.sq = SensorQueueClient_write("../../../configs/weatherqueue.ini") self.sq.register(self.qv_temp) self.sq.register(self.qv_humi) self.sq.register(self.qv_pressure) self.sq.register(self.qv_light) self.sq.register(self.qv_airquality) self.cpu = CPU() self.bme680 = BME680(i2c_addr=BME_680_BASEADDR, \ qv_temp=self.qv_temp, qv_humi=self.qv_humi, \ qv_pressure=self.qv_pressure, qv_airquality=self.qv_airquality) self.tsl2561 = TSL2561(qvalue=self.qv_light) self.udp = UDP.Sender(CREDENTIALS)
class Receiver_Homeautomation (object): def __init__ (self): self.udp = UDP.Receiver(CREDENTIALS_HA) self.data = { p: None for p in PIs } self.sq = SensorQueueClient_write(QUEUE_INI) self.qv_kb_i_t = SensorValue("ID_21", "Temp KB indoor", SensorValue_Data.Types.Temp, "°C") self.qv_kb_i_h = SensorValue("ID_22", "Humi KB indoor", SensorValue_Data.Types.Humi, "% rF") self.qv_kb_p = SensorValue("ID_23", "Pressure KB", SensorValue_Data.Types.Pressure, "hPa") self.qv_kb_a_t = SensorValue("ID_24", "Temp KB outdoor", SensorValue_Data.Types.Temp, "°C") self.qv_kb_a_h = SensorValue("ID_25", "Humi KB outdoor", SensorValue_Data.Types.Humi, "% rF") self.qv_kb_k_t = SensorValue("ID_26", "Temp KB basement", SensorValue_Data.Types.Temp, "°C") self.qv_kb_k_h = SensorValue("ID_27", "Humi KB basement", SensorValue_Data.Types.Humi, "% rF") self.sq.register(self.qv_kb_i_t) self.sq.register(self.qv_kb_i_h) self.sq.register(self.qv_kb_p) self.sq.register(self.qv_kb_a_t) self.sq.register(self.qv_kb_a_h) self.sq.register(self.qv_kb_k_t) self.sq.register(self.qv_kb_k_h) def process (self): if self.data[pik_i] is not None: self.qv_kb_i_t.value = "{:.1f}".format(float(self.data[pik_i].split(':')[1])) self.qv_kb_i_h.value = "{:.1f}".format(float(self.data[pik_i].split(':')[4])) self.qv_kb_p.value = "{:.1f}".format(float(self.data[pik_i].split(':')[5])) if self.data[pik_a] is not None: self.qv_kb_a_t.value = "{:.1f}".format(float(self.data[pik_a].split(':')[1])) self.qv_kb_a_h.value = "{:.1f}".format(float(self.data[pik_a].split(':')[4])) if self.data[pik_k] is not None: self.qv_kb_k_t.value = "{:.1f}".format(float(self.data[pik_k].split(':')[1])) self.qv_kb_k_h.value = "{:.1f}".format(float(self.data[pik_k].split(':')[4])) def run (self): while True: payload = self.udp.receive() Log(f"Data received: {payload}") (source, values) = payload.split(',') self.data[source] = values self.process()
# Exit ######################################################################## def shutdown_application(): """cleanup stuff""" sys.exit(0) ############################################################################### ############################################################################### if __name__ == '__main__': shutdown = Shutdown(shutdown_func=shutdown_application) parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group(required=True) group.add_argument("--sensor", help="read data from sensor and send to udp server", action="store_true") group.add_argument("--receiver", help="receive data via udp and store in rrd database", action="store_true") args = parser.parse_args() if args.sensor: sq = SensorQueueClient_write( os.path.expanduser("~/configs/weatherqueue.ini")) Sensor() if args.receiver: Receiver() # eof #
SensorValue_Data.Types.Temp, "°C"), 'humidity_owm_{}'.format(i): SensorValue("ID_OWM_{}2".format(i), "HumiOWM_{}".format(i), SensorValue_Data.Types.Humi, "% rF"), 'wind_owm_{}'.format(i): SensorValue("ID_OWM_{}3".format(i), "WindOWM_{}".format(i), SensorValue_Data.Types.Wind, "km/h"), 'wind direction_owm_{}'.format(i): SensorValue("ID_OWM_{}4".format(i), "WindDirOWM_{}".format(i), SensorValue_Data.Types.WindDir, None), 'desc_owm_{}'.format(i): SensorValue("ID_OWM_{}5".format(i), "DescOWM_{}".format(i), SensorValue_Data.Types.Desc, None), }) sq = SensorQueueClient_write("../config.ini") list(map(sq.register, qv.values())) # sq.start() while True: # print("sending values ...") qv['qv_temp_wardrobe'].value = "55.{:02d}".format(random.randint( 0, 99)) qv['qv_humi_wardrobe'].value = "56.{:02d}".format(random.randint( 0, 99)) qv['qv_light_wardrobe'].value = "823.{:02d}".format( random.randint(0, 99)) qv['qv_temp_indoor'].value = "11.{:02d}".format(random.randint(0, 99)) qv['qv_humi_indoor'].value = "12.{:02d}".format(random.randint(0, 99)) qv['qv_temp_outdoor'].value = "22.{:02d}".format(random.randint(0, 99)) qv['qv_humi_outdoor'].value = "23.{:02d}".format(random.randint(0, 99))
sys.exit(0) ############################################################################### ############################################################################### if __name__ == '__main__': shutdown = Shutdown(shutdown_func=shutdown_application) qv_temp_wardrobe = SensorValue("ID_31", "TempWardrobe", SensorValue_Data.Types.Temp, "°C") qv_humi_wardrobe = SensorValue("ID_32", "HumiWardrobe", SensorValue_Data.Types.Humi, "% rF") qv_light_wardrobe = SensorValue("ID_33", "LightWardrobe", SensorValue_Data.Types.Light, "lux") sq = SensorQueueClient_write("../../configs/weatherqueue.ini") sq.register(qv_temp_wardrobe) sq.register(qv_humi_wardrobe) sq.register(qv_light_wardrobe) lightness = Lightness(qv=qv_light_wardrobe) forecast = Forecast(central_i2c_lock) controls = { 'doors': Control(Sensor1_Pin, Actuator1_ID), 'drawer': Control(Sensor2_Pin, Actuator2_ID), 'button': Control_Button(Sensor3_Pin, Actuator3_ID) } main() # eof #
class Sensors(threading.Thread, metaclass=Singleton): def __init__(self): threading.Thread.__init__(self) self.values = { 'temp': "n/a", 'tempcpu': "n/a", 'humi': "n/a", 'airpressure': "n/a", 'lightness': "n/a", 'airquality': "n/a" } self.qv_temp = SensorValue("ID_40", "TempKueche", SensorValue_Data.Types.Temp, "°C") self.qv_humi = SensorValue("ID_41", "HumiKueche", SensorValue_Data.Types.Humi, "% rF") self.qv_pressure = SensorValue("ID_42", "PressureKueche", SensorValue_Data.Types.Pressure, "hPa") self.qv_light = SensorValue("ID_43", "LightKueche", SensorValue_Data.Types.Light, "lux") self.qv_airquality = SensorValue("ID_44", "AirQualityKueche", SensorValue_Data.Types.AirQuality, "%") self.sq = SensorQueueClient_write("../../../configs/weatherqueue.ini") self.sq.register(self.qv_temp) self.sq.register(self.qv_humi) self.sq.register(self.qv_pressure) self.sq.register(self.qv_light) self.sq.register(self.qv_airquality) self.cpu = CPU() self.bme680 = BME680(i2c_addr=BME_680_BASEADDR, \ qv_temp=self.qv_temp, qv_humi=self.qv_humi, \ qv_pressure=self.qv_pressure, qv_airquality=self.qv_airquality) self.tsl2561 = TSL2561(qvalue=self.qv_light) self.udp = UDP.Sender(CREDENTIALS) def run(self): self._running = True while self._running: self.bme680.get_sensor_data() self.values['temp'] = self.bme680.data.temperature self.values['tempcpu'] = self.cpu.read_temperature() self.values['humi'] = self.bme680.data.humidity self.values['airpressure'] = self.bme680.data.pressure self.values['lightness'] = self.tsl2561.lux() self.values['airquality'] = self.bme680.data.air_quality_score \ if self.bme680.data.air_quality_score != None else 0 rrd_data = "N:{:.2f}".format(self.values['temp']) + \ ":{:.2f}".format(self.values['tempcpu']) + \ ":{:.2f}".format(self.values['humi']) + \ ":{:.2f}".format(self.values['airpressure']) + \ ":{:.2f}".format(self.values['lightness']) + \ ":{:.2f}".format(self.values['airquality']) + \ ":{}".format(0) + \ ":{}".format(0) + \ ":{}".format(0) + \ ":{}".format(0) self.udp.send(rrd_data) for _ in range(50): # interruptible sleep if self._running: time.sleep(1) # brightness control needs higher frequency self.values['lightness'] = self.tsl2561.lux() else: break def stop(self): self._running = False
cpu_temp]) # print(rrd_template) print(strftime("%Y%m%d %X:"), rrd_data) rrdtool.update(DATAFILE, "--template", rrd_template, rrd_data) sleep(45) ############################################################################### # Exit ######################################################################## def shutdown_application(): """cleanup stuff""" print("in _exit()") if platform == Platform.NANOPI: print("calling ds1820_1.consume_cpu_stop()") ds1820_1.consume_cpu_stop() sys.exit(0) ############################################################################### ############################################################################### if __name__ == '__main__': shutdown = Shutdown(shutdown_func=shutdown_application) ds1820_1 = None sq = SensorQueueClient_write("../../configs/weatherqueue.ini") main() # eof #