Wariant 1 -- Arduino
- #include <DHT.h>
- #define DHTPIN 6
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- void setup()
- {
- Serial.begin(115200);
- dht.begin();
- }
- void loop()
- {
- delay(2000);
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- Serial.print("Wilgotność: ");
- Serial.print(h);
- Serial.print("% Temperatura: ");
- Serial.print(t);
- Serial.print("*C ");
- }
------------
IDF - Platformio
- #include <stdio.h>
- #include <freertos/FreeRTOS.h>
- #include <freertos/task.h>
- #include <esp_err.h>
- #include <dht/dht.h>
- #define DHTPIN 6 // Definiujemy pin danych dla czujnika DHT, a więc GPIO6, czyli D6 na płytce D1
- #define DHT_TYPE_DHT22 DHT22 // Definiujemy typ czujnika
- void odczytDHT(void *arg)
- {
- ESP_ERROR_CHECK(dht_init(DHTPIN, false)); // Sprawdzamy, czy DHT22 jest podłączony
- vTaskDelay(2000); // Oczekujemy 2 sekundy na pomiar
- while (1)
- {
- int wilgotnosc = 0;
- int temperatura = 0;
- if (dht_read_data(DHT22, DHTPIN, &wilgotnosc, &temperatura) == ESP_OK)
- {
- // Wynik, jaki odczytamy z DHT, to: 604 = 60,4%, 252 = 25,2°C, więc wymaga
- // formatowania
- printf("Wilgotność: %d Temperatura: %d\n", wilgotnosc, temperatura);
- }
- else {
- printf("Błąd odczytu czujnika DHT\n"); // Wyświetlenie błędu w razie problemów z czujnikiem
- }
- vTaskDelay(5000);
- }
- vTaskDelete(NULL);
- }
- void app_main()
- {
- xTaskCreate(odczytDHT, "odczytDHT", 2048, NULL, tskIDLE_PRIORITY, NULL); // Uruchamiamy
- // task odczytu danych
- }
Wariant drugi :
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <DHT.h>
- // Dane do sieci Wi-Fi
- const char *ssid = "<twoja sieć WiFi> ";
- const char *password = "<hasło do WiFi>";
- // PIN, na którym podłączony jest czujnik DHT22
- #define DHTPIN 2
- #define DHTTYPE DHT22
- // Inicjalizacja obiektu DHT
- DHT dht(DHT_PIN, DHTTYPE);
- // Obiekt serwera
- ESP8266WebServer server(80);
- void handleRoot()
- {
- // Odczyt danych z czujnika DHT22
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- // Przygotowanie strony HTML
- String html = "<html><body>";
- html += "<h1>Dane z czujnika DHT22:</h1>";
- html += "<p>Temperatura: " + String(t) + " ℃</p>";
- html += "<p>Wilgotność: " + String(h) + " %</p>";
- html += "</body></html>";
- // Wysłanie odpowiedzi do klienta
- server.send(200, "text/html", html);
- }
- void setup()
- {
- // Inicjalizacja portu szeregowego
- Serial.begin(115200);
- // Inicjalizacja Wi-Fi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(250);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println(WiFi.localIP()); // Wyświetla w terminalu adres IP
- // Inicjalizacja czujnika DHT22
- dht.begin();
- // Konfiguracja obsługi ścieżki głównej ("/")
- server.on("/", HTTP_GET, handleRoot);
- // Uruchomienie serwera WWW
- server.begin();
- Serial.println("HTTP server started");
- }
- void loop()
- {
- // Obsługa żądań klientów
- server.handleClient();
- }
--------------------------------------------------------------------------------------------------------------
Wariant trzeci
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <DHT.h>
- const char* ssid = "<twoja sieć WiFi> ";
- const char* password = "<hasło do WiFi>";
- const char* mqtt_server = "<adres brokera MQTT>";
- const char* mqtt_topic = "<Temat MQTT> ";
- #define DHTPIN 6
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- WiFiClient espClient;
- PubSubClient client(espClient);
- void reconnect()
- {
- while (!client.connected())
- {
- Serial.print("Łączenie z MQTT brokerem...");
- if (client.connect("ESP8266Client"))
- {
- Serial.println("połączony");
- }
- else
- {
- Serial.print("nieudane, rc=");
- Serial.print(client.state());
- Serial.println(" ponowna próba za 5 sekund");
- delay(5000);
- }
- }
- }
- void setup()
- {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(250);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi Połączono");
- dht.begin();
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- }
- void loop()
- {
- if (!client.connected())
- {
- reconnect();
- }
- client.loop();
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- Serial.print("Temperatura: ");
- Serial.print(t);
- Serial.print(" °C, Wilgotność: ");
- Serial.print(h);
- Serial.println(" %");
- String temperatura = String(t);
- String wilgotnosc = String(h);
- // Publikowanie danych w temacie MQTT
- client.publish((String(mqtt_topic) + "/temperatura").c_str(), temperatura.c_str());
- client.publish((String(mqtt_topic) + "/wilgotnosc").c_str(), wilgotnosc.c_str());
- delay(5000); // Odczyt i publikacja co 5 sekund
- }