-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch_dec04a.ino
73 lines (50 loc) · 1.87 KB
/
sketch_dec04a.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
#include<WiFi.h>
#include<HTTPClient.h>
const int soilMoisturePin = 32; // Replace with the actual pin connected to the soil moisture sensor
String URL = "http://192.168.10.14/dht11_project/test_data.php"; // Replace with the actual URL of the server
const char* ssid = "Redmi 12C"; // Replace with the actual SSID
const char* password = "password"; // Replace with the actual password
int moisture = 0;
void setup() {
Serial.begin(115200);
connectWiFi();
}
void loop() {
int soilMoistureValue = analogRead(soilMoisturePin);
// Reverse the mapping if lower values mean wet and higher values mean dry
int moisturePercentage = map(soilMoistureValue, 4095, 0, 0, 100);
Serial.print("Soil Moisture Value: ");
Serial.println(soilMoistureValue);
Serial.print("Moisture Percentage: ");
Serial.print(moisturePercentage);
Serial.println("%");
if(WiFi.status() != WL_CONNECTED){
connectWiFi();
}
String postData = "Moisture=" + String(moisturePercentage);
HTTPClient http;
http.begin(URL);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST(postData);
String payload = http.getString();
Serial.print("URL : "); Serial.println(URL);
Serial.print("Data: "); Serial.println(postData);
Serial.print("httpCode: "); Serial.println(httpCode);
Serial.print("payload : "); Serial.println(payload);
Serial.println("--------------------------------------------------");
delay(5000);
}
void connectWiFi() {
WiFi.mode(WIFI_OFF);
delay(1000);
//This line hides the viewing of ESP as wifi hotspot
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("connected to : "); Serial.println(ssid);
Serial.print("IP address: "); Serial.println(WiFi.localIP());
}