def wifi_connect(wifi): global connected sta_if = network.WLAN(network.STA_IF) sta_if.active(True) oled.set_annunciator(common.ANNUNCIATOR_WIFI, "s", True) common.log("WiFi scan") aps = sta_if.scan() for ap in aps: # Note 1 for ssid in wifi: if ssid[0].encode() in ap: print(W + "Connecting: " + ssid[0]) oled.set_annunciator(common.ANNUNCIATOR_WIFI, "c", True) common.log("WiFi connecting:" + ssid[0]) sta_if.connect(ssid[0], ssid[1]) for retry in range(WIFI_CONNECTING_RETRY_LIMIT): if sta_if.isconnected(): print(W + "Connected: " + sta_if.ifconfig()[0]) common.log("WiFi connected: " + sta_if.ifconfig()[0]) connected = True wifi_configuration_update(wifi) break # for retry # print(W + "Waiting") sleep_ms(WIFI_CONNECTING_CLIENT_PERIOD) if sta_if.isconnected(): break # for ssid print(W + "Timeout: Bad password ?") common.log("Timeout: Bad password ?") if sta_if.isconnected(): break # for ap return sta_if
def on_upgrade_message(topic, payload_in): global file_count, manifest_checksum, manifest_size, manifest_url, version if payload_in.startswith("(upgrade "): tokens = payload_in[9:-1].split() if tokens[0] > common.AIKO_VERSION: version = tokens[0] manifest_url = tokens[1] manifest_checksum = tokens[2] manifest_size = int(tokens[3]) file_count = int(tokens[4]) common.annunicator_log_symbol = "F" oled.set_annunciator(common.ANNUNCIATOR_LOG, common.annunicator_log_symbol, True) common.log("Firmware upgrade available: " + version) return True
def wifi_configure(wifi): print(W + "WiFi configuration using SSID: " + WIFI_AP_SSID) common.log("Configure WiFi: " + WIFI_AP_SSID) ap_if = network.WLAN(network.AP_IF) ap_if.active(True) ap_if.config(essid=WIFI_AP_SSID) ap_if.config(max_clients=1) ip_address = ap_if.ifconfig()[0] aiko.net.set_status(led.yellow) oled.set_annunciator(common.ANNUNCIATOR_WIFI, "A", True) common.log("Try http://" + ip_address) ssid_password = web_server() ap_if.active(False) return ssid_password
def mqtt_thread(): global client while True: # print(M + "Wi-Fi connected check") if aiko.net.is_connected(): # print(M + "connect()") oled.set_annunciator(common.ANNUNCIATOR_MQTT, "c", True) connect() aiko.net.set_status(led.green) oled.set_annunciator(common.ANNUNCIATOR_MQTT, "M", True) while is_connected(): if client: # print(M + "poll()") # TODO: Refactor poller into own function ? poller = uselect.poll( ) # TODO: Create poller once per connection ? poller.register(client.sock, uselect.POLLIN) result = poller.poll(WAIT_MQTT_INCOMING_MESSAGE) if result: # print(M + "wait_msg()") try: client.wait_msg() except Exception: break # inner loop else: sleep_ms(WAIT_MQTT_CONNECTED_PERIOD) oled.set_annunciator(common.ANNUNCIATOR_MQTT, " ", True) disconnect("mqtt_thread") sleep_ms(WAIT_WIFI_CONNECTED_PERIOD)
def net_thread(): global wifi_configuration_updated wifi = configuration.net.wifi while True: set_status(led.red) oled.set_annunciator(common.ANNUNCIATOR_WIFI, " ", True) if len(wifi): print(W + "Checking WiFi configuration with available networks") for retry in range(WIFI_CONNECT_RETRY_LIMIT): sta_if = wifi_connect(wifi) if sta_if.isconnected(): break sleep_ms(WIFI_CONNECTED_CHECK_PERIOD) if sta_if.isconnected(): # TODO: Consolidate Wi-FI and MQTT status set_status(led.blue) while sta_if.isconnected(): oled.set_annunciator(common.ANNUNCIATOR_WIFI, "W", True) sleep_ms(WIFI_CONNECTED_CLIENT_PERIOD) wifi_disconnect(sta_if) set_status(led.red) oled.set_annunciator(common.ANNUNCIATOR_WIFI, " ", True) # TODO: If Wi-Fi disconnect, then retry Wi-Fi before going to Wi-Fi AP mode ssid_password = aiko.web_server.wifi_configure(wifi) if len(ssid_password[0]): wifi.insert(0, ssid_password) wifi_configuration_updated = True