#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverName =
"https://smartsite.gerakpinasti.com/backend/esp-post-data.php";
String apiKeyValue = "tPmAT5Ab3j7F9";
String sensorName = "SMART SITE";
String sensorLocation = "Jabar";
String urlencode(String str) {
String encoded = "";
char c;
char buf[4];
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (isalnum(c)) encoded += c;
else {
sprintf(buf, "%%%02X", c);
encoded += buf;
}
}
return encoded;
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(300);
Serial.print(".");
}
Serial.println("\nWiFi OK");
sendData();
}
void loop() {}
void sendData() {
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String payload =
"api_key=" + apiKeyValue +
"&sensor=" + urlencode(sensorName) +
"&location=" + urlencode(sensorLocation) +
"&value1=25&value2=60&value3=123&value4=0&value5=456";
Serial.println(payload);
int code = http.POST(payload);
Serial.print("[HTTP CODE] ");
Serial.println(code);
String resp = http.getString();
Serial.println("[RESPONSE]");
Serial.println(resp);
http.end();
}