-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblomkruka_esp32.ino
121 lines (88 loc) · 2.18 KB
/
blomkruka_esp32.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <PubSubClient.h>
#include "DHT.h"
//#include "MoistCheck.h"
#include "wifiConnect.h"
int M_Sensor = 34;
int Moisture = 0;
int moistPercent = 0;
int oldStatus = 0;
const int dryValue = 4096;
const int wetValue = 2800;
WiFiClient espClient;
PubSubClient client(espClient);
#define mqttMoist "lucifer/moist"
#define mqtt_server "10.0.1.19"
const int Relay = 13;
void setup()
{
pinMode(Relay, OUTPUT);
Serial.begin(115200);
wifiBegin();
client.setServer(mqtt_server, 1883);
client.connect("blomkruka");
delay(1000);
}
void setStatus(int newStatus) {
if (oldStatus != newStatus) {
oldStatus = newStatus;
if (oldStatus == 1) {
Serial.println("case 1 DRY");
delay(500);
client.publish(mqttMoist, String (moistPercent).c_str(), true);
delay(500);
}
else if (oldStatus == 2)
{
Serial.println("case 2 Moist");
delay(500);
client.publish(mqttMoist, String (moistPercent).c_str(), true);
delay(500);
}
else if (oldStatus == 3) {
Serial.println("case 3 Wet");
delay(500);
client.publish(mqttMoist, String (moistPercent).c_str(), true);
}
}
}
void loop()
{
int newStatus = 0;
Moisture = analogRead(M_Sensor);
moistPercent = map(Moisture, dryValue, wetValue, 0, 100);
client.publish(mqttMoist, String (moistPercent).c_str(), true);
Serial.println(Moisture);
delay(1000);
if (Moisture > 3950) // for dry soil
{
newStatus = 1;
setStatus(newStatus);
Serial.println ("DRY SOIL");
Serial.print(moistPercent);
Serial.println("%");
digitalWrite(Relay, HIGH);
delay(4000);
client.publish(mqttMoist, "watered");
digitalWrite(Relay, LOW);
}
else if (Moisture >= 3000 && Moisture <= 3950) //for Moist Soil
{
newStatus = 2;
setStatus(newStatus);
digitalWrite(Relay, LOW);
Serial.println("MOIST SOIL");
Serial.print(moistPercent);
Serial.println("%");
}
else
{
newStatus = 3;
setStatus(newStatus);
digitalWrite(Relay, LOW);
Serial.println("WET");
Serial.print(moistPercent);
client.publish(mqttMoist, String (moistPercent).c_str(), true);
Serial.println("%");
}
delay(60000);
}