8000 MicroPython 1.20.0.rc11.1 & Pybytes 0.9.12 · afcarl/pycom-micropython-sigfox@19bd9e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 19bd9e7

Browse files
committed
MicroPython 1.20.0.rc11.1 & Pybytes 0.9.12
1 parent d78dcb2 commit 19bd9e7

8 files changed

+57
-28
lines changed

esp32/frozen/Pybytes/_mqtt_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def connect(self, clean_session=True):
7878
self.sock = ussl.wrap_socket(self.sock, **self.ssl_params)
7979
print("Using MQTT over TLS")
8080
else:
81-
print("WARNING: consider enabling TLS by using \"ssl\": true in pybytes_config.json")
81+
print("WARNING: consider enabling TLS by using pybytes.enable_ssl()")
8282
premsg = bytearray(b"\x10\0\0\0\0\0")
8383
msg = bytearray(b"\x04MQTT\x04\x02\0\0")
8484

esp32/frozen/Pybytes/_pybytes.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import os
22
from machine import Timer
3-
try:
4-
from pybytes_protocol import PybytesProtocol
5-
except:
6-
from _pybytes_protocol import PybytesProtocol
73
try:
84
from pybytes_connection import PybytesConnection
95
except:
@@ -222,14 +218,14 @@ def get_config(self, key=None):
222218
else:
223219
return self.__conf.get(key)
224220

225-
def set_config(self, key=None, value=None, permanent=True):
221+
def set_config(self, key=None, value=None, permanent=True, silent=False):
226222
if key is None and value is not None:
227223
self.__conf = value
228224
elif key is not None:
229225
self.__conf[key] = value
230226
else:
231227
raise ValueError('You need to either specify a key or a value!')
232-
if permanent: self.write_config()
228+
if permanent: self.write_config(silent=silent)
233229

234230
def read_config(self, file='/flash/pybytes_config.json'):
235231
try:
@@ -259,7 +255,8 @@ def enable_ssl(self, ca_file='/flash/cert/pycom-ca.pem', dump_ca = True):
259255
self.set_config('dump_ca', dump_ca, permanent=False)
260256
if ca_file is not None:
261257
self.set_config('ssl_params', {'ca_certs': ca_file}, permanent=False)
262-
self.set_config('ssl', True)
258+
self.set_config('ssl', True, silent=True)
259+
print('Please reset your module to apply the new settings.')
263260

264261
def dump_ca(self, ca_file='/flash/cert/pycom-ca.pem'):
265262
try:

esp32/frozen/Pybytes/_pybytes_config.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,29 @@ def read_config(self, file='/flash/pybytes_config.json'):
108108
band = None
109109
apn = None
110110
reset = False
111+
type = None
112+
113+
if len(extra_preferences[index + 1]) > 0 and extra_preferences[index + 1].lower() != 'none':
114+
carrier = extra_preferences[index + 1]
111115
if len(extra_preferences[index + 2]) > 0 and extra_preferences[index + 2].isdigit():
112116
cid = int(extra_preferences[index + 2])
113117
if len(extra_preferences[index + 3]) > 0 and extra_preferences[index + 3].isdigit():
114118
band = int(extra_preferences[index + 3])
115-
if len(extra_preferences[index + 1]) > 0 and extra_preferences[index + 1].lower() != 'none':
116-
carrier = extra_preferences[index + 1]
117119
if len(extra_preferences[index + 4]) > 0 and extra_preferences[index + 4].lower() != 'none':
118120
apn = extra_preferences[index + 4]
119121
if len(extra_preferences[index + 5]) > 0 and extra_preferences[index + 5].lower() == 'true':
120122
reset = True
123+
if len(extra_preferences[index + 6]) > 0 and extra_preferences[index + 6].lower() != 'none':
124+
type = extra_preferences[index + 6]
125+
121126

122127
lte_config = { 'lte':
123128
{ 'carrier': carrier,
124129
'cid': cid,
125130
'band': band,
126131
'apn': apn,
127-
'reset': reset
132+
'reset': reset,
133+
'type': type
128134
}
129135
}
130136
except:

esp32/frozen/Pybytes/_pybytes_connection.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import socket
2222
import struct
2323
import binascii
24+
from machine import WDT
2425

2526
class PybytesConnection:
2627
def __init__(self, config, message_callback):
@@ -42,7 +43,7 @@ def __init__(self, config, message_callback):
4243
self.lte = None
4344
self.wlan = None
4445
self.__network_type = None
45-
46+
self.__wifi_lte_watchdog = None
4647

4748
def lte_ping_routine(self, delay):
4849
while True:
@@ -56,8 +57,17 @@ def print_pretty_response(self, rsp):
5657
if line not in ['OK']:
5758
pri 8000 nt(line)
5859

60+
def __initialise_watchdog(self):
61+
if self.__conf.get('connection_watchdog', True):
62+
self.__wifi_lte_watchdog = WDT(timeout=constants.__WDT_TIMEOUT_MILLISECONDS)
63+
print('Initialized watchdog for WiFi and LTE connection with timeout {} ms'.format(constants.__WDT_TIMEOUT_MILLISECONDS))
64+
else:
65+
print('Watchdog for WiFi and LTE was disabled, enable with "connection_watchdog": true in pybytes_config.json')
66+
67+
# Establish a connection through WIFI before connecting to mqtt server
5968
def connect_wifi(self, reconnect=True, check_interval=0.5):
60-
"""Establish a connection through WIFI before connecting to mqtt server"""
69+
self.__initialise_watchdog()
70+
6171
if self.__connection_status != constants.__CONNECTION_STATUS_DISCONNECTED:
6272
print("Error connect_wifi: Connection already exists. Disconnect First")
6373
return False
@@ -110,8 +120,10 @@ def connect_wifi(self, reconnect=True, check_interval=0.5):
110120
print("Exception connect_wifi: {}".format(ex))
111121
return False
112122

123+
# Establish a connection through LTE before connecting to mqtt server
113124
def connect_lte(self, reconnect=True, check_interval=0.5):
114-
"""Establish a connection through LTE before connecting to mqtt server"""
125+
self.__initialise_watchdog()
126+
115127
lte_cfg = self.__conf.get('lte')
116128
if lte_cfg is not None:
117129
if (os.uname()[0] not in ['FiPy', 'GPy']):

esp32/frozen/Pybytes/_pybytes_constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class constants:
2525
__TYPE_BATTERY_INFO = 0x04
2626
__TYPE_OTA = 0x05
2727
__TYPE_FCOTA = 0x06
28+
__TYPE_PONG = 0x07
2829
__TYPE_PYBYTES = 0x0E
2930
__PYBYTES_PROTOCOL = ">B%ds"
3031
__PYBYTES_PROTOCOL_PING = ">B"
@@ -65,3 +66,7 @@ class constants:
6566
__SIGFOX_WARNING = """WARNING! Your sigfox radio configuration (RC) is currently using the default (1)
6667
You can set your RC with command (ex: RC 3): pybytes.set_config('sigfox', {'RCZ': 3})
6768
See all available zone options for RC at https://support.sigfox.com/docs/radio-configuration """
69+
70+
__KEEP_ALIVE_PING_INTERVAL = 600 # in seconds
71+
# set watch dog timeout one minute after __KEEP_ALIVE_PING_INTERVAL
72+
__WDT_TIMEOUT_MILLISECONDS = (__KEEP_ALIVE_PING_INTERVAL + 60) * 1000

esp32/frozen/Pybytes/_pybytes_library.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import re
1414
from network import WLAN
1515
from time import sleep
16+
import binascii
1617

1718
try:
1819
from network import LoRa
@@ -29,10 +30,12 @@ def pack_user_message(self, message_type, body):
2930
return self.__pack_message(message_type, body)
3031

3132
def pack_pybytes_message(self, command, pin, value):
33+
print_debug(5, "This is pack_pybytes_message({}, {}, {})".format(command, pin, value))
3234
body = struct.pack(constants.__PYBYTES_INTERNAL_PROTOCOL, command, pin, value)
3335
return self.__pack_message(constants.__TYPE_PYBYTES, body)
3436

3537
def pack_pybytes_message_variable(self, command, pin, parameters):
38+
print_debug(5, "This is pack_pybytes_message_variable({}, {}, {})".format(command, pin, parameters))
3639
body = struct.pack(constants.__PYBYTES_INTERNAL_PROTOCOL_VARIABLE % len(parameters),
3740
command, pin, parameters)
3841
return self.__pack_message(constants.__TYPE_PYBYTES, body)
@@ -152,7 +155,7 @@ def __pack_message(self, message_type, body):
152155
header = header | (message_type & constants.__TYPE_MASK)
153156

154157
if body is not None:
155-
print_debug(3, '__pack_message: %s' % struct.pack(constants.__PYBYTES_PROTOCOL % len(body), header, body))
158+
print_debug(3, '__pack_message: %s' % binascii.hexlify(struct.pack(constants.__PYBYTES_PROTOCOL % len(body), header, body)))
156159
return struct.pack(constants.__PYBYTES_PROTOCOL % len(body), header, body)
157160
return struct.pack(constants.__PYBYTES_PROTOCOL_PING, header)
158161

esp32/frozen/Pybytes/_pybytes_protocol.py

Lines changed: 17 additions & 11 deletions
< 8000 /tr>
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@
2828
from machine import PWM
2929
from machine import Timer
3030
from machine import reset
31+
from machine import WDT
3132

3233
import os
3334
import sys
3435
import _thread
3536
import time
3637
import socket
3738
import struct
39+
import machine
40+
import binascii
3841

3942
class 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):

esp32/pycom_version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
#ifndef VERSION_H_
1111
#define VERSION_H_
1212

13-
#define SW_VERSION_NUMBER "1.20.0.rc11"
13+
#define SW_VERSION_NUMBER "1.20.0.rc11.1"
1414

1515
#define LORAWAN_VERSION_NUMBER "1.0.2"
1616

1717
#define SIGFOX_VERSION_NUMBER "1.0.1"
1818

1919
#if (VARIANT == PYBYTES)
20-
#define PYBYTES_VERSION_NUMBER "0.9.7"
20+
#define PYBYTES_VERSION_NUMBER "0.9.12"
2121
#endif
2222

2323
#endif /* VERSION_H_ */

0 commit comments

Comments
 (0)
0