It’s hard to tell how much water does a plant need. Remember those times when you had to leave for a vacation? You risk finding your plants dead upon your return. I always find myself unprepared when I have to travel. Once, I watered my mint and lemon balm herbs excessively to make sure the plant had enough water until I come back. Little did I know, my plant would have tiny gnat flies all over it and was covered in spider nets. Those creatures thrived in the wet environment! I had no idea that excessive watering could cause so much trouble. After I almost lost both of my herbs, I decided to solve this problem through automation.
This was an excellent opportunity to write some code while solving a real problem, as well as learn how to play with my new toy - Arduino. Arduino has an excellent website with instructions on how to get started.
NOTE: Initially, I chose Arduino Mega 2560 for this project. Later, I realized that Arduino Uno was a more suitable choice due to its better compatibility with the USB Host Shield and the smaller physical size. Uno had just enough memory to hold the program I’ve written. If I had any more code, I would have been force to switch to Mega. Uno is only a few dollars cheaper than Mega 2560
Specification
- When the moisture sensor reading indicates that the moisture level is too low, the pump should turn on for a given amount of seconds.
- When the moisture sensor reading indicates that the moisture level is above the given threshold, the pump turns off.
- When the pump is on more than X number of seconds, the pump turns off.
- When the board is connected to a PC via USB, or to an Android device vie Bluetooth, the board is listening for commands and the user can change board configuration.
- When connected, a user can set the following configuration values:
- Threshold moisture level - the pump will turn on when the moisture level is below this value.
- Delay between waterings - the pump will turn on only once every X minutes.
Implementation
- Step 1: Controlling an Electric Outlet
- Step 2: Bluetooth Control
- Step 3: Moisture sensor and water pump
Takeaways
- Iterative development can and should be applied to hardware just as much as it applies to software: make several small changes and make sure that everything is still working. You don’t want to find yourself in a situation where the board doesn’t work the way as expected and you make a change to the hardware and the software at the same time - it is much harder to find out what went wrong at that time.
- Power source matters
- I bought a power adapter of eBay and connected it to the board. Some time later I noticed that the readings from the moisture sensor were jumping every second from 300 to 700. The poor quality power adapter must have generated too much noise, disrupting the signal from the analog moisture sensor. This not a problem that I ever had to deal with as a software engineer :)
- Stability of the Bluetooth connection also depends on the power source. When plugged into the USB hub, the board powered up, but it was impossible to establish the Bluetooth connection.
- Think ahead of how you are going to debug your program.
- You can’t always output everything to the serial port through Serial.print() in the main loop. It executes many times per second as the amount of data will be overwhelming and sometimes even hanged the Arduino IDE.
- Use constructor injections to manage dependencies. This will help in case you need to mock any objects to help you with debugging.
- Arduino Ide for Visual Studio was of a great help when I tried to debug some strange behavior, which, by the way, was caused by bad power source and wasn’t software-related. Nonetheless, this tool is great for debugging your Arduino program.
- Open Source is great. Thanks to BlueSerial, I got my Android application communicating with the board in no time!