Can someone help i am using rpi4 as a microcontroller, the dht22 sends a temperature and humidity other sensor data to the rpi4b but it does not connect to blynk cloud I already check the auth token and v0 and v1 digital pins in the web dashboard and it alligned with my code 0,1 am I missing something? The app is offline but it reads.
Our project is A-IoT greenhouse monitoring and controlling system using raspi 4 and blynk as application.
This is the code I use
import board
import blynklib
import time
BLYNK_AUTH = 'remove for this post' # Replace with your actual Blynk Auth Token
blynk = blynklib.Blynk(BLYNK_AUTH)
# Attempt to connect to Blynk server
def connect_to_blynk():
global blynk
try:
blynk.connect()
except Exception as e:
print(f"Error connecting to Blynk server: {e}")
time.sleep(5) # Wait for 5 seconds before retrying
connect_to_blynk()
dht_device = adafruit_dht.DHT22(board.D5) # DHT22 connected to GPIO5
def read_sensor_data():
try:
# Read temperature and humidity
temperature = dht_device.temperature
humidity = dht_device.humidity
if humidity is not None and temperature is not None:
print(f'Temp={temperature:0.1f}*C Humidity={humidity:0.1f}%')
try:
blynk.virtual_write(0, temperature)
blynk.virtual_write(1, humidity)
except AttributeError:
print("Lost connection to Blynk server. Reconnecting...")
connect_to_blynk()
except Exception as e:
print(f"Failed to write to Blynk: {e}")
else:
print('Failed to get reading from the sensor')
except RuntimeError as error:
print(error.args[0])
# Ensure initial connection to Blynk
connect_to_blynk()
while True:
read_sensor_data()
if blynk.connected():
blynk.run()
else:
connect_to_blynk()
time.sleep(10) # Delay between readings```