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