-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral_system.ino
146 lines (123 loc) · 2.94 KB
/
general_system.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
char auth[] = "----------------";
char ssid[] = "-------";
char pass[] = "-------";
#define pirPin D1
int pirValue;
int pinValue;
#define sensorPin D6
#define rainPin D5
int sensorState = 0;
int rainState = 0;
int lastState = 0;
int lastRainState = 0;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
BLYNK_WRITE(V0)
{
pinValue = param.asInt();
}
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(sensorPin, INPUT);
pinMode(rainPin, INPUT);
pinMode(pirPin, INPUT);
dht.begin();
timer.setInterval(1000L, sendSensor);
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
sensors.begin();
}
int sensor=0;
void sendTemps()
{
sensor=analogRead(A0);
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
Serial.println(temp);
Serial.println(sensor);
Blynk.virtualWrite(V1, temp);
Blynk.virtualWrite(V2,sensor);
delay(1000);
}
void getPirValue(void)
{
pirValue = digitalRead(pirPin);
if (pirValue)
{
Serial.println("Motion detected");
Blynk.notify("Motion detected");
}
}
void loop()
{
Blynk.run();
timer.run();
sendTemps();
sensorState = digitalRead(sensorPin);
Serial.println(sensorState);
if (sensorState == 1 && lastState == 0) {
Serial.println("needs water, send notification");
Blynk.notify("Water your plants");
lastState = 1;
delay(1000);
}
else if (sensorState == 1 && lastState == 1) {
//do nothing, has not been watered yet
Serial.println("has not been watered yet");
delay(1000);
}
else {
Serial.println("does not need water");
lastState = 0;
delay(1000);
}
rainState = digitalRead(rainPin);
Serial.println(rainState);
if (rainState == 0 && lastRainState == 0) {
Serial.println("Its Raining!");
Blynk.notify("Its Raining!");
lastRainState = 1;
delay(1000);
}
else if (rainState == 0 && lastRainState == 1) {
delay(1000);
}
else {
Serial.println("No Rains");
lastRainState = 0;
delay(1000);
}
if (pinValue == HIGH)
{
getPirValue();
}
Blynk.run();
delay(100);
}