Skip to content

Commit

Permalink
Merge pull request #2 from balena-io-playground/pi4-testrun
Browse files Browse the repository at this point in the history
Refresh project with Pi4 testrun
  • Loading branch information
andrewnhem committed Aug 14, 2020
2 parents f3b1225 + a0a9951 commit 50fa53d
Show file tree
Hide file tree
Showing 19 changed files with 167 additions and 30 deletions.
148 changes: 146 additions & 2 deletions README.md
@@ -1,5 +1,149 @@
# Balena Plant Saver

This is a Raspberry Pi balenaCloud starter project to help you water your precious plants. See temperature, humidity, and soil moisture levels. Set watering thresholds and timings to automate the watering of your plants.

![](https://github.com/balena-io-playground/balena-plant-saver/blob/master/img/img01.jpg?raw=true)
![](https://github.com/balena-io-playground/balena-plant-saver/blob/master/img/img02.jpg?raw=true)
![](https://github.com/balena-io-playground/balena-plant-saver/blob/master/img/img03.png?raw=true)
![](https://github.com/balena-io-playground/balena-plant-saver/blob/master/img/img03.png?raw=true)

Access your plant's dashboard from anywhere in the world using balenaCloud. Add new plant monitors quickly and easily using the same application code on different devices. Use your imagination (and some extra parts) and turn balenaPlant into a balenaGarden. :)

![](https://github.com/balena-io-playground/balena-plant-saver/blob/master/img/01-all-parts.jpg)

## Hardware you'll need
This project uses the Pimironi Automation pHAT to make things a bit more user-friendly. Here's the list of materials:

* Raspberry Pi ZeroWH/3B/4 (that's what we've tested so far)
* A 32GB+ SD car
* Pimironi Automation pHAT
* DHT22 temperature and humidity sensor
* Capacitive soil moisture sensor
* Float switch
* 5V peristaltic pump (with water tube-- some vendors don't include the tubing-- *remember the tubing!!!*)

## Software you'll need
Here's the software that you'll need to get going:

* a free [balenaCloud](https://dashboard.balena-cloud.com/signup) account (first ten devices are fully featured and free)
* [balenaEtcher](https://www.balena.io/etcher/) to burn OS images to SD cards
* (optional) [balenaCLI](https://www.balena.io/docs/reference/balena-cli/) if you want to hack on this project, push code locally, etc.

![](/img/01b-plant-water.jpg)

Oh yeah... you'll want a plant to test with. We recommend something resilient that you can over or underwater as you test. A bamboo works great. And the most important part: *water*.

## Set up the hardware

### Connect the Automation pHAT to the Raspberry Pi

After preparing the Automation pHAT (soldering terminals on, etc.), connect it to the Raspberry Pi via GPIO pins.

### Solder on DHT22 sensor

![](/img/02-dht22.jpg)

Connect the power and ground to the 3.3V pins. Connect the data pin to the `SCLK` pin, which is GPIO pin 11 (you'll see this referenced in the plant watering code).

### Add capacitive moisture sensor

![](/img/03-soil-moisture-sensor.jpg)

As seen on this [balenaForum post](https://forums.balena.io/t/building-a-smart-houseplant-monitor-and-waterer/9170/21), it's helpful to paint or coat the exposed electronic components of this sensor with outdoor paint or nail polish. Power the soil moisture sensor using the 5V terminals on the Automation pHAT, and insert the data wire into the `ADC 1` terminal (since the sensor is giving us an analog signal to convert).

### Add the float switch

![](/img/04-float-switch.jpg)

Power the float switch using the 5V terminal and connect the other wire into the `INPUT 1` terminal.

### Add the water pump

![](/img/05-pump-relay.jpg)

Power the peristaltic pump using the 5V terminal on the Automation pHAT. Connect the pump's ground wire to the `NO` (Normally Open) terminal.

![](/img/06-pump-replay-close.jpg)

Close the circuit for the water pump by using a wire to connect the `COM` terminal to `GND`.

### Set everything up

![](/img/07-test-setup.jpg)

At this point, you can set your plant watering test up. Add the moisture sensor to the plant's soil (or lack thereof in the case of this bamboo). Add the pumping end of the water pump into your water source and the dousing end into the plant's soil. NOTE: It's not clear on some pumps as to which port does what-- once you test, mark or tape a line to help tell which is which.

#### Build or 3D print additional accessories

![](/img/img02.jpg?raw=true)

The original project creators built 3D-printed parts to hold various parts or to enclose the electronics for the project. Check out this [forum thread](https://forums.balena.io/t/building-a-smart-houseplant-monitor-and-waterer/9170/21) to learn more.

## Add and deploy the application

### Deploy with balena

Once you're logged into your balenaCloud account (remember: first ten devices are free and fully-featured), click the button below to automagically deploy the project to your account.

[![Deploy with balena](https://camo.githubusercontent.com/610358f5d0de9bfe856b58d4e22dcc64db6383cc/68747470733a2f2f62616c656e612e696f2f6465706c6f792e706e67)](https://dashboard.balena-cloud.com/deploy)

![](https://www.balena.io/blog/content/images/2020/07/deploy-default.png)

Name your application, select your device type, and click `Create and deploy`. You'll be taken to your dashboard and our builders will start creating your application in the background.

![](https://www.balena.io/blog/content/images/2020/06/build-indicator.png)

### Add a device

![](https://www.balena.io/blog/content/images/2020/06/os-download.png)

Once that step is complete, add a new device by clicking `Add device` and selecting your device type. If you plan on using Wi-Fi, add your credentials here. Otherwise, proceed with an Ethernet cable connected to your internet setup. Complete the modal and your OS will download.

### Flash the OS onto your SD card

![](https://www.balena.io/blog/content/images/2020/07/etcher-1.png)

Insert the SD card into your computer, boot up Etcher and flash the downloaded balenaOS to your card. Insert the card into your device and wait for it to power on and show up on your dashboard within your app. Once the device downloads all the software, the application should be ready to use.

## Using balenaPlant

![](/img/device-url.jpg)

Now it's time to put everything to the test. Access your device within the application. You'll see its details including tags that show moisture levels and whether or not the water level is high or low. You'll also see a local or public URL option for the device. Use either of these to access the Grafana dashboard for the sensors.

![](/img/grafana-dash.jpg)

Clicking on either URL takes you to the Grafana dashboard for the setup. From here you can see a timeseries graph of temperature and humidity, water level, and recorded pump activity. For advanced Grafana users, feel free to experiment with adding alerts for each chart (we won't cover that in this readme though).

### Automated watering

By default, balenaPlant checks the water level of the plant approximately every 15 minutes. This is a reasonable time for a few reasons:

* running the pump infrequently prevents the motor from burning out
* watering with long intervals between each session allows soil or growing material to absorb water for a more accurate reading

![](/img/device-var.jpg)

You can change the pump delay time by changing the `Device variable` (located on the left-side menu in balenaCloud). Set `pump_delay` to a higher number for a longer wait time (more pump cycle counts) and a lower one for a shorter wait between watering. If properly set, you'll never overwater because even if the elapsed delay goes by, the pump won't start if the moisure level meets your requirements.

Other device variables to change include:

| Device variable | Definition | Default value |
|-----------------------|--------------------------------------------|---------------|
| target_soil_moisture | Sets target moisture percentage | 60 |
| target_soil_threshold | Sets threshold of moisture past target | 15 |
| pump_delay | Adds time between how often pump code runs | 15 |

*NOTE: When you change a device variable,it will reset the device.*

## Experiment and explore

![](/img/08-basil-test.jpg)

Once you test a few things here and there, try other kinds of plants, adjust the soil moisture targets, and take one chore off your to-do list.

### Build a fleet of balenaPlant devices

Why automate the watering of one plant when you could grow and nurture many more? What if you upgrade the pump? What if you add LED grow lights?? The possibilities are endless.

### Additional resources

Check out this [livestream build](https://www.youtube.com/watch?v=r0ZR6COwdRo) (uses the balenaCLI method) or [visit our Forums](https://forums.balena.io/t/building-a-smart-houseplant-monitor-and-waterer/9170/21) for more information.
23 changes: 5 additions & 18 deletions grafana/Dockerfile.template
@@ -1,26 +1,11 @@
# FROM balenalib/%%BALENA_MACHINE_NAME%%

# COPY ./grafana.ini /usr/share/grafana/conf/custom.ini
# COPY ./entry.sh /usr/src/app/entry.sh
# COPY ./provisioning /usr/src/app/provisioning

# RUN install_packages apt-transport-https && \
# echo "deb https://packages.grafana.com/oss/deb stable main" > /etc/apt/sources.list.d/grafana.list && \
# curl https://packages.grafana.com/gpg.key | apt-key add - && \
# apt-get update && \
# install_packages grafana && \
# chown -R root:grafana /usr/src/app/provisioning

# RUN chmod +x /usr/src/app/entry.sh

# CMD ["/usr/src/app/entry.sh"]

FROM balenalib/%%BALENA_MACHINE_NAME%%:buster

COPY ./grafana.ini /usr/share/grafana/conf/custom.ini
COPY ./provisioning /usr/src/app/provisioning
COPY ./*.sh /usr/src/app/

RUN chmod +x /usr/src/app/*.sh

RUN install_packages \
fontconfig-config \
fonts-dejavu-core \
Expand All @@ -29,8 +14,10 @@ RUN install_packages \
jq \
wget

RUN chmod +x /usr/src/app/download.sh && /usr/src/app/download.sh "%%BALENA_MACHINE_NAME%%"
RUN /usr/src/app/download.sh "%%BALENA_ARCH%%"

RUN dpkg -i /tmp/grafana.deb && rm /tmp/grafana.deb

RUN mkdir "/usr/src/app/provisioning/notifiers"

CMD ["/bin/sh","/usr/src/app/entry.sh"]
15 changes: 10 additions & 5 deletions grafana/download.sh
@@ -1,7 +1,12 @@
#!/bin/sh

if [ "$1" = "raspberry-pi" ]; then
wget -O /tmp/grafana.deb https://dl.grafana.com/oss/release/grafana-rpi_6.2.5_armhf.deb;
else
wget -O /tmp/grafana.deb https://dl.grafana.com/oss/release/grafana_6.2.5_armhf.deb;
fi
outfile="/tmp/grafana.deb"
download_base="https://dl.grafana.com/oss/release/"
case $1 in
rpi) package_file="grafana-rpi_7.0.5_armhf.deb"
;;
aarch64) package_file="grafana_7.0.5_arm64.deb"
;;
*) package_file="grafana_7.0.5_armhf.deb"
esac
wget -O "${outfile}" "${download_base}${package_file}"
1 change: 1 addition & 0 deletions grafana/grafana.ini
Expand Up @@ -3,3 +3,4 @@ data = /data/grafana

[auth.anonymous]
enabled = true
org_role = Admin
Binary file added img/01-all-parts.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/01b-plant-water.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/02-dht22.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/03-soil-moisture-sensor.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/04-float-switch.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/05-pump-relay.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/06-pump-replay-close.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/07-test-setup.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/08-basil-test.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/device-url.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/device-var.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/grafana-dash.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/pump-working.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions influxdb/Dockerfile.template
Expand Up @@ -2,6 +2,6 @@ FROM balenalib/%%BALENA_MACHINE_NAME%%-alpine

RUN apk add influxdb

RUN sed -i 's|/var/lib/influxdb|/data/influxdb|g' /etc/influxdb/influxdb.conf
RUN sed -i 's|/var/lib/influxdb|/data/influxdb|g' /etc/influxdb.conf

CMD influxd
CMD ["influxd"]
6 changes: 3 additions & 3 deletions plantsaver/plantinha.py
Expand Up @@ -18,7 +18,7 @@ def __init__(self):
self.dht_pin = int(self.set_variable("dht_pin", 11))
self.max_value = float(self.set_variable("max_value", 2.77))
self.min_value = float(self.set_variable("min_value", 1.46))
self.target_soil_moisture = int(self.set_variable("target_soil_moisture", 60))
self.target_soil_moisture = int(self.set_variable("target_soil_moisture", 60))
self.target_soil_threshold = int(self.set_variable("target_soil_threshold", 15))
self.pump_delay = int(self.set_variable("pump_delay", 15))

Expand All @@ -45,7 +45,7 @@ def set_variable(self, name, default_value):
else:
self.value = default_value
return self.value

def read_moisture(self):
self.moisture_level= 100-(automationhat.analog.one.read()-self.min_value)/((self.max_value-self.min_value)/100)

Expand Down Expand Up @@ -124,4 +124,4 @@ def pump_water(self, action):
def tick(self):
self.update_sensors()
self.update_status()
self.write_measurements()
self.write_measurements()

0 comments on commit 50fa53d

Please sign in to comment.