Skip to content

Commit 7305686

Browse files
committed
Implementation of selecting network carrier (WIFI, LTE).
1 parent c6deecb commit 7305686

File tree

3 files changed

+60
-29
lines changed

3 files changed

+60
-29
lines changed

1_send_data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import time
88
import LiveObjects
99

10-
board = LiveObjects.BoardsFactory()
10+
board = LiveObjects.BoardsFactory(net_type=LiveObjects.BoardsInterface.WIFI)
1111

1212
apikey = board.get_apikey()
1313
client_id = board.get_client_id()
@@ -16,7 +16,7 @@
1616
# Create LiveObjects with parameters: Board - ClientID - Security - APIKEY
1717
lo = LiveObjects.Connection(board, client_id, security_level, apikey)
1818

19-
messageRate = 5
19+
MESSAGE_RATE = 5
2020

2121
# Main program
2222
board.network_connect()
@@ -25,7 +25,7 @@
2525
uptime = time.time()
2626

2727
while True:
28-
if time.time() >= last+messageRate:
28+
if time.time() >= last + MESSAGE_RATE:
2929
lo.addToPayload("uptime", int(time.time() - uptime)) # Add value to payload: name - value
3030
lo.sendData() # Sending data to cloud
3131
lo.loop() # Check for incoming messages and if connection is still active

LiveObjects/credentials.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@
99
class Credentials:
1010

1111
def __init__(self, net_type):
12+
1213
self._apikey = <APIKEY>
14+
self._net_type = net_type
1315

1416
if net_type == LiveObjects.BoardsInterface.WIFI:
1517
self._wifi_ssid = <WIFI_SSID>
16-
self._wifi_password = <WIFI PASS>
18+
self._wifi_password = <WIFI_PASS>
1719
elif net_type == LiveObjects.BoardsInterface.LTE:
1820
self._pin = <PIN>
1921
self._apn = <APN_NAME>
2022

2123
def get_apikey(self):
2224
return self._apikey
2325

24-
def get_wifi_creds(self):
25-
return {'ssid': self._wifi_ssid, 'password': self._wifi_password}
26-
27-
def get_lte_creds(self):
28-
return {'pin': self._pin, 'apn_name': self._apn}
26+
def get_creds(self):
27+
if self._net_type == LiveObjects.BoardsInterface.WIFI:
28+
return {'ssid': self._wifi_ssid, 'password': self._wifi_password}
29+
elif self._net_type == LiveObjects.BoardsInterface.LTE:
30+
return {'pin': self._pin, 'apn_name': self._apn}

LiveObjects/hal.py

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
class BoardsInterface:
1414

15-
EXISTING = 1
16-
WIFI = 2
17-
LTE = 3
15+
EXISTING_NETWORK = 2
16+
WIFI = 3
17+
LTE = 4
1818

1919
@staticmethod
20-
def create_credentials(mode):
21-
return LiveObjects.Credentials(mode)
20+
def create_credentials(net_type):
21+
return LiveObjects.Credentials(net_type)
2222

2323
def get_apikey(self):
2424
return self._credentials.get_apikey()
@@ -35,7 +35,7 @@ def mqtt_lib_import_str(lang):
3535
return import_strings[lang]
3636

3737
def get_security_level(self):
38-
return LiveObjects.SSL if self._wifi_tls_capability else LiveObjects.NONE
38+
pass
3939

4040
def network_connect(self):
4141
pass
@@ -49,63 +49,91 @@ class LoPy(BoardsInterface):
4949

5050

5151
class GPy(BoardsInterface):
52-
def __init__(self):
52+
def __init__(self, net_type):
5353
self._lang = 'microPython'
54+
self._net_type = net_type
5455
self._wifi_tls_capability = True
55-
self._lte_tls_capability = True
56+
self._lte_tls_capability = False
5657
self._mqtt_lib = super().mqtt_lib_import_str(self._lang)
57-
self._credentials = super().create_credentials(BoardsInterface.WIFI)
58+
self._credentials = super().create_credentials(net_type)
5859

5960
def network_connect(self):
60-
pycom_wifi_connect(self._credentials.get_wifi_creds()['ssid'], self._credentials.get_wifi_creds()['password'])
61+
if self._net_type == BoardsInterface.WIFI:
62+
pycom_wifi_connect(self._credentials.get_creds()['ssid'], self._credentials.get_creds()['password'])
63+
elif self._net_type == BoardsInterface.LTE:
64+
lte_connect(self._credentials.get_creds()['pin'])
65+
66+
def get_security_level(self):
67+
if self._net_type == BoardsInterface.WIFI:
68+
return LiveObjects.SSL if self._wifi_tls_capability else LiveObjects.NONE
69+
elif self._net_type == BoardsInterface.LTE:
70+
return LiveObjects.SSL if self._lte_tls_capability else LiveObjects.NONE
6171

6272

6373
class Esp8266(BoardsInterface):
64-
def __init__(self):
74+
def __init__(self, net_type):
6575
self._lang = 'microPython'
76+
self._net_type = net_type
6677
self._wifi_tls_capability = False
6778
self._wifi_lte_capability = False
6879
self._mqtt_lib = super().mqtt_lib_import_str(self._lang)
6980
self._credentials = super().create_credentials(BoardsInterface.WIFI)
7081

7182
def network_connect(self):
72-
wifi_connect(self._credentials.get_wifi_creds()['ssid'], self._credentials.get_wifi_creds()['password'])
83+
if self._net_type == BoardsInterface.WIFI:
84+
wifi_connect(self._credentials.get_creds()['ssid'], self._credentials.get_creds()['password'])
85+
86+
def get_security_level(self):
87+
if self._net_type == BoardsInterface.WIFI:
88+
return LiveObjects.SSL if self._wifi_tls_capability else LiveObjects.NONE
7389

7490

7591
class Win32(BoardsInterface):
7692
pass
7793

7894

7995
class Esp32(BoardsInterface):
80-
def __init__(self):
96+
def __init__(self, net_type):
8197
self._lang = 'microPython'
98+
self._net_type = net_type
8299
self._wifi_tls_capability = True
83100
self._wifi_lte_capability = False
84101
self._mqtt_lib = super().mqtt_lib_import_str(self._lang)
85102
self._credentials = super().create_credentials(BoardsInterface.WIFI)
86103

87104
def network_connect(self):
88-
wifi_connect(self._credentials.get_wifi_creds()['ssid'], self._credentials.get_wifi_creds()['password'])
105+
if self._net_type == BoardsInterface.WIFI:
106+
wifi_connect(self._credentials.get_creds()['ssid'], self._credentials.get_creds()['password'])
107+
108+
def get_security_level(self):
109+
if self._net_type == BoardsInterface.WIFI:
110+
return LiveObjects.SSL if self._wifi_tls_capability else LiveObjects.NONE
89111

90112

91113
class Linux(BoardsInterface):
92-
def __init__(self):
114+
def __init__(self, net_type):
93115
self._lang = 'Python'
116+
self._net_type = net_type
94117
self._wifi_tls_capability = True
95118
self._wifi_lte_capability = False
96119
self._mqtt_lib = super().mqtt_lib_import_str(self._lang)
97-
self._credentials = super().create_credentials(BoardsInterface.EXISTING)
120+
self._credentials = super().create_credentials(self._net_type)
98121

99122
def network_connect(self):
100-
use_existing_network_connection()
123+
if self._net_type == BoardsInterface.EXISTING_NETWORK:
124+
use_existing_network_connection()
125+
126+
def get_security_level(self):
127+
if self._net_type == BoardsInterface.EXISTING_NETWORK:
128+
return LiveObjects.SSL if self._wifi_tls_capability else LiveObjects.NONE
101129

102130

103131
class BoardsFactory:
104132

105-
def __new__(cls):
133+
def __new__(cls, net_type):
106134
s = os.uname().sysname
107135
sn = s[0].upper() + s[1:] # capitalize first letter
108-
board = eval(sn)() # instance of board
136+
board = eval(sn)(net_type) # instance of board w/ net type: WiFi, LTE, etc.
109137
return board
110138

111139

@@ -130,6 +158,7 @@ def wifi_connect(ssid, password):
130158
CONN_TIMEOUT = 20
131159

132160

161+
# noinspection PyUnresolvedReferences
133162
def pycom_wifi_connect(ssid, password):
134163
from network import WLAN
135164

@@ -149,10 +178,10 @@ def pycom_wifi_connect(ssid, password):
149178
break
150179

151180

181+
# noinspection PyUnresolvedReferences
152182
def lte_connect(pin):
153183

154184
from network import LTE
155-
import socket
156185

157186
lte = LTE()
158187
time.sleep(2)

0 commit comments

Comments
 (0)