Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

Commit f113e2b

Browse files
committed
Generic virtual pin events, UTF8 support
1 parent 58defe3 commit f113e2b

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

BlynkLib.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def _send(self, cmd, *args, **kwargs):
127127
data = b''
128128
dlen = args[0]
129129
else:
130-
data = ('\0'.join(map(str, args))).encode('ascii')
130+
data = ('\0'.join(map(str, args))).encode('utf8')
131131
dlen = len(data)
132132

133133
self.log('<', cmd, id, '|', *args)
@@ -178,7 +178,10 @@ def process(self, data=b''):
178178
self.state = CONNECTED
179179
dt = now - self.lastSend
180180
self._send(MSG_INTERNAL, 'ver', _VERSION, 'h-beat', self.heartbeat//1000, 'buff-in', self.buffin, 'dev', 'python')
181-
self.emit('connected', ping=dt)
181+
try:
182+
self.emit('connected', ping=dt)
183+
except TypeError:
184+
self.emit('connected')
182185
else:
183186
if dlen == STA_INVALID_TOKEN:
184187
print("Invalid auth token")
@@ -193,16 +196,18 @@ def process(self, data=b''):
193196
data = self.bin[5:5+dlen]
194197
self.bin = self.bin[5+dlen:]
195198

196-
args = list(map(lambda x: x.decode('ascii'), data.split(b'\0')))
199+
args = list(map(lambda x: x.decode('utf8'), data.split(b'\0')))
197200

198201
self.log('>', cmd, i, '|', ','.join(args))
199202
if cmd == MSG_PING:
200203
self._send(MSG_RSP, STA_SUCCESS, id=i)
201204
elif cmd == MSG_HW or cmd == MSG_BRIDGE:
202205
if args[0] == 'vw':
203206
self.emit("V"+args[1], args[2:])
207+
self.emit("V*", args[1], args[2:])
204208
elif args[0] == 'vr':
205209
self.emit("readV"+args[1])
210+
self.emit("readV*", args[1])
206211
elif cmd == MSG_INTERNAL:
207212
self.emit("int_"+args[1], args[2:])
208213
else:

examples/other_functions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- custom server
1616
- changing heartbeat
1717
- connected/disconnected events
18+
- generic virtual pin events
1819
"""
1920

2021
from __future__ import print_function
@@ -38,5 +39,14 @@ def blynk_connected(ping):
3839
def blynk_disconnected():
3940
print('Blynk disconnected')
4041

42+
@blynk.ON("V*")
43+
def blynk_handle_vpins(pin, value):
44+
print("V{} value: {}".format(pin, value))
45+
46+
@blynk.ON("readV*")
47+
def blynk_handle_vpins_read(pin):
48+
print("Server asks a value for V{}".format(pin))
49+
blynk.virtual_write(pin, 0)
50+
4151
while True:
4252
blynk.run()

0 commit comments

Comments
 (0)