#include <Wire.h>
#include "DHTesp.h"
#include <Adafruit_BMP085.h>
#include <SPI.h>
#include <BH1750.h>
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/e07e4a85-2e35-47db-9708-5bc4e86135e6
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float ambientLight;
float atmosphericHumidity;
float atmosphericPressure;
float atmosphericTemperature;
float soilMoisture;
CloudSchedule schedule;
bool activate;
bool mode;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#define ANALOGPIN0 36
#define MAXADCRESOLUTION 1024
int soilMoistureAcquired = 0; // value read
float soilMoistureVoltage = 0.0;
float soilMoistureThreshold = 1.0;
float powerSupply = 3.3;
#include "thingProperties.h"
Adafruit_BMP085 bmp;
BH1750 brightnessMeasure;
DHTesp dht;
#define DHT22_PIN 5
unsigned long measureDelay = 600000; // NOT LESS THAN 2000!!!!! BETTER 600000 (10 MINUTES)
unsigned long lastTimeRan;
#define RELAY_1 18
#define RELAY_2 19
void reset_valve_high() {
// Sends 0V to the valve setting HIGH both control pins.
digitalWrite(RELAY_1, HIGH);
digitalWrite(RELAY_2, HIGH);
Serial.println("Reset HIGH\n");
}
void reset_valve_low() {
// Sends 0V to the valve setting LOW both control pins.
digitalWrite(RELAY_1, LOW);
digitalWrite(RELAY_2, LOW);
Serial.println("Reset LOW\n");
}
void open_valve() {
// Sends a 10ms impulse to open the valve.
digitalWrite(RELAY_1, LOW);
digitalWrite(RELAY_2, HIGH);
delay(200);
Serial.println("\nValve opened. Water Flowing\n");
}
void close_valve() {
// Sends a 10ms impulse to open the valve.
digitalWrite(RELAY_1, HIGH);
digitalWrite(RELAY_2, LOW);
delay(200);
Serial.println("\nValve closed. Water not Flowing\n");
}
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(115200);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
Wire.begin();
brightnessMeasure.begin();
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
while (1) {}
}
dht.setup(DHT22_PIN, DHTesp::DHT22);
mode = true;
lastStatus = false;
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
close_valve();
reset_valve_high();
}
void loop() {
ArduinoCloud.update();
// Your code here
if (millis() > lastTimeRan + measureDelay) {
atmosphericHumidity = dht.getHumidity();
atmosphericTemperature = dht.getTemperature();
ambientLight = brightnessMeasure.readLightLevel();
atmosphericPressure = bmp.readPressure() / 100.0;
soilMoistureAcquired = analogRead(ANALOGPIN0);
soilMoisture = (powerSupply / MAXADCRESOLUTION) * soilMoistureAcquired / 0.033;
Serial.println("Sending data...");
lastTimeRan = millis();
}
// if mode == true automatic mode
if (mode) {
switch (schedule.isActive()) {
// if valve state is CLOSED, closes the valve
case 0:
if (lastStatus != schedule.isActive()) {
close_valve();
reset_valve_high();
}
lastStatus = false;
break;
// if valve state is OPEN, opens the valve
case 1:
if (lastStatus != schedule.isActive()) {
open_valve();
reset_valve_low();
}
lastStatus = true;
break;
}
// if mode == false manual mode
} else {
switch (activate) {
// if valve state is CLOSED, closes the valve
case 0:
if (lastStatus != activate) {
close_valve();
reset_valve_high();
}
lastStatus = false;
break;
// if valve state is OPEN, opens the valve
case 1:
if (lastStatus != activate) {
open_valve();
reset_valve_low();
}
lastStatus = true;
break;
}
}
}
/*
Since Mode is READ_WRITE variable, onModeChange() is
executed every time a new value is received from IoT Cloud.
*/
void onModeChange() {
// Add your code here to act upon Mode change
}
/*
Since Schedule is READ_WRITE variable, onScheduleChange() is
executed every time a new value is received from IoT Cloud.
*/
void onScheduleChange() {
// Add your code here to act upon Schedule change
}
/*
Since Activate is READ_WRITE variable, onActivateChange() is
executed every time a new value is received from IoT Cloud.
*/
void onActivateChange() {
// Add your code here to act upon Activate change
}
//https://www.techrm.com/how-to-make-an-automatic-sprinkler-with-esp8266-controlled-by-arduino-cloud/