2828from machine import PWM
2929from machine import Timer
3030from machine import reset
31+ from machine import WDT
3132
3233import os
3334import sys
3435import _thread
3536import time
3637import socket
3738import struct
39+ import machine
40+ import binascii
3841
3942class PybytesProtocol :
4043 def __init__ (self , config , message_callback , pybytes_connection ):
@@ -83,12 +86,14 @@ def __start_recv_mqtt(self):
8386
8487 _thread .stack_size (self .__thread_stack_size )
8588 _thread .start_new_thread (self .__check_mqtt_message , ())
86- self .__connectionAlarm = Timer .Alarm (self .__keep_connection , 60 * 10 , periodic = True )
89+ self .__connectionAlarm = Timer .Alarm (self .__keep_connection , constants .__KEEP_ALIVE_PING_INTERVAL , periodic = True )
90+
91+ def __wifi_or_lte_connection (self ):
92+ return self .__pybytes_connection .__connection_status == constants .__CONNECTION_STATUS_CONNECTED_MQTT_WIFI or self .__pybytes_connection .__connection_status == constants .__CONNECTION_STATUS_CONNECTED_MQTT_LTE
8793
8894 def __check_mqtt_message (self ):
8995 print_debug (5 , "This is PybytesProtocol.__check_mqtt_message()" )
90- while (self .__pybytes_connection .__connection_status == constants .__CONNECTION_STATUS_CONNECTED_MQTT_WIFI
91- or self .__pybytes_connection .__connection_status == constants .__CONNECTION_STATUS_CONNECTED_MQTT_LTE ):
96+ while self .__wifi_or_lte_connection ():
9297 try :
9398 self .__pybytes_connection .__connection .check_msg ()
9499 time .sleep (self .__mqtt_check_interval )
@@ -99,8 +104,7 @@ def __check_mqtt_message(self):
99104
100105 def __keep_connection (self , alarm ):
101106 print_debug (5 , "This is PybytesProtocol.__keep_connection(alarm={})" .format (alarm ))
102- if (self .__pybytes_connection .__connection_status == constants .__CONNECTION_STATUS_CONNECTED_MQTT_WIFI
103- or self .__pybytes_connection .__connection_status == constants .__CONNECTION_STATUS_CONNECTED_MQTT_LTE ):
107+ if self .__wifi_or_lte_connection ():
104108 self .send_ping_message ()
105109
106110 def __check_lora_messages (self ):
@@ -132,6 +136,10 @@ def __process_recv_message(self, message):
132136 if (message_type == constants .__TYPE_PING ):
133137 self .send_ping_message ()
134138
139+ elif message_type == constants .__TYPE_PONG and self .__conf .get ('connection_watchdog' , True ):
140+ print_debug (1 ,'message type pong received, feeding watchdog...' )
141+ self .__pybytes_connection .__wifi_lte_watchdog .feed ()
142+
135143 elif (message_type == constants .__TYPE_INFO ):
136144 self .send_info_message ()
137145
@@ -331,9 +339,8 @@ def __send_message(self, message, topic=None):
331339 try :
332340 finalTopic = self .__mqtt_upload_topic if topic is None else self .__mqtt_upload_topic + "/" + topic
333341
334- print_debug (2 , "Sending message:[{}] with topic:[{}] and finalTopic: [{}]" .format (message , topic , finalTopic ))
335- if (self .__pybytes_connection .__connection_status == constants .__CONNECTION_STATUS_CONNECTED_MQTT_WIFI
336- or self .__pybytes_connection .__connection_status == constants .__CONNECTION_STATUS_CONNECTED_MQTT_LTE ):
342+ print_debug (2 , "Sending message:[{}] with topic:[{}] and finalTopic: [{}]" .format (binascii .hexlify (message ), topic , finalTopic ))
343+ if self .__wifi_or_lte_connection ():
337344 self .__pybytes_connection .__connection .publish (finalTopic , message )
338345 elif (self .__pybytes_connection .__connection_status == constants .__CONNECTION_STATUS_CONNECTED_LORA ):
339346 with self .__pybytes_connection .lora_lock :
@@ -390,14 +397,13 @@ def send_pybytes_digital_value(self, pin_number, pull_mode):
390397 if (not pin_number in self .__pins ):
391398 self .__configure_digital_pin (pin_number , Pin .IN , pull_mode )
392399 pin = self .__pins [pin_number ]
393- self .__send_pybytes_message ( constants . __COMMAND_DIGITAL_WRITE , pin_number , pin ())
400+ self .send_pybytes_custom_method_values ( pin_number , [ pin ()] )
394401
395402 def send_pybytes_analog_value (self , pin_number ):
396403 if (not pin_number in self .__pins ):
397404 self .__configure_analog_pin (pin_number )
398405 pin = self .__pins [pin_number ]
399-
400- self .__send_pybytes_message (constants .__COMMAND_ANALOG_WRITE , pin_number , pin ())
406+ self .send_pybytes_custom_method_values (pin_number , [pin ()])
401407
402408
403409 def send_pybytes_custom_method_values (self , method_id , parameters ):
0 commit comments