# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries # SPDX-License-Identifier: MIT import os import time import ssl import sys import wifi import socketpool import microcontroller import board import busio import adafruit_requests import adafruit_ahtx0 import adafruit_dht from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError from microcontroller import cpu from analogio import AnalogIn analog_in0 = AnalogIn(board.A0) # Pin 31 (Right side, 10 down from top) analog_in1 = AnalogIn(board.A1) # Pin 32 (Right side, 9 down from top) minLightValue = .1 maxLightValue = 2.8 minPotValue = .1 maxPotValue = 3.3 def getAnalogValue(pin): return [pin.value,(pin.value * 3.3) / 65536] def readDHTSensor(): runFlag = True while runFlag: try: # Print the values to the serial port temperature_c = dhtDevice.temperature temperature_f = temperature_c * (9 / 5) + 32 humidity = dhtDevice.humidity print( "Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format( temperature_f, temperature_c, humidity ) ) runFlag = False except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going print(error.args[0]) time.sleep(2.0) continue except Exception as error: dhtDevice.exit() raise error return [temperature_f, humidity, temperature_c] # Initial the dht device, with data pin connected to: dhtDevice = adafruit_dht.DHT11(board.GP4) wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) aio_username = os.getenv('aio_username') aio_key = os.getenv('aio_key') pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) print("connected to io") # use Pico W's GP0 for SDA and GP1 for SCL #i2c = busio.I2C(board.GP1, board.GP0) #aht20 = adafruit_ahtx0.AHTx0(i2c) try: # get feed picowTemp_feed = io.get_feed("mytemperature") picowHumid_feed = io.get_feed("myhumidity") picowLight_feed = io.get_feed("mylightlevel") picowPot_feed = io.get_feed("mypotlevel") except AdafruitIO_RequestError: # if no feed exists, create one print("Feed error") sys.exit(-1) # pack feed names into an array for the loop feed_names = [picowTemp_feed, picowHumid_feed,picowLight_feed,picowPot_feed] print("feeds created") clock = 300 while True: try: # when the clock runs out.. if clock > 10: # read sensors lightLevel = getAnalogValue(analog_in0) scaledLightValue = lightLevel[1]/maxLightValue * 100 potLevel = getAnalogValue(analog_in1) scaledPotValue = potLevel[1]/maxPotValue * 100 print("Scaled Light Value = ",scaledLightValue) print("Scaled Pot Value = ",scaledPotValue) # print("light level: ",lightLevel) # lightPerCent = lightLevel[1] /3 dhtData = readDHTSensor() data = [dhtData[0], dhtData[1], scaledLightValue,scaledPotValue] # send sensor data to respective feeds for z in range(4): io.send_data(feed_names[z]["key"], data[z]) # print("sent %0.1f" % data[z]) time.sleep(1) # print sensor data to the REPL # print(type(data[0]), type(data[1]), type(data[2])) print(f"{data[0]:.1f}F {data[1]:.1f}% Humidity {data[2]:.1f}% Light Level {data[3]:.1f}% Pot Level") # print("\nTemperature: %0.1f C" % data[0]) # print("Humidity: %0.1f %%" % data[1]) # print("\nLight percent: %0.1f " % data[2]) # print() time.sleep(1) # reset clock clock = 0 else: clock += 1 # pylint: disable=broad-except # any errors, reset Pico W except Exception as e: print("Error:\n", str(e)) # print("Resetting microcontroller in 10 seconds") time.sleep(3) # microcontroller.reset() # delay time.sleep(1) print(clock)