BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

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?