-
-
Save fake-name/e09d4eb1f67fea721c5209af6b13804a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file is executed on every boot (including wake-boot from deepsleep) | |
#import esp | |
#esp.osdebug(None) | |
#import webrepl | |
#webrepl.start() | |
print("Booting....") | |
import network | |
import json | |
import machine | |
import time | |
import creds | |
nic = network.WLAN(network.STA_IF) | |
print("Enabling WiFi", nic.active(True)) | |
print("Availble networks:") | |
networks = nic.scan() | |
print("Connecting to ", creds, ) | |
ssids = [nwk[0] for nwk in networks] | |
if creds.network not in ssids: | |
print("Selected network not available?") | |
print("Available networks:", networks) | |
for line in ssids: | |
print(" '%s'" % (line, )) | |
print("Trying to connect to: '%s'" % (creds.network)) | |
time.sleep(10) | |
machine.reset() | |
nic.connect(creds.network, creds.password) | |
loops = 30 | |
while not nic.isconnected(): | |
if loops <= 0: | |
print("Failed to connect to WiFi. Rebooting in 10") | |
time.sleep(10) | |
machine.reset() | |
else: | |
print("Waiting for wifi %s...." % loops) | |
time.sleep(1) | |
loops -= 1 | |
print("Connected!") | |
try: | |
import ubinascii | |
except ImportError: | |
print("UBinAscii missing. Installing and rebooting") | |
import upip | |
upip.install("ubinascii") | |
print("Rebooting!") | |
time.sleep(2) | |
machine.reset() | |
try: | |
from umqtt.simple2 import MQTTClient | |
from umqtt.robust2 import MQTTClient | |
except ImportError: | |
print("micropython-umqtt.simple2 Library not installed. Installing and rebooting") | |
import upip | |
import os | |
upip.install("micropython-umqtt.simple2") | |
upip.install("micropython-umqtt.robust2") | |
# os.rename("/lib/umqtt", "/lib/umqtt") | |
print("Importing installed file") | |
from umqtt.robust2 import MQTTClient | |
from umqtt.simple2 import MQTTClient | |
print("Rebooting!") | |
time.sleep(2) | |
machine.reset() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment