BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

RPi Python Buster re-connect to App

#1

I had to reformat the sd card in my RPi 3B+ due to problems not related to Blynk. I am now on the latest “Buster” version.

I have my app on my Android phone - and it has been working well for months. I use the Blynk Cloud server.

Now my RPi isn’t showing up as being “active” on my phone, I get the board icon with the little red “1” indicating it is not connected.

How can I check the connection between my RPi and the cloud server?
Thanks
Bruce

0 Likes

#2

As I typed the first message, I was thinking that maybe the new Raspian version (Buster) was the problem. So I reformatted another SD card using Jessie. Reinstalled Blynk etc
But problem is still there - the RPi is not connecting to the cloud server.

0 Likes

#3

Double check that your RPi is in fact connecting to the internet… and confirm you are using the latest Python version (although that might require some tweaks to your code?).

0 Likes

#4

OK Checked that it is able to get to the internet - works fine.

When I run my program that uses Blynk from a terminal then I get an “ascii - graphic” that basically says "Blynk for Python V0.2.0 (Linux).

Does that help?
Thanks
Bruce

0 Likes

#5

The Pi can access the internet - even tried https:/blynk.io - just to make sure!
What is the url or ip address of the server that my programme is trying to connect to when it signs in to Blynk?

I also tried a new authorisation code but that hasn’t helped.
Any more ideas?

0 Likes

#6

Depends on your region… you can ping blynk-cloud.com from a PC to see.

Post your code (properly formatted for viewing on this forum) so others can try to assist.

Blynk%20-%20FTFC

0 Likes

#7

Hi Gunnar, thanks for the replies.
So I pinged blynk-cloud.com from a terminal window on the same RPi and that works fine. Got a response from 139.59.206.133 in around 60ms, so that all seems OK.

By the way I am in the UK, if that makes any difference.

Here is the code I am running, it seems to run on the RPi OK and I get the “splash” screen on the console window OK. The issue I am having is that the App on my Moto E5 Android phone has the icon at the top of the screen with a red “1” on it. In the past that has always shown that Blynk wasn’t running on the RPi.

So my guess is that although the code appears to be running on the RPi maybe it isn’t communicating with the cloud server, or my phone App isn’t “seeing” the session that is running on the cloud server?


import BlynkLib, time
from gpiozero import PWMLED
import paho.mqtt.client as mqtt

red = PWMLED(21)                                      # set up LEDs to pulse using PWM
yellow = PWMLED(20)                                   # for 201 r=3  y=4  g=2
green = PWMLED(16)                                    # for Pi3 r=21 y=20 g=16

BLYNK_AUTH = '7f91432283c84457822eec87592a9b63'
# old replaced 7/7/19 BLYNK_AUTH = '61cc1d16f6e0490db628c25423adba54'       # Blynk code
blynk = BlynkLib.Blynk(BLYNK_AUTH)                    # Initialise Blynk
status = 0                                            # This is for the virtual read for the LED

mqttc=mqtt.Client()
#mqttc.connect("192.168.1.203", 1883,60)               #sub Q on 203, port 1883, 60 sec keep alive


# These functions are called asynchonously by Blynk ....
@blynk.VIRTUAL_WRITE(1)                               #Register Virtual Pin
def my_write_handler(value):
    global status
    if int(value)==1:
        red.value=1                                   # Switch on LED
        status=255
        print('ON  Heating')                  # Print update
        mqttc.connect("192.168.1.203", 1883,60)       # connect
        mqttc.publish("hello/world", "RED on" ,1)     # Publish to MQ try QOS 1
        mqttc.publish("highmount/heating", "R3on" ,1) # Publish to 199
        mqttc.disconnect()                            # disconnect
    else:
        red.value=0
        status=0
        print('OFF Heating')
        mqttc.connect("192.168.1.203", 1883,60) 
        mqttc.publish("hello/world", "RED off" ,1)
        mqttc.publish("highmount/heating", "R3off" ,1)     # Publish to 199
        mqttc.disconnect()

@blynk.VIRTUAL_WRITE(2)                               #Register Virtual Pins
def my_write_handler(value):
    if int(value)==1:
        yellow.value=1
        print('ON  Front light')
        mqttc.connect("192.168.1.203", 1883,60)
        mqttc.publish("highmount/frontdoor/light", "YELLOW on")
        mqttc.disconnect()  
    else:
        yellow.value=0
        print('OFF Front light')
        mqttc.connect("192.168.1.203", 1883,60)
        mqttc.publish("highmount/frontdoor/light", "YELLOW off")
        mqttc.disconnect()
        
@blynk.VIRTUAL_WRITE(3)                               #Register Virtual Pins
def my_write_handler(value):
    if int(value)==1:
        green.value=1
        print('ON  GREEN')
        mqttc.connect("192.168.1.203", 1883,60)
        mqttc.publish("hello/world", "GREEN on")
        mqttc.disconnect() 
    else:
        green.value=0
        print('OFF GREEN')
        mqttc.connect("192.168.1.203", 1883,60)
        mqttc.publish("hello/world", "GREEN off")
        mqttc.disconnect()          
        
# Now write back to the LED on V4 ...
def update_led():
    global status
    blynk.virtual_write(4, status)


# --- Start of MAIN programme  -----------------------------------------    
blynk.run()                                     # Start Blynk (this call should never return)

Basically, the code waits for a signal from the Blynk App and generates an MQTT message to other RPi’s that do stuff like turn on my heating, a light, waters the garden. It is written in Python3.

Thanks for taking a look
Regards
Bruce

0 Likes

#8

Z[quote=“Bruce_Crowthorne, post:7, topic:38479”]
Basically, the code waits for a signal from the Blynk App and generates an MQTT message to other RPi’s that do stuff like turn on my heating, a light, waters the garden.
[/quote]

There’s a much simpler solution. Install Node-Red on your RPI then install the Blynk plug-in for Node-Red.

Pete.

0 Likes

#9

Hi Pete, two points.
First I have several RPis (and Arduinos) scattered around, some detect stuff (light, temperature, water level etc) and publish that on MQTT.
Others subscribe to that data to do “stuff”. I actually use NodeRed (fed from MQTT) to do some of my reporting. I find it OK, but not as reliable as I would like.

Second (and key thing!) I had to rebuild a RPi that Blynk was / is running on and it has stopped working. Initially I suspected the OS as I moved to Buster, but I have since downgraded to Jessie. At the same time I reloaded the Blynk library and I think it has changed version as well - so it may be something to do with that???

Key thing right now is just to get Blynk to run again!!!
Bruce

0 Likes

#10

I’m not sure that you understand the solution I was suggesting.
I have one RPI that is my Mosquitto MQTT and Node-Red server. It has the Blynk plug-in installed and acts as my Blynk/MQTT gateway.

I then have around 20 ESP8266 devices around the home that just run MQTT code - no Blynk code on these devices at all.

The system is rock solid and works very well. Much better than trying to run Blynk on multiple devices or to create your own Blynk/MQTT bridge.

Pete.

0 Likes

#11

Actually Pete, I think our set ups are VERY similar.
I too have a RPi “Server” that runs Apache, php, MySQL, MQTT and NodeRed and all of the others just Pub or Sub the data. This currently includes a web client (using JavaScript) to publish MQTT messages.

Currently I am still testing Blynk so it is currently on a different “test” RPi. Once I am happy I will migrate Blynk onto my main RPi Server.
So, like you I have Blynk on just one machine and I use MQTT to publish messages to the other machines based on the button presses on Blynk. (actually the same messages as are issued by the web client).
I really don’t have a Blynk/MQTT bridge - just a few lines of code that output messages that are similar to the messages that are issued by the web client.

The main difference is that I am generally using RPi Zeros as my client machines ( I only have 6) so that I can access them remotely using SSH and update/modify/fix them. Plus one Arduino with a LAN shield and one ESP8266. I migrated a couple of these from standalone Arduinos to network connected RPi Zeros.

Like you the machines are pretty reliable - until I start messing with them :wink:

0 Likes

#12

Then why not install the Blynk websockets contrib in Node-Red?

You can install it via Pallet Manager or command line, whichever you prefer.

From your IP address it looks like you’re in North London. We’re in North Kensington (well, in Sunny Spain at the moment) so we’re not to far away geographically either.

Pete.

0 Likes

#13

I guess mainly because I am not a huge fan of NodeRed! I use it and it is OK, but I find it uses a lot of resource just running my display of half a dozen graphs!
I am actually in West Sussex, my ISP probably uses an ip address that is located in London somewhere…
Where in Spain are you? We regularly go to Javea.

0 Likes

#14

I don’t use Node-Red dashboard at all, I hate it. I use Blynk instead, so I’m with you on that score.

West Sussex certainly beats North London! I worked in Horsham for a while, nice, except for the commute.
We’re near Torrevieja, south east Spain. Almost 9pm and 28°, what more could you want?

Pete.

0 Likes

#15

Air Conditioning! :stuck_out_tongue: OK, so not that hot, but sometimes I am picky :innocent:

0 Likes

#16

Can you try with one of the basic examples for that “new” version?

0 Likes

#17

OK, so I entered a couple of examples. “event_VIRTUAL_WRITE.py” and that worked.
Then I wrote a simple button and that worked too, the code is attached.

However, in my original code I am trying to detect a state of a button and that is what is failing.

Here is a code snippet (v11) …

import BlynkLib

BLYNK_AUTH = 'xGk6QmtezLoT93BsnWOLjxBfXO0vpxmf'

blynk = BlynkLib.Blynk(BLYNK_AUTH)          # Initialize Blynk

@blynk.VIRTUAL_WRITE(1)                   # Register virtual pin handler
def v1_write_handler(value):
    print('value: {}'.format(value[0]))
    print('int(value) is  ' + int(value))
    

while True:
    blynk.run()

and that gives the error …

    print('int(value) is  ' + int(value))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

So now I have v12a …

import BlynkLib

BLYNK_AUTH = 'xGk6QmtezLoT93BsnWOLjxBfXO0vpxmf'

blynk = BlynkLib.Blynk(BLYNK_AUTH)          # Initialize Blynk

@blynk.VIRTUAL_WRITE(1)                   # Register virtual pin handler
def v1_write_handler(value):
    print('value: {}'.format(value[0]))
    if int(value[0]) == 1:
        print('ON')
    else:
        print('OFF')
    

while True:
    blynk.run()

So it appears that the value of “value” is now brought back as a list that doen’t seem to be the case in the previous version.
By using value[0] I get the correct value and I can check it’s value.

It’s fairly detailed but can this be reflected in some documentation somewhere?

At least I have my bit of scrap code working, now I will try updating my original code…

Thanks in advance
Bruce

0 Likes

#18

So now I have updated my original code with int(value[0]) replacing int(value) - and it works!

Thanks for your help
Regards
Bruce

0 Likes