The Plant Monitor receives single command letters over the serial connection and responds to the command letter. So, for example, sending the letter w
causes the Plant Monitor to read the wetness level and respond with a message w=67
where the number after the =
is the wetness as a percentage. Other commands are t
for temperature, h
for humidity and j
which returns a JSON representation of wetness, temperature and humidity in one message.
This human readable message format, means that we can experiment by sending commands to the Plant Monitor using Mu's REPL.
Connect your Circuit Playground Express to your computer with a known good data+power USB cable. Run Mu and click the Serial button in the list of buttons at the top of the editor window. You should see the Mu window split to show the serial window towards the bottom. If you do not get the >>>
prompt, press the control key and then the c key at the same time.
This is a CircuitPython REPL session. Type in the following commands after the >>>
prompts:
Adafruit CircuitPython 8.0.5 on 2023-03-31; Adafruit CircuitPlayground Express with samd21g18 >>> import board >>> import busio >>> uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=0.1) >>> uart.write(b"h") 1 >>> uart.read() b'h=33.24\r\n' >>>
After we have made the necessary imports of board
and busio
, an instance of busio.UART
is created. This specifies the pins to use (in this case the default RX
and TX
pins for the board) and the baud rate of 9600
(expected by the Plant Monitor). The timeout
parameter shortens the 2 second default timeout that would otherwise occur.
The line uart.write(b"h")
writes the byte array string of h
(for humidity) to the Plant Monitor.
If you are not getting responses, check your wiring.
To read the Plant Monitor's response use uart.read()
. In this case, you can see that the response message contains the humidity value of 33.24
followed by the end of line characters.
The wetness can be read by sending the w
command. But before running the first of the commands below, grip the prong of the Plant Monitor in the palm of your hand (see above). This will register the moisture of your hand so that the reading isn't 0
.
>>> uart.write(b"w") 1 >>> uart.read() b'w=74\r\n' >>>
Here you can see that the wetness level is reported as 74%.
If you wanted to read the temperature, you would send the message t
. Note that this is in degrees Celsius. To convert this into Fahrenheit, multiply the number by 9, divide the result by 5 and then add 32.
Here's how use serial communication in a program that prints out the wetness reading roughly once a second.
import board import busio from time import sleep uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=0.1) while True: uart.write(b"w") wetness_str = uart.read() # b'w=53\r\n' wetness = float(wetness_str[2:-2]) # extract number from response print(wetness) sleep(1)
Inside the main while loop, the w
message is sent and the result read. We are only interested in the number part of the message, so we can chop off the first two characters (w=)
and the last two end of line characters with the command wetness_str[2:-2]
before converting the string into a float.
The following example program makes use of the Circuit Playground Express built-in NeoPixels to display the wetness level.
import board import busio import neopixel from time import sleep num_leds = 10 uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=0.1) pixels = neopixel.NeoPixel(board.NEOPIXEL, num_leds, brightness=0.2, auto_write=False) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) OFF = (0, 0, 0) def show_wetness(percent): led_index = int((percent - 1) / 10) color = RED if led_index > 6: color = GREEN elif led_index > 3: color = YELLOW print(led_index) for i in range(0, num_leds): if i <= led_index: pixels[i] = color else: pixels[i] = OFF pixels.show() while True: uart.write(b"w") wetness_str = uart.read() # b'w=53\r\n' wetness = int(wetness_str[2:-2]) # extract number from response show_wetness(wetness)
The code for reading the wetness is the same as the previous example. The code that displays the wetness is in the show_wetness
function. This calculates the number of LEDs to light (led_index)
as the percentage wetness minus 1 divided by 10 to give an range of 0 to 9, corresponding to the Circuit Playground Express' 10 NeoPixel LEDs.
The function then chooses a color of RED
, YELLOW
or GREEN
using the value of led_index
.
The for
loop goes through all 10 LED positions, either lighting the LED in the chosen color (if its index position is less than or equal to led_index
) or otherwise turning that LED position off.
Text editor powered by tinymce.