vshymanskyy / blynk-library-python

Blynk library for Python. Works with Python 2, Python 3, MicroPython.
https://blynk.io/
MIT License
281 stars 97 forks source link

How to use Blynk.Inject with ESP32? #67

Open EugeneRymarev opened 1 year ago

EugeneRymarev commented 1 year ago

Hello! Where can I find an example of using Blynk.Inject with this package?

EugeneRymarev commented 1 year ago

@vshymanskyy, could you suggest?

ebolisa commented 1 year ago

could you suggest?

I used wifimgr.py. Modified the html/py code to collect credentials by storing them in wifi.dat and then, used them in my main code.

EugeneRymarev commented 1 year ago

@ebolisa, I mean the implementation of the Blynk.Inject function for connecting in the application, as in this video

ebolisa commented 1 year ago

@ebolisa, I mean the implementation of the Blynk.Inject function for connecting in the application, as in this video

I undestood you. wifimgr.py puts your device in AP mode like shown in the video. Then you introduce your router and Blynk credentials. At reboot, your device will automatically connect to Blynk. At least, that's the way I do it.

EugeneRymarev commented 1 year ago

@ebolisa, could you show a complete example?

ebolisa commented 1 year ago

@ebolisa, could you show a complete example?

First make wifimgr.py work without Blynk. Then modify its html code to accept the blynk token. Modify its py code so it can be read.

Then in boot.py:

try:
    wlan = wifimgr.get_connection()
    BLYNK_AUTH = wifimgr.get_profiles()[2]
    print('** Wifi connection established **')
    if wlan is None:
        print("Could not initialize the network connection.")
        while True:
            pass  # you shall not pass :D
except Exception:
    print('Wifi connection error...')
    umachine.reset()

in main.py:

# Initialize Blynk
try:
#     blynk = BlynkLib.Blynk(BLYNK_AUTH) # faster

#      or....

    blynk = BlynkLib.Blynk(BLYNK_AUTH,
        insecure=True,                     # disable SSL/TLS
        server='lon1.blynk.cloud',  # lon1 fra1.blynk.cloud or blynk.cloud
        port=80,                                   # set server port
        heartbeat=30,              # set heartbeat to 30 secs      
        log=dprint                         # use print function for debug logging
        )
except OSError as e:
    umachine.restart()

@blynk.on("connected")
def blynk_connected(ping):
    print('Blynk ready. Ping:', ping, 'ms')
    blynk.sync_virtual()

@blynk.on("disconnected")
def blynk_disconnected():
    print('Blynk disconnected')
    utime.sleep(5)
    umachine.reset()