..

Raspberry Pi Pico Watering System (Part III)

Using Enviro Grow (Pico W Abroad) to water my plants (Part II).

Part II is here.

So after much dithering, trying and changing, I went with something I’m OK with. I’m not an architect, just a programmer who starts coding too soon.

Anyway my “old” architecture was a state machine, starting with nothing and moving between “wifi”, “dns”, “ntp”, “connection” and finally “mqtt” states. But I didn’t like it, and it didn’t feel very resilient (more on that later).

This time I thought “why would we ever move from a ‘wifi’ state, when we’re actually connected to Wi-Fi?”. So we don’t.

Instead, it’s a “component” architecture where each component has its own state (if it needs it), but all the components are active at the same time. Communication between components is done using messages, and messages can also be broadcast to all components.

Each component has a super-simple interface: init and handle-message. And that’s pretty much it.

Components:

  1. Wi-Fi component, responsible for connecting to the network
  2. NTP component, responsible for fetching the current time
  3. DNS component, responsible for resolving the hostname
  4. MQTT component, responsible for the communication with the MQTT broker
  5. Pump component, responsible for controlling the water pumps
  6. LED component, responsible for controlling the on-board LED.
  7. Heartbeat component, responsible for sending a message every 3 seconds.

This worked, more or less. For example, if the MQTT component detects that the MQTT connection is gone, it closes the TCP connection and tries again. It also listens to messages from the Wi-Fi component, in case the connection goes away.

The result: it didn’t work. It worked when I tested it at home, but when I finally tried it remotely during my holiday, both devices weren’t online. So resilience is not a trivial thing to be taken for granted, and deserves some thought.

But it did work when I tested it at home. To see the device connecting to AWS IoT and the web app also subscribing to it, and seeing the heartbeat messages and sending pump messages which watered my plants, that was lovely. But now that my plants are probably not going to survive, it proves the software must be written responsibly.

EDIT: I’m back from holiday and my plants seem to have survived. Maybe not well, but we’ll see how they cope. In any case, it was a bit of a disappointment. I’d like to know what went wrong, but there is zero debug information. I did get charged £0.22 for IoT Core though.