def init_connect(self): self.esp.soft_reset() self.wifi = adafruit_espatcontrol_wifimanager.ESPAT_WiFiManager( self.esp, self.secrets) ### A few retries here seems to greatly improve reliability for _ in range(4): if self.debug: print("Connecting to WiFi...") try: self.wifi.connect() self.esp_version = self.esp.get_version() if self.debug: print("Connected!") break except (RuntimeError, TypeError, adafruit_espatcontrol.OKError) as ex: if self.debug: print("EXCEPTION: Failed to publish()", repr(ex)) time.sleep(RECONNECT_SLEEP) ### This uses global variables socket.set_interface(self.esp) ### MQTT Client ### pylint: disable=protected-access self.mqtt_client = MQTT.MQTT(broker="io.adafruit.com", username=self.secrets["aio_username"], password=self.secrets["aio_key"], socket_pool=socket, ssl_context=MQTT._FakeSSLContext( self.esp)) self.io = IO_MQTT(self.mqtt_client) ### Callbacks of interest on io are ### on_connect on_disconnect on_subscribe self.io.connect()
TX = board.ESP_RX resetpin = DigitalInOut(board.ESP_WIFI_EN) rtspin = DigitalInOut(board.ESP_CTS) uart = busio.UART(TX, RX, timeout=0.1) esp_boot = DigitalInOut(board.ESP_BOOT_MODE) esp_boot.direction = Direction.OUTPUT esp_boot.value = True status_light = None print("ESP AT commands") esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, reset_pin=resetpin, rts_pin=rtspin, debug=False) wifi = adafruit_espatcontrol_wifimanager.ESPAT_WiFiManager( esp, secrets, status_light) counter = 0 while True: try: print("Posting data...", end='') data = counter feed = 'test' payload = {'value': data} response = wifi.post("https://io.adafruit.com/api/v2/" + secrets['aio_username'] + "/feeds/" + feed + "/data", json=payload, headers={"X-AIO-KEY": secrets['aio_key']}) print(response.json())
except ImportError: print("WiFi secrets are kept in secrets.py, please add them there!") raise PIN_RX = board.GP1 PIN_TX = board.GP0 uart = busio.UART(PIN_TX, PIN_RX, baudrate=115200, receiver_buffer_size=2048) print("ESP AT commands") esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, reset_pin=None, rts_pin=None, debug=secrets["debugflag"]) wifi = adafruit_espatcontrol_wifimanager.ESPAT_WiFiManager(esp, secrets, None) # try to connect while True: try: print("connecting to AP...") wifi.connect() break except Exception as e: print("Failed:\n", e) continue # setup UDP while True: try: print("connecting to UDP-server %s:%d" %