#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#include <WiFi.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const String url = "http://web.mgiga.com.tw/mgiga_ajax.jsp?action=GET_CURRENT_DATETIME";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin(ssid, password);
//非同步wifi連線
Serial.println("connecting to wifi");
while( WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
//連線中
}
lcd.init();
lcd.backlight();
Serial.print("ok! ip=");
//連線成功到網頁端
Serial.println(WiFi.localIP());
//顯示IP
}
void loop() {
HTTPClient http;
http.begin(url);
//HTTP request請求連線
int httpResponseCode = http.GET();
//重要
Serial.print(httpResponseCode);
if(httpResponseCode >0){
Serial.print("HTTP OK!");
//連線成功的回應
String payload = http.getString();
//把回應的網頁資訊丟給payload再轉成字串
Serial.println();
Serial.println(payload);
int ind1 = payload.indexOf("[RESP]");
int ind2 = payload.indexOf("[/RESP]");
//印出網頁回應的字串
Serial.println(ind1);
Serial.println(ind2);
String strD = payload.substring(ind1+7,ind2-8);
String strT = payload.substring(ind1+14,ind2);
lcd.setCursor(1,0);//設定游標
lcd.print(strD);//印出文字
lcd.setCursor(1,1);//設定游標
lcd.print(strT);//印出文字
}else{
Serial.print("Error code");
//連線失敗的回應
}
delay(1000);
}