Add refresh param in config file

Put functions def in separate file "main_lib"
This commit is contained in:
Pierrick C 2020-03-27 22:53:00 +01:00
parent 0c1d7ab026
commit 9b4cbd6883
4 changed files with 49 additions and 38 deletions

View File

@ -12,4 +12,6 @@ MQTT_PASSWD = "passwd"
WIFI_SSID = "my_wifi_ssid"
WIFI_PSK = "my_wifi_wpa_key"
REFRESH_RATE = 5
ALTITUDE = 150

BIN
code/lib/main_lib.mpy Normal file

Binary file not shown.

40
code/lib/main_lib.py Normal file
View File

@ -0,0 +1,40 @@
"""Divers fonctions utiles
"""
import time
import machine
import network
from umqtt.robust import MQTTClient
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)

View File

@ -1,32 +1,17 @@
"""Station météo connectée vers MQTT via wifi
"""
import time
import machine
import network
import esp
import bme280_i2c
import adafruit_max31865
from ntptime import settime
from umqtt.robust import MQTTClient
from main_lib import *
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")
# Connect to WLAN
wlan = WifiConnect(config.WIFI_SSID, config.WIFI_PSK)
# Create a micropython I2C object with the appropriate device pins
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
@ -59,21 +44,6 @@ max31865=adafruit_max31865.MAX31865(spi, max31865_cs)
rtc=machine.RTC()
settime()
def now():
"""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=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,
@ -81,7 +51,7 @@ client = MQTTClient(client_id=config.CLIENT_ID,
port=config.MQTT_PORT)
client.connect()
MqttPublish(client, "last_boot", now())
MqttPublish(client, "last_boot", now(rtc))
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)
@ -95,7 +65,6 @@ MqttPublish(client, "temperature2/unit", "degC", 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/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)
@ -116,7 +85,7 @@ topics = {"time" : ["time/last_values", "{}", 1, 1],
while 1:
bme_data = bme.get_measurement()
bme_data['time'] = now()
bme_data['time'] = now(rtc)
bme_data['pressure2'] = bme_data['pressure']/SMLP_coef
bme_data['temperature2'] = max31865.temperature
bme_data['wifi_rssi'] = wlan.status('rssi')
@ -129,4 +98,4 @@ while 1:
qos=param[3],
sleep=20)
time.sleep_ms(1000)
time.sleep(config.REFRESH_RATE)