#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
HTTPClient http;
LiquidCrystal_I2C lcd(0x27, 16, 2);
String serverName = "https://iot-and-software-erc-oprec2023.glitch.me";
String noticePath = "/api/earthquake-notice-service";
String dataPath = "/api/request-earthquake-data";
String httpGET(String path) {
String payload = "";
http.begin(path.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode = 200) {
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
return payload;
}
void setup() {
pinMode(2, OUTPUT);
pinMode(15, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("-OPREC ERC 2023-");
lcd.setCursor(0, 1);
lcd.print("IoT dan Software");
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Monitoring Gempa");
lcd.setCursor(0, 1);
lcd.print("Sektor:-");
lcd.setCursor(10, 1);
lcd.print("Mag:-");
}
void loop() {
if(WiFi.status()== WL_CONNECTED){
Serial.println("Start HTTP GET....")
if(httpGET(serverName + noticePath).toInt()) {
String str = httpGET(serverName+dataPath);
char *cstr = new char [str.length()+1];
strcpy(cstr, str.c_str());
lcd.setCursor(7, 1);
lcd.print(cstr[1]);
lcd.setCursor(14, 1);
lcd.print(cstr[3]);
digitalWrite(2, HIGH);
tone(15, 1000, 100);
digitalWrite(2, LOW);
}
else {
digitalWrite(2, LOW);
lcd.setCursor(7, 1);
lcd.print("-");
lcd.setCursor(14, 1);
lcd.print("-");
}
}
else {
Serial.println("WiFi Disconnected");
}
delay(1000);
}