Raspberry pi 3b, running python3. Blynk Lib Python v1.0.0 (linux)
My python code is running great thanks to John93 for his help.
The only problem I’m having now is when my raspberry pi goes offline,
The code keeps running fine but my connection to Blynk cloud is shown offline on the template, and never regains its connections. Unlike Blynk 1.0, where it would keep running.
Only thing I can do is reboot the RPI, to regain the connections.
Again it’s probably some simple code, but I cannot find it anywhere on the internet or your forums.
Any help would be greatly appreciated.
Thank you
Richard
#!/usr/bin/python
import time
import datetime
import os
import RPi.GPIO as GPIO
import BlynkLib
from BlynkTimer import BlynkTimer
from time import sleep
GPIO.setwarnings(False)
BLYNK_AUTH = ""
BLYNK_TEMPLATE_ID = ""
BLYNK_DEVICE_NAME = "Quickstart Template"
GPIO.setmode(GPIO.BOARD) #Use Board pin out numbers
# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)
@blynk.on("connected")
def blynk_connected(ping):
print('Blynk ready. Ping:', ping, 'ms')
@blynk.on("disconnected")
def blynk_disconnected():
print('Blynk disconnected')
@blynk.on("V*")
def blynk_handle_vpins(pin, value):
print("V{} value: {}".format(pin, value))
@blynk.on("V0") #Garage
def V0_write_handler(value):
if int(value[0]) != 0:
GPIO.output(R17, GPIO.LOW) #Turn On Relay 7 if all above conditions are true
time.sleep(SleepTimeSR);
GPIO.output(R17, GPIO.HIGH)
print('Garage ON')
else:
GPIO.output(R18, GPIO.LOW) #Turn On Relay 7 if all above conditions are true
time.sleep(SleepTimeSR);
GPIO.output(R18, GPIO.HIGH)
print('Garage OFF')
@blynk.on("V1") #Back Door Plug
def V1_write_handler(value):
if int(value[0]) != 0:
GPIO.output(R5, GPIO.LOW) #Turn On Relay 7 if all above conditions are true
time.sleep(SleepTimeSR);
GPIO.output(R5, GPIO.HIGH)
print('Xmas ligths ON')
else:
GPIO.output(R6, GPIO.LOW) #Turn On Relay 7 if all above conditions are true
time.sleep(SleepTimeSR);
GPIO.output(R6, GPIO.HIGH)
print('Xmas lights OFF')
@blynk.on("V2") #Kitchen Sink
def V2_write_handler(value):
if int(value[0]) != 0:
GPIO.output(R5, GPIO.LOW) #Turn On Relay 7 if all above conditions are true
time.sleep(SleepTimeSR);
GPIO.output(R5, GPIO.HIGH)
print('Kitchsn Sink ON')
else:
GPIO.output(R6, GPIO.LOW) #Turn On Relay 7 if all above conditions are true
time.sleep(SleepTimeSR);
GPIO.output(R6, GPIO.HIGH)
print('Kitchen Sink OFF')
@blynk.on("V3") #Family Room #4
def V3_write_handler(value):
if int(value[0]) != 0:
GPIO.output(R1, GPIO.LOW) #Turn On Relay 7 if all above conditions are true
time.sleep(SleepTimeSR);
GPIO.output(R1, GPIO.HIGH)
print('Family Room 4 ON')
else:
GPIO.output(R2, GPIO.LOW) #Turn On Relay 7 if all above conditions are true
time.sleep(SleepTimeSR);
GPIO.output(R2, GPIO.HIGH)
print('Family Room 4 OFF')
@blynk.on("V4") #Front Door
def V4_write_handler(value):
if int(value[0]) != 0:
GPIO.output(R3, GPIO.LOW)
time.sleep(SleepTimeSR);
GPIO.output(R3, GPIO.HIGH)
print('Front Door ON')
else:
GPIO.output(R4, GPIO.LOW)
time.sleep(SleepTimeSR);
GPIO.output(R4, GPIO.HIGH)
print('Front Door OFF')
@blynk.on("V5") #Down Stairs Light on
def V5_write_handler(value):
if int(value[0]) != 0:
GPIO.output(R13, GPIO.LOW)
time.sleep(SleepTimeSR);
GPIO.output(R13, GPIO.HIGH)
print('downstairs_on')
Blynk.logEvent('downstairs_on') #*Get an error here when light is turned on*
else:
GPIO.output(R14, GPIO.LOW)
time.sleep(SleepTimeSR);
GPIO.output(R14, GPIO.HIGH)
print('Downstairs OFF')
@blynk.on("V6") #Living Room
def V6_write_handler(value):
if int(value[0]) != 0:
GPIO.output(R15, GPIO.LOW)
time.sleep(SleepTimeSR);
GPIO.output(R15, GPIO.HIGH)
print('Living Room ON')
else:
GPIO.output(R16, GPIO.LOW)
time.sleep(SleepTimeSR);
GPIO.output(R16, GPIO.HIGH)
print('Living Room OFF')
@blynk.on("V8") #Relay Power
def V8_write_handler(value):
if int(value[0]) != 0:
GPIO.output(m, GPIO.HIGH)
print('Relay Power ON')
else:
GPIO.output(m, GPIO.LOW)
print('Relay Power OFF')
pinList = [11, 8, 10, 13, 15, 16, 18, 19, 22, 24, 26, 32, 36, 29, 31, 33, 21, 23]
pinList1 = [12]
pinList2 = [35]
pinList3 = [40]
pinList4 = [38] #Relay Power
pinList5 = [37] #Door bell Power
# loop through pins and set mode and state to 'low'
for i in pinList:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
for l in pinList3: #Door Bell
GPIO.setup(l, GPIO.IN)
for n in pinList5:
GPIO.setup(n, GPIO.OUT)
GPIO.output(n, GPIO.LOW)
#Virtual inputs Door Bell
def detect(ev=None):
state=GPIO.input(l)
blynk.virtual_write(12, state)
Blynk.logEvent('some_one_at_the_door') #**Get an Error when door bell rings**
GPIO.add_event_detect(l, GPIO.BOTH, callback=detect, bouncetime=200)
#Define Relay outputs
R1 = 11
R2 = 13
R3 = 15
R4 = 16
R5 = 18
R6 = 22
R7 = 32
R8 = 36
R9 = 29
R10 = 31
R11 = 33
R12 = 23
R13 = 19
R14 = 21
R15 = 8
R16 = 10
R17 = 24
R18 = 26
PIR_PIN = 12
PIR_IN = 35
PIR_DOOR = 40
SleepTimeS = 0.5
SleepTimeL = 1
SleepTimeSR = 0.4 #Time between Relay on off
#SleepTimePIR = 15 #Time for PIR light to be on
# Setup GPIO pins
GPIO.setup(R1, GPIO.OUT)
GPIO.setup(R2, GPIO.OUT)
GPIO.setup(R3, GPIO.OUT)
GPIO.setup(R14, GPIO.OUT)
GPIO.setup(R15, GPIO.OUT)
GPIO.setup(R16, GPIO.OUT)
GPIO.setup(R17, GPIO.OUT)
GPIO.setup(R18, GPIO.OUT)
time.sleep(SleepTimeL)
##Turn on House Relay Power "m" & Door Bell Power "n"
GPIO.output(n, GPIO.HIGH)
Current_State = 0
Previous_State = 0
Variable = 0
###today = datetime.date.today()
try:
# Loop until users quits with CTRL-C
while True :
blynk.run()
except KeyboardInterrupt:
print (" Quit")
# Reset GPIO settings
GPIO.cleanup()
#GPIO.cleanup()