Skip to content

Commit 6e88b72

Browse files
committed
All example codes (5) have been refactored and adapted to new conception. Previously prepared modification connected to 'wait_msg' method has been cancelled because it doesn't work with all examples - therefore on GPy there is necessary to switch off TLS mode both for Wi-Fi and LTE. Examples have been checked on Python (Linux) and microPython boards (ESP32, ESP8266 and GPy: both on Wi-Fi and LTE).
1 parent 0cc7f5b commit 6e88b72

File tree

7 files changed

+81
-125
lines changed

7 files changed

+81
-125
lines changed

1_send_data.py

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

10+
# Create LiveObjects
1011
lo = LiveObjects.Connection()
1112

1213
MESSAGE_RATE = 5
1314

1415
# Main program
15-
lo.connect() # Connect to LiveObjects
16+
lo.connect() # Connect to LiveObjects
1617
last = uptime = time.time()
1718

1819
while True:
19-
if (time.time()) >= last + MESSAGE_RATE:
20-
lo.addToPayload("uptime", int(time.time() - uptime)) # Add value to payload: name - value
21-
lo.sendData() # Sending data to cloud
22-
last = time.time()
23-
lo.loop() # Check for incoming messages and if connection is still active
20+
if (time.time()) >= last + MESSAGE_RATE:
21+
lo.addToPayload("uptime", int(time.time() - uptime)) # Add value to payload: name - value
22+
lo.sendData() # Sending data to cloud
23+
last = time.time()
24+
lo.loop() # Check for incoming messages and if connection is still active

2_simple_parameters.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@
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 os
8-
import sys
97
import time
10-
118
import LiveObjects
129

13-
#Create LiveObjects with parameters: ClientID - Security - APIKEY
14-
lo = LiveObjects.Connection("PythonMQTT", LiveObjects.NONE, "<APIKEY>")
10+
# Create LiveObjects
11+
lo = LiveObjects.Connection()
12+
13+
# Main program
14+
# Available types: INT, BINARY, STRING, FLOAT
15+
lo.addParameter("messageRate", 5, LiveObjects.INT) # Add parameter: Name - Value - Type
16+
17+
lo.connect() # Connect to LiveObjects
18+
last = uptime = time.time()
1519

16-
#Main program
17-
lo.addParameter("messageRate", 5 , LiveObjects.INT) #Add parameter: Name - Value - Type
18-
#Available types: INT BINARY STRING FLOAT
19-
lo.connect() #Connect to LiveObjects
20-
last = time.time()
21-
uptime = time.time()
2220
while True:
23-
if time.time()>=last+lo.getParameter("messageRate"):#Get the parameter using its name
24-
lo.addToPayload("uptime", int(time.time() - uptime) ) #Add value to payload: name - value
25-
lo.sendData() #Sending data to cloud
26-
lo.loop() #Check for incoming messages and if connection is still active
27-
last = time.time()
21+
if time.time() >= last + lo.getParameter("messageRate"): # Get the parameter using its name
22+
lo.addToPayload("uptime", int(time.time() - uptime)) # Add value to payload: name - value
23+
lo.sendData() # Sending data to cloud
24+
last = time.time()
25+
lo.loop() # Check for incoming messages and if connection is still active

3_parameter_with_callback.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,29 @@
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 os
8-
import sys
9-
import time
107

8+
import time
119
import LiveObjects
1210

1311

14-
#Create LiveObjects with parameters: ClientID - Security - APIKEY
15-
lo = LiveObjects.Connection("PythonMQTT", LiveObjects.NONE, "<APIKEY>")
12+
# Create LiveObjects
13+
lo = LiveObjects.Connection()
1614

17-
#Callback for a parameter change
15+
16+
# Callback for a parameter change
1817
def callback(parameterName, newValue):
19-
print("Changed value of the parameter "+parameterName+" to " + str(newValue))
18+
print("Changed value of the parameter " + parameterName + " to " + str(newValue))
19+
2020

21+
# Main program
22+
# Available types: INT, BINARY, STRING, FLOAT
23+
lo.addParameter("messageRate", 5, LiveObjects.INT, callback) # Add parameter: Name - Value - Type - Callback
24+
lo.connect() # Connect to LiveObjects
25+
last = uptime = time.time()
2126

22-
#Main program
23-
lo.addParameter("messageRate", 5 , LiveObjects.INT, callback) #Add parameter: Name - Value - Type - Callback
24-
#Available types: INT BINARY STRING FLOAT
25-
lo.connect() #Connect to LiveObjects
26-
last = time.time()
27-
uptime = time.time()
2827
while True:
29-
if time.time()>=last+lo.getParameter("messageRate"):#Get the parameter using its name
30-
lo.addToPayload("uptime", int(time.time() - uptime) ) #Add value to payload: name - value
31-
lo.sendData() #Sending data to cloud
32-
lo.loop() #Check for incoming messages and if connection is still active
33-
last = time.time()
28+
if time.time() >= last + lo.getParameter("messageRate"): # Get the parameter using its name
29+
lo.addToPayload("uptime", int(time.time() - uptime)) # Add value to payload: name - value
30+
lo.sendData() # Sending data to cloud
31+
last = time.time()
32+
lo.loop() # Check for incoming messages and if connection is still active

4_simple_command.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,29 @@
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 os
8-
import sys
97
import time
10-
118
import LiveObjects
129

13-
#Create LiveObjects with parameters: ClientID - Security - APIKEY
14-
lo = LiveObjects.Connection("PythonMQTT", LiveObjects.NONE, "<APIKEY>")
10+
# Create LiveObjects
11+
lo = LiveObjects.Connection()
12+
13+
MESSAGE_RATE = 5
1514

16-
messageRate = 5
1715

18-
#Define command function
16+
# Define command function
1917
def foo(arg={}):
20-
lo.outputDebug(LiveObjects.INFO,"Called function foo")
18+
lo.outputDebug(LiveObjects.INFO, "Called function foo")
2119
return {}
2220

23-
#Main program
24-
lo.addCommand("foo",foo) #Add command to LiveObjects: name - function
25-
lo.connect() #Connect to LiveObjects
26-
last = time.time()
27-
uptime = time.time()
21+
22+
# Main program
23+
lo.addCommand("foo", foo) # Add command to LiveObjects: name - function
24+
lo.connect() # Connect to LiveObjects
25+
last = uptime = time.time()
26+
2827
while True:
29-
if time.time()>=last+messageRate:
30-
lo.addToPayload("uptime", int(time.time() - uptime) ) #Add value to payload: name - value
31-
lo.sendData() #Sending data to cloud
32-
lo.loop() #Check for incoming messages and if connection is still active
33-
last = time.time()
28+
if time.time() >= last + MESSAGE_RATE:
29+
lo.addToPayload("uptime", int(time.time() - uptime)) # Add value to payload: name - value
30+
lo.sendData() # Sending data to cloud
31+
last = time.time()
32+
lo.loop() # Check for incoming messages and if connection is still active

5_command_with_arguments.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@
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 os
8-
import sys
97
import time
108
import json
119
import LiveObjects
1210

13-
#Create LiveObjects with parameters: ClientID - Security - APIKEY
14-
lo = LiveObjects.Connection("PythonMQTT", LiveObjects.NONE, "<APIKEY>")
11+
# Create LiveObjects
12+
lo = LiveObjects.Connection()
1513

16-
messageRate = 5
14+
MESSAGE_RATE = 5
1715

18-
#Define command function with arguments handling
16+
17+
# Define command function with arguments handling
1918
def foo(args={}):
20-
lo.outputDebug(LiveObjects.INFO,"Called function foo with args", json.dumps(args))
21-
counter = 0
22-
for i in range(args["repetitions"]):
23-
print("Repetition nr "+str(i))
24-
counter+=1
25-
return { "Repeated" : str(counter)+" times"}
26-
27-
#Main program
28-
lo.addCommand("foo",foo) #Add command to LiveObjects: name - function
29-
lo.connect() #Connect to LiveObjects
30-
last = time.time()
31-
uptime = time.time()
19+
lo.outputDebug(LiveObjects.INFO, "Called function foo with args", json.dumps(args))
20+
counter = 0
21+
for i in range(args["repetitions"]):
22+
print("Repetition nr " + str(i))
23+
counter += 1
24+
return {"Repeated": str(counter) + " times"}
25+
26+
27+
# Main program
28+
lo.addCommand("foo", foo) # Add command to LiveObjects: name - function
29+
lo.connect() # Connect to LiveObjects
30+
last = uptime = time.time()
31+
3232
while True:
33-
if time.time()>=last+messageRate:
34-
lo.addToPayload("uptime", int(time.time() - uptime) ) #Add value to payload: name - value
35-
lo.sendData() #Sending data to cloud
36-
lo.loop() #Check for incoming messages and if connection is still active
37-
last = time.time()
33+
if time.time() >= last + MESSAGE_RATE:
34+
lo.addToPayload("uptime", int(time.time() - uptime)) # Add value to payload: name - value
35+
lo.sendData() # Sending data to cloud
36+
last = time.time()
37+
lo.loop() # Check for incoming messages and if connection is still active

LiveObjects/Connection.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -60,49 +60,8 @@ def __init__(self, debug=True):
6060
if self.mode == LiveObjects.BoardsInterface.PYTHON:
6161
self.__mqtt = paho.Client(self.__device_id)
6262
elif self.mode == LiveObjects.BoardsInterface.MICROPYTHON:
63-
64-
class MQTTClient2(MQTTClient):
65-
TIMEOUT = 1000 # [ms]
66-
67-
def wait_msg(self): # overriding original method wait_msg due to avoid infinite waiting
68-
import select
69-
poller = select.poll()
70-
poller.register(self.sock, select.POLLIN)
71-
res = poller.poll(MQTTClient2.TIMEOUT)
72-
if not res:
73-
res = None
74-
self.sock.setblocking(True)
75-
if res is None:
76-
return None
77-
if res == b"":
78-
raise OSError(-1)
79-
if res == b"\xd0": # PINGRESP
80-
sz = self.sock.read(1)[0]
81-
assert sz == 0
82-
return None
83-
op = res[0]
84-
if op & 0xf0 != 0x30:
85-
return op
86-
sz = self._recv_len()
87-
topic_len = self.sock.read(2)
88-
topic_len = (topic_len[0] << 8) | topic_len[1]
89-
topic = self.sock.read(topic_len)
90-
sz -= topic_len + 2
91-
if op & 6:
92-
pid = self.sock.read(2)
93-
pid = pid[0] << 8 | pid[1]
94-
sz -= 2
95-
msg = self.sock.read(sz)
96-
self.cb(topic, msg)
97-
if op & 6 == 2:
98-
pkt = bytearray(b"\x40\x02\0\0")
99-
struct.pack_into("!H", pkt, 2, pid)
100-
self.sock.write(pkt)
101-
elif op & 6 == 4:
102-
assert 0
103-
10463
self.ssl = self.__port == SSL
105-
self.__mqtt = MQTTClient2(self.__device_id, self.__server, self.__port, "json+device",
64+
self.__mqtt = MQTTClient(self.__device_id, self.__server, self.__port, "json+device",
10665
self.__apiKey, 0, self.ssl, {'server_hostname': self.__server})
10766

10867
def loop(self):

LiveObjects/hal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def __init__(self, net_type):
6363
self._lang_id = BoardsInterface.MICROPYTHON
6464
self._net_type = BoardsInterface.WIFI if net_type == BoardsInterface.DEFAULT_CARRIER else net_type
6565
self._carrier_capability = (BoardsInterface.WIFI, BoardsInterface.LTE)
66-
self._wifi_tls_capability = True
67-
self._lte_tls_capability = True
66+
self._wifi_tls_capability = False
67+
self._lte_tls_capability = False
6868
self._credentials = super().create_credentials(self._net_type)
6969

7070
def connect(self):

0 commit comments

Comments
 (0)