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

Python GPIO switches no longer work after upgrade to Raspberry 3

#1

I’m a new Blynker . … Started with Python on Raspberry 2A+. Wrote app to open garage doors. All was workin OK. Upgraded to Raspberry 3 after 2A+ shorted out. I took a few months off and now I am unable to get GPx buttons to work. Used reguests library to access Blynk before. Tried using Blynklib but still no progress. Virtual pins read OK. Terminal writes OK. But GPx buttons are inop via Blynk app. They work OK when manually set.

Where to go for help/example/code for my GPx button in python.

Was using private-anon-093dde407a-blynkapi.apiary-proxy.com as Blynk server but I get a hash error on it so now using blynk-cloud.com .

import RPi.GPIO as GPIO
import time
import datetime 
import os
import requests
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN)
GPIO.setup(6, GPIO.IN)
# these are two switches to be controlled by Blynk app.
GPIO.setup(2, GPIO.OUT) #activate left door
GPIO.setup(3, GPIO.OUT) #activate Right door

GPIO.output(2, 1) #make sure switches are open (off)
GPIO.output(3, 1)

blynktoken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
blynkprivatedomain = 'xxxxx://private-anon-093dde407a-blynkapi.apiary-proxy.com/'
blynkdomain =  "xxxx://blynk-cloud.com/"
.
.
S0arg = ' -vw 0 0'
S1arg = ' -vw 1 0'

def waitforconnect():
    global sleeping
    while True:
        response = requests.get(blynkdomain + blynktoken + "/isAppConnected")
        response = response.text
        if response == "true" :
            lasttime = time.time()
            print(time.strftime('%Y%m%d'), time.strftime('%H%M'), "App Connected")
            sleeping = False
            break
        else:
            if not sleeping:
                print(time.strftime('%Y%m%d'), time.strftime('%H%M'), "sleeping ...")
                sendtermmsg(" Sleeping since " + str(time.strftime('%H%M') ) )
                sleeping = True
            time.sleep(appsleeptime)
.
.
waitforconnect()
elapsed = 0
os.system("sudo python /home/pi/blynk/blynk_ctrl.py -t " + blynktoken + " -vw 0 0  -vw 1 0 ")
sendtermmsg("\n")
sendtermmsg("Program start")
getnotifytimes()
print("Notify times:", dooropenstart, dooropenend)
sendtermmsg("Notify: " + str(dooropenstart)[:5] + " " + str(dooropenend)[:5])
sendtermmsg("AppCycle:" + str(waittime))
sendtermmsg("AppSleep:" + str(appsleeptime)) 

while True:
    timestamp = datetime.datetime.now().time()
    
    if elapsed > elapsedmax:
        waitforconnect()
        elapsed = 0
        
    if GPIO.input(5) == False: # door close
        if door0open:
            S0arg = ' -vw 0 0'
            sendtermmsg("Left door closed")
            door0open = False
            needtoreset = True
        else:
            pass
    else: # door open 
        if not door0open:
            S0arg = ' -vw 0 255'
            print (time.strftime('%Y%m%d'), time.strftime('%H%M'), "Door 0 open")
            sendtermmsg("Left door open")
            values = {"body": "Left garage door open" }
            door0open = True
            needtoreset = True
           
    if GPIO.input(6) == False: # door closed
        if door1open:
            S1arg = ' -vw 1 0'
            sendtermmsg("Right door closed")
            door1open = False
            needtoreset = True
        else:
            pass
    else: # door open
        if not door1open:
            S1arg = ' -vw 1 255'
            print (time.strftime('%Y%m%d'), time.strftime('%H%M'), "Door 1 open")
            sendtermmsg("Right door open")
            values = {"body": "Right garage door open" }
            door1open = True
            needtoreset = True
            
    if door1open or door0open:
        if doortimeroff:
            dooropentime =  time.time()
            doortimeroff = False
        else:
            pass
    else:
        doortimeroff = True
    # Send message if doors open after hours 
    if  (door0open or door1open ) and time.time() - dooropentime > dooropendelay:
        print(time.strftime('%Y%m%d'), time.strftime('%H%M'), "Door open in notify range")
        rrequest = requests.post('xxxxs://' + blynkprivatedomain + '/' + blynktoken + '/notify', json=values)
        preq = (rrequest.text)
        if len(preq) > 0 :
            print (time.strftime('%Y%m%d'), time.strftime('%H%M'), "Error sending Notify: ", preq)
        dooropentime = time.time()    
    
    if needtoreset:
        os.system("sudo python /home/pi/blynk/blynk_ctrl.py -t " + blynktoken + " " + S0arg + S1arg)
        delaycount = 0
        needtoreset = False

    time.sleep(waittime)
    elapsed = time.time() - lasttime
    
print("finished")
0 Likes

#2

it looks like you’re just calling the api to control the gpio pins?

0 Likes

#3

Yes, I have set up 2 GPx buttons in the Blynk app. Before when I pressed the button the switch would trip on the Raspberry. Now nothing. I also have 7 virtual switches that work as they should.

0 Likes

#4

Here is missing code:

def waitforconnect():
    global sleeping
    while True:
        response = requests.get(blynkdomain + blynktoken + "/isAppConnected")
        response = response.text
        if response == "true" :
            lasttime = time.time()
            print(time.strftime('%Y%m%d'), time.strftime('%H%M'), "App Connected")
            sleeping = False
            break
        else:
            if not sleeping:
                print(time.strftime('%Y%m%d'), time.strftime('%H%M'), "sleeping ...")
                sendtermmsg(" Sleeping since " + str(time.strftime('%H%M') ) )
                sleeping = True
            time.sleep(appsleeptime)

waitforconnect()
elapsed = 0
os.system("sudo python /home/pi/blynk/blynk_ctrl.py -t " + blynktoken + " -vw 0 0  -vw 1 0 ")
sendtermmsg("\n")
sendtermmsg("Program start")
getnotifytimes()
print("Notify times:", dooropenstart, dooropenend)
sendtermmsg("Notify: " + str(dooropenstart)[:5] + " " + str(dooropenend)[:5])
sendtermmsg("AppCycle:" + str(waittime))
sendtermmsg("AppSleep:" + str(appsleeptime)) 

while True:
    timestamp = datetime.datetime.now().time()
    
    if elapsed > elapsedmax:
        waitforconnect()
        elapsed = 0
        
    if GPIO.input(5) == False: # door close
        if door0open:
            S0arg = ' -vw 0 0'
            sendtermmsg("Rose door closed")
            door0open = False
            needtoreset = True
        else:
            pass
    else: # door open 
        if not door0open:
            S0arg = ' -vw 0 255'
            print (time.strftime('%Y%m%d'), time.strftime('%H%M'), "Door 0 open")
            sendtermmsg("Rose door open")
            values = {"body": "Rose garage door open" }
            door0open = True
            needtoreset = True
           
    if GPIO.input(6) == False: # door closed
        if door1open:
            S1arg = ' -vw 1 0'
            sendtermmsg("Ben door closed")
            door1open = False
            needtoreset = True
        else:
            pass
    else: # door open
        if not door1open:
            S1arg = ' -vw 1 255'
            print (time.strftime('%Y%m%d'), time.strftime('%H%M'), "Door 1 open")
            sendtermmsg("Ben door open")
            values = {"body": "Ben garage door open" }
            door1open = True
            needtoreset = True
            
    if door1open or door0open:
        if doortimeroff:
            dooropentime =  time.time()
            doortimeroff = False
        else:
            pass
    else:
        doortimeroff = True
    
    if  (door0open or door1open ) and time.time() - dooropentime > dooropendelay:
        print(time.strftime('%Y%m%d'), time.strftime('%H%M'), "Door open in notify range")
        rrequest = requests.post('https://' + blynkprivatedomain + '/' + blynktoken + '/notify', json=values)
        preq = (rrequest.text)
        if len(preq) > 0 :
            print (time.strftime('%Y%m%d'), time.strftime('%H%M'), "Error sending Notify: ", preq)
        dooropentime = time.time()    
    
    if needtoreset:
        os.system("sudo python /home/pi/blynk/blynk_ctrl.py -t " + blynktoken + " " + S0arg + S1arg)
        delaycount = 0
        needtoreset = False
    GPIO.output(2, 1) # make sure switch is off
    GPIO.output(3, 1)   
    time.sleep(waittime)
    elapsed = time.time() - lasttime
0 Likes

#5

You need to use backticks at the top and bottom of your code, not periods
Blynk%20-%20FTFC

I’ve corrected your code (twice) but try to use the correct character next time please.

Pete.

0 Likes

#6

First of all, you probably should switch to using blynk library for python:

0 Likes

#7

I have tried the Blynklib but can’t get past the " Blynk instance has no attribute ‘VIRTUAL_READ’ when running the following code:

import BlynkLib
import time

BLYNK_AUTH = 'YourAuthToken'

# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)

# Register virtual pin handler
@blynk.VIRTUAL_READ(2)
def v2_read_handler():
    # This widget will show some time in seconds..
    blynk.virtual_write(2, time.ticks_ms() // 1000)

while True:
blynk.run()
0 Likes

#8

Sorry, I had old copy of BlynkLib in directory. The missing attribute error is gone.

0 Likes

closed #9
0 Likes