#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
#define BUZZPIN 15 // Pin Buzzer
#define CM_ALERT_SOUND 10 // jarak minimal buzzer berbunyi
const char *ssid = "SensorParkir";
// OakOLED oled;
// void displayJarak(int i, int angka) {
// oled.clearDisplay();
// for (int z = 0; z <= i; z++) {
// for (int y = 0; y < 7; y++) {
// oled.drawPixel(z, y, 1);
// }
// }
// oled.setTextSize(6);
// oled.setCursor(10, 16);
// oled.println(String(angka));
// oled.display();
// }
void setup() {
// oled.begin();
// oled.setTextSize(6);
// oled.setTextColor(1);
// oled.setCursor(10, 16);
// oled.println("OKE"); // test display
// oled.display();
// Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid);
// Tunggu WiFi terhubung
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
tone(BUZZPIN, 4978);
delay(100);
noTone(BUZZPIN);
}
Serial.println();
Serial.print("Terhubung ke ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, "http://192.168.4.1/data")) { // HTTP
Serial.print("[HTTP] GET...\n");
int httpCode = http.GET();
Serial.println(httpCode);
if (httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
int jarak = payload.toInt();
int bar = map(jarak, 0, 127, 127, 0);
displayJarak(bar, jarak);
if (jarak <= CM_ALERT_SOUND) // bunyikan buzzer
tone(BUZZPIN, 4978);
else
noTone(BUZZPIN);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
delay(100);
}