44# This software is distributed under the terms and conditions of the 'MIT'
55# license which can be found in the file 'LICENSE.md' in this package distribution
66
7- import time
7+ # import time
88import sys
99
1010import LiveObjects
@@ -68,6 +68,7 @@ def __init__(self, net_type):
6868 def connect (self ):
6969 super ().check_network_capabilities (self ._net_type )
7070 if self ._net_type == BoardsInterface .WIFI :
71+ from LiveObjects .services import pycom_wifi_connect
7172 pycom_wifi_connect (self ._credentials .get_creds ()['ssid' ], self ._credentials .get_creds ()['password' ],
7273 self ._hostname )
7374
@@ -76,6 +77,7 @@ def get_security_level(self):
7677 return LiveObjects .SSL if self ._wifi_tls_capability else LiveObjects .NONE
7778
7879 def get_client_id (self ):
80+ from LiveObjects .services import get_pycom_mac
7981 return self .get_lang_str () + 'MQTT_' + get_pycom_mac ()
8082
8183
@@ -92,9 +94,11 @@ def __init__(self, net_type):
9294 def connect (self ):
9395 super ().check_network_capabilities (self ._net_type )
9496 if self ._net_type == BoardsInterface .WIFI :
97+ from LiveObjects .services import pycom_wifi_connect
9598 pycom_wifi_connect (self ._credentials .get_creds ()['ssid' ], self ._credentials .get_creds ()['password' ],
9699 self ._hostname )
97100 elif self ._net_type == BoardsInterface .LTE :
101+ from LiveObjects .services import lte_connect
98102 lte_connect (self ._credentials .get_creds ()['pin' ])
99103
100104 def get_security_level (self ):
@@ -105,8 +109,10 @@ def get_security_level(self):
105109
106110 def get_client_id (self ):
107111 if self ._net_type == BoardsInterface .WIFI :
112+ from LiveObjects .services import get_pycom_mac
108113 return self .get_lang_str () + 'MQTT_' + get_pycom_mac ()
109114 elif self ._net_type == BoardsInterface .LTE :
115+ from LiveObjects .services import get_pycom_imei
110116 return self .get_lang_str () + 'MQTT_' + get_pycom_imei ()
111117
112118
@@ -119,13 +125,15 @@ def __init__(self, net_type):
119125 self ._credentials = super ().create_credentials (self ._net_type )
120126
121127 def connect (self ):
128+ from LiveObjects .services import wifi_connect
122129 super ().check_network_capabilities (self ._net_type )
123130 wifi_connect (self ._credentials .get_creds ()['ssid' ], self ._credentials .get_creds ()['password' ])
124131
125132 def get_security_level (self ):
126133 return LiveObjects .SSL if self ._wifi_tls_capability else LiveObjects .NONE
127134
128135 def get_client_id (self ):
136+ from LiveObjects .services import get_esp_mac
129137 return self .get_lang_str () + 'MQTT_' + get_esp_mac ()
130138
131139
@@ -138,6 +146,7 @@ def __init__(self, net_type):
138146 self ._credentials = super ().create_credentials (self ._net_type )
139147
140148 def connect (self ):
149+ from LiveObjects .services import use_existing_network_connection
141150 super ().check_network_capabilities (self ._net_type )
142151 use_existing_network_connection ()
143152
@@ -153,6 +162,7 @@ def get_store_cert_filename(self):
153162 sys .exit ()
154163
155164 def get_client_id (self ):
165+ from LiveObjects .services import get_mac
156166 return self .get_lang_str () + 'MQTT_' + get_mac ()
157167
158168
@@ -165,13 +175,15 @@ def __init__(self, net_type):
165175 self ._credentials = super ().create_credentials (self ._net_type )
166176
167177 def connect (self ):
178+ from LiveObjects .services import wifi_connect
168179 super ().check_network_capabilities (self ._net_type )
169180 wifi_connect (self ._credentials .get_creds ()['ssid' ], self ._credentials .get_creds ()['password' ])
170181
171182 def get_security_level (self ):
172183 return LiveObjects .SSL if self ._wifi_tls_capability else LiveObjects .NONE
173184
174185 def get_client_id (self ):
186+ from LiveObjects .services import get_esp_mac
175187 return self .get_lang_str () + 'MQTT_' + get_esp_mac ()
176188
177189
@@ -185,6 +197,7 @@ def __init__(self, net_type):
185197 self ._cert_store_filename = "/etc/ssl/certs/ca-certificates.crt"
186198
187199 def connect (self ):
200+ from LiveObjects .services import use_existing_network_connection
188201 super ().check_network_capabilities (self ._net_type )
189202 use_existing_network_connection ()
190203
@@ -195,6 +208,7 @@ def get_store_cert_filename(self):
195208 return self ._cert_store_filename
196209
197210 def get_client_id (self ):
211+ from LiveObjects .services import get_mac
198212 return self .get_lang_str () + 'MQTT_' + get_mac ()
199213
200214
@@ -205,95 +219,3 @@ def __new__(cls, net_type):
205219 sn = s [0 ].upper () + s [1 :] # capitalize first letter
206220 board = eval (sn )(net_type ) # instance of board w/ net type: WiFi, LTE, etc.
207221 return board
208-
209-
210- def use_existing_network_connection ():
211- print ('Using existing network connection' )
212-
213-
214- def get_mac ():
215- import uuid
216- return '' .join (['{:02x}' .format ((uuid .getnode () >> ele ) & 0xff ) for ele in range (0 , 8 * 6 , 8 )][::- 1 ]).upper ()
217-
218-
219- def wifi_connect (ssid , password ):
220- from network import WLAN , STA_IF
221-
222- sta_if = WLAN (STA_IF )
223- sta_if .active (True )
224- while not sta_if .isconnected ():
225- print ('Connecting to network...' )
226- sta_if .connect (ssid , password )
227- if sta_if .isconnected ():
228- break
229- time .sleep (2 )
230- print ('Network config:' , sta_if .ifconfig ())
231-
232-
233- def get_esp_mac ():
234- from network import WLAN
235- import binascii
236- return binascii .hexlify (WLAN ().config ('mac' )).decode ('ascii' ).upper ()
237-
238-
239- CONN_TIMEOUT = 20
240-
241-
242- def pycom_wifi_connect (ssid , password , hostname ):
243- from network import WLAN
244-
245- wlan = WLAN (mode = WLAN .STA )
246- wlan .hostname (hostname )
247- start_time = time .time ()
248- while 1 :
249- print ("Trying to connect..." )
250- wlan .connect (ssid = ssid , auth = (WLAN .WPA2 , password ))
251- time .sleep_ms (3000 )
252- if wlan .isconnected ():
253- print ("WiFi connected successfully" )
254- print ('IPs:' , wlan .ifconfig (), 'Channel:' , wlan .channel ())
255- break
256- elif time .time () - start_time > CONN_TIMEOUT :
257- print ("WiFi not connected. Stopped." )
258- break
259-
260-
261- def get_pycom_mac ():
262- from network import WLAN
263- import binascii
264- return binascii .hexlify (WLAN ().mac ()[0 ]).decode ('ascii' ).upper ()
265-
266-
267- def lte_connect (pin ):
268- from network import LTE
269-
270- def is_sim_waiting_for_pin ():
271- if lte .send_at_cmd ('AT+CPIN?' ).strip () == '+CPIN: SIM PIN\r \n \r \n OK' :
272- return True
273- else :
274- return False
275-
276- lte = LTE ()
277- time .sleep (2 )
278-
279- if is_sim_waiting_for_pin ():
280- print ("PIN" , (lte .send_at_cmd ('AT+CPIN="%s"' % pin )).strip ())
281- else :
282- print ("PIN PRESENT: OK" )
283-
284- lte .attach ()
285- print ("Attaching... " , end = '' )
286- while not lte .isattached ():
287- time .sleep (1 )
288- print ("attached!" )
289-
290- lte .connect ()
291- print ("Connecting... " , end = '' )
292- while not lte .isconnected ():
293- time .sleep (1 )
294- print ("connected!" )
295-
296-
297- def get_pycom_imei ():
298- from network import LTE
299- return LTE ().imei ()
0 commit comments