def f_state_running(): """ ### ### ### ### # RUNNING: if the loco is registered to the # server, normal operation can start. # A recurring function is executed to keep the # connection alive. Received inputs are being # processed and eventually feedbacks to the # server is added to the transmission list. # It accesses the global variables so # include the following in your file: # from memory_context include * ### ### ### ### """ print("STATE RUNNING") if not wifi.is_linked(): mem.my_state = mem.States.WIFI_START # stop the timer if the tcp connection is broken # useless if a new connection is started everytime? """_, _, err_list = select( [], [], socket_list, timeout = 0 ) if err_list: my_com_timer.clear() my_state = States.WIFI_TCP_LINK """ # check incoming messages # do what has to be done f.f_run_read_msg() # after reading the messages updates # to be done once per cycle at least f.f_update_loco_status() # add eventually new message to send f.f_run_prepare_msg()
def is_connected(): """ Returns wether the device is connected to wifi or not. """ try: return wifi.is_linked() except Exception as e: print("Could not check for existing WIFI connection. Returning false.") return False
def f_state_wifi_configure(): """ ### ### ### ### # WIFI_CONFIGURE: if wifi connection settings # are unknown then try to connect to a # predifined network that allows the storage # of updated wifi connection params # it opens a server listening at 87246 # it expect a MessageConfig with the new # settings. If everything goes right then a # new config file is stored in flash memory # and the state machine is re-set to CONFIG # it accesses the global variables so # include the following in your file: # from memory_context include * ### ### ### ### """ print("STATE WIFI_CONFIGURE") # connect to the standard wifi to configure # wifi settings for retry in range(10): try: wifi.link(mem.my_config.wifi_net, wifi.WIFI_WPA2, mem.my_config.wifi_pwd) except Exception as e: print(e) if wifi.is_linked(): test_socket = socket.socket() test_socket.bind(('', WiDCCProtocol.SERVER_PORT)) test_socket.listen(1) test_client, _ = test_socket.accept() msg = '' while 1: data = test_client.recv(64) msg += data if not data: break test_client.close() test_socket.close() try: message = WiDCCProtocol.read_message(msg) if message.msg_type == WiDCCProtocol.MsgTypes.WIFI_CONFIG: #mem.my_config.loco_id = message.loco_id mem.my_config.wifi_net = message.wifi_net mem.my_config.wifi_pwd = message.wifi_pwd mem.my_config.save() mem.my_state = mem.States.CONFIG except: pass wifi.unlink() else: sleep(500)
def loop_failure(self): print("Loop Failure!") while True: try: if not wifi.is_linked(): print("WiFi was not linked. Relinking...") wifi.link(SSID, wifi.WIFI_WPA2, PASSWORD) print("Attempting to reconnect...") self.reconnect() break except Exception as e: pass sleep(5000) return mqtt.RECOVERED
def f_state_wifi_start(): """ ### ### ### ### # WIFI START: if wifi connection settings are # are known then try to connect to the network # it accesses the global variables so # include the following in your file: # from memory_context include * ### ### ### ### """ print("STATE WIFI_START") for retry in range(10): try: wifi.link(mem.my_config.wifi_net, mem.wifi.WIFI_WPA2, mem.my_config.wifi_pwd) except Exception as e: mem.my_state = mem.States.WIFI_CONFIGURE print(e) sleep(250) if wifi.is_linked(): mem.my_state = mem.States.WIFI_TCP_LINK
def f_state_wifi_tcp_link(): """ ### ### ### ### # WIFI_TCP_LINK: if wifi connection is up # and running, it opens a TCP connection and # tries to Login to the WiDCC server. # A MessageLogin is sent and login is considered # successful only if a MessageRegistered is # received. # It accesses the global variables so # include the following in your file: # from memory_context include * ### ### ### ### """ print("STATE WIFI_TCP_LINK") if not wifi.is_linked(): mem.my_state = mem.States.WIFI_START return try: mem.my_socket.connect(mem.my_config.server) # register our decoder to the server msg = WiDCCProtocol.create_message(mem.my_loco, WiDCCProtocol.MsgTypes.LOGIN ) f.f_tcp_send_msg(msg) # wait for feedback msg = f.f_tcp_feedback_msg() mem.my_socket.close() message = WiDCCProtocol.read_message(msg) if message.type == WiDCCProtocol.MsgTypes.REGISTERED: mem.my_com_timer.interval( WiDCCProtocol.MSG_PERIOD, f.f_tcp_communication) mem.my_com_timer_counter = 0 # if no error raised, move to running mem.my_state = mem.States.RUNNING except: print("error by tcp link")
# Establish a connection to the WolkAbout IoT Platform try: print("Connecting to WolkAbout IoT Platform") wolk.connect() print("Done") except Exception as e: print("Something went wrong while connecting: ", e) publish_period = 5000 wolk.publish_actuator_status("R") try: while True: if not wifi.is_linked(): wifi_connect() sleep(publish_period) print("Publishing sensor readings") temperature = SW01.get_temp() humidity = SW01.get_hum() pressure = SW01.get_press() print("T", temperature, "H", humidity, "P", pressure) wolk.add_sensor_reading("T", temperature) wolk.add_sensor_reading("H", humidity) wolk.add_sensor_reading("P", pressure) wolk.publish() except Exception as e: