-
Notifications
You must be signed in to change notification settings - Fork 11
/
IOTClientExample.py
63 lines (54 loc) · 1.89 KB
/
IOTClientExample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
Developed by Abhijith Boppe - linkedin.com/in/abhijith-boppe/
client example for raspberry
'''
from IOTSocket import IOTSocketClient as sock
import time
from clrprint import *
host = '127.0.0.1'
port = 9000
device_id = '1234567890'
device_key ='1432qrzd23'
certfile_path = "/user/cert.pem" # for key pinning (certificate pinning)
prev_call = 0
def someThingtoSend():
'''
this function is called recursively.
read data from sensor and return data
'''
global prev_call
time_now = time.time()
if (abs(time_now - prev_call) > 10) or prev_call == 0: # send sensor data every 10 seconds
example = 'temp=33.5&humid=40%'
prev_call = time_now
return example
else:
return ''
def handleCmdsFromServer(data):
'''
This function is called when ever there is
data/command from the server.
'''
clrprint(data,clr='b')
while 1: # reconnect if socket is closed
try:
clrprint(f"\nEstablishing socket connection to {host}:{port}",clr='y')
sock.connectionSet(host,port,device_id,device_key,Encrypt=False, cert_path= certfile_path) # set IOT Socket connection with valid Device ID and Key.
# Continiously check for receiving / tansmiting of data
clrprint(f"Connection established successfully",clr='g')
while 1:
data = someThingtoSend()
if data != '':
sock.sendData(data) # send data to server if data is available to send
rcv_data = sock.recvData() # receive data from server if available
if len(rcv_data) > 5:
handleCmdsFromServer(rcv_data) # handle your data here
except Exception as n:
clr = 'r' if "ERROR:" in str(n) else 'y'
clrprint(n,clr='r')
clrprint('closing socket',clr='y')
try:
sock.sock.close()
except:
pass
time.sleep(10)