import dht
from machine import Pin, ADC
import utime
import random
import network
import BlynkLib
from BlynkTimer import BlynkTimer
# Pins for sensors
SOIL_MOISTURE_PIN = ADC(Pin(26)) # Simulated soil moisture pin (replace with real pin if needed)
WIFI_SSID = 'Wokwi-GUEST'
WIFI_PASS = ''
BLYNK_AUTH = 'jM0qdYPtiZdAUBT9yt7NpDe5ciZexNkA'
wifi = network.WLAN(network.STA_IF)
if not wifi.isconnected():
print("Connecting to WiFi...")
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASS)
while not wifi.isconnected():
print(".", end="_")
time.sleep(1)
print('\nWifi connected, IP:', wifi.ifconfig()[0])
blynk = BlynkLib.Blynk(BLYNK_AUTH, insecure=True)
timer = BlynkTimer()
# DHT22 sensor setup
DHT_PIN = Pin(4, Pin.IN)
dht_sensor = dht.DHT22(DHT_PIN)
def read_soil_moisture():
# Simulate random soil moisture level (replace with actual sensor reading)
return random.randint(0, 100)
def read_crop_health():
# Simulate random crop health (replace with actual sensor reading)
return random.uniform(0.0, 1.0)
def generate_random_coordinates():
# Generate random latitude and longitude values
latitude = random.uniform(-90, 90)
longitude = random.uniform(-180, 180)
return latitude, longitude
def send_data_to_cloud(location, soil_moisture, temperature, humidity, crop_health):
# Simulate cloud interaction (in a real project, use MQTT, HTTP, etc.)
print(f"Sending data to cloud - Location: {location}, Soil Moisture: {soil_moisture}, Temperature: {temperature}, Humidity: {humidity}, Crop Health: {crop_health}")
# Main loop
while True:
# Simulate random soil moisture level
soil_moisture = read_soil_moisture()
# Read DHT22 sensor values
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
# Simulate random crop health
crop_health = read_crop_health()
# Generate random GPS coordinates
latitude, longitude = generate_random_coordinates()
gps_location = f"({latitude:.6f}, {longitude:.6f})"
blynk.virtual_write(0, temperature)
time.sleep(0.1)
blynk.virtual_write(1, soil_moisture)
time.sleep(0.1)
blynk.virtual_write(2, humidity)
time.sleep(0.1)
blynk.virtual_write(3, crop_health)
# Simulate sending data to the cloud without caution display
send_data_to_cloud(gps_location, soil_moisture, temperature, humidity, crop_health)
# Simulate data transmission every 5 seconds
utime.sleep(5)
@blynk.on("connected")
def blynk_connected(ping):
print('Blynk Server is connected, ready. Ping:', ping, 'ms')
@blynk.on("disconnected")
def blynk_disconnected():
print('Blynk Server is disconnected')
# Main program
while True:
blynk.run()
timer.run()
time.sleep(1)