"""Station météo connectée au service Adafruit IO """ import time import machine import network import esp import bme280_i2c from ntptime import settime from umqtt.robust import MQTTClient import config # connect to WLAN wlan = network.WLAN(network.STA_IF) wlan.active(True) nets = wlan.scan() for net in nets: net_ssid = net[0].decode() if net_ssid == config.WIFI_SSID: print('Network found!') wlan.connect(net_ssid, config.WIFI_PSK) while not wlan.isconnected(): machine.idle() # save power while waiting print('WLAN connection succeeded!') break if not wlan.isconnected(): print("WLAN not found/not connected") # Create a micropython I2C object with the appropriate device pins i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) # Create a sensor object to represent the BME280 # Note that this will error if the device can't be reached over I2C. bme = bme280_i2c.BME280_I2C(address=bme280_i2c.BME280_I2C_ADDR_SEC, i2c=i2c) # Configure the sensor for the application in question. bme.set_measurement_settings({ 'filter': bme280_i2c.BME280_FILTER_COEFF_16, 'standby_time': bme280_i2c.BME280_STANDBY_TIME_500_US, 'osr_h': bme280_i2c.BME280_OVERSAMPLING_1X, 'osr_p': bme280_i2c.BME280_OVERSAMPLING_16X, 'osr_t': bme280_i2c.BME280_OVERSAMPLING_2X}) # Start the sensor automatically sensing bme.set_power_mode(bme280_i2c.BME280_NORMAL_MODE) def MqttPublish(client, topic, message, retain=False, qos=0, sleep=10): """MQTT publish helper""" client.publish("test/{device}/{topic}".format(device=config.CLIENT_ID, topic=topic), message, retain=retain, qos=qos) time.sleep_ms(sleep) client = MQTTClient(client_id=config.CLIENT_ID, server=config.MQTT_HOST, user=config.MQTT_USERNAME, password=config.MQTT_PASSWD, port=config.MQTT_PORT) client.connect() MqttPublish(client, "location", config.LOCATION, retain=True, qos=1) MqttPublish(client, "humidity/unit", "%", retain=True) MqttPublish(client, "humidity/desc", "Capteur Bosch BME280", retain=True, qos=0) MqttPublish(client, "pressure/unit", "hPa", retain=True, qos=0) MqttPublish(client, "pressure/desc", "Capteur Bosch BME280", retain=True, qos=0) MqttPublish(client, "temperature/unit", "degC", retain=True, qos=0) MqttPublish(client, "temperature/desc", "Capteur Bosch BME280", retain=True, qos=0) MqttPublish(client, "wifi/ssid", config.WIFI_SSID, retain=True, qos=0) MqttPublish(client, "wifi/ip", wlan.ifconfig()[0], retain=True, qos=0) MqttPublish(client, "wifi/channel", "{:d}".format(net[2]), retain=True, qos=0) MqttPublish(client, "sys/esp8266_device_id", "{:d}".format(esp.flash_id()), retain=True, qos=0) time.sleep_ms(1000) # For each nature, a list describes: # - mqtt topic # - string format # - factor (multiplier) # - qos topics = {"humidity" : ["humidity/value", "{:.0f}", 1, 1], "pressure" : ["pressure/value", "{:.2f}", 0.01, 1], "temperature" : ["temperature/value", "{:.1f}", 1, 1]} while 1: bme_data = bme.get_measurement() print(bme_data) for nature, param in topics.items(): MqttPublish(client, param[0], param[1].format(bme_data[nature]*param[2]), qos=param[3], sleep=50) MqttPublish(client, "wifi/rssi", "{:.0f}".format(wlan.status('rssi'))) time.sleep_ms(1000)