-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblynk.py
54 lines (45 loc) · 1.5 KB
/
blynk.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
# (c) [email protected]
# Blynk access via REST API
class blynk:
import urequests
AUTH_TOKEN = ''
initialise = False
def _parse_in(text):
out = str(text)
out = out.replace('[','')
out = out.replace(']','')
out = out.replace('"','')
return out
def _parse_out(text):
out = str(text)
out = out.replace(' ','%20')
return out
def init(token):
blynk.AUTH_TOKEN = token
blynk.initialise = True
def write_pin(pin, value):
if not blynk.initialise:
return
url = 'http://blynk-cloud.com/' + blynk.AUTH_TOKEN + '/update/' + str(pin) + '?value=' + str(blynk._parse_out(value))
response = blynk.urequests.get( url )
return( blynk._parse_in(response.text) )
def read_pin(pin):
if not blynk.initialise:
return
url = 'http://blynk-cloud.com/' + blynk.AUTH_TOKEN + '/get/' + str(pin)
response = blynk.urequests.get( url )
return( blynk._parse_in(response.text) )
def notify(message):
if not blynk.initialise:
return
headers = {'Content-Type':'application/json'}
values = '{"body":"' + str(message) + '"}'
url = 'http://blynk-cloud.com/' + blynk.AUTH_TOKEN + '/notify'
response = blynk.urequests.post( url, data = values ,headers = headers)
return( blynk._parse_in(response.text) )
def is_app_connected():
if not blynk.initialise:
return
url = 'http://blynk-cloud.com/' + blynk.AUTH_TOKEN + '/isAppConnected'
response = blynk.urequests.get( url )
return( blynk._parse_in(response.text) )