Skip to content
Snippets Groups Projects
Commit 74e2fe93 authored by Carsten Presser's avatar Carsten Presser
Browse files

Fix CO2 sensor stuff

parent fa2dea2a
Branches master
No related tags found
No related merge requests found
......@@ -8,10 +8,11 @@ import math
import pca9685
import mqtt
import bme280
import pasCO2
import adt7410
import timer
TMR = timer.Softtimer(50, tid=3)
TMR = timer.Softtimer(100, tid=3)
UID = ubinascii.hexlify(machine.unique_id()).decode()
MQTT = mqtt.MQTTClient("Sensor_{}".format(UID), "mqtt.intern.ca.rstenpresser.de")
#I2C = machine.I2C(1, scl=machine.Pin(19), sda=machine.Pin(18), freq=400000) #blue pcb
......@@ -19,8 +20,9 @@ I2C = machine.I2C(0, freq=400000) #green pcb
PCA = pca9685.PCA9685(I2C)
BME = None
ADT = None
CO2 = None
BME_PUBLISH_INTERVAL = 60000
ENV_PUBLISH_INTERVAL = 60000
red = machine.Pin(12, machine.Pin.OUT) #green pcb
#red = machine.Pin(13, machine.Pin.OUT) #blue pcb
......@@ -83,7 +85,7 @@ class MQTT_SensorCombine():
"unit_of_meas": unit
}
self.MQTT.publish(self.base + "_" + claz + "/config", json.dumps(cfg))
print (self.base + "/config")
print (self.base + "_" + claz + "/config")
def publish(self, val_map):
self.MQTT.publish(self.base + "/state", json.dumps(val_map))
......@@ -241,27 +243,28 @@ def pwm_set(channel, color, value = 0, bright = 0):
v = int(value * bright / 255)
PCA.duty(chan, g8[v])
def publish_bme280():
def publish_env():
try:
if BME != None:
(temperature, pressure, humidity) = BME.values
except:
pass
else:
sensorBME.publish({"temperature": temperature, "pressure": pressure, "humidity": humidity})
#print("{}\t{}\t{}".format(temperature, pressure, humidity))
finally:
TMR.add(BME_PUBLISH_INTERVAL, publish_bme280)
def publish_adt7410():
try:
temperature = ADT.values
print("BME280: \t Temp={}\tPressure={}\tHumidity={}".format(temperature, pressure, humidity))
if CO2 != None:
if "pressure" in locals():
CO2.set_pressure(int(pressure))
(ppm) = CO2.values
sensorCO2.publish({"carbon_dioxide": ppm})
print("PAS Co2:\t PPM={}".format(ppm))
if ADT != None:
(temp) = ADT.values
sensorADT.publish({"temperature": temp})
print("ADT7140:\t Temp={}".format(temp))
except:
pass
else:
print(temperature)
sensorADT.publish({"temperature": temperature})
pass
finally:
TMR.add(BME_PUBLISH_INTERVAL, publish_adt7410)
TMR.add(ENV_PUBLISH_INTERVAL, publish_env)
# red and green are inverted.
# show that we are not ready by lighting up red
......@@ -322,28 +325,26 @@ if False:
for idd, ma in btn_map.items():
ma['button'].irq(button_callback, trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING)
# add Bosch BME280 if found
if 118 in I2C.scan():
# the BME280 lib already set force mode (LSB in 0xF4 register)
BME = bme280.BME280(i2c=I2C, mode=bme280.BME280_OSAMPLE_1)
# configure temp sensor
if BME != None:
sensorBME = MQTT_SensorCombine(MQTT, UID, "bme280", {"temperature": "C", "pressure": "hPa", "humidity": "%"})
publish_bme280()
# add ADT7410 if found
# add Infinion PAS Co2 Sensor if found
if 40 in I2C.scan():
CO2 = pasCO2.PASCo2(i2c=I2C)
sensorCO2 = MQTT_SensorCombine(MQTT, UID, "pasCo2", {"carbon_dioxide": "ppm"})
# add Analog ADT7410 temperature sensor if found
if 72 in I2C.scan():
ADT = adt7410.ADT7410(i2c=I2C)
# configure temp sensor
if ADT != None:
sensorADT = MQTT_SensorCombine(MQTT, UID, "adt7410", {"temperature": "C"})
publish_adt7410()
sensorADT = MQTT_SensorCombine(MQTT, UID, "ADT7140", {"temperature": "C"})
TMR._start()
# if any sensor was found, publish them
if CO2 != None or BME != None or ADT != None:
TMR.add(1000, publish_env)
TMR._start()
# main loop does nothing
while True:
......
......@@ -16,8 +16,12 @@ class PASCo2:
#self.i2c.writeto_mem(self.address, 0x02, bytearray([0x00, 0x0A])) # cont. period
status = self.i2c.readfrom_mem(self.address, 0x01, 1)
print("status = 0x%02x" % int(status[0]))
#print("status = 0x%02x" % int(status[0]))
def set_pressure(self, pressure):
highbyte = int(pressure / 256)
lowbyte = int(pressure % 256)
self.i2c.writeto_mem(self.address, 0x0B, bytearray([highbyte, lowbyte]))
def read_data(self, result=None, delay=1000):
""" Reads the data from the sensor
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment