Functions back in main.py + lint
This commit is contained in:
parent
9b4cbd6883
commit
1774f764b7
104
code/main.py
104
code/main.py
@ -1,15 +1,65 @@
|
|||||||
"""Station météo connectée vers MQTT via wifi
|
|
||||||
"""
|
"""
|
||||||
|
Station météo connectée vers MQTT via wifi.
|
||||||
|
|
||||||
|
A bit longer description.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
variable (type): description
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
type: description
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception: description
|
||||||
|
|
||||||
|
"""
|
||||||
|
import time
|
||||||
|
import machine
|
||||||
|
import network
|
||||||
import esp
|
import esp
|
||||||
import bme280_i2c
|
import bme280_i2c
|
||||||
import adafruit_max31865
|
import adafruit_max31865
|
||||||
from ntptime import settime
|
from ntptime import settime
|
||||||
from main_lib import *
|
from umqtt.robust import MQTTClient
|
||||||
import config
|
import config
|
||||||
|
|
||||||
|
|
||||||
|
def WifiConnect(ssid, psk):
|
||||||
|
"""Connect to the wifi with given credentials."""
|
||||||
|
wlan = network.WLAN(network.STA_IF)
|
||||||
|
wlan.active(True)
|
||||||
|
nets = wlan.scan()
|
||||||
|
for net in nets:
|
||||||
|
net_ssid = net[0].decode()
|
||||||
|
if net_ssid == ssid:
|
||||||
|
print('Network found!')
|
||||||
|
wlan.connect(net_ssid, 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")
|
||||||
|
return wlan
|
||||||
|
|
||||||
|
|
||||||
|
def now(rtc):
|
||||||
|
"""Return a string representing date/time now."""
|
||||||
|
return "{0:04d}/{1:02d}/{2:02d}_{4:02d}:{5:02d}:{6:02d}".format(
|
||||||
|
*rtc.datetime()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def MqttPublish(client, topic, message, retain=False, qos=0, sleep=10):
|
||||||
|
"""MQTT publish helper."""
|
||||||
|
client.publish("test/{device}/{topic}".format(device=client.client_id,
|
||||||
|
topic=topic),
|
||||||
|
message,
|
||||||
|
retain=retain,
|
||||||
|
qos=qos)
|
||||||
|
time.sleep_ms(sleep)
|
||||||
|
|
||||||
|
|
||||||
# Connect to WLAN
|
# Connect to WLAN
|
||||||
wlan = WifiConnect(config.WIFI_SSID, config.WIFI_PSK)
|
wlan = WifiConnect(config.WIFI_SSID, config.WIFI_PSK)
|
||||||
|
|
||||||
@ -26,7 +76,8 @@ bme.set_measurement_settings({
|
|||||||
'standby_time': bme280_i2c.BME280_STANDBY_TIME_500_US,
|
'standby_time': bme280_i2c.BME280_STANDBY_TIME_500_US,
|
||||||
'osr_h': bme280_i2c.BME280_OVERSAMPLING_1X,
|
'osr_h': bme280_i2c.BME280_OVERSAMPLING_1X,
|
||||||
'osr_p': bme280_i2c.BME280_OVERSAMPLING_16X,
|
'osr_p': bme280_i2c.BME280_OVERSAMPLING_16X,
|
||||||
'osr_t': bme280_i2c.BME280_OVERSAMPLING_2X})
|
'osr_t': bme280_i2c.BME280_OVERSAMPLING_2X
|
||||||
|
})
|
||||||
|
|
||||||
# Start the sensor automatically sensing
|
# Start the sensor automatically sensing
|
||||||
bme.set_power_mode(bme280_i2c.BME280_NORMAL_MODE)
|
bme.set_power_mode(bme280_i2c.BME280_NORMAL_MODE)
|
||||||
@ -35,8 +86,14 @@ bme.set_power_mode(bme280_i2c.BME280_NORMAL_MODE)
|
|||||||
SMLP_coef = pow(1.0-((0.0065*config.ALTITUDE)/288.15), 5.255)
|
SMLP_coef = pow(1.0-((0.0065*config.ALTITUDE)/288.15), 5.255)
|
||||||
|
|
||||||
# PT100 (via max81865 module) sensor setup
|
# PT100 (via max81865 module) sensor setup
|
||||||
spi=machine.SPI(-1, sck=machine.Pin(14, machine.Pin.OUT), mosi=machine.Pin(13, machine.Pin.OUT), miso=machine.Pin(12, machine.Pin.OUT))
|
|
||||||
spi.init(baudrate=115200, polarity=0, phase=1) #, firstbit=machine.SPI.MSB)
|
spi = machine.SPI(
|
||||||
|
-1,
|
||||||
|
sck=machine.Pin(14, machine.Pin.OUT),
|
||||||
|
mosi=machine.Pin(13, machine.Pin.OUT),
|
||||||
|
miso=machine.Pin(12, machine.Pin.OUT)
|
||||||
|
)
|
||||||
|
spi.init(baudrate=115200, polarity=0, phase=1)
|
||||||
max31865_cs = machine.Pin(2)
|
max31865_cs = machine.Pin(2)
|
||||||
max31865 = adafruit_max31865.MAX31865(spi, max31865_cs)
|
max31865 = adafruit_max31865.MAX31865(spi, max31865_cs)
|
||||||
|
|
||||||
@ -49,23 +106,32 @@ client = MQTTClient(client_id=config.CLIENT_ID,
|
|||||||
user=config.MQTT_USERNAME,
|
user=config.MQTT_USERNAME,
|
||||||
password=config.MQTT_PASSWD,
|
password=config.MQTT_PASSWD,
|
||||||
port=config.MQTT_PORT)
|
port=config.MQTT_PORT)
|
||||||
|
client.DEBUG = True
|
||||||
client.connect()
|
client.connect()
|
||||||
|
|
||||||
MqttPublish(client, "last_boot", now(rtc))
|
MqttPublish(client, "last_boot", now(rtc), retain=True, qos=1)
|
||||||
MqttPublish(client, "location", config.LOCATION, retain=True, qos=1)
|
MqttPublish(client, "location", config.LOCATION, retain=True, qos=1)
|
||||||
MqttPublish(client, "humidity/unit", "%", retain=True)
|
MqttPublish(client, "humidity/unit", "%", retain=True)
|
||||||
MqttPublish(client, "humidity/desc", "Capteur Bosch BME280", retain=True, qos=0)
|
MqttPublish(client, "humidity/desc",
|
||||||
|
"Capteur Bosch BME280", retain=True, qos=0)
|
||||||
MqttPublish(client, "pressure/unit", "hPa", 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, "pressure/desc",
|
||||||
|
"Capteur Bosch BME280", retain=True, qos=0)
|
||||||
MqttPublish(client, "pressure2/unit", "hPa", retain=True, qos=0)
|
MqttPublish(client, "pressure2/unit", "hPa", retain=True, qos=0)
|
||||||
MqttPublish(client, "pressure2/desc", "Capteur Bosch BME280 revenu au niveau de la mer", retain=True, qos=0)
|
MqttPublish(client, "pressure2/desc",
|
||||||
|
"Capteur Bosch BME280 revenu au niveau de la mer",
|
||||||
|
retain=True, qos=0)
|
||||||
MqttPublish(client, "temperature/unit", "degC", 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, "temperature/desc",
|
||||||
|
"Capteur Bosch BME280", retain=True, qos=0)
|
||||||
MqttPublish(client, "temperature2/unit", "degC", retain=True, qos=0)
|
MqttPublish(client, "temperature2/unit", "degC", retain=True, qos=0)
|
||||||
MqttPublish(client, "temperature2/desc", "Sonde PT100/MAX31865", retain=True, qos=0)
|
MqttPublish(client, "temperature2/desc",
|
||||||
|
"Sonde PT100/MAX31865", retain=True, qos=0)
|
||||||
MqttPublish(client, "wifi/ssid", config.WIFI_SSID, 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/ip", wlan.ifconfig()[0], retain=True, qos=1)
|
||||||
MqttPublish(client, "sys/esp8266_device_id", "{:d}".format(esp.flash_id()), retain=True, qos=0)
|
MqttPublish(client, "sys/esp8266_device_id",
|
||||||
|
"{:d}".format(esp.flash_id()),
|
||||||
|
retain=True, qos=1)
|
||||||
|
|
||||||
time.sleep_ms(1000)
|
time.sleep_ms(1000)
|
||||||
|
|
||||||
@ -75,12 +141,12 @@ time.sleep_ms(1000)
|
|||||||
# - factor (multiplier)
|
# - factor (multiplier)
|
||||||
# - qos
|
# - qos
|
||||||
topics = {"time": ["time/last_values", "{}", 1, 1],
|
topics = {"time": ["time/last_values", "{}", 1, 1],
|
||||||
"humidity" : ["humidity/value", "{:.0f}", 1, 1],
|
"humidity": ["humidity/value", "{:.0f}", 1, 0],
|
||||||
"pressure" : ["pressure/value", "{:.2f}", 0.01, 1],
|
"pressure": ["pressure/value", "{:.2f}", 0.01, 0],
|
||||||
"pressure2" : ["pressure2/value", "{:.2f}", 0.01, 1],
|
"pressure2": ["pressure2/value", "{:.2f}", 0.01, 0],
|
||||||
"temperature" : ["temperature/value", "{:.1f}", 1, 1],
|
"temperature": ["temperature/value", "{:.1f}", 1, 0],
|
||||||
"temperature2" : ["temperature/value2", "{:.1f}", 1, 1],
|
"temperature2": ["temperature/value2", "{:.1f}", 1, 0],
|
||||||
"wifi_rssi" : ["wifi/rssi", "{:.0f}", 1, 1],
|
"wifi_rssi": ["wifi/rssi", "{:.0f}", 1, 0],
|
||||||
}
|
}
|
||||||
|
|
||||||
while 1:
|
while 1:
|
||||||
|
Loading…
Reference in New Issue
Block a user