programowanie:esp32:mqtt

Różnice

Różnice między wybraną wersją a wersją aktualną.

Odnośnik do tego porównania

Poprzednia rewizja po obu stronach Poprzednia wersja
Nowa wersja
Poprzednia wersja
programowanie:esp32:mqtt [2024/08/10 00:41] sasesprogramowanie:esp32:mqtt [2024/09/08 18:10] (aktualna) sases
Linia 1: Linia 1:
-====== MQTT ======+====== MQTT na ESP32 ====== 
 +\\
 \\ \\
  
-=== Deklaracje ===+===== Deklaracje =====
  
 <code c> <code c>
 +#include "Arduino.h"
 +#include <WiFi.h>
 #include <PubSubClient.h> #include <PubSubClient.h>
  
-const String Device_ID = "11"; //ID urządzenia [String]+const String Device_ID = "11";  // ID urządzenia [String]
 const String myHostname = "ssh_device_" + Device_ID; const String myHostname = "ssh_device_" + Device_ID;
  
-String Mqtt_Server = "adres_serwera"; //MQTT broker address +String Mqtt_Server = "adres_serwera";  // MQTT broker address 
-int Mqtt_Port = numer_portu; //MQTT broker port +int Mqtt_Port = numer_portu;  // MQTT broker port 
-String Mqtt_User = "nazwa_uzytkownika"; //MQTT broker username +String Mqtt_User = "nazwa_uzytkownika";  // MQTT broker username 
-String Mqtt_Password = "haslo"; //MQTT broker user password +String Mqtt_Password = "haslo";  // MQTT broker user password 
-// const int Mqtt_Buffer_Size = 512; //powiększenie rozmiaru bufora publikowania+// const int Mqtt_Buffer_Size = 512;  // powiększenie rozmiaru bufora publikowania
  
 PubSubClient mqttClient(wifiClient); PubSubClient mqttClient(wifiClient);
Linia 21: Linia 24:
 void connectMqtt(); void connectMqtt();
 </code> </code>
 +\\
 \\ \\
  
-=== Połączenie podczas uruchomienia ===+===== Połączenie podczas uruchomienia =====
  
 <code c> <code c>
Linia 32: Linia 36:
 } }
 </code> </code>
 +\\
 \\ \\
  
-=== Ponowne łączenie w przypadku rozłączenia ===+===== Ponowne łączenie w przypadku rozłączenia =====
  
 <code c> <code c>
Linia 45: Linia 50:
 } }
 </code> </code>
 +\\
 \\ \\
  
-=== Funkcja łączenia ===+===== Funkcja łączenia =====
  
 <code c> <code c>
Linia 59: Linia 65:
   mqttClient.setServer(Mqtt_Server.c_str(), Mqtt_Port);   mqttClient.setServer(Mqtt_Server.c_str(), Mqtt_Port);
   mqttClient.setCallback(callback);   mqttClient.setCallback(callback);
-  mqttClient.setBufferSize(Mqtt_Buffer_Size);+  // mqttClient.setBufferSize(Mqtt_Buffer_Size);  // powiększenie rozmiaru bufora publikowania
  
   while (!mqttClient.connected() and apiGetSuccess) {   while (!mqttClient.connected() and apiGetSuccess) {
Linia 72: Linia 78:
     + "\",\"online\":false}}";     + "\",\"online\":false}}";
  
-    if (mqttClient.connect(myMqttName, Mqtt_User.c_str(), Mqtt_Password.c_str(), String("ssh/devices/status/+ Device_ID).c_str(), 1, true, json.c_str()))+    if (mqttClient.connect(myMqttName, Mqtt_User.c_str(), Mqtt_Password.c_str(), "will_message_topic", 1, true, json.c_str()))
     {     {
       Serial.println("Connected to MQTT");       Serial.println("Connected to MQTT");
-      mqttClient.subscribe("adres_kanalu"); //subskrypcja+      mqttClient.subscribe("adres_kanalu");  // subskrypcja
     }      } 
     else      else 
Linia 94: Linia 100:
 } }
 </code> </code>
 +\\
 \\ \\
  
-=== Funkcja odbierania wiadomości ===+===== Funkcja odbierania wiadomości =====
  
 <code c> <code c>
Linia 114: Linia 121:
   }   }
  
-  if (splitTopic[0] == "lvl_1" and splitTopic[1] == "lvl_2" and splitTopic[2] == "lvl_3") //kanał odebranej wiadomości+  if (splitTopic[0] == "lvl_1" and splitTopic[1] == "lvl_2" and splitTopic[2] == "lvl_3" // kanał odebranej wiadomości
   {   {
     ...         ...    
Linia 120: Linia 127:
 } }
 </code> </code>
 +\\
 \\ \\
  
-=== Publikowanie wiadomości ===+===== Publikowanie wiadomości ===== 
 + 
 +== Proste ==
  
-Proste 
 <code c> <code c>
 mqttClient.publish("topic", "message"); mqttClient.publish("topic", "message");
 </code> </code>
-Sprawdzające rozmiar wiadomości+ 
 +== Sprawdzające rozmiar wiadomości == 
 <code c> <code c>
 String topic = "topic"); String topic = "topic");
Linia 135: Linia 146:
 { {
   Serial.println("MQTT publish fail!");   Serial.println("MQTT publish fail!");
-  Serial.println("json size: " + String(json.length()) + " bajts");+  Serial.println("json size: " + String("message".length()) + " bajts");
   Serial.println("message buffor: " + String(Mqtt_Buffer_Size));   Serial.println("message buffor: " + String(Mqtt_Buffer_Size));
 } }
 </code> </code>
 +\\
 \\ \\
  
-=== Kody błędów połączenia ===+===== Kody błędów połączenia =====
  
 ^ -4 | MQTT_CONNECTION_TIMEOUT | the server didn't respond within the keepalive time | ^ -4 | MQTT_CONNECTION_TIMEOUT | the server didn't respond within the keepalive time |
  • programowanie/esp32/mqtt.1723243309.txt.gz
  • ostatnio zmienione: 2024/08/10 00:41
  • przez sases