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

Commit f4c1924

Browse files
committed
Compatibility improvements
1 parent cf99401 commit f4c1924

File tree

3 files changed

+82
-33
lines changed

3 files changed

+82
-33
lines changed

BlynkLib.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ def process(self, data=None):
207207

208208
import socket
209209

210+
try:
211+
SOCK_TIMEOUT = eval('0.05')
212+
except:
213+
SOCK_TIMEOUT = 0
214+
210215
class Blynk(BlynkProtocol):
211216
def __init__(self, auth, **kwargs):
212217
self.insecure = kwargs.pop('insecure', False)
@@ -223,38 +228,37 @@ def redirect(self, server, port):
223228

224229
def connect(self):
225230
print('Connecting to %s:%d...' % (self.server, self.port))
231+
s = socket.socket()
232+
s.connect(socket.getaddrinfo(self.server, self.port)[0][-1])
226233
try:
227-
s = socket.socket()
228-
s.connect(socket.getaddrinfo(self.server, self.port)[0][4])
229234
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
230-
if self.insecure:
231-
self.conn = s
232-
else:
233-
try:
234-
import ssl
235-
ssl_context = ssl.create_default_context()
236-
self.conn = ssl_context.wrap_socket(sock=s, server_hostname=self.server)
237-
except ImportError:
238-
import ussl
239-
self.conn = ussl.wrap_socket(s, server_hostname=self.server)
240-
235+
except:
236+
pass
237+
if self.insecure:
238+
self.conn = s
239+
else:
241240
try:
242-
self.conn.settimeout(eval('0.05'))
243-
except:
244-
self.conn.settimeout(0)
245-
BlynkProtocol.connect(self)
241+
import ussl
242+
ssl_context = ussl
243+
except ImportError:
244+
import ssl
245+
ssl_context = ssl.create_default_context()
246+
self.conn = ssl_context.wrap_socket(s, server_hostname=self.server)
247+
try:
248+
self.conn.settimeout(SOCK_TIMEOUT)
246249
except:
247-
raise Exception('Connection with the Blynk server %s:%d failed' % (self.server, self.port))
250+
s.settimeout(SOCK_TIMEOUT)
251+
BlynkProtocol.connect(self)
248252

249253
def _write(self, data):
250254
#print('<', data.hex())
251-
self.conn.send(data)
255+
self.conn.write(data)
252256
# TODO: handle disconnect
253257

254258
def run(self):
255259
data = b''
256260
try:
257-
data = self.conn.recv(self.buffin)
261+
data = self.conn.read(self.buffin)
258262
#print('>', data.hex())
259263
except KeyboardInterrupt:
260264
raise

examples/05_utc_and_timezone.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Blynk is a platform with iOS and Android apps to control
3+
Arduino, Raspberry Pi and the likes over the Internet.
4+
You can easily build graphic interfaces for all your
5+
projects by simply dragging and dropping widgets.
6+
7+
Downloads, docs, tutorials: http://www.blynk.cc
8+
Sketch generator: http://examples.blynk.cc
9+
Blynk community: http://community.blynk.cc
10+
Social networks: http://www.fb.com/blynkapp
11+
http://twitter.com/blynk_app
12+
13+
This example shows how to get UTC time and Timezone info
14+
"""
15+
16+
import BlynkLib
17+
import time
18+
19+
BLYNK_AUTH = 'YourAuthToken'
20+
21+
# Initialize Blynk
22+
blynk = BlynkLib.Blynk(BLYNK_AUTH)
23+
24+
@blynk.on("connected")
25+
def blynk_connected(ping):
26+
print('Blynk ready. Ping:', ping, 'ms')
27+
blynk.send_internal("utc", "time")
28+
blynk.send_internal("utc", "tz_name")
29+
30+
@blynk.on("disconnected")
31+
def blynk_disconnected():
32+
print('Blynk disconnected')
33+
34+
@blynk.on("internal:utc")
35+
def on_utc(value):
36+
if value[0] == "time":
37+
ts = int(value[1])//1000
38+
# on embedded systems, you may need to subtract time difference between 1970 and 2000
39+
#ts -= 946684800
40+
tm = time.gmtime(ts)
41+
print("UTC time: ", time.asctime(tm))
42+
elif value[0] == "tz_name":
43+
print("Timezone: ", value[1])
44+
45+
while True:
46+
blynk.run()

examples/hardware/ESP8266_ESP32.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,36 @@
2525

2626
BLYNK_AUTH = 'YourAuthToken'
2727

28-
print("Connecting to WiFi...")
2928
wifi = network.WLAN(network.STA_IF)
30-
wifi.active(True)
31-
wifi.connect(WIFI_SSID, WIFI_PASS)
32-
while not wifi.isconnected():
33-
pass
29+
if not wifi.isconnected():
30+
print("Connecting to WiFi...")
31+
wifi.active(True)
32+
wifi.connect(WIFI_SSID, WIFI_PASS)
33+
while not wifi.isconnected():
34+
pass
3435

3536
print('IP:', wifi.ifconfig()[0])
3637

37-
print("Connecting to Blynk...")
3838
blynk = BlynkLib.Blynk(BLYNK_AUTH)
3939

4040
@blynk.on("connected")
4141
def blynk_connected(ping):
4242
print('Blynk ready. Ping:', ping, 'ms')
4343

44+
@blynk.on("disconnected")
45+
def blynk_disconnected():
46+
print('Blynk disconnected')
47+
4448
def runLoop():
4549
while True:
4650
blynk.run()
4751
machine.idle()
4852

49-
# Run blynk in the main thread:
53+
# Run blynk in the main thread
5054
runLoop()
5155

52-
# Or, run blynk in a separate thread (unavailable for esp8266):
56+
# You can also run blynk in a separate thread (ESP32 only)
5357
#import _thread
5458
#_thread.stack_size(5*1024)
5559
#_thread.start_new_thread(runLoop, ())
5660

57-
58-
# Note:
59-
# Threads are currently unavailable on some devices like esp8266
60-
# ESP32_psRAM_LoBo has a bit different thread API:
61-
# _thread.start_new_thread("Blynk", runLoop, ())

0 commit comments

Comments
 (0)