/* ESP32 HTTP IoT Server Example for Wokwi.com
https://wokwi.com/projects/320964045035274834
To test, you need the Wokwi IoT Gateway, as explained here:
https://docs.wokwi.com/guides/esp32-wifi#the-private-gateway
Then start the simulation, and open http://localhost:9080
in another browser tab.
Note that the IoT Gateway requires a Wokwi Club subscription.
To purchase a Wokwi Club subscription, go to https://wokwi.com/club
*/
#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
const char* host = "pavelka.asp2.cz";
WebServer server(80);
const int LED1 = 26;
const int LED2 = 27;
bool led1State = false;
bool led2State = false;
void setup(void) {
Serial.begin(115200);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(void) {
download_nastaveni();
delay(100);
}
void download_nastaveni() {
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// poptani vsech definovanych nastaveni
String url = "/data.ashx";
// prvni parametr, jen kvuli pouziti znaku &
url += "?par=0";
// poslani poptavanych nastaveni
for (int i = 0; i < nastaveni_pocet; i++) {
url += "&" + nastaveni[i] + "=1";
}
// definice vsech stavu, kde doslo ke zmene
for (int i = 0; i < stavy_pocet; i++) {
// odesila se jen zmeneny stav
if (stavy_hodnoty[i] != stavy_hodnoty_prev[i]) {
url += "&" + stavy[i] + "=" + (String)stavy_hodnoty[i];
}
}
//Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
//Serial.println("Pripojeno 1");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
//Serial.println("Pripojeno 2");
// cteni vsech nastaveni z resultu
String result = "";
while(client.available()){
result += client.readStringUntil('\r');
}
//Serial.println(result);
for (int i = 0; i < nastaveni_pocet; i++) {
int pozice = result.indexOf(nastaveni[i] + ":");
int pozice2 = result.indexOf(":", pozice);
nastaveni_hodnoty[i] = result.substring(pozice2 + 1, pozice2 + 2);
}
// prenastaveni stavu na novou a jiz odeslanou hodnotu
for (int i = 0; i < stavy_pocet; i++) {
stavy_hodnoty_prev[i] = stavy_hodnoty[i];
}
//Serial.println("closing connection");
}