import network
import time
import mip
# Connect WiFi to install packeges
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print("Connecting WiFi...")
wlan.connect("Wokwi-GUEST", "")
while not wlan.isconnected():
time.sleep(1)
print("Connected")
print(wlan.ifconfig())
# Install packages
try:
print("Installing Arduino IoT Cloud...")
mip.install("github:arduino/arduino-iot-cloud-py")
print("Installation Complete")
except Exception as e:
print("Install Error:", e)
import time
from machine import Pin
import dht
import network
from arduino_iot_cloud import ArduinoCloudClient
from secrets import WIFI_SSID, WIFI_PASS, DEVICE_ID, SECRET_KEY
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
wlan.connect(WIFI_SSID, WIFI_PASS)
while not wlan.isconnected():
time.sleep(1)
print("WiFi Connected")
print(wlan.ifconfig())
# Connect wifi to connect cloud
connect_wifi()
#sensors init
sensor = dht.DHT22(Pin(15))
led = Pin(12, Pin.OUT)
# define led variable read&write
def on_led_changed(client, value):
print("LED =", value)
if value:
led.on()
else:
led.off()
# Connect to cloud
client = ArduinoCloudClient(
device_id=DEVICE_ID,
username=DEVICE_ID,
password=SECRET_KEY,
sync_mode=True
)
# Variables defintion
client.register("Temprature", value=0.0)
client.register("Humidity", value=0.0)
client.register("Led", value=False, on_write=on_led_changed)
# Client start
try:
client.start()
except Exception as e:
print("Start Error:", e)
while True:
try:
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
# readings
client["Temprature"] = float(temperature)
client["Humidity"] = float(humidity)
print("Temp:", temperature, "Humidity:", humidity)
except Exception as e:
print("Sensor Error:", e)
try:
# readings update
client.update()
except Exception as e:
print("Cloud Update Error:", e)
time.sleep(5)