-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.ino
84 lines (57 loc) · 2.17 KB
/
Code.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "Etisalat 4G iModem-9B77";
const char* password = "17265880";
String postData;
int moisture = 0;
int relay = D3;
char incommingByte = ' ';
int threshold = 1;
int sensorValMapped = 0;
StaticJsonDocument<300> JSONencoder;
WiFiServer server(80);
void setup() {
delay(1000);
Serial.begin(115200);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
server.begin();
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
sensorValMapped = map(analogRead(A0), 1000, 0, 0, 1000);
}
void loop() {
HTTPClient http; //Declare object of class HTTPClient
http.begin("http://159.122.174.163:31175/api/user/update-moisture"); //Specify request destination
http.addHeader("Content-Type", "application/json"); //Specify content-type header
String userRequest = "{\"email\":\"testing\", \"password\":\"testing\", \"positionX\": 0, \"positionY\": 0, \"current_moisture\":" + String(sensorValMapped) + "}";
int httpCode = http.PUT(userRequest); //Send the request
String payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
DeserializationError error = deserializeJson(JSONencoder, payload);
String threshold_str = JSONencoder["data"]["moisture_threhold"];
int threshold = atoi(threshold_str.c_str() );
sensorValMapped = map(analogRead(A0), 1023, 0, 0, 1000);
Serial.println(sensorValMapped);
if (sensorValMapped <= threshold ) {
digitalWrite(relay, LOW);
}
else {
digitalWrite(relay, HIGH);
}
http.end(); //Close connection
delay(1000); //Post Data at every 5 seconds
}