-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMotor_control.ino
More file actions
214 lines (180 loc) · 4.97 KB
/
Motor_control.ino
File metadata and controls
214 lines (180 loc) · 4.97 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <TridentTD_LineNotify.h>
#include <Adafruit_ADS1015.h>
#include <BlynkSimpleEsp8266.h>
#include <time.h>
#define Relay D2 //Variable Relay is D2
#define BLYNK_PRINT Serial
#define ssid "BNK48" //SSID WIFI
#define pass "0922530944" //PASSWORD WIFI
#define LINE_TOKEN "OK7wIuUyLwFI7XMzyLpAHP9bGaX4yk96g2cZroJlXhp" //LINE TOKEN
Adafruit_ADS1015 ads;
TridentTD_LineNotify myLINE(LINE_TOKEN);
//Variable
int Moisture, check = 0, sensorPin = A0 , time_min, time_hour;
int16_t Light;
int Check_mode = 0;
//blynk code
char auth[] = "6cac4a5950f24bbdbbcb4cde09136e02";
//Time zone
int timezone = 7 * 3600;
int dst = 0;
//function
int Blynk_print(int Moisture, int Light);
void Watering(void);
void StopMotor(void);
void Watering(void) // สั่งให้มอเตอร์ทำงานช่วงเวลาตามดีเล
{
digitalWrite(Relay, LOW);
Blynk.virtualWrite(V4, "Watering");
Moisture = analogRead(sensorPin);
delay(500);
}
void StopMotor(void) // สั่งปิดมอเตอร์
{
myLINE.notify("หยุดการรดน้ำต้นไม้");
Blynk.virtualWrite(V4, "Pump off");
digitalWrite(Relay, HIGH);
delay(1000);
}
int Blynk_print(int Moisture, int Light) //แสดงผลค่าแสงและค่าความชิ้นจากเซ็นเซอร์ไปในBlynk
{
Blynk.virtualWrite(V1, Moisture);
Blynk.virtualWrite(V2, Light);
delay(500);
}
void setup() {
//Serial begin at 115200
Serial.begin(115200);
// Connect to Wi-Fi network
Serial.println();
Serial.println();
Serial.print("Connecting to WIFI... : ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Wi-Fi connected successfully");
//Connect to Blynk
Blynk.begin(auth, ssid, pass);
//Connect to Line
myLINE.wificonnect(ssid,pass);
//Start I2C
ads.begin();
//for time
Serial.setDebugOutput(true);
configTime(timezone, dst, "pool.ntp.org", "time.nist.gov");
//Turn off Motor
pinMode(Relay, OUTPUT);
digitalWrite(Relay, HIGH);
}
//Blynk select mode
BLYNK_WRITE(V5)
{
int y = param.asInt();
if(y==1)
{
Blynk.virtualWrite(V3, "Mode: Time");
Check_mode = 1;
}
else
{
Blynk.virtualWrite(V3, "Mode:Moisture");
Check_mode = 0;
}
}
//Blynk turn on pump
BLYNK_WRITE(V6)
{
int y = param.asInt();
if(y==1)
{
myLINE.notify("กำลังทำการรดน้ำต้นไม้");
Watering();
}
else
{
StopMotor();
}
}
//Blynk Time set
BLYNK_WRITE(V7) // set หน่วยนาทีของtime mode
{
time_min = param.asInt();
}
BLYNK_WRITE(V8) // set หน่วยชั่วโมงของtime mode
{
time_hour = param.asInt();
}
void loop()
{
//Start Blynk
Blynk.run();
//รับค่าความเข้มแสงจากI2C
Light = ads.readADC_SingleEnded(0)/20;
//รับค่าความชื้นของดินจากช่องDigital 2
Moisture = analogRead(sensorPin)/12;
//Show data in blynk dashboard
Blynk_print(Moisture, Light);
check = 0;
//Time mode
if(Check_mode == 1)
{
time_t now = time(nullptr);
struct tm* p_tm = localtime(&now);
Serial.print(p_tm->tm_sec);
Serial.print(":");
Serial.print(p_tm->tm_min);
Serial.print(":");
Serial.print(p_tm->tm_hour);
Serial.print("\n");
Serial.print(time_hour);
Serial.print(":");
Serial.print(time_min);
Serial.print("\n");
if((p_tm->tm_sec == 0)&&(p_tm->tm_min == time_min)&&(p_tm->tm_hour == time_hour))
{
myLINE.notify("กำลังทำการรดน้ำต้นไม้");
for(int i=0; i<=10; i++)
{
Watering();
Moisture = analogRead(sensorPin);
}
check++;
}
if(check != 0)
{
StopMotor();
check = 0;
}
}
//Moisture mode
else{
while(Moisture<=40) //ถ้าความชิ้นน้อยกว่า40จะทำการรดน้ำ
{
if(check == 0)
{
myLINE.notify("ความชื้นต่ำ");
delay(500);
myLINE.notify("กำลังทำการรดน้ำต้นไม้");
delay(500);
}
for(int i=0; i<=10; i++) //ทำการรดน้ำ5วินาทีแล้วทำการเช็คความชื้นใหม่
{
Watering();
Moisture = analogRead(sensorPin);
}
check++;
}
if(check!=0)
{
StopMotor();
check = 0;
}
}
}