#include <WiFiClientSecure.h>
#include <esp_sntp.h>
String ssid = "Wokwi-GUEST";
String password = "";
const char* server = "www.idokep.hu"; // Server URL
String service = "tfi.glitch.me";
WiFiClientSecure client;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
delay(100);
// attempt to connect to Wifi network:
configTzTime("CET-1CEST,M3.5.0,M10.5.0/3","europe.pool.ntp.org");
sntp_set_time_sync_notification_cb([](timeval *v){
Serial.println("SNTP Sync");
time_t tsec = time(nullptr);
Serial.println(localtime(&tsec), "%H:%M:%S");
});
uint32_t start = millis();
// WiFi.begin(ssid, password, 6);
// while (WiFi.status() != WL_CONNECTED) {
// Serial.print(".");
// // wait 1 second for re-trying
// if(millis() - start < 70000 ) delay(1000);
// else break;
// }
while(!WiFi.isConnected()){
Serial.println("Enter ssid password");
while (!Serial.available()) delay(1000);
ssid = Serial.readStringUntil(' ');
password = Serial.readStringUntil('\n');
Serial.printf("Attempting to connect to SSID: %s password: %s\n", ssid, password);
WiFi.begin(ssid, password, 6);
uint32_t start = millis();
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
// wait 1 second for re-trying
if(millis() - start < 7000 ) delay(1000);
else break;
}
}
Serial.print("Connected to ");
Serial.println(ssid);
sntp_set_sync_interval(15000);
sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
timeval *tv;
sntp_sync_time(tv);
time_t ts = tv->tv_sec;
Serial.println(localtime(&ts), "%H:%M:%S");
client.setInsecure();
Serial.println("\nStarting connection to server...");
start = millis();
if (!client.connect(service.c_str(), 443, 300))
Serial.println("Connection failed!");
else {Serial.println(millis()-start);
Serial.println("Connected to server!");
// Make a HTTP request:
// client.println("GET /terkep/hu/hoterkep_data.js HTTP/1.0");
// client.println("Host: www.idokep.hu");
// client.println("User-Agent: Mozilla/5.0");
// client.println();
bool getapp = false;
while (!getapp) {
client.println("GET /?loc=ethuri HTTP/1.1");
client.println("Host: tfi.glitch.me");
//client.println("Connection: keep-alive");
client.println("User-Agent: Mozilla/5.0");
client.println();
while (!client.available()){Serial.print("."); delay(1000);}
uint16_t respCode = client.readStringUntil(' ').toInt();
if(respCode == 200){
if(!client.findUntil("app","\r\n\r\n")){
client.flush();
Serial.printf("available: %d", client.available());
delay(1000);
}
else{
client.find("\r\n\r\n");
getapp = true;
Serial.print(client.readStringUntil('\n'));
Serial.println(client.readStringUntil('\n'));
break;
}
}
// while (client.available()) {
// String line = client.readStringUntil('\n');
// Serial.println(line);
// if (line == "\r") {
// Serial.println("headers received");
// break;
// }
//}
}
// uint32_t start = millis();
// String loc, temp, locCode = "DHMZ_7804fa", tempStart = "=\"", locStart = "br>";
// char tempEnd = '&', locEnd = ',';
// while (client.available()) {
// if (client.findUntil(locCode.c_str(), "\n")) {
// client.find(tempStart.c_str());
// temp = client.readStringUntil(tempEnd);
// client.find(locStart.c_str());
// loc = client.readStringUntil(locEnd);
// Serial.println(loc + ": " + temp);
// break;
// }
// }
// Serial.println(millis() - start);
// if there are incoming bytes available
// from the server, read them and print them:
// while (client.available()) {
// char c = client.read();
// // Serial.write(c);
// }
sntp_sync_time(tv);
Serial.println("end");
client.stop();
}
}
void loop() {
delay(1);
// do nothing
}