code.py
  1  # SPDX-FileCopyrightText: 2021 Eva Herrada for Adafruit Industries
  2  # SPDX-License-Identifier: MIT
  3  
  4  import time
  5  
  6  import board
  7  from digitalio import DigitalInOut
  8  from adafruit_esp32spi import adafruit_esp32spi
  9  from adafruit_esp32spi import adafruit_esp32spi_wifimanager
 10  import adafruit_esp32spi.adafruit_esp32spi_socket as socket
 11  import neopixel
 12  import adafruit_minimqtt.adafruit_minimqtt as MQTT
 13  from adafruit_io.adafruit_io import IO_MQTT
 14  from adafruit_seesaw.seesaw import Seesaw
 15  import busio
 16  
 17  # Used to make sure that the Adafruit IO Trigger is only run once when the moisture value is below
 18  # the desired threshold, set in MIN
 19  LOW = False
 20  # The minimum moisture value. If the value is below this number, it will activate the Adafruit IO
 21  # trigger. This number should match the number you set in your Adafruit IO trigger. Feel free
 22  # to mess around and try out different moisture values as how wet this actually is can vary a lot
 23  # depending on where the sensor is and the soil in the pot.
 24  MIN = 500
 25  
 26  # Set up moisture sensor with seesaw
 27  i2c_bus = board.I2C()
 28  seesaw = Seesaw(i2c_bus, addr=0x36)
 29  
 30  # Get wifi details and more from a secrets.py file
 31  try:
 32      from secrets import secrets
 33  except ImportError:
 34      print("WiFi secrets are kept in secrets.py, please add them there!")
 35      raise
 36  
 37  # Set up WiFi
 38  esp32_cs = DigitalInOut(board.D13)
 39  esp32_ready = DigitalInOut(board.D11)
 40  esp32_reset = DigitalInOut(board.D12)
 41  
 42  spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
 43  esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
 44  status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
 45  wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
 46  
 47  # Define callback functions which will be called when certain events happen.
 48  # pylint: disable=unused-argument
 49  def connected(client):
 50      # This method is called when the client connects to Adafruit IO
 51      client.subscribe("plant")
 52  
 53  
 54  def subscribe(client, userdata, topic, granted_qos):
 55      # This method is called when the client subscribes to a new feed.
 56      print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
 57  
 58  
 59  def message(client, feed_id, payload):
 60      # This method is called when a feed receives a new message
 61      print("Feed {0} received new value: {1}".format(feed_id, payload))
 62  
 63  
 64  # Connect to WiFi
 65  print("Connecting to WiFi...")
 66  wifi.connect()
 67  print("Connected!")
 68  
 69  # Initialize MQTT interface with the esp interface
 70  MQTT.set_socket(socket, esp)
 71  
 72  # Initialize a new MQTT Client object
 73  mqtt_client = MQTT.MQTT(
 74      broker="io.adafruit.com",
 75      username=secrets["aio_username"],
 76      password=secrets["aio_key"],
 77  )
 78  
 79  
 80  # Initialize an Adafruit IO MQTT Client
 81  io = IO_MQTT(mqtt_client)
 82  
 83  # Connect the callback methods defined above to Adafruit IO
 84  io.on_connect = connected
 85  io.on_subscribe = subscribe
 86  io.on_message = message
 87  
 88  # Connect to Adafruit IO
 89  print("Connecting to Adafruit IO...")
 90  io.connect()
 91  plant_feed = "plant"
 92  
 93  START = 0
 94  
 95  while True:
 96      # read moisture level through capacitive touch pad
 97      touch = seesaw.moisture_read()
 98  
 99      # read temperature from the temperature sensor
100      temp = seesaw.get_temp()
101  
102      if touch < MIN:
103          if not LOW:
104              io.publish(plant_feed, touch)
105              print("published")
106          LOW = True
107  
108      elif touch >= MIN and time.time() - START > 10:
109          io.publish(plant_feed, touch)
110          print("published to Adafruit IO")
111          START = time.time()
112          LOW = False
113  
114      print("temp: " + str(temp) + "  moisture: " + str(touch))
115      time.sleep(1)