Components and supplies
Water Pump
5V Relay Module
Gravity: Analog Capacitive Soil Moisture Sensor - Corrosion Resistant
Arduino MKR WiFi 1010
Apps and platforms
Arduino IoT Cloud
Project description
Code
Irrigation_System.ino
cpp
Main Sketch
1#include "arduino_secrets.h" 2#include "thingProperties.h" 3 4const int rpin_1 = 3; 5const int rpin_2 = 4; 6const int rpin_3 = 5; 7 8const int threshold = 80; 9 10Schedule schedValues = plant_schedule_1.getValue(); 11 12void setup() { 13 14 Serial.begin(9600); 15 16 //declare relay pins 17 pinMode(rpin_1, OUTPUT); 18 pinMode(rpin_2, OUTPUT); 19 pinMode(rpin_3, OUTPUT); 20 21 delay(1500); 22 23 initProperties(); 24 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 25 setDebugMessageLevel(2); 26 ArduinoCloud.printDebugInfo(); 27} 28 29void loop() { 30 ArduinoCloud.update(); 31 sensor_1 = sensorReading(A1); 32 sensor_2 = sensorReading(A2); 33 sensor_3 = sensorReading(A3); 34 35 onPlantSchedule1Change(); 36 onPlantSchedule2Change(); 37 onPlantSchedule3Change(); 38 39} 40 41int sensorReading(int pin){ 42 int result; 43 int reading = analogRead(pin); 44 result = map(reading, 0, 1023, 100, 0); 45 return result; 46} 47 48void onPlantSchedule1Change() { 49 if (automatic_mode) { 50 if (sensor_1 < threshold && plant_schedule_1.isActive()) { 51 digitalWrite(rpin_1, HIGH); 52 } else { 53 digitalWrite(rpin_1, LOW); 54 } 55 } 56} 57 58void onPlantSchedule2Change() { 59 if (automatic_mode) { 60 if (sensor_2 < threshold && plant_schedule_1.isActive()) { 61 digitalWrite(rpin_2, HIGH); 62 } else { 63 digitalWrite(rpin_2, LOW); 64 } 65 } 66} 67 68void onPlantSchedule3Change() { 69 if (automatic_mode) { 70 if (sensor_3 < threshold && plant_schedule_1.isActive()) { 71 digitalWrite(rpin_3, HIGH); 72 } else { 73 digitalWrite(rpin_3, LOW); 74 } 75 } 76} 77 78void onRelay1Change() { 79 if (relay_1) { 80 digitalWrite(rpin_1, HIGH); 81 } else { 82 digitalWrite(rpin_1, LOW); 83 } 84} 85 86void onRelay2Change() { 87 if (relay_2) { 88 digitalWrite(rpin_2, HIGH); 89 } else { 90 digitalWrite(rpin_2, LOW); 91 } 92} 93 94void onRelay3Change() { 95 if (relay_3) { 96 digitalWrite(rpin_3, HIGH); 97 } else { 98 digitalWrite(rpin_3, LOW); 99 } 100} 101 102void onAutomaticModeChange() { 103}
thingProperties.h
cpp
Header file for IoT Cloud, do not edit.
1// Code generated by Arduino IoT Cloud, DO NOT EDIT. 2 3#include <ArduinoIoTCloud.h> 4#include <Arduino_ConnectionHandler.h> 5 6const char SSID[] = SECRET_SSID; // Network SSID (name) 7const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP) 8 9void onPlantSchedule1Change(); 10void onPlantSchedule2Change(); 11void onPlantSchedule3Change(); 12void onAutomaticModeChange(); 13void onRelay1Change(); 14void onRelay2Change(); 15void onRelay3Change(); 16 17int sensor_1; 18int sensor_2; 19int sensor_3; 20CloudSchedule plant_schedule_1; 21CloudSchedule plant_schedule_2; 22CloudSchedule plant_schedule_3; 23bool automatic_mode; 24bool relay_1; 25bool relay_2; 26bool relay_3; 27 28void initProperties(){ 29 30 ArduinoCloud.addProperty(sensor_1, READ, ON_CHANGE, NULL); 31 ArduinoCloud.addProperty(sensor_2, READ, ON_CHANGE, NULL); 32 ArduinoCloud.addProperty(sensor_3, READ, ON_CHANGE, NULL); 33 ArduinoCloud.addProperty(plant_schedule_1, READWRITE, ON_CHANGE, onPlantSchedule1Change); 34 ArduinoCloud.addProperty(plant_schedule_2, READWRITE, ON_CHANGE, onPlantSchedule2Change); 35 ArduinoCloud.addProperty(plant_schedule_3, READWRITE, ON_CHANGE, onPlantSchedule3Change); 36 ArduinoCloud.addProperty(automatic_mode, READWRITE, ON_CHANGE, onAutomaticModeChange); 37 ArduinoCloud.addProperty(relay_1, READWRITE, ON_CHANGE, onRelay1Change); 38 ArduinoCloud.addProperty(relay_2, READWRITE, ON_CHANGE, onRelay2Change); 39 ArduinoCloud.addProperty(relay_3, READWRITE, ON_CHANGE, onRelay3Change); 40 41} 42 43WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
Comments
Only logged in users can leave comments