Pierwszy projekt -- przebudzenie

Tu będą się pojawiać kody źródłowe,
ODPOWIEDZ
Awatar użytkownika
SunRiver
Użytkownik
Posty: 1513
Rejestracja: 08 paź 2017, 11:27
Lokalizacja: Festung Oppeln
Kontakt:

Pierwszy projekt -- przebudzenie

Post autor: SunRiver »

Kody są omówione w książce, ale tu mozna też na ich temat zadawać pytania


Wariant 1 -- Arduino
  1.  
  2. #include <DHT.h>
  3. #define DHTPIN 6
  4. #define DHTTYPE DHT22
  5. DHT dht(DHTPIN, DHTTYPE);
  6. void setup()
  7. {
  8.     Serial.begin(115200);
  9.     dht.begin();
  10. }
  11. void loop()
  12. {
  13.     delay(2000);
  14.     float h = dht.readHumidity();
  15.     float t = dht.readTemperature();
  16.     Serial.print("Wilgotność: ");
  17.     Serial.print(h);
  18.     Serial.print("% Temperatura: ");
  19.     Serial.print(t);
  20.     Serial.print("*C ");
  21. }
  22.  
  23.  

------------
IDF - Platformio
  1.  
  2. #include <stdio.h>
  3. #include <freertos/FreeRTOS.h>
  4. #include <freertos/task.h>
  5. #include <esp_err.h>
  6. #include <dht/dht.h>
  7. #define DHTPIN 6 // Definiujemy pin danych dla czujnika DHT, a więc GPIO6, czyli D6 na płytce D1
  8. #define DHT_TYPE_DHT22 DHT22 // Definiujemy typ czujnika
  9. void odczytDHT(void *arg)
  10. {
  11.     ESP_ERROR_CHECK(dht_init(DHTPIN, false)); // Sprawdzamy, czy DHT22 jest podłączony
  12.     vTaskDelay(2000); // Oczekujemy 2 sekundy na pomiar
  13.     while (1)
  14.     {
  15.         int wilgotnosc = 0;
  16.         int temperatura = 0;
  17.  
  18.         if (dht_read_data(DHT22, DHTPIN, &wilgotnosc, &temperatura) == ESP_OK)
  19.         {
  20.             // Wynik, jaki odczytamy z DHT, to: 604 = 60,4%, 252 = 25,2°C, więc wymaga
  21.             // formatowania
  22.             printf("Wilgotność: %d Temperatura: %d\n", wilgotnosc, temperatura);
  23.         }
  24.         else {
  25.             printf("Błąd odczytu czujnika DHT\n"); // Wyświetlenie błędu w razie problemów z czujnikiem
  26.         }
  27.         vTaskDelay(5000);
  28.     }
  29.     vTaskDelete(NULL);
  30. }
  31. void app_main()
  32. {
  33.     xTaskCreate(odczytDHT, "odczytDHT", 2048, NULL, tskIDLE_PRIORITY, NULL); // Uruchamiamy
  34.         // task odczytu danych
  35. }
  36.  
  37.  
----------------------------------------------------------------------------------------

Wariant drugi :
  1.  
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266WebServer.h>
  4. #include <DHT.h>
  5. // Dane do sieci Wi-Fi
  6. const char *ssid = "<twoja sieć WiFi> ";
  7. const char *password = "<hasło do WiFi>";
  8. // PIN, na którym podłączony jest czujnik DHT22
  9. #define DHTPIN 2
  10. #define DHTTYPE DHT22
  11. // Inicjalizacja obiektu DHT
  12. DHT dht(DHT_PIN, DHTTYPE);
  13. // Obiekt serwera
  14. ESP8266WebServer server(80);
  15. void handleRoot()
  16. {
  17.     // Odczyt danych z czujnika DHT22
  18.     float h = dht.readHumidity();
  19.     float t = dht.readTemperature();
  20.     // Przygotowanie strony HTML
  21.     String html = "<html><body>";
  22.     html += "<h1>Dane z czujnika DHT22:</h1>";
  23.     html += "<p>Temperatura: " + String(t) + " &#8451;</p>";
  24.     html += "<p>Wilgotność: " + String(h) + " %</p>";
  25.     html += "</body></html>";
  26.     // Wysłanie odpowiedzi do klienta
  27.     server.send(200, "text/html", html);
  28. }
  29. void setup()
  30. {
  31.     // Inicjalizacja portu szeregowego
  32.     Serial.begin(115200);
  33.     // Inicjalizacja Wi-Fi
  34.     WiFi.begin(ssid, password);
  35.     while (WiFi.status() != WL_CONNECTED)
  36.     {
  37.         delay(250);
  38.         Serial.print(".");
  39.     }
  40.     Serial.println("");
  41.     Serial.println("WiFi connected");
  42.     Serial.println(WiFi.localIP()); // Wyświetla w terminalu adres IP
  43.     // Inicjalizacja czujnika DHT22
  44.     dht.begin();
  45.     // Konfiguracja obsługi ścieżki głównej ("/")
  46.     server.on("/", HTTP_GET, handleRoot);
  47.     // Uruchomienie serwera WWW
  48.     server.begin();
  49.     Serial.println("HTTP server started");
  50. }
  51. void loop()
  52. {
  53.     // Obsługa żądań klientów
  54.     server.handleClient();
  55. }
  56.  
  57.  

--------------------------------------------------------------------------------------------------------------
Wariant trzeci
  1.  
  2. #include <ESP8266WiFi.h>
  3. #include <PubSubClient.h>
  4. #include <DHT.h>
  5.  
  6. const char* ssid = "<twoja sieć WiFi> ";
  7. const char* password = "<hasło do WiFi>";
  8. const char* mqtt_server = "<adres brokera MQTT>";
  9. const char* mqtt_topic = "<Temat MQTT> ";
  10.  
  11. #define DHTPIN 6
  12. #define DHTTYPE DHT22
  13. DHT dht(DHTPIN, DHTTYPE);
  14.  
  15. WiFiClient espClient;
  16. PubSubClient client(espClient);
  17.  
  18. void reconnect()
  19. {
  20.     while (!client.connected())
  21.     {
  22.         Serial.print("Łączenie z MQTT brokerem...");
  23.         if (client.connect("ESP8266Client"))
  24.         {
  25.             Serial.println("połączony");
  26.         }
  27.         else
  28.         {
  29.             Serial.print("nieudane, rc=");
  30.             Serial.print(client.state());
  31.             Serial.println(" ponowna próba za 5 sekund");
  32.             delay(5000);
  33.         }
  34.     }
  35. }
  36.  
  37. void setup()
  38. {
  39.     Serial.begin(115200);
  40.     WiFi.begin(ssid, password);
  41.     while (WiFi.status() != WL_CONNECTED)
  42.     {
  43.     delay(250);
  44.     Serial.print(".");
  45.     }
  46.     Serial.println("");
  47.     Serial.println("WiFi Połączono");
  48.  
  49.     dht.begin();
  50.  
  51.     client.setServer(mqtt_server, 1883);
  52.     client.setCallback(callback);
  53. }
  54.  
  55. void loop()
  56. {
  57.     if (!client.connected())
  58.     {
  59.         reconnect();
  60.     }
  61.     client.loop();
  62.  
  63.     float h = dht.readHumidity();
  64.     float t = dht.readTemperature();
  65.  
  66.     Serial.print("Temperatura: ");
  67.     Serial.print(t);
  68.     Serial.print(" °C, Wilgotność: ");
  69.     Serial.print(h);
  70.     Serial.println(" %");
  71.  
  72.     String temperatura = String(t);  
  73.     String wilgotnosc = String(h);
  74.  
  75.     // Publikowanie danych w temacie MQTT
  76.     client.publish((String(mqtt_topic) + "/temperatura").c_str(), temperatura.c_str());
  77.     client.publish((String(mqtt_topic) + "/wilgotnosc").c_str(), wilgotnosc.c_str());
  78.  
  79.     delay(5000); // Odczyt i publikacja co 5 sekund
  80. }
  81.  
ODPOWIEDZ

Wróć do „Kody żródłowe”