After my last question, I got everything working to the point where I could send requests to e.g. https://blynk.cloud/external/api/logEvent?token=(my token here)&code=hello and have it all work correctly. However, I would like to be able to also control my Pico device from the dashboard and as far as I understand that requires a different approach, where I must use BlynkLib.py from vshymanskyy/blynk-library-python: Blynk library for Python. Works with Python 2, Python 3, MicroPython. (github.com).
I’m afraid my knowledge of the underlying networking concepts is quite shaky, so I’m hitting a wall trying to troubleshoot.
These are the steps I’ve taken (apologies for the long post, skip to the end for the summary):
- Flashed the Pico with MicroPython v1.17 using Thonny
- Copied BlynkLib.py to /lib folder on the Pico
- Tried running the following code example:
""" Blynk is a platform with iOS and Android apps to control Arduino, Raspberry Pi and the likes over the Internet. You can easily build graphic interfaces for all your projects by simply dragging and dropping widgets. Downloads, docs, tutorials: http://www.blynk.cc Sketch generator: http://examples.blynk.cc Blynk community: http://community.blynk.cc Social networks: http://www.fb.com/blynkapp http://twitter.com/blynk_app """ import BlynkLib import time BLYNK_AUTH =(my token here) # initialize Blynk blynk = BlynkLib.Blynk(BLYNK_AUTH, insecure=True) tmr_start_time = time.time() while True: blynk.run() t = time.time() if t - tmr_start_time > 1: print("1 sec elapsed, sending data to the server...") blynk.virtual_write(0, "time:" + str(t)) tmr_start_time += 1
This is the result:
___ __ __ / _ )/ /_ _____ / /__ / _ / / // / _ \/ '_/ /____/_/\_, /_//_/_/\_\ /___/ for Python v1.0.0 (rp2) Traceback (most recent call last): File "<stdin>", line 14, in <module> File "/lib/BlynkLib.py", line 207, in <module> ImportError: no module named 'socket'
(Line 207 is just “import socket”)
- At this point my understanding is that I need a ‘socket’ library, I downloaded one from micropython-lib/python-stdlib at master · micropython/micropython-lib (github.com)
- I saved socket.py to the /lib folder on the Pico as Socket.py (from what I’ve learned so far, naming the file the same as the library causes problems) and changed line 207 in BlynkLib.py to from Socket import socket. That gives this result:
/ _ )/ /_ _____ / /__ / _ / / // / _ \/ '_/ /____/_/\_, /_//_/_/\_\ /___/ for Python v1.0.0 (rp2) Traceback (most recent call last): File "<stdin>", line 14, in <module> File "/lib/BlynkLib.py", line 207, in <module> File "/lib/Socket.py", line 1, in <module> ImportError: no module named 'usocket'
- Seems like I now need a ‘usocket’ library. There is none in the set I downloaded previously but the Thonny package manager has one which is described as MicroPython module usocket ported to CPython so I installed that.
- I now get the error “ImportError: no module named ‘socket’” from line 2 in the new usocket.py, so I checked that to find it only contains this:
import micropython from socket import *
- I don’t know if that’s normal - it seems very strange - but I changed the second line to “from Socket import *” to match the casing again, and now I get this:
/ _ )/ /_ _____ / /__ / _ / / // / _ \/ '_/ /____/_/\_, /_//_/_/\_\ /___/ for Python v1.0.0 (rp2) Traceback (most recent call last): File "<stdin>", line 14, in <module> File "/lib/BlynkLib.py", line 207, in <module> File "/lib/Socket.py", line 45, in <module> AttributeError: 'module' object has no attribute 'socket'
- This seems to be the error caused by the file name clashing. Line 45 is “class socket(_socket.socket):”, where _socket is referring to the usocket library I just installed, so I changed the import line to be “from usocket import usocket as _socket”
- That gives the error “ImportError: can’t import name usocket”. So maybe my previous fix wasn’t the right one and I need to rename the usocket file. I changed it to Usocket.py and the imports in the Socket.py file to:
from Usocket import * from Usocket import usocket as _socket
- Now I’m getting “ImportError: can’t import name usocket”. So maybe the file being mostly empty is actually the problem?
At this point I feel like I’m going in circles and just doing things slightly randomly. From reading through this forum it seems that other people have Blynk working properly on a Pico so presumably there is an approach that will work, but I haven’t been able to discover exactly what that is.
I’d very much like to validate my assumptions, as well as find out if there’s a working example somewhere that I can build on.
- Is getting BlynkLib working (to be able to use blynk.run()) the only way to have a full 2-way integration?
- Is it possible to run that on a standard Pico setup or is something else needed? (I was previously using the special Pimoroni build to use their wireless card but I had the same issues with that; I have been able to get it connecting to the open weather api following How To Get Wi-Fi, Internet on Raspberry Pi Pico | Tom’s Hardware (tomshardware.com))