· 6 years ago · Oct 24, 2019, 06:44 AM
1#include <WiFi.h>
2#include <HTTPClient.h>
3
4
5// Replace with your network credentials
6const char* ssid = "rumahjamur";
7const char* password = "12345678";
8
9// REPLACE with your Domain name and URL path or IP address with path
10const char* serverName = "http://3.85.239.221/home.php";
11const int ldrPin = 32;
12
13// Keep this API Key value to be compatible with the PHP code provided in the project page.
14// If you change the apiKeyValue value, the PHP file also needs to have the same key
15//String apiKeyValue = "tPmAT5Ab3j7F9";
16
17void setup() {
18 Serial.begin(115200);
19
20 WiFi.begin(ssid, password);
21 Serial.println("Connecting");
22 while(WiFi.status() != WL_CONNECTED) {
23 delay(500);
24 Serial.print(".");
25 }
26 Serial.println("");
27 Serial.print("Connected to WiFi network with IP Address: ");
28 Serial.println(WiFi.localIP());
29}
30
31void loop() {
32 //Check WiFi connection status
33
34 if(WiFi.status()== WL_CONNECTED){
35 HTTPClient http;
36
37 // Your Domain name with URL path or IP address with path
38 http.begin(serverName);
39
40 // Specify content-type header
41 http.addHeader("Content-Type", "application/x-www-form-urlencoded");
42
43 // Prepare your HTTP POST request data
44 String httpRequestData = String("ldr=") + analogRead(ldrPin) + String("&milis=") + millis();
45 Serial.print("httpRequestData: ");
46 Serial.println(httpRequestData);
47
48 // You can comment the httpRequestData variable above
49 // then, use the httpRequestData variable below (using sensor's data)
50 //String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=24.75&value2=49.54&value3=1005.14";
51
52 // Send HTTP POST request
53 int httpResponseCode = http.POST(httpRequestData);
54
55 // If you need an HTTP request with a content type: text/plain
56 //http.addHeader("Content-Type", "text/plain");
57 //int httpResponseCode = http.POST("Hello, World!");
58
59 // If you need an HTTP request with a content type: application/json, use the following:
60 //http.addHeader("Content-Type", "application/json");
61 //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
62
63 if (httpResponseCode>0) {
64 Serial.print("HTTP Response code: ");
65 Serial.println(httpResponseCode);
66 }
67 else {
68 Serial.print("Error code: ");
69 Serial.println(httpResponseCode);
70 }
71 // Free resources
72 http.end();
73 }
74 else {
75 Serial.println("WiFi Disconnected");
76 }
77 //Send an HTTP POST request every 30 seconds
78 delay(5000);
79}