def __connect(cs_pin, ready_pin, reset_pin, secrets) -> ESPSPI_WiFiManager: logger = logging.getLogger("log") spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, cs_pin, ready_pin, reset_pin) wifi = ESPSPI_WiFiManager(esp, secrets, attempts=5) MQTT.set_socket(socket, esp) logger.debug("MAC addr: " + ", ".join([hex(i) for i in esp.MAC_address])) logger.debug("Connecting to AP...") wifi.connect() logger.info("Connected to " + str(esp.ssid, "utf-8") + "\tRSSI: " + str(esp.rssi)) logger.debug("My IP address is " + esp.pretty_ip(esp.ip_address)) logger.debug("Setting time") ntp = NTP(esp) while not ntp.valid_time: ntp.set_time() logger.debug("Failed to obtain time, retrying in 1 second...") time.sleep(1) logger.info("Time: " + str(time.time())) return wifi
def __connect(self, spi, cs, ready, reset, log): esp = adafruit_esp32spi.ESP_SPIcontrol(spi, cs, ready, reset) requests.set_socket(socket, esp) if log: print("MAC addr:", [hex(i) for i in esp.MAC_address]) print("Connecting to AP...") while not esp.is_connected: try: esp.connect_AP(secrets['ssid'], secrets['password']) except RuntimeError as e: if log: print("could not connect to AP, retrying: ",e) continue if log: print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi) print("My IP address is", esp.pretty_ip(esp.ip_address)) self.__wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets) ntp = NTP(self.__esp) while not ntp.valid_time: ntp.set_time() print("Failed to obtain time, retrying in 1 seconds...") time.sleep(1) print("Time:", time.time())
def get_time(): try: ntp = NTP(esp) # Wait for a valid time to be received while not ntp.valid_time: print("Setting time..", end='') ntp.set_time() time.sleep(1) current_time = time.time() print("Got time:", current_time) if current_time < 1619250585: print('Time sanity check failed, rebooting') data_display_label.text = "TimeERR" time.sleep(10) supervisor.reload() else: return (current_time) except Exception as e: print("Unable to get time, restarting!!!", e) #network_check() supervisor.reload( ) # Restart the program if unable to get time, hack to work around intermittent "repeated socket failures" errors
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) print("Connecting to WiFi...") wifi.connect() print("Connected to WiFi!") print("Getting the time...") ntp = NTP(esp) # Wait for a valid time to be received while not ntp.valid_time: time.sleep(5) ntp.set_time() print("Time:", str(time.time())) # To use Azure IoT Central, you will need to create an IoT Central app. # You can either create a free tier app that will live for 7 days without an Azure subscription, # Or a standard tier app that will last for ever with an Azure subscription. # The standard tiers are free for up to 2 devices # # If you don't have an Azure subscription: # # If you are a student, head to https://aka.ms/FreeStudentAzure and sign up, validating with your # student email address. This will give you $100 of Azure credit and free tiers of a load of # service, renewable each year you are a student # # If you are not a student, head to https://aka.ms/FreeAz and sign up to get $200 of credit for 30
if choice == 0: esp.connect_AP(secrets["ssid"], secrets["password"]) elif choice == 1: esp.connect_AP(secrets["ssid2"], secrets["password2"]) except RuntimeError as e: print("could not connect to AP, retrying: ", e) choice += 1 if choice >= 2: choice = 0 continue print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) # Get NTP time, may make several attempts ntp = NTP(esp) ntp.set_time() while not ntp.valid_time: print("time not valid...") time.sleep(5) ntp.set_time(60 * 60) # Get the RTC time, not NTP updates RTC silently rtc = RTC() print("{:02}/{:02}/{:04} {:02}:{:02}".format(rtc.datetime.tm_mday, rtc.datetime.tm_mon, rtc.datetime.tm_year, rtc.datetime.tm_hour, rtc.datetime.tm_min)) # Use the OpenWeather API # london,gb is the location and can be changed appropriately