Change MQTT broker for personal one

This commit is contained in:
Pierrick C 2020-03-24 22:49:37 +01:00
parent 72e9c256bf
commit 83523d9e44

View File

@ -11,6 +11,7 @@ import config
# connect to WLAN # connect to WLAN
wlan = network.WLAN(network.STA_IF) wlan = network.WLAN(network.STA_IF)
wlan.active(True)
nets = wlan.scan() nets = wlan.scan()
for net in nets: for net in nets:
net_ssid = net[0].decode() net_ssid = net[0].decode()
@ -18,7 +19,7 @@ for net in nets:
print('Network found!') print('Network found!')
wlan.connect(net_ssid, config.WIFI_PSK) wlan.connect(net_ssid, config.WIFI_PSK)
while not wlan.isconnected(): while not wlan.isconnected():
machine.idle() # save power while waiting machine.idle() # save power while waiting
print('WLAN connection succeeded!') print('WLAN connection succeeded!')
break break
if not wlan.isconnected(): if not wlan.isconnected():
@ -43,19 +44,27 @@ bme.set_measurement_settings({
bme.set_power_mode(bme280_i2c.BME280_NORMAL_MODE) bme.set_power_mode(bme280_i2c.BME280_NORMAL_MODE)
client = MQTTClient(client_id="int_weather_station", client = MQTTClient(client_id=config.CLIENT_ID,
server="io.adafruit.com", server=config.MQTT_HOST,
user=config.ADAFRUIT_IO_USERNAME, user=config.MQTT_USERNAME,
password=config.ADAFRUIT_IO_KEY, password=config.MQTT_PASSWD,
port=1883) port=config.MQTT_PORT)
client.connect() client.connect()
client.publish("test/bme280/humidity/unit", "%", retain=True, qos=0)
time.sleep_ms(100)
client.publish("test/bme280/pressure/unit", "hPa", retain=True, qos=0)
time.sleep_ms(100)
client.publish("test/bme280/temperature/unit", "oC", retain=True, qos=0)
time.sleep_ms(1000)
while 1: while 1:
bme_data = bme.get_measurement() bme_data = bme.get_measurement()
print(bme_data) print(bme_data)
client.publish("{}/feeds/weather.interior-hum".format(config.ADAFRUIT_IO_USERNAME), "{:.0f}".format(bme_data["humidity"])) client.publish("test/bme280/humidity/value",
time.sleep_ms(2000) "{:.0f}".format(bme_data["humidity"]))
client.publish("{}/feeds/weather.interior-press".format(config.ADAFRUIT_IO_USERNAME), "{:.2f}".format(bme_data["pressure"]/100)) client.publish("test/bme280/pressure/value",
time.sleep_ms(2000) "{:.2f}".format(bme_data["pressure"]/100))
client.publish("{}/feeds/weather.interior-temp2".format(config.ADAFRUIT_IO_USERNAME), "{:.1f}".format(bme_data["temperature"])) client.publish("test/bme280/temperature/value",
time.sleep_ms(16000) "{:.1f}".format(bme_data["temperature"]))
time.sleep_ms(1000)