from machine import Pin
from time import sleep
import network
import logging
import sys
from aht10 import *
from arduino_iot_cloud import ArduinoCloudClient
WIFI_SSID="MySSID"
WIFI_PASSWORD = "MyPassword"
# picoW
DEVICE_ID="097998f9-f161-xxxx-xxxx-xxxxxxxx9cb7"
CLOUD_PASSWORD="pZ#117xxxxxxx!L?LAnyL1H3"
sw1 = Pin(13,Pin.OUT)
sw2 = Pin(14,Pin.OUT)
sw3 = Pin(15,Pin.OUT)
i2c = machine.I2C(0, scl=machine.Pin(1), sda=machine.Pin(0), freq=400000)
detect_sensor(i2c)
reading=convert(i2c)
Temp=reading[0]
Humidity=reading[1]
def wifi_connect():
if not WIFI_SSID or not WIFI_PASSWORD:
raise (Exception("Network is not configured."))
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
while not wlan.isconnected():
logging.info("Trying to connect. Note this may take a while...")
sleep(0.5)
logging.info(f"WiFi Connected {wlan.ifconfig()}")
def onSW1Change(client,value):
if value:
sw1.value(1)
print('SW1 ON!')
else:
sw1.value(0)
print('SW1 OFF!')
def onSW2Change(client,value):
if value:
sw2.value(1)
print('SW2 ON!')
else:
sw2.value(0)
print('SW2 OFF!')
def onSW3Change(client,value):
if value:
sw3.value(1)
print('SW3 ON!')
else:
sw3.value(0)
print('SW3 OFF!')
def read_temperature(client):
detect_sensor(i2c)
reading=convert(i2c)
temperature_Celcius=reading[0]
humidity_percent=reading[1]
return temperature_Celcius
def read_humidity(client):
detect_sensor(i2c)
reading=convert(i2c)
temperature_Celcius=reading[0]
humidity_percent=reading[1]
return humidity_percent
if __name__ == "__main__":
logging.basicConfig(datefmt="%H:%M:%S",format="%(asctime)s.%(msecs)03d %(message)s",level=logging.INFO,)
wifi_connect()
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=CLOUD_PASSWORD)
client.register('sw1', value=None, on_write=onSW1Change, interval=0.25)
client.register('sw2', value=None, on_write=onSW2Change, interval=0.25)
client.register('sw3', value=None, on_write=onSW3Change, interval=0.25)
client.register('Temp', on_read=read_temperature, interval=10)
client.register('Humidity', on_read=read_humidity, interval=10)
client.start()


