-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_sensors_freeRTOS.ino
352 lines (280 loc) · 9.27 KB
/
wifi_sensors_freeRTOS.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
const int MOVEMENT_SENSOR_PIN = 12;
const int LIGHT_SENSOR_PIN = 14;
const int FLUID_LEVEL_SENSOR_PIN = 27;
const int FLUID_LEVEL_LED_PIN = 32;
const int MOISTURE_SENSOR_PIN = 26;
const int PUMP_CONTROL_PIN = 13;
const int BUZZER_PIN = 33;
const int BUZZER_PWM_CHANNEL = 0;
const int GROWTH_LED_PIN = 34;
const char *ssid = "yourAP";
const char *password = "yourPassword";
WiFiServer server(8080);
void checkIfPlantNeedsWater( void *pvParameters );
TaskHandle_t checkIfPlantNeedsWaterHandle;
void checkWaterLevel( void *pvParameters );
void pumpingTask( void *pvParameters );
TaskHandle_t pumpingTaskHandle;
void watchForMovement( void *pvParameters );
TaskHandle_t watchForMovementHandle;
void checkLightLevel( void *pvParameters );
int getMoisture();
int getWaterLevel();
void activateWaterPump();
void deactivateWaterPump();
void sendMessageToUser(String msg);
void buzz();
bool detectMovement();
String connectionParams[5];
// the setup function runs once when you press reset or power the board
void setup() {
// initialize serial communication at 115200 bits per second:
Serial.begin(115200);
pinMode(MOVEMENT_SENSOR_PIN, INPUT); //digital movement sensor
pinMode(LIGHT_SENSOR_PIN, INPUT); //digital light sensor
pinMode(FLUID_LEVEL_SENSOR_PIN, INPUT); //analog fluid level sensor
pinMode(FLUID_LEVEL_LED_PIN, OUTPUT); //fluid level indicator led
pinMode(MOISTURE_SENSOR_PIN, INPUT); //analog moisture sensor
pinMode(PUMP_CONTROL_PIN, OUTPUT);
ledcSetup(BUZZER_PWM_CHANNEL, 1E5, 12);
ledcAttachPin(BUZZER_PIN, BUZZER_PWM_CHANNEL);
// Now set up tasks to run independently.
xTaskCreate(checkIfPlantNeedsWater, "check if plant shoud be watered", 1024, NULL, 2, &checkIfPlantNeedsWaterHandle);
xTaskCreate(checkWaterLevel, "check water level in tank", 1024, NULL, 2, NULL);
xTaskCreate(pumpingTask, "pump water", 1024, NULL, 2, &pumpingTaskHandle);
xTaskCreate(watchForMovement, "watch for movement", 1024, NULL, 2, &watchForMovementHandle);
xTaskCreate(checkLightLevel, "check light level", 1024, NULL, 2, NULL);
xTaskCreate(runWifi, "runWifi", 20000, NULL, 2, NULL);
}
void loop()
{
// Empty. Things are done in Tasks.
}
int getMoisture() {
return analogRead(MOISTURE_SENSOR_PIN);
}
int getWaterLevel() {
return analogRead(FLUID_LEVEL_SENSOR_PIN);
}
void activateWaterPump() {
digitalWrite(PUMP_CONTROL_PIN, HIGH);
Serial.println("Activated water pump....");
}
void deactivateWaterPump() {
digitalWrite(PUMP_CONTROL_PIN, LOW);
Serial.println("Deactivated water pump....");
}
void sendMessageToUser(String msg) {
Serial.print("Sending message: ");
Serial.println(msg);
}
bool detectMovement() {
return digitalRead(MOVEMENT_SENSOR_PIN);
}
void buzz() {
Serial.println("BUZZZZZZZZZZ BUZZZZZZ BUUUUUUZZZ!1!");
ledcWriteNote(BUZZER_PWM_CHANNEL, NOTE_C, 1);
}
void checkIfPlantNeedsWater(void *pvParameters) {
int checkDelay = 1000;
int moistureThreshold = 3000;
for (;;) {
vTaskDelay(checkDelay);
Serial.print("checking moisture: ");
Serial.print(getMoisture());
Serial.print(" | moisture threshold: ");
Serial.println(moistureThreshold);
if (getMoisture() > moistureThreshold) {
vTaskResume(pumpingTaskHandle);
vTaskSuspend(checkIfPlantNeedsWaterHandle);
}
}
}
void checkWaterLevel(void *pvParameters) {
int checkDelay = 1000;
int waterLevelThreshold = 500;
for (;;) {
vTaskDelay(checkDelay);
Serial.print("checking water level: ");
Serial.print(getWaterLevel());
Serial.print(" | water level threshold: ");
Serial.println(waterLevelThreshold);
if (getWaterLevel() < waterLevelThreshold) {
digitalWrite(FLUID_LEVEL_LED_PIN, HIGH);
sendMessageToUser("Refill the tank");
vTaskResume(watchForMovementHandle);
}
else {
digitalWrite(FLUID_LEVEL_LED_PIN, LOW);
vTaskSuspend(watchForMovementHandle);
}
}
}
void checkLightLevel(void *pvParameters) {
int checkDelay = 1000;
int ledDuration = 10000;
for (;;) {
vTaskDelay(checkDelay);
if (digitalRead(LIGHT_SENSOR_PIN) == 0) {
sendMessageToUser("Low light level");
digitalWrite(GROWTH_LED_PIN, HIGH);
vTaskDelay(ledDuration);
digitalWrite(GROWTH_LED_PIN, LOW);
}
}
}
void pumpingTask(void *pvParameters) {
int pumpingPeriod = 1000;
vTaskSuspend(pumpingTaskHandle);
for (;;) {
activateWaterPump();
vTaskDelay(pumpingPeriod);
deactivateWaterPump();
vTaskResume(checkIfPlantNeedsWaterHandle);
vTaskSuspend(pumpingTaskHandle);
}
}
void watchForMovement(void *pvParameters) {
int checkInterval = 200;
vTaskSuspend(watchForMovementHandle);
for (;;) {
vTaskDelay(checkInterval);
if (detectMovement()) {
buzz();
vTaskDelay(2000);
}
}
}
void runWifi( void *pvParameters ) {
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
for (;;) {
WiFiClient client = server.available();
if (client) {
String currentLine = "";
int start_ = 0;
int stop_ = 0;
int counter = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("<h1>Network configuration</h1><form method=\"GET\"><label>SSID: <label><input type=\"text\" name=\"ssid\" /><br /><label>Password: <label><input type=\"text\" name=\"password\" /><br /><label>IP: <label><input type=\"text\" name=\"ip\" /><br /><label>Netmask: <label><input type=\"text\" name=\"netmask\" /><br /><label>Gateway: <label><input type=\"text\" name=\"gateway\" /><br /><button type=\"submit\">Save</button>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
if(currentLine.endsWith("=")){
start_ = currentLine.length();
}
if(currentLine.endsWith("&")){
stop_ = currentLine.length()-1;
}
if(currentLine.endsWith(" HTTP/1.1")){
stop_ = currentLine.length() - 9;
}
if (counter < 5 && (currentLine.endsWith("&") || currentLine.endsWith(" HTTP/1.1") ) ){
connectionParams[counter] = currentLine.substring(start_, stop_);
counter++;
}
}
}
client.stop();
Serial.println("Client Disconnected.");
for(int i = 0; i < 5; i++){
Serial.println(connectionParams[i]);
}
Serial.println(counter);
if(counter == 5){
Serial.write("Run as client\n");
xTaskCreate(runWifi2, "runWifi", 40000, NULL, 2, NULL);
vTaskDelete(NULL);
}
}
}
}
void runWifi2( void *pvParameters ) {
delay(2000);
const char* ssid = connectionParams[0].c_str();;
const char* password = connectionParams[1].c_str();;
IPAddress ip;
IPAddress netmask;
IPAddress gateway;
ip.fromString(connectionParams[2]);
netmask.fromString(connectionParams[3]);
gateway.fromString(connectionParams[4]);
WiFiServer server2(80);
delay(10);
WiFi.config(ip, gateway, netmask);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server2.begin();
for(;;){
WiFiClient client = server2.available();
if (client) {
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("Movement sensor ");
client.print(digitalRead(MOVEMENT_SENSOR_PIN));
client.print("<br>Light sensor ");
client.print(digitalRead(LIGHT_SENSOR_PIN));
client.print("<br>Fluid level sensor ");
client.print(analogRead(FLUID_LEVEL_SENSOR_PIN));
client.print("<br>Moisture sensor ");
client.print(analogRead(MOISTURE_SENSOR_PIN));
client.print(" ");
client.print(digitalRead(25));
client.print("<br>Pump ");
client.print(digitalRead(PUMP_CONTROL_PIN));
client.print("<br>Buzzer ");
client.print(digitalRead(BUZZER_PIN));
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
if (currentLine.endsWith("GET /H")) {
Serial.write("first action\n");
}
if (currentLine.endsWith("GET /L")) {
Serial.write("second action\n");
}
}
}
client.stop();
Serial.println("Client Disconnected.");
}
}
}