#include #include #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); char ssid[] = "TU WPISZ NAZWĘ HOTSPOT WIFI"; char password[] = "TU WPISZ HASŁO DO WIFI"; WiFiServer server(80); // Serwer na porcie 80 void setup() { Serial.begin(115200); dht.begin(); // Łączenie z WiFi Serial.print("Łączenie z siecią "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi połączone! Oczekiwanie na adres IP..."); // Pętla blokująca, dopóki adres IP to 0.0.0.0 while (WiFi.localIP() == IPAddress(0, 0, 0, 0)) { delay(500); Serial.print("?"); } Serial.println("\nAdres IP pobrany pomyślnie!"); Serial.print("Właściwy adres IP Arduino: "); Serial.println(WiFi.localIP()); server.begin(); } void loop() { WiFiClient client = server.available(); if (client) { Serial.println("Nowe zapytanie od ESP32"); String currentLine = ""; while (client.connected()) { if (client.available()) { char c = client.read(); if (c == '\n') { if (currentLine.length() == 0) { // Odczyt z czujnika float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { t = 0.0; h = 0.0; } // Wysyłamy nagłówek HTTP JSON client.println("HTTP/1.1 200 OK"); client.println("Content-Type: application/json"); client.println("Connection: close"); client.println(); // Wysyłamy czysty JSON z danymi client.print("{\"temp\":"); client.print(t, 1); client.print(",\"wilg\":"); client.print(h, 1); client.println("}"); delay(10); break; } else { currentLine = ""; } } else if (c != '\r') { currentLine += c; } } } client.stop(); Serial.println("Połączenie zamknięte"); } }