vencinachev

SmartHome - ESP32 - TKS

Jan 13th, 2026
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <DHT.h>
  4.  
  5.  
  6. // WiFi
  7. const char* ssid = "Wokwi-GUEST";
  8. const char* password = "";
  9.  
  10.  
  11. // MQTT
  12. const char* mqtt_server = "test.mosquitto.org";
  13. const int   mqtt_port   = 1883;
  14.  
  15.  
  16. const char* topic_control = "tks/lamp/control";  
  17. const char* topic_temp    = "tks/dht22/temperature";
  18. const char* topic_hum     = "tks/dht22/humidity";
  19.  
  20.  
  21. // Хардуер пинове
  22. const int RELAY_PIN = 32;
  23. const int DHT_PIN   = 33;
  24. #define DHTTYPE DHT22
  25.  
  26.  
  27. WiFiClient espClient;
  28. PubSubClient client(espClient);
  29. DHT dht(DHT_PIN, DHTTYPE);
  30.  
  31.  
  32. // Таймер за изпращане на всеки 2 секунди
  33. unsigned long lastSendMs = 0;
  34. const unsigned long SEND_INTERVAL_MS = 2000;
  35.  
  36.  
  37. const bool RELAY_ACTIVE_LOW = false;
  38.  
  39.  
  40. void setRelay(bool on) {
  41.   if (RELAY_ACTIVE_LOW) {
  42.     digitalWrite(RELAY_PIN, on ? LOW : HIGH);
  43.   } else {
  44.     digitalWrite(RELAY_PIN, on ? HIGH : LOW);
  45.   }
  46. }
  47.  
  48.  
  49. void callback(char* topic, byte* payload, unsigned int length) {
  50.   String msg;
  51.   msg.reserve(length);
  52.   for (unsigned int i = 0; i < length; i++) msg += (char)payload[i];
  53.   msg.trim();
  54.  
  55.  
  56.   if (String(topic) == topic_control) {
  57.     if (msg == "on")  setRelay(true);
  58.     if (msg == "off") setRelay(false);
  59.   }
  60. }
  61.  
  62.  
  63. void connectWiFi() {
  64.   WiFi.begin(ssid, password);
  65.   while (WiFi.status() != WL_CONNECTED) {
  66.     delay(200);
  67.   }
  68. }
  69.  
  70.  
  71. void reconnectMQTT() {
  72.   while (!client.connected()) {
  73.     // уникален clientId !!!
  74.     String clientId = "esp32-dht22-relay-";
  75.     clientId += String((uint32_t)ESP.getEfuseMac(), HEX);
  76.  
  77.  
  78.     if (client.connect(clientId.c_str())) {
  79.       client.subscribe(topic_control);
  80.     } else {
  81.       delay(1000);
  82.     }
  83.   }
  84. }
  85.  
  86.  
  87. void setup() {
  88.   pinMode(RELAY_PIN, OUTPUT);
  89.   setRelay(false);
  90.   dht.begin();
  91.   connectWiFi();
  92.   client.setServer(mqtt_server, mqtt_port);
  93.   client.setCallback(callback);
  94.   reconnectMQTT();
  95. }
  96.  
  97.  
  98. void loop() {
  99.   if (WiFi.status() != WL_CONNECTED) {
  100.     connectWiFi();
  101.   }
  102.   if (!client.connected()) {
  103.     reconnectMQTT();
  104.   }
  105.   client.loop();
  106.   unsigned long now = millis();
  107.   if (now - lastSendMs >= SEND_INTERVAL_MS) {
  108.     lastSendMs = now;
  109.  
  110.  
  111.     float t = dht.readTemperature(); // Celsius
  112.     float h = dht.readHumidity();
  113.  
  114.  
  115.     if (!isnan(t)) {
  116.       char buf[16];
  117.       dtostrf(t, 0, 2, buf);          // 2 знака след десетичната
  118.       client.publish(topic_temp, buf);
  119.     }
  120.  
  121.  
  122.     if (!isnan(h)) {
  123.       char buf[16];
  124.       dtostrf(h, 0, 2, buf);
  125.       client.publish(topic_hum, buf);
  126.     }
  127.   }
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment