Help connecting with Raspberry Pi Pico?

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

  1. Flashed the Pico with MicroPython v1.17 using Thonny
  2. Copied BlynkLib.py to /lib folder on the Pico
  3. 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ā€)

  1. 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)
  2. 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'
  1. 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.
  2. 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 *
  1. 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'
  1. 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ā€
  2. 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
  1. 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))

Updating with progress in case it helps someone else in the same situation.

I went back to basics but tried it step by step from the pimoroni side instead of the blynk side. Following the ā€˜Getting startedā€™ section on Pico Wireless Pack ā€“ Pimoroni and making the recommended changes to How To Get Wi-Fi, Internet on Raspberry Pi Pico | Tom's Hardware, it successfully made the call to the weather API (as expected, I had that working previously).

I then added BlynkLib.py from vshymanskyy/blynk-library-python: Blynk library for Python. Works with Python 2, Python 3, MicroPython. (github.com) to the /lib folder, added the following code to code.py, and ran it again:

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

That did NOT work (as expected, since it had never worked before), giving me the dreaded ā€œImportError: no module named ā€˜socketā€™ā€ error that Iā€™ve seen so many times over the past month or soā€¦

I went into the BlynkLib.py file and changed ā€œimport socketā€ to ā€œimport adafruit_esp32spi.adafruit_esp32spi_socket as socketā€. Iā€™m sure Iā€™ve tried that previously, but I guess I didnā€™t have everything else quite aligned correctly, because this time it worked!

Thanks to anyone whoā€™s made it through the thread, and good luck to anyone who comes across this by searching with the same issues.

(My next problem is that changing the code to blynk.logEvent(ā€œhelloā€) instead of blynk.virtual_write(0, ā€œtime:ā€ + str(t)) gave an error AttributeError: ā€˜Blynkā€™ object has no attribute ā€˜logEventā€™, but I guess Iā€™ll try troubleshooting that alone before asking for more help.)

The Log event needs to be defined on the platform first so that it knows where to place the details:
Login to the platform
select Templates
Go to the ā€œEventsā€ tab
select ā€œEditā€ in the top right hand corner
Add a new ā€œEventā€
then save and try from you device

Cheers :slight_smile:

I seem to have managed to trigger all sorts of unusual errors. I actually had an event created already, but there was something else wrong that I canā€™t even remember at this pointā€¦

I almost despaired yesterday afternoon when I put this working code back into my project and started getting socket errors again - this time something like ā€œNoType object has no function ā€˜socketā€™ā€. At least by then I knew that there was a possible way of getting it working; the final solution turned out to be making sure there was an active wifi connection before allowing Blynk to try to connect (I had a bunch of async things happening so that wasnā€™t originally guaranteed since I didnā€™t realise it would give an error like this rather than just failing to connect). A whole lot of refactoring later itā€™s now nicely integrated into my state machine and sending notifications just the way I hoped.

Great to hear :metal:

1 Like

Hey, I am having the same error. Did you find another way to fix it?