Rozdíly
Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.
Obě strany předchozí revize Předchozí verze | |||
it:iot:baxi:arduino-wemos-d1-r2-uno-esp8266 [2023/12/03 20:05] – Petr Nosek | it:iot:baxi:arduino-wemos-d1-r2-uno-esp8266 [2024/01/01 21:19] (aktuální) – Petr Nosek | ||
---|---|---|---|
Řádek 326: | Řádek 326: | ||
</ | </ | ||
+ | |||
+ | |||
+ | ==== Připojení k zabezpečenému MQTT ==== | ||
+ | |||
+ | Toto už jsem připravoval vlastní kód. Je to základní kód pro připojení k MQTT serveru. Tady je chování: | ||
+ | |||
+ | * připojí se k nadefinované Wi-Fi. Pokud dojde k výpadku Wi-Fi, tak se pokouší znovu připojit. Měl by se o to pokoušet do té doby, dokud se nepodaří spojení obnovit. | ||
+ | * poté se připojí k MQTT serveru. Opět může docházet k výpadkům MQTT serveru, takže se kód postará o vytvoření nového připojení. | ||
+ | * připojení k MQTT je zabezpečeno pomocí [[it: | ||
+ | * program zapne LED diodu na zařízení a posílá zprávu každou vteřinu do MQTT kanálu | ||
+ | |||
+ | Program řeší základní problematiku navázání a obnovení síťových spojení. | ||
+ | |||
+ | <code cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | |||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | |||
+ | const char* WiFiName = " | ||
+ | const char* WiFiPassword = " | ||
+ | |||
+ | |||
+ | // MQTT connection | ||
+ | const char* clientName = " | ||
+ | const char* mqttBroker = " | ||
+ | const int mqttPort = 8883; | ||
+ | const char* mqttUser = " | ||
+ | const char* mqttPassword = " | ||
+ | const char* mqttTopic = " | ||
+ | |||
+ | |||
+ | #define LED_SCK 14 | ||
+ | |||
+ | WiFiClientSecure espClient; | ||
+ | PubSubClient mqttClient(espClient); | ||
+ | |||
+ | |||
+ | void setupWiFi() { | ||
+ | | ||
+ | | ||
+ | WiFi.mode(WIFI_STA); | ||
+ | WiFi.begin(WiFiName, | ||
+ | | ||
+ | // connect to WiFi | ||
+ | Serial.print(" | ||
+ | | ||
+ | // wait for connecting to the server | ||
+ | // during this waiting it will write dot to serial link | ||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | |||
+ | // write new line, Wi-Fi network and IP address of connection | ||
+ | Serial.println("" | ||
+ | Serial.print(" | ||
+ | Serial.println(WiFiName); | ||
+ | Serial.print(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | |||
+ | WiFi.setAutoReconnect(true); | ||
+ | WiFi.persistent(true); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | void reconnectMQTT() { | ||
+ | |||
+ | int retryCount = 0; | ||
+ | |||
+ | while (!mqttClient.connected()) { | ||
+ | |||
+ | Serial.println(" | ||
+ | |||
+ | // SSL/TLS certificate and key settings | ||
+ | // The BearSSL:: | ||
+ | // for the duration of their usage by espClient. If these objects are defined | ||
+ | // within a separate function and used here, they will be destroyed when that | ||
+ | // function exits, leading to undefined behavior and potential device resets. | ||
+ | // A solution to this is to declare them as global variables if they need to | ||
+ | // be used across multiple functions. | ||
+ | BearSSL:: | ||
+ | BearSSL:: | ||
+ | BearSSL:: | ||
+ | |||
+ | |||
+ | espClient.setInsecure(); | ||
+ | // | ||
+ | espClient.setClientRSACert(& | ||
+ | |||
+ | mqttClient.setServer(mqttBroker, | ||
+ | |||
+ | |||
+ | if (mqttClient.connect(clientName, | ||
+ | Serial.println(" | ||
+ | // Subscribe to topics here if necessary | ||
+ | // mqttClient.subscribe(" | ||
+ | } else { | ||
+ | Serial.print(" | ||
+ | Serial.print(mqttClient.state()); | ||
+ | Serial.println(" | ||
+ | delay(5000); | ||
+ | |||
+ | retryCount++; | ||
+ | |||
+ | if (retryCount > 5) { // Restart the ESP if it fails to connect 5 times | ||
+ | // | ||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | void setup() { | ||
+ | |||
+ | // put your setup code here, to run once: | ||
+ | |||
+ | // set LED diode as OUTPUT a turn on the diode | ||
+ | pinMode(LED_SCK, | ||
+ | |||
+ | // start communication on serial line | ||
+ | Serial.begin(9600); | ||
+ | |||
+ | setupWiFi(); | ||
+ | | ||
+ | reconnectMQTT(); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | // put your main code here, to run repeatedly: | ||
+ | |||
+ | if (!mqttClient.connected()) { | ||
+ | reconnectMQTT(); | ||
+ | } | ||
+ | mqttClient.loop(); | ||
+ | |||
+ | // Odesílání zprávy každou vteřinu | ||
+ | static unsigned long lastMsg = 0; | ||
+ | unsigned long now = millis(); | ||
+ | if (now - lastMsg > 1000) { | ||
+ | lastMsg = now; | ||
+ | // Tvořte zde svou zprávu | ||
+ | const char* message = "Hello MQTT"; | ||
+ | mqttClient.publish(mqttTopic, | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||