My devices (Raspberry Pi 4) is offline after 1 minute online .
But it still update new value until 11pm. (I set timer for the devices sleep 4 hour a day) Moreover I set it is a startup app. (It open when the window turn on)
And here my code:
from Lib import BlynkLib
from Class import dht_class as dc
from Class import relay_class as rc
import RPi.GPIO as GPIO
import board
import time
CONNECTING = True
while (CONNECTING):
try:
blynk = BlynkLib.Blynk('ql4jRS1qYC1XxotnreusAjcweppnafhb')
CONNECTING = False
except:
pass
time.sleep(5)
@blynk.on('connected')
def blynk_connected():
print("Blynk connected!")
@blynk.on('V4')
def v4_write_handler(value):
if value[0] == '1':
relay1.turn_on()
else:
relay1.turn_off()
@blynk.on('V5')
def v6_write_handler(value):
if value[0] == '1':
relay2.turn_on()
else:
relay2.turn_off()
@blynk.on('V6')
def v6_write_handler(value):
if value[0] == '1':
relay3.turn_on()
else:
relay3.turn_off()
#_______Relay pin initialization________#
Relay_Ch1 = 26
Relay_Ch2 = 20
Relay_Ch3 = 21
sensor_pin1 = board.D17
sensor_pin2 = board.D27
sensor_pin3 = board.D22
#_________Relay module initialization_________#
relay1 = rc.relay_module(Relay_Ch1)
relay2 = rc.relay_module(Relay_Ch2)
relay3 = rc.relay_module(Relay_Ch3)
#_________DHT22 sensor pin initialization_________#
sensor1 = dc.dht_module(dht_pin=sensor_pin1, relay_module=relay1)
sensor2 = dc.dht_module(dht_pin=sensor_pin2, relay_module=relay2)
sensor3 = dc.dht_module(dht_pin=sensor_pin3, relay_module=relay3)
def safe_write(pin, value):
try: blynk.virtual_write(pin, value)
except: print("Error writing!")
while 1:
blynk.run()
sensor1.read_humid()
sensor2.read_humid()
sensor3.read_humid()
sensor1.write_online(virtual_pin=1)
sensor2.write_online(virtual_pin=2)
sensor3.write_online(virtual_pin=3)
time.sleep(300)
state_list = [sensor1.relay.status, sensor2.relay.status, sensor3.relay.status]
print(state_list)
if sensor1.last_humid < 82 and state_list[1] is False and state_list[2] is False:
sensor1.relay.turn_on()
# blynk.virtual_write(4,1)
safe_write(4,1)
continue
elif sensor1.last_humid >= 88:
sensor1.relay.turn_off()
# blynk.virtual_write(4,0)
safe_write(4,0)
if sensor2.last_humid < 82 and state_list[0] is False and state_list[2] is False:
sensor2.relay.turn_on()
# blynk.virtual_write(5,1)
safe_write(5,1)
continue
elif sensor2.last_humid >= 88:
sensor2.relay.turn_off()
# blynk.virtual_write(5,0)
safe_write(5,0)
if sensor3.last_humid < 80 and state_list[1] is False and state_list[0] is False:
sensor3.relay.turn_on()
# blynk.virtual_write(6,1)
safe_write(6,1)
continue
elif sensor3.last_humid >= 88:
sensor3.relay.turn_off()
# blynk.virtual_write(6,0)
safe_write(6,0)
I know that the device is still working is good, but my devices is set up far from me so I don’t know when it is offline real or fake.