Różnice
Różnice między wybraną wersją a wersją aktualną.
| Nowa wersja | Poprzednia wersja | ||
| programowanie:esp32:mqtt [2024/08/10 00:36] – utworzono sases | programowanie:esp32:mqtt [2024/09/08 18:10] (aktualna) – sases | ||
|---|---|---|---|
| Linia 1: | Linia 1: | ||
| - | ====== MQTT ====== | + | ====== MQTT na ESP32 ====== |
| + | \\ | ||
| \\ | \\ | ||
| - | === Deklaracje === | + | ===== Deklaracje |
| <code c> | <code c> | ||
| + | #include " | ||
| + | #include < | ||
| #include < | #include < | ||
| - | const String Device_ID = " | + | const String Device_ID = " |
| const String myHostname = " | const String myHostname = " | ||
| - | String Mqtt_Server = " | + | String Mqtt_Server = " |
| - | int Mqtt_Port = numer_portu; | + | int Mqtt_Port = numer_portu; |
| - | String Mqtt_User = " | + | String Mqtt_User = " |
| - | String Mqtt_Password = " | + | String Mqtt_Password = " |
| - | // const int Mqtt_Buffer_Size = 512; // | + | // 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(); | ||
| </ | </ | ||
| + | \\ | ||
| \\ | \\ | ||
| - | === Połączenie podczas uruchomienia === | + | ===== Połączenie podczas uruchomienia |
| <code c> | <code c> | ||
| Linia 32: | Linia 36: | ||
| } | } | ||
| </ | </ | ||
| + | \\ | ||
| \\ | \\ | ||
| - | === Ponowne łączenie w przypadku rozłączenia === | + | ===== Ponowne łączenie w przypadku rozłączenia |
| <code c> | <code c> | ||
| Linia 45: | Linia 50: | ||
| } | } | ||
| </ | </ | ||
| + | \\ | ||
| \\ | \\ | ||
| - | === Funkcja łączenia === | + | ===== Funkcja łączenia |
| <code c> | <code c> | ||
| Linia 59: | Linia 65: | ||
| mqttClient.setServer(Mqtt_Server.c_str(), | mqttClient.setServer(Mqtt_Server.c_str(), | ||
| mqttClient.setCallback(callback); | mqttClient.setCallback(callback); | ||
| - | mqttClient.setBufferSize(Mqtt_Buffer_Size); | + | |
| while (!mqttClient.connected() and apiGetSuccess) { | while (!mqttClient.connected() and apiGetSuccess) { | ||
| Linia 72: | Linia 78: | ||
| + " | + " | ||
| - | if (mqttClient.connect(myMqttName, | + | if (mqttClient.connect(myMqttName, |
| { | { | ||
| Serial.println(" | Serial.println(" | ||
| - | mqttClient.subscribe(" | + | mqttClient.subscribe(" |
| } | } | ||
| else | else | ||
| Linia 94: | Linia 100: | ||
| } | } | ||
| </ | </ | ||
| + | \\ | ||
| \\ | \\ | ||
| - | === Funkcja odbierania wiadomości === | + | ===== Funkcja odbierania wiadomości |
| <code c> | <code c> | ||
| Linia 114: | Linia 121: | ||
| } | } | ||
| - | if (splitTopic[0] == " | + | if (splitTopic[0] == " |
| { | { | ||
| ... | ... | ||
| Linia 120: | Linia 127: | ||
| } | } | ||
| </ | </ | ||
| + | \\ | ||
| \\ | \\ | ||
| - | === Publikowanie wiadomości === | + | ===== Publikowanie wiadomości ===== |
| + | |||
| + | == Proste | ||
| - | Proste | ||
| <code c> | <code c> | ||
| mqttClient.publish(" | mqttClient.publish(" | ||
| </ | </ | ||
| - | Sprawdzające rozmiar wiadomości | + | |
| + | == Sprawdzające rozmiar wiadomości | ||
| <code c> | <code c> | ||
| String topic = " | String topic = " | ||
| Linia 135: | Linia 146: | ||
| { | { | ||
| Serial.println(" | Serial.println(" | ||
| - | Serial.println(" | + | Serial.println(" |
| Serial.println(" | Serial.println(" | ||
| } | } | ||
| </ | </ | ||
| + | \\ | ||
| + | \\ | ||
| + | |||
| + | ===== Kody błędów połączenia ===== | ||
| + | |||
| + | ^ -4 | MQTT_CONNECTION_TIMEOUT | the server didn't respond within the keepalive time | | ||
| + | ^ -3 | MQTT_CONNECTION_LOST | the network connection was broken | | ||
| + | ^ -2 | MQTT_CONNECT_FAILED | the network connection failed | | ||
| + | ^ -1 | MQTT_DISCONNECTED | the client is disconnected cleanly | | ||
| + | ^ 0 | MQTT_CONNECTED | the client is connected | | ||
| + | ^ 1 | MQTT_CONNECT_BAD_PROTOCOL | the server doesn' | ||
| + | ^ 2 | MQTT_CONNECT_BAD_CLIENT_ID | the server rejected the client identifier | | ||
| + | ^ 3 | MQTT_CONNECT_UNAVAILABLE | the server was unable to accept the connection | | ||
| + | ^ 4 | MQTT_CONNECT_BAD_CREDENTIALS | the username/ | ||
| + | ^ 5 | MQTT_CONNECT_UNAUTHORIZED | the client was not authorized to connect | | ||