Blynk 2 and Raspberry Pi - How to get Blynk 2 working?

Hello,
is there a way to get Blynk 2.0 work on Raspberry Pi (RPi) ?
I tried node.js… and failed…
Hardware model: RPiZeroW
Communication type: Wifi
Smartphone OS Android
Blynk server (cloud)
Blynk Library version
node -v :v11.15.0
npm -v : 7.20.5
asmv-module@1.0.0 /home/pi/asmv-module
├── blynk-library@0.5.4
└── onoff@6.0.3

It does connect to Blynk 1… and Blynk1 script seems to work.

As a newbee I had to learn and Blynk2 and Blynk 1… kind of frustrating.

I understand, I think, what Blynk 2 is about (deployment) and I like this idea.

BUT how do I get RPi to be working with Blynk 2, either node.js or else ?

Help!!

Best

Robert

Pete.

2 Likes

@PeteKnight , thank you. Python is fine. I am an old boy , so Python better than JavaScript… :slight_smile:
I will try asap.

Hi @rfv-370,

Were you able to make any progress on using Blynk 2.0 on a Raspberry?
As a complete beginner, I’m currently stuck after these steps:

  1. Install new Python Blynk library on Raspberry (https://github.com/blynkkk/lib-python)
  2. Install app on iPhone
  3. Create project in Blynk with generation of Auth token
  4. Setup of test.py file with the following code:
import blynklib

BLYNK_AUTH = '<YourAuthToken>' #my token was used here

blynk = blynklib.Blynk(BLYNK_AUTH)

while True:
    blynk.run()

When executing the above test.py file, I see the Blynk logo appearing, but no further info or updates on succesful connection or initialisation.
Can the test.py file run without any Virtual Pin code or declaration?

I just wanted to get the RP3 online before I proceed with using the Virtual Pins or other functionality.

Thanks,
Maarten

@MDurie Add widgets switch (V0 set to pulse) and led (V1) on your app then use this test code:

import BlynkLib
import BlynkTimer
import socket, errno
import RPi.GPIO as GPIO
from datetime import datetime as dt

#set GPIO pinout
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#set physical relay pins as output
rel_S1 = 5      #pin 29 CH1  V0

#leds status as input
in_ledS = 17    #pin 11

arr_relays = [rel_S1]

#setup pins as output
GPIO.setup(arr_relays, GPIO.OUT)

arr_leds = [in_ledS]

#setup pins as input
GPIO.setup(arr_leds, GPIO.IN)

#cloud server
BLYNK_TEMPLATE_ID = ''
BLYNK_DEVICE_NAME = ''
BLYNK_AUTH = ''

blynk = BlynkLib.Blynk(BLYNK_AUTH)

# Create BlynkTimer Instance
timer = BlynkTimer.BlynkTimer()

def init():
  #set putput state of relay pins
  GPIO.setup(arr_relays, GPIO.OUT)
  GPIO.output(arr_relays, GPIO.HIGH)
  GPIO.cleanup(arr_relays)

def update_virtual_led():
  #S
  if(GPIO.input(arr_leds[0]) == 1):
    blynk.virtual_write(1, 0)
  else:
    blynk.virtual_write(1, 255)

@blynk.on('V0')
def S1_write_handler(value):
  if int(value[0]) == 1:
    print('relay on')
    GPIO.setup(arr_relays[0], GPIO.OUT)
    GPIO.output(arr_relays[0], GPIO.LOW)
    timer.set_timeout(1, S1_timeout)


#--- timeouts ---
def S1_timeout():
  GPIO.output(arr_relays[0], GPIO.HIGH)
  GPIO.cleanup(arr_relays[0])
  print('relay off')

@blynk.on("connected")
def blynk_connected():
  print('Updating leds values from the server...')
  blynk.sync_virtual(1)

#--- activate timers ---
timer.set_interval(2, update_virtual_led) #update virtual leds for 2 secs


if __name__ == "__main__":
    init() #runs once
    while True:
      try:
        blynk.run()
        timer.run()
      except socket.error as e:
        print(e)
        if(e.errno != errno.EPIPE):
          t = time.time()
          s = dt.fromtimestamp(t).strftime('%d/%m/%Y %H:%M:%S - ')
          logging.error(s + "err blynkmain " + str(e))
          raise
        blynk.connect()

@MDurie Let me know if this test code worked for you. I’ve a project running with Blynk1.0 since BlynkLib was first written (2-3 years) and it’s been running like a charm. But, I cannot say the same for Blynk2.0. The latter is a bit immature for Python projects.

Hi, do you need any physical LED/circuitry parts for this to work?

In the test test code, pin 5 is used as an output as you can see. So, yes, you can connect a LED through a resistor to it.

Can I have the wiring diagram for this? Especially for the relay. There’s also no Pin 29 on the RPi 4 Model b. I’m confused about this.

Pin 29 on the 40-pin header = GPIO5

Does it make sense now?

Pete.

Sorry, my bad, I meant GPIO5 instead of pin5 as @PeteKnight has illustrated. The test code shown above is using arrays to hold relays and LEDs pins #s as I used them for a project implementing my own relays board. You may want to simplify the code to make it a bit easier on you. If you need further help, just post your code.