def main(config): try: client = connect_to_mqtt(config) except OSError: restart_and_reconnect() while True: oled.fill(0) oled.show() temperature = sensor.temperature humidity = sensor2.relative_humidity pressure = sensor.pressure light = bh1750fvi.sample(i2c, mode=OP_SINGLE_HRES2) oled.text("T:"+("%.4s" % temperature)+" C", 0, 0) oled.text("H:"+("%.4s" % humidity)+" %", 0, 10) oled.text(("%.4s" % pressure)+" hPa", 0, 20) oled.text(("%.4s" % light)+" lux", 0, 30) oled.show() print("T:"+("%.4s" % temperature)+" C") print("H:"+("%.4s" % humidity)+" %") print(("%.4s" % pressure)+" hPa") print(("%.4s" % light)+" lux") try: client.publish(topic1, str(temperature), qos=QOS) client.publish(topic2, str(humidity), qos=QOS) client.publish(topic3, str(pressure), qos=QOS) client.publish(topic4, str(light), qos=QOS) except OSError: restart_and_reconnect() sleep(10)
def main(config): try: client = connect_to_mqtt(config) except OSError: restart_and_reconnect() while True: oled.fill(0) oled.show() sensor.measure() temperature = sensor.temperature() humidity = sensor.humidity() light = bh1750fvi.sample(i2c, mode=OP_SINGLE_HRES2) oled.text("Temp=", 0, 0) oled.text(str(temperature)+" C", 0, 10) oled.text("Humid=", 0, 20) oled.text(str(humidity)+" %", 0, 30) oled.text("Lux="+str(light), 0, 40) oled.show() print(temperature) print(humidity) print(light) try: client.publish(topic1, str(temperature), qos=QOS) client.publish(topic2, str(humidity), qos=QOS) client.publish(topic3, str(light), qos=QOS) except OSError: restart_and_reconnect() sleep(10)
def monitor_light_level(self): try: value = bh1750fvi.sample(self.i2c) except Exception as e: value = 0 print("ERROR:bh1750fvi is not found:%s" % e) self.light_level = value print("light level:%s" % value) # 读取值0-54613 return value
def main(config): try: client = connect_to_mqtt(config) except OSError: sleep(10) restart_and_reconnect() if (display_present1 or display_present2): writer = Writer(oled, Arial11) while True: if (display_present1 or display_present2): oled.fill(0) oled.show() temperature, humidity = sensor.measure() temp_only = ("%.4s" % temperature) humid_only = ("%.4s" % humidity) if (bmx_present1 or bmx_present2): pressure = sensor2.pressure press_float = float(pressure[:-3]) press_only = ("%.0f" % press_float) if (light_present1 or light_present2): light = bh1750fvi.sample(i2c, mode=OP_SINGLE_HRES2) if ((display_present1 or display_present2) and (light_present1 or light_present2) and light > 0): writer.set_textpos(0, 0) writer.printstring("T: " + temp_only + "°C") writer.set_textpos(12, 0) writer.printstring("H: " + humid_only + " %") if (bmx_present1 or bmx_present2): writer.set_textpos(24, 0) writer.printstring(press_only + " hPa") if (light_present1 or light_present2): writer.set_textpos(36, 0) writer.printstring(("%.4s" % light) + " lux") oled.show() print("Temperature: " + temp_only + "°C") print("Humidity: " + humid_only + " %") if (bmx_present1 or bmx_present2): print("Pressure: " + press_only + " hPa") if (light_present1 or light_present2): print("Light Intensity: " + ("%.4s" % light) + " lux") sleep(25) if (display_present1 or display_present2): oled.fill(0) oled.show() mq = MQ() perc = mq.MQPercentage() gas_lpg = perc["GAS_LPG"] co = perc["CO"] methane = perc["SMOKE"] light = bh1750fvi.sample(i2c, mode=OP_SINGLE_HRES2) if ((display_present1 or display_present2) and (light_present1 or light_present2) and light > 0): writer.set_textpos(0, 0) writer.printstring("LP " + str(float("%.2g" % gas_lpg))) writer.set_textpos(12, 0) writer.printstring("CO " + str(float("%.2g" % co))) writer.set_textpos(24, 0) writer.printstring("ME " + str(float("%.2g" % methane))) oled.show() print("Gas_LPG: " + str(float("%.2g" % gas_lpg))) print("CO: " + str(float("%.2g" % co))) print("Methane: " + str(float("%.2g" % methane))) try: client.publish(topic1, str(temp_only), qos=QOS) client.publish(topic2, str(humid_only), qos=QOS) if (bmx_present1 or bmx_present2): client.publish(topic3, str(press_only), qos=QOS) if (light_present1 or light_present2): client.publish(topic4, str(light), qos=QOS) client.publish(topic5, str(gas_lpg), qos=QOS) client.publish(topic6, str(co), qos=QOS) client.publish(topic7, str(methane), qos=QOS) except OSError: restart_and_reconnect() sleep(25) if (display_present1 or display_present2): oled.fill(0) oled.show() sleep(25)
def main(): esp.osdebug(None) import gc gc.collect() i2c = I2C(scl=machine.Pin(5), sda=machine.Pin(4)) sensor = dht.DHT11(Pin(12)) bmp180 = BMP180(i2c) bmp180.oversample_sett = 2 bmp180.baseline = 101325 mqtt_server = '192.168.10.124' net = do_connect('HoltAtHome4','anthony050192') client_id = ubinascii.hexlify(machine.unique_id()) client = MQTTClient(client_id, mqtt_server) client.connect() count = 255 base = '/home/office/' print("Free : ", gc.mem_free()) while True: lightLevel = bh1750fvi.sample(i2c) print("Light Level : ",lightLevel) print("Count : ",count) client.publish(base + 'light', str(lightLevel)) count = count+1 if count > 2: sensor.measure() humidity = sensor.humidity() temp1 = sensor.temperature() temp = bmp180.temperature p = bmp180.pressure mbar = p/100 freeMem = gc.mem_free() dewPoint = calcRH(temp,humidity) print("Temperature :", temp) print("Temperature1:", temp1) print("Pressure :", mbar) print("Humidity :", humidity) print("Dew Point :", dewPoint) print("Free : ", freeMem) client.publish(base + 'temperature', str(temp)) client.publish(base + 'pressure', str(mbar)) client.publish(base + 'humidity', str(humidity)) client.publish(base + 'dewpoint', str(dewPoint)) client.publish(base + 'envNode/freemem' , str(freeMem)) count = 0 gc.collect() time.sleep(15)
def Sensor_GY3o2(): i2csensor = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) result = bh1750fvi.sample(i2csensor) return result
def get_light_data(): return bh1750fvi.sample(light_sensor_i2c)
def main(config): try: client = connect_to_mqtt(config) except OSError: sleep(10) restart_and_reconnect() if (display_present1 or display_present2): writer = Writer(oled, Arial11) while True: if (display_present1 or display_present2): oled.fill(0) oled.show() if ahtx0_present: temperature = sensor.temperature humidity = sensor.relative_humidity if (sht3x_present1 or sht3x_present2): temperature, humidity = sensor.measure() if (bmx_present1 or bmx_present2): temperature = sensor2.temperature if (ahtx0_present or sht3x_present1 or sht3x_present2 or bmx_present1 or bmx_present2): temp_only = ("%.4s" % temperature) if (sht3x_present1 or sht3x_present2 or ahtx0_present): humid_only = ("%.4s" % humidity) if (bmx_present1 or bmx_present2): pressure = sensor2.pressure press_float = float(pressure[:-3]) press_only = ("%.0f" % press_float) if (light_present1 or light_present2): light = bh1750fvi.sample(i2c, mode=OP_SINGLE_HRES2) if ((display_present1 or display_present2) and (light_present1 or light_present2) and light > 0): writer.set_textpos(0, 0) writer.printstring("T: " + temp_only + "°C") writer.set_textpos(12, 0) writer.printstring("H: " + humid_only + " %") if (bmx_present1 or bmx_present2): writer.set_textpos(24, 0) writer.printstring(press_only + " hPa") if (light_present1 or light_present2): writer.set_textpos(36, 0) writer.printstring(("%.4s" % light) + " lux") oled.show() if (ahtx0_present or sht3x_present1 or sht3x_present2 or bmx_present1 or bmx_present2): print("Temperature: " + temp_only + "°C") if (sht3x_present1 or sht3x_present2 or ahtx0_present): print("Humidity: " + humid_only + " %") if (bmx_present1 or bmx_present2): print("Pressure: " + press_only + " hPa") if (light_present1 or light_present2): print("Light Intensity: " + ("%.4s" % light) + " lux") sleep(25) if (display_present1 or display_present2): oled.fill(0) oled.show() if mq9_present: mq = MQ() perc = mq.MQPercentage() gas_lpg = perc["GAS_LPG"] co = perc["CO"] methane = perc["SMOKE"] print("Gas_LPG: " + str(float("%.2g" % gas_lpg))) print("CO: " + str(float("%.2g" % co))) print("Methane: " + str(float("%.2g" % methane))) if (light_present1 or light_present2): light = bh1750fvi.sample(i2c, mode=OP_SINGLE_HRES2) if ((display_present1 or display_present2) and (light_present1 or light_present2) and light > 0): writer.set_textpos(0, 0) writer.printstring("LP " + str(float("%.2g" % gas_lpg))) writer.set_textpos(12, 0) writer.printstring("CO " + str(float("%.2g" % co))) writer.set_textpos(24, 0) writer.printstring("ME " + str(float("%.2g" % methane))) oled.show() if ds1307_present: rtc.halt(False) # power-up RTC oscillator datetime = rtc.datetime() date_str = str(datetime[1]) + "-" + str(datetime[2]) + "-" + str( datetime[0])[-2:] time_str = str(datetime[4]) + "h:" + "{:0>{w}}".format( str(datetime[5]), w=2) + "m" if (display_present1 or display_present2): sleep(25) oled.fill(0) oled.show() writer.set_textpos(0, 0) writer.printstring(date_str) writer.set_textpos(12, 0) writer.printstring(time_str) oled.show() print(date_str) print(time_str) try: client.publish(topic1, str(temp_only), qos=QOS) if (sht3x_present1 or sht3x_present2 or ahtx0_present): client.publish(topic2, str(humid_only), qos=QOS) if (bmx_present1 or bmx_present2): client.publish(topic3, str(press_only), qos=QOS) if (light_present1 or light_present2): client.publish(topic4, str(light), qos=QOS) if mq9_present: client.publish(topic5, str(gas_lpg), qos=QOS) client.publish(topic6, str(co), qos=QOS) client.publish(topic7, str(methane), qos=QOS) except OSError: restart_and_reconnect() sleep(25) if (display_present1 or display_present2): oled.fill(0) oled.show() sleep(25)
# i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) import bh1750fvi from machine import I2C, Pin i2c = I2C(scl=machine.Pin(5), sda=machine.Pin(4)) result = bh1750fvi.sample(i2c) print(result)
def monitor_light_level(self): value = bh1750fvi.sample(self.i2c) self.light_level = value print(value) # 读取值0-54613 return value
# Logging has been configured in PuTTY for all session output # Simply connect to the COM port with PuTTY and the output will be logged to light.log from machine import I2C, Pin from micropython import const import bh1750fvi import utime # Alternative driver # from bh1750 import BH1750 # IO Pin numbers on U1 connecting to I2C0_SCL and I2C0_SDA which make up the I2C bus that the light sensor is # connected to I2C0_SCL = const(26) I2C0_SDA = const(25) scl = Pin(I2C0_SCL) sda = Pin(I2C0_SDA) i2c = I2C(-1, scl=scl, sda=sda) while True: lux = bh1750fvi.sample(i2c) print(lux) utime.sleep(1) # Alternative driver # s = BH1750(i2c) # while True: # print(s.luminance(BH1750.CONT_HIRES_1)) # time.sleep(1)