Using mqtt broker status messages as connection alive checker
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Using mqtt broker status messages as connection alive checker
I'm glad it's working
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 67
- Joined: Fri Sep 04, 2020 9:27 am
- Location: Hanko, Finland
- Contact:
Re: Using mqtt broker status messages as connection alive checker
I changed my boot.py so that it authenticates to WiFi AP having best signal strenght and found better solution to add ssid and password for your script:
Code: Select all
use_password = None
if network.WLAN(network.STA_IF).config('essid') == SSID1:
use_password = SSID1_PASSWORD
elif network.WLAN(network.STA_IF).config('essid') == SSID2:
use_password = SSID2_PASSWORD
config['server'] = MQTT_SERVER
config['ssid'] = network.WLAN(network.STA_IF).config('essid')
config['wifi_pw'] = use_password
Code: Select all
""" snip """
import utime
import machine
import network
import time
import ntptime
import webrepl
from time import sleep
# 0 = power on, 6 = hard reset, 1 = WDT reset, 5 = DEEP_SLEEP reset, 4 soft reset
print("Previous boot reason %s" % machine.reset_cause())
try:
from parametrit import SSID1, SSID2, SALASANA1, SALASANA2, WEBREPL_SALASANA, NTPPALVELIN, DHCP_NIMI
except ImportError as e:
print("Import error %s", e)
if (SSID1 is not None) and (SALASANA1 is not None):
SSID2 = None
SALASANA2 = None
WEBREPL_SALASANA = None
NTPPALVELIN = None
DHCP_NIMI = None
else:
print("Needs at least SSID1 and PASSWORD1!")
raise
def ei_voida_yhdistaa():
print("No connection. Boot in 1 s. ")
sleep(1)
machine.reset()
def aseta_aika():
try:
ntptime.settime()
except OSError as e:
print("NTP-server %s did not respond! Error %s" % (NTPPALVELIN, e))
ei_voida_yhdistaa()
print("Time: %s " % str(utime.localtime(utime.time())))
def kaynnista_webrepl():
if WEBREPL_SALASANA is not None:
try:
webrepl.start(password=WEBREPL_SALASANA)
except OSError:
pass
else:
try:
webrepl.start()
except OSError as e:
print("WebREPL does not start. Error %s" % e)
raise Exception("WebREPL is not installed! Execute import webrepl_setup")
# We are connected already. Previous reset was 1, 2 tai 4
if network.WLAN(network.STA_IF).config('essid') != '':
print("Connected to network %s" % network.WLAN(network.STA_IF).config('essid'))
print('IP-address:', network.WLAN(network.STA_IF).ifconfig()[0])
print("WiFi-signal strength %s" % (network.WLAN(network.STA_IF).status('rssi')))
aseta_aika()
kaynnista_webrepl()
else:
etsi_lista = []
ssid_lista = []
kaytettava_salasana = ""
kaytettava_ssid = ""
wificlient_if = network.WLAN(network.STA_IF)
wificlient_if.active(False)
time.sleep(1)
wificlient_if.active(True)
time.sleep(2)
if DHCP_NIMI is not None:
wificlient_if.config(dhcp_hostname=DHCP_NIMI)
if NTPPALVELIN is not None:
ntptime.host = NTPPALVELIN
try:
ssid_lista = wificlient_if.scan()
time.sleep(3)
except KeyboardInterrupt:
raise
except ssid_lista == []:
print("WiFi-networks not found!")
ei_voida_yhdistaa()
except OSError:
ei_voida_yhdistaa()
# Check if SSID1 or SSID2 are on the list
try:
etsi_lista = [item for item in ssid_lista if item[0].decode() == SSID1 or item[0].decode() == SSID2]
except ValueError:
# SSDI not found
pass
# If list is 2, then both found, check which one's signal stregth is best
if len(etsi_lista) == 2:
# third from the end is the rssi
if etsi_lista[0][-3] > etsi_lista[1][-3]:
kaytettava_ssid = etsi_lista[0][0].decode()
kaytettava_salasana = SALASANA1
else:
kaytettava_ssid = etsi_lista[1][0].decode()
kaytettava_salasana = SALASANA2
else:
# only one SSID in the list
kaytettava_ssid = etsi_lista[0][0].decode()
kaytettava_salasana = SALASANA1
# machine.freq(240000000)
print("Connecting to %s" % kaytettava_ssid)
try:
wificlient_if.connect(kaytettava_ssid, kaytettava_salasana)
time.sleep(5)
except wificlient_if.ifconfig()[0] == '0.0.0.0':
print("No IP address!")
ei_voida_yhdistaa()
except OSError:
ei_voida_yhdistaa()
finally:
if wificlient_if.ifconfig()[0] != '0.0.0.0':
aseta_aika()
kaynnista_webrepl()
print('IP address:', wificlient_if.ifconfig()[0])
print("WiFi signal strength %s" % (wificlient_if.status('rssi')))
else:
ei_voida_yhdistaa()