blynk-library-python icon indicating copy to clipboard operation
blynk-library-python copied to clipboard

How to use Blynk.Inject with ESP32?

Open EugeneRymarev opened this issue 2 years ago • 6 comments

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

EugeneRymarev avatar Nov 25 '22 11:11 EugeneRymarev

@vshymanskyy, could you suggest?

EugeneRymarev avatar Dec 07 '22 15:12 EugeneRymarev

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.

ebolisa avatar Dec 07 '22 15:12 ebolisa

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

EugeneRymarev avatar Dec 07 '22 17:12 EugeneRymarev

@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.

ebolisa avatar Dec 07 '22 17:12 ebolisa

@ebolisa, could you show a complete example?

EugeneRymarev avatar Dec 08 '22 03:12 EugeneRymarev

@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()

ebolisa avatar Dec 08 '22 07:12 ebolisa