Compare commits
5 Commits
alternativ
...
main
Author | SHA1 | Date | |
---|---|---|---|
5d5c27d8d6 | |||
a45cccad06 | |||
faca6d7f7c | |||
6cb101108b | |||
482704896d |
10
config.py
10
config.py
@ -1,16 +1,18 @@
|
||||
import secrets
|
||||
|
||||
SSID = 'telemetry'
|
||||
SSID = 'gast1000'
|
||||
WPA_KEY = secrets.WPA_KEY
|
||||
|
||||
MQTT_SERVER = '172.16.10.47'
|
||||
MQTT_LOGIN = 'thermo-anna'
|
||||
MQTT_SERVER = 'broker.hottis.de'
|
||||
MQTT_LOGIN = secrets.MQTT_LOGIN
|
||||
MQTT_PASSWORD = secrets.MQTT_PASSWORD
|
||||
|
||||
LOCATION = secrets.LOCATION
|
||||
|
||||
HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat'
|
||||
WATCHDOG_TOPIC = b'IoT/Watchdog'
|
||||
PAYLOAD_TOPIC = b'IoT/rpipwThermometer/0/Payload'
|
||||
STARTUP_TOPIC = b'IoT/rpipwThermometer/0/Startup'
|
||||
|
||||
MAX_CONNECT_RETRY_CNT = 25
|
||||
MAIN_PERIOD_MS = 60000
|
||||
MAIN_PERIOD_S = 60
|
128
main.py
128
main.py
@ -1,36 +1,32 @@
|
||||
# main.py -- put your code here!
|
||||
|
||||
from umqttsimple import MQTTClient, MQTTException
|
||||
import machine
|
||||
from umqttsimple import MQTTClient
|
||||
import time
|
||||
import ubinascii
|
||||
from machine import WDT
|
||||
from machine import Pin, WDT
|
||||
from onewire import OneWire
|
||||
from ds18x20 import DS18X20
|
||||
import network
|
||||
import os
|
||||
import json
|
||||
import config
|
||||
|
||||
last_message = 0
|
||||
message_interval = 5
|
||||
counter = 0
|
||||
ringFlag = False
|
||||
wdt = None
|
||||
wdtCnt = 0
|
||||
client = None
|
||||
wlan = None
|
||||
|
||||
SENSOR_PIN = 16
|
||||
|
||||
|
||||
def init():
|
||||
global wdt
|
||||
wdt = WDT(timeout=8000)
|
||||
wdt.feed()
|
||||
print("Watchdog enabled")
|
||||
print("Hello")
|
||||
resetCause = machine.reset_cause()
|
||||
|
||||
global wlan
|
||||
wdt = WDT(timeout=8000)
|
||||
wdt.feed()
|
||||
print("Watchdog enabled")
|
||||
|
||||
try:
|
||||
wlan = network.WLAN(network.STA_IF)
|
||||
wlan.active(True)
|
||||
wlan.connect(config.SSID, config.WPA_KEY)
|
||||
|
||||
connectCnt = 0
|
||||
while (not wlan.isconnected() or connectCnt > config.MAX_CONNECT_RETRY_CNT):
|
||||
while (not wlan.isconnected() or (connectCnt > config.MAX_CONNECT_RETRY_CNT)):
|
||||
print("Not yet connected")
|
||||
wdt.feed()
|
||||
connectCnt += 1
|
||||
@ -40,64 +36,52 @@ def init():
|
||||
print("Failed to connect to wlan, restarting")
|
||||
machine.reset()
|
||||
|
||||
print(f"Connected to WLAN: {machine.reset_cause()} {connectCnt} {wlan.ifconfig()}")
|
||||
|
||||
global client
|
||||
clientId = ubinascii.hexlify(bytearray(os.urandom(16))).decode()
|
||||
client = MQTTClient(clientId, config.MQTT_SERVER, keepalive=60,
|
||||
ssl=True, user=config.MQTT_LOGIN, password=config.MQTT_PASSWORD)
|
||||
client.set_callback(mqtt_callback)
|
||||
client.connect()
|
||||
print(f"Connected to MQTT broker {config.MQTT_SERVER} ")
|
||||
client.subscribe(config.WATCHDOG_TOPIC)
|
||||
print(f"Subscribed to {config.WATCHDOG_TOPIC}")
|
||||
client.publish(config.STARTUP_TOPIC, f"Hello, rpipw thermometer is alive now at {wlan.ifconfig()}")
|
||||
|
||||
|
||||
def mqtt_callback(topic, msg):
|
||||
global wdt, wdtCnt
|
||||
# print(f"Received: {topic}: {msg}")
|
||||
if topic == config.WATCHDOG_TOPIC:
|
||||
wdtCnt += 1
|
||||
# print(f"feed watchdog {wdtCnt}")
|
||||
print(f"Connected to WLAN: {resetCause} {connectCnt} {wlan.ifconfig()}")
|
||||
wdt.feed()
|
||||
|
||||
def main():
|
||||
global client
|
||||
print("Hello")
|
||||
time.sleep(1)
|
||||
|
||||
try:
|
||||
init()
|
||||
except Exception as e:
|
||||
except Exception as e:
|
||||
print(f"Some error when starting: {e.__class__.__name__}, {e}")
|
||||
time.sleep(15)
|
||||
print("Restarting")
|
||||
time.sleep(5)
|
||||
machine.reset()
|
||||
|
||||
last_time_mqtt_loop = time.ticks_ms()
|
||||
last_time_1_second = time.ticks_ms()
|
||||
last_time_main_period = time.ticks_ms()
|
||||
payload_cnt = 0
|
||||
while True:
|
||||
current_time = time.ticks_ms()
|
||||
|
||||
if (time.ticks_diff(current_time, last_time_mqtt_loop) > 250):
|
||||
last_time_mqtt_loop = current_time
|
||||
client.ping()
|
||||
client.wait_msg()
|
||||
|
||||
if (time.ticks_diff(current_time, last_time_1_second) > 1000):
|
||||
last_time_1_second = current_time
|
||||
client.publish(config.HEARTBEAT_TOPIC, f"still alive {current_time}")
|
||||
|
||||
if (time.ticks_diff(current_time, last_time_main_period) > config.MAIN_PERIOD_MS):
|
||||
last_time_main_period = current_time
|
||||
client.publish(config.PAYLOAD_TOPIC, f"measurement {payload_cnt}")
|
||||
payload_cnt += 1
|
||||
print("Payload sent")
|
||||
while True:
|
||||
try:
|
||||
clientId = f"rpipwtherm-{ubinascii.hexlify(machine.unique_id())}"
|
||||
client = MQTTClient(clientId, config.MQTT_SERVER, keepalive=60,
|
||||
ssl=True, user=config.MQTT_LOGIN, password=config.MQTT_PASSWORD)
|
||||
client.connect()
|
||||
print(f"Connected to MQTT broker {config.MQTT_SERVER} ")
|
||||
wdt.feed()
|
||||
|
||||
# client.publish(config.STARTUP_TOPIC, f"Hello, rpipw thermometer is alive now at {wlan.ifconfig()}")
|
||||
sensor = DS18X20(OneWire(Pin(SENSOR_PIN, Pin.IN, Pin.PULL_UP)))
|
||||
devices = sensor.scan()
|
||||
sensor.convert_temp()
|
||||
time.sleep(1)
|
||||
for idx, device in enumerate(devices):
|
||||
t = sensor.read_temp(device)
|
||||
msg = {
|
||||
"sensor" : idx,
|
||||
"location": config.LOCATION,
|
||||
"temperature": t
|
||||
}
|
||||
print(f"Sensor: {idx}, {json.dumps(msg)}")
|
||||
client.publish(config.PAYLOAD_TOPIC, json.dumps(msg))
|
||||
print(f"Payload {idx} sent")
|
||||
wdt.feed()
|
||||
|
||||
client.disconnect()
|
||||
print("Disconnected from broker")
|
||||
|
||||
for w in range(config.MAIN_PERIOD_S):
|
||||
print(f"Waiting {w}...")
|
||||
wdt.feed()
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
print(f"Error in main loop: {e.__class__.__name__}, {e}")
|
||||
print("Restarting")
|
||||
time.sleep(5)
|
||||
machine.reset()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
6
secrets.py-example
Normal file
6
secrets.py-example
Normal file
@ -0,0 +1,6 @@
|
||||
WPA_KEY = 'geheim'
|
||||
|
||||
MQTT_LOGIN = 'testuser'
|
||||
MQTT_PASSWORD = 'geheim'
|
||||
|
||||
LOCATION = "somewhere"
|
Loading…
x
Reference in New Issue
Block a user