def is_wifi_ok(): if (not database.exists(StoreKey.ssid)) or (not database.exists( StoreKey.password)): print("wifi is no ok, return imediatly") return False else: ssid = database.get(StoreKey.ssid) password = database.get(StoreKey.password) sta_if = network.WLAN(network.STA_IF) if not sta_if.isconnected(): print('connecting to network...') sta_if.active(True) sta_if.connect(ssid, password) start_time = utime.ticks_ms() while not sta_if.isconnected(): now = utime.ticks_ms() diff = utime.ticks_diff(now, start_time) if diff > 10000: return False ifconfig = sta_if.ifconfig() print("""network config: IP address: {} subnet mask: {} gateway: {} DNS server: {}""".format(*ifconfig)) return True else: return True
def swich_page(page_name: str): text_area1.set_text("") if (page_name == PAGENAMES.set_device_name): label1.set_text("Please input your device name") # Set an initial text utility.show_widget(text_area1, True) utility.show_widget(button_matrix1, False) utility.show_widget(keyboard1, True) if (database.exists(StoreKey.device_name)): set_text_area_text(database.get(StoreKey.device_name)) elif (page_name == PAGENAMES.set_wifi_name): label1.set_text("Please input your WiFi name") # Set an initial text utility.show_widget(text_area1, True) utility.show_widget(button_matrix1, False) utility.show_widget(keyboard1, True) if (database.exists(StoreKey.ssid)): set_text_area_text(database.get(StoreKey.ssid)) elif (page_name == PAGENAMES.set_wifi_password): label1.set_text("Please input your WiFi password") # Set an initial text utility.show_widget(text_area1, True) utility.show_widget(button_matrix1, False) utility.show_widget(keyboard1, True) if (database.exists(StoreKey.password)): set_text_area_text(database.get(StoreKey.password)) elif (page_name == PAGENAMES.set_service_ip_address): label1.set_text("Please input your server IP address") # Set an initial text utility.show_widget(text_area1, True) utility.show_widget(button_matrix1, False) utility.show_widget(keyboard1, True) if (database.exists(StoreKey.service_ip)): set_text_area_text(database.get(StoreKey.service_ip)) elif (page_name == PAGENAMES.verify_password): label1.set_text("Please input your passwrod to open the door") # Set an initial text utility.show_widget(text_area1, True) utility.show_widget(button_matrix1, True) utility.show_widget(keyboard1, False) elif (page_name == PAGENAMES.only_label): label1.set_text("") # Set an initial text utility.show_widget(text_area1, False) utility.show_widget(button_matrix1, False) utility.show_widget(keyboard1, False) #swich_page(PAGENAMES.set_service_ip_address) #swich_page("set_wifi_password")
def update_info(): global GateName global CLIENT_ID global SERVICE_IP global PublishDict global SubscribeDict GateName = database.get(StoreKey.device_name) CLIENT_ID = "client/" + GateName SERVICE_IP = database.get(StoreKey.service_ip) PublishDict = { "gate_name": GateName, } SubscribeDict = { "doors_that_open": [], "password": "", } if database.exists("gate_password"): SubscribeDict["password"] = database.get("gate_password")
import utility import ui_page from logic_database import database, StoreKey import logic_wifi import logic_mqtt from machine import Timer if ui_page.TOUCH_READY: try: background_timer.deinit() except Exception as e: print(e) background_timer = Timer(-1) print(database.dict) if database.get(StoreKey.reset, default_value="true") == "true": ui_page.swich_page(ui_page.PAGENAMES.set_device_name) while (not ui_page.was_OK_button_pressed()): pass database.set(StoreKey.device_name, ui_page.get_text_area_text()) ui_page.cancel_OK_state() ui_page.swich_page(ui_page.PAGENAMES.set_wifi_name) while (not ui_page.was_OK_button_pressed()): pass database.set(StoreKey.ssid, ui_page.get_text_area_text()) ui_page.cancel_OK_state() ui_page.swich_page(ui_page.PAGENAMES.set_wifi_password) while (not ui_page.was_OK_button_pressed()): pass
utility.sleep(1) mqtt_subscribe() utility.sleep(1) mqtt_publish() try: MQTT_Timer.deinit() except Exception as e: print(e) MQTT_Timer.init(mode=Timer.PERIODIC, period=1000, callback=mqtt_timer_callback) print("start to listeing...") def restart_mqtt_timer(delay: int): global MQTT_Timer MQTT_Timer.deinit() utility.sleep(delay) MQTT_Timer.init(mode=Timer.PERIODIC, period=1000, callback=mqtt_timer_callback) if __name__ == "__main__": import logic_main status = utility.is_port_open(database.get("service_ip"), 1883) print(status)