Skip to main content

Week 11: Wifi and Networking

·523 words·3 mins
Sophie Fan
Author
Sophie Fan

Can I make my plants less thirsty?
#

For this week’s project, I wanted to be able to sense the soil moisture levels in the plants that I take care of, so I can know when to water them. Currently I have been relying by eye, but I want to use a more quantitative approach to watering them.

To do this, I used wifi to relay sensor data with the Xiao ESP32C6 module, then established an smtp protocol to send myself an email reminder to detect when soil humidity levels are low.

The Board
#

I used a capacitive sensor to detect soil humidity that outputs 0-3V, which is inversely related to soil moisture. The sensor itself also takes a 5V signal and a ground. I also included a footprint on the pcb for external source in case I wanted to use a battery pack to supply 4.5 V so the module could run independently, without USBC.

PCB Schematic
PCB
Final Board after Soldering

The capacitanc sensor was a bit tricky because it was hard to tell what was quantified as “dry” soil, but I found a value of > 1 usually indicated dry soil. The voltage measured by the sensor is inversely proportional to soil humidity.

The Code
#

The ESP32C6 comes with a built-in wifi module that I used to establish an smtp connection and send myself emails. I initially had a lot of trouble using Arduino IDE and then Platform IDE with the ESP32, the necessary ESP packages did not download. Thonny and micropython worked just fine, although flashing the bootloader was a bit trickier than I expected. Note, use “esptool.exe” instead of the suggested “esptool.py” (I am running on Windows). I mostly followed Seeed Studio’s helpful guide.

I used the helpful umail library from shawwwn on github for smtp connections.

Here is the final code: (Wifi and other credentials removed for security purposes)

import time
import utime
import umail
import network
from machine import Pin, ADC
LED_PIN = 15

ssid = 'MIT'
password = '****'

# Email details
sender_email = '****'
sender_name = 'Sophies Plants' #sender name
sender_app_password = '******'
recipient_email ='*****'
email_subject ='Plants are thirsty'

# Wifi connection
def connect_wifi(ssid, password):
  #Connect to your network
  station = network.WLAN(network.STA_IF)
  station.active(True)
  station.connect(ssid, password)
  while station.isconnected() == False:
    pass
  print('Connection successful')
  print(station.ifconfig())
  
# Function for sending email, port is 465 for gmail
def send_email():
    smtp = umail.SMTP('smtp.gmail.com', 465, ssl=True) # Gmail's SSL port
    smtp.login(sender_email, sender_app_password)
    smtp.to(recipient_email)
    smtp.write("From:" + sender_name + "<"+ sender_email+">\n")
    smtp.write("Subject:" + email_subject + "\n")
    smtp.write("Please water plants")
    smtp.send()
    smtp.quit()

# Checking sensor data every hour
while True :
    moisture = soil_in.read_u16() / 65535.0
    if moisture > 1.0:
        connect_wifi(ssid, password)
        send_email()
        time.sleep(3600)

And when I placed the capacitor in dry soil, it worked!

Received Plant Email!

Reflections
#

  • This week was pretty fun and it was a good way to introduce new skills to my coding repetoire.
  • Tape the bottom of the Xiao’s to prevent possible shorts due to bowing in the copper clad!
  • This time I also made sure that the USBC port on the Xiao hung off of the PCB for easy access, which was very helpful!