Skip to content

Commit 83531be

Browse files
authored
Merge pull request #263 from arduino/sync/karlsoderby/mkr-iot-carrier-smart-garden
[MKC-441] MKR IoT Carrier Smart Garden Setup
2 parents 99fb582 + c1830af commit 83531be

File tree

9 files changed

+276
-0
lines changed

9 files changed

+276
-0
lines changed
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
---
2+
title: "IoT Smart Garden Setup with MKR IoT Carrier"
3+
description: "Build a smart garden setup with the MKR IoT Carrier, a pump, and a moisture sensor."
4+
tags: [IoT Cloud, Water Pump, Moisture Sensor, Temperature, Humidity]
5+
difficulty: beginner
6+
author: "Jacob Hylén, Hannes Siebeneicher, Karl Söderby"
7+
---
8+
9+
![Smart garden setup with MKR IoT Carrier.](assets/hero-image.png)
10+
11+
Decorating your home with plants is an easy way to bring some life into your day-to-day. The only problem is - those plants need water to survive, and if you forget to pay attention to them for a while you may need to start over. So instead of staying ever vigilant, why don't you spend an afternoon creating a setup that will let you both monitor the amount of moisture in your plants soil, and water your plants from afar using the [Arduino IoT Cloud](https://docs.arduino.cc/cloud/iot-cloud)?
12+
13+
## How It Works
14+
15+
The MKR IoT Carrier has built in relays that can let you control circuits that are powered separately. In this tutorial we will be using one of the relay modules on the carrier to control a pump that can provide one of your plants with water from the Arduino IoT Cloud thanks to the functionality of the [Arduino MKR WiFi 1010](https://store.arduino.cc/products/arduino-mkr-wifi-1010).
16+
17+
We will also connect a soil moisture sensor, and together with the sensors onboard the MKR IoT Carrier, we will create a sophisticated smart garden setup, capable of:
18+
19+
- Remote watering of a plant **(with a pump)**.
20+
- Check the moisture of your plant **(with a moisture sensor)**.
21+
- Check the temperature/humidity **(using the onboard HTS221 sensor)**.
22+
23+
![Live data streamed to the Arduino IoT Cloud.](assets/cloud-dashboard.gif)
24+
25+
## Hardware & Software Needed
26+
27+
- [MKR WiFi 1010](https://store.arduino.cc/products/arduino-mkr-wifi-1010)
28+
- [MKR IoT Carrier](https://store.arduino.cc/products/arduino-mkr-iot-carrier)
29+
- 5V submersible pump.
30+
- 1 meter watering pipe.
31+
- Water container.
32+
- USB adapter with at least 2 USB ports.
33+
- Micro-USB cable.
34+
- Open ended USB Cable.
35+
- [Soil moisture sensor](https://store.arduino.cc/products/grove-moisture-sensor).
36+
37+
### Apps and Online Services
38+
39+
- [Arduino IoT Cloud](https://docs.arduino.cc/cloud/iot-cloud)
40+
41+
## Hardware & Circuit Assembly
42+
43+
Begin by connecting the MKR WiFi 1010 board to the MKR IoT Carrier. Then, we need to connect the moisture sensor via a grove cable to the "A6" connector.
44+
45+
From the open ended USB cable, connect the positive wire (+)directly to relay 1 on the carrier, labelled "NO" (normally open). Connect the negative wire (-) together with the pump's negative wire.
46+
47+
Finally, connect the pump's positive wire, to the "COM" (common) pin on the relay. As a result, you have created a circuit that can be switched on/off via a relay. See next section for the full circuit.
48+
49+
### Circuit
50+
51+
Below is the complete circuit for this setup.
52+
53+
![Circuit for this project.](assets/circuit.png)
54+
55+
To connect the wires to the relay, see the image below:
56+
57+
![Connecting the wires to relay 1.](assets/relay-connection.png)
58+
59+
***Tip: to connect the power, pump & relay, you can use a connector strip. This will make your connections much more reliable & sturdy.***
60+
61+
## IoT Cloud Setup
62+
63+
***If you are new to the Arduino IoT Cloud, please refer to the [Getting Started Guide](https://docs.arduino.cc/cloud/iot-cloud/tutorials/iot-cloud-getting-started) or visit the [full documentation](https://docs.arduino.cc/cloud/iot-cloud) to learn more about the service.***
64+
65+
Begin by navigating to the [Arduino IoT Cloud](https://create.arduino.cc/iot/things). You will need to have a registered account with Arduino to use it. Follow the steps below to set up the Arduino IoT Cloud.
66+
67+
**1.** Create a new Thing, and select/configure the MKR WiFi 1010 board. Note that the board needs to be connected to your computer during this setup.
68+
69+
**2.** Create variables according to the table below:
70+
71+
| Name | Data Type | Function | Permission |
72+
| ----------- | --------- | -------------------------------------- | ------------ |
73+
| watering | boolean | Activate / de-activate pump | Read & Write |
74+
| waterTime | int | How long the pump should run (seconds) | Read & Write |
75+
| moisture | int | Read moisture | Read Only |
76+
| temperature | float | Read temperature | Read Only |
77+
| humidity | float | Read humidity | Read Only |
78+
79+
**3.** Enter your credentials to your Wi-Fi network in the network section.
80+
81+
**4.** Your Thing overview should now look like the following:
82+
83+
![Thing overview complete.](assets/thing-overview.png)
84+
85+
**5.** Go to the sketch tab, and use the following code:
86+
87+
```arduino
88+
/*
89+
MKR IoT Carrier Smart Garden Setup Project
90+
91+
A setup that allows for remote/local control of
92+
a pump, as well as reading sensors (moisture,
93+
temperature, humidity).
94+
95+
Built using the Arduino IoT Cloud service.
96+
97+
Components used:
98+
- MKR WiFi 1010
99+
- MKR IoT Carrier
100+
- Moisture Sensor
101+
- 5V water pump
102+
- USB Adapter with 2x slots
103+
- Micro USB Cable
104+
- Open ended USB Cable
105+
- Grove cable
106+
107+
Code by (c) Alessandro Ranelucci for Arduino.
108+
*/
109+
110+
#include "arduino_secrets.h"
111+
#include "thingProperties.h"
112+
113+
#include <Arduino_MKRIoTCarrier.h>
114+
#include <Arduino_OplaUI.h>
115+
116+
const int moistPin = A6;
117+
118+
unsigned long startedWatering;
119+
120+
MKRIoTCarrier opla;
121+
CycleWidgetsApp app;
122+
Gauge2_Widget moistureWidget;
123+
Bool_Widget wateringToggleWidget;
124+
125+
void setup() {
126+
Serial.begin(9600);
127+
delay(1500);
128+
129+
// Make sure the pump is not running
130+
stopWatering();
131+
132+
// Connect to Arduino IoT Cloud
133+
initProperties();
134+
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
135+
setDebugMessageLevel(4);
136+
ArduinoCloud.printDebugInfo();
137+
138+
// Configure widgets
139+
moistureWidget.attachValue(moisture);
140+
moistureWidget.setTitle("MOISTURE");
141+
moistureWidget.setRange(0, 100);
142+
moistureWidget.setDigits(0);
143+
moistureWidget.setSuffix(" %");
144+
moistureWidget.setReadOnly(true);
145+
146+
wateringToggleWidget.attachValue(watering);
147+
wateringToggleWidget.setTitle("PUMP");
148+
wateringToggleWidget.onValueChange(onWateringChange);
149+
150+
// Initialize Oplà
151+
CARRIER_CASE = true;
152+
opla.begin();
153+
154+
// Initialize the widget application
155+
app.begin(opla);
156+
app.addWidget(moistureWidget);
157+
app.addWidget(wateringToggleWidget);
158+
}
159+
160+
void loop() {
161+
ArduinoCloud.update();
162+
app.loop();
163+
164+
// Read the sensor and convert its value to a percentage
165+
// (0% = dry; 100% = wet)
166+
int raw_moisture = analogRead(moistPin);
167+
moisture = map(raw_moisture, 0, 1023, 0, 100);
168+
169+
temperature = opla.Env.readTemperature();
170+
humidity = opla.Env.readHumidity();
171+
172+
// Set the LED color according to the moisture percentage
173+
if (moisture > 40) {
174+
opla.leds.setPixelColor(1, 50, 0 , 0); // green
175+
} else if (moisture > 10) {
176+
opla.leds.setPixelColor(1, 50, 50 , 0); // yellow
177+
} else {
178+
opla.leds.setPixelColor(1, 0, 50 , 0); // red
179+
}
180+
opla.leds.show();
181+
182+
// Stop watering after the configured duration
183+
if (watering && (millis() - startedWatering) >= waterTime*1000) {
184+
stopWatering();
185+
}
186+
187+
delay(100);
188+
}
189+
190+
// This function is triggered whenever the server sends a change event,
191+
// which means that someone changed a value remotely and we need to do
192+
// something.
193+
void onWateringChange() {
194+
if (watering) {
195+
startWatering();
196+
} else {
197+
stopWatering();
198+
}
199+
}
200+
201+
void startWatering () {
202+
watering = true;
203+
startedWatering = millis();
204+
opla.Relay2.open();
205+
}
206+
207+
void stopWatering () {
208+
watering = false;
209+
opla.Relay2.close();
210+
}
211+
212+
void onWaterTimeChange() {
213+
// Add your code here to act upon WaterTime change
214+
}
215+
```
216+
217+
**6.** Upload the code. When successful, you can navigate over to the **"Dashboards"** section. Create a new dashboard.
218+
219+
**7.** Inside the dashboard view, click on **"Add"** then **"Things"** and select your Thing. This will generate a list of widgets and you can click on **"Create Widget"** to complete it. You should now see something similar to this dashboard:
220+
221+
![Dashboard overview.](assets/dashboard-overview.png)
222+
223+
Once you see the values changing, we know that the connection is successful, and we can monitor and interact with our device.
224+
225+
***In this dashboard, we replaced some of the widgets with nicer representations, like gauges and percentage.***
226+
227+
## Final Setup
228+
229+
We have now assembled the hardware + configured the Arduino IoT Cloud, and we are ready to start using our setup. Now, let's start using it.
230+
231+
**1.** If you have confirmed that the connection works, we can unplug the setup from the computer, and move it to the plant we want to monitor.
232+
233+
**2.** Place the moisture sensor into the soil of the plant.
234+
235+
![Moisture sensor in a plant.](assets/soil-sensor.png)
236+
237+
**3.** Place the pump inside a water container. Attach the plastic pipe to the pump, and place the other end into the plant pot. Place the MKR IoT Carrier next to the plant. Your setup could now look like this:
238+
239+
![Plant setup.](assets/smart-garden-setup.png)
240+
241+
**4.** Finally, plug in the USB adapter into the wall. This will now power the MKR IoT Carrier, which should now connect to the IoT Cloud, via your Wi-Fi network. And that is it, you now have a Smart Garden setup!
242+
243+
## Usage
244+
245+
Let's take a look at what our Smart Garden can do. To control it, we can either use the dashboard in the Arduino IoT Cloud, or the Arduino Remote app ([Playstore](https://play.google.com/store/apps/details?id=cc.arduino.cloudiot&hl=en&gl=US) / [Appstore](https://apps.apple.com/us/app/arduino-iot-cloud-remote/id1514358431)).
246+
247+
![Control and monitor your Smart Garden!](assets/cloud-dashboard.gif)
248+
249+
***In this dashboard, we have also added a chart widget for each of the variables we monitor.***
250+
251+
**Watering:** to activate the pump, do the following:
252+
- Select number of seconds that you want the pump to run for.
253+
- Click on the switch widget. The pump will now run for `x` amount of seconds, and then it will turn off.
254+
- You can also activate the pump through the MKR IoT Carrier. This is done via the touch buttons.
255+
256+
**Moisture:** monitor the moisture of your plant: if it is low, turn on the pump, and watch the moisture levels rise. The moisture of your plant can be viewed in the cloud dashboard, on the carrier's display or through the LED indiciator (red is bad, green is good).
257+
258+
**Display, buttons:** you can also view the moisture level locally, and activate the pump through the capacitive buttons.
259+
260+
**Temperature:** check temperature levels. Note that this may be inaccurate if placed directly in a sunny window!
261+
262+
**Humidity:** measure the relative humidity of your room. Some plants like it dry; some doesn't!
263+
264+
## Conclusion
265+
266+
With a smart garden setup, you can easily monitor the environment of your plant, and water it remotely. In this tutorial, we have gone through the basic elements needed for achieving just that: but there are more things you can do. Below is a list of some fun ideas that you can do with your plant:
267+
268+
- **Automatic watering** - instead of watering your plant remotely, you can also activate the pump automatically whenever moisture drops too low. We do however think it is more fun to control it from a phone, but the choice is yours.
269+
- **Cooling/heating fan** - you can use the other relay to connect a cooling/heating fan. This can help you bring the temperature to a perfect level (some plants like it cold, some hot).
270+
- **Humidifier** - a humidifier is an awesome component that increases the humidity (a perfect combo with the humidity sensor).
271+
- **UV lights** - a UV light allows you to grow plants even when there's no natural sun light.
272+
273+
274+
275+
276+

0 commit comments

Comments
 (0)