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