-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb hosting blm berhasil
134 lines (103 loc) · 3.17 KB
/
web hosting blm berhasil
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
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
//
//const char* ssid = "Agnes"; //nama wifi
//const char* password = "agnes2004"; //password
const char* ssid = "Ali"; //nama wifi
const char* password = "uioppoiu"; //password
const char* serverName = "https://agneshandayani.000webhostapp.com/add.php";
String apiKeyValue = "tPmAT5Ab3j7F9";
String sensorName = "Ultrasonic";
String sensorLocation = "LOCATION 1";
const int TRIGGER1 = D1;
const int ECHO1 = D2;
const int TRIGGER2 = D3;
const int ECHO2 = D4;
const int TRIGGER3 = D5;
const int ECHO3 = D6;
long duration1, duration2, duration3;
int distance1, distance2, distance3;
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
pinMode(TRIGGER1, OUTPUT);
pinMode(ECHO1, INPUT);
pinMode(TRIGGER2, OUTPUT);
pinMode(ECHO2, INPUT);
pinMode(TRIGGER3, OUTPUT);
pinMode(ECHO3, INPUT);
}
void loop() {
digitalWrite(TRIGGER1, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER1, LOW);
duration1 = pulseIn(ECHO1, HIGH);
distance1 = duration1*0.034/2;
digitalWrite(TRIGGER2, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER2, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER2, LOW);
duration2 = pulseIn(ECHO2, HIGH);
distance2 = duration2*0.034/2;
digitalWrite(TRIGGER3, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER3, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER3, LOW);
duration3 = pulseIn(ECHO3, HIGH);
distance3 = duration3*0.034/2;
Serial.print("Centimeter:");
Serial.println(distance1);
Serial.print("Centimeter:");
Serial.println(distance2);
Serial.print("Centimeter:");
Serial.print(distance3);
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare your HTTP POST request data
String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
+ "&location=" + sensorLocation + "&value1=" + distance1
+ "&value2=" + distance2 + "&value3=" + distance3 + "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
//Send an HTTP POST request every 30 seconds
delay(30000);
}