it:iot:baxi:arduino-wemos-d1-r2-uno-esp8266

Rozdíly

Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.

Odkaz na výstup diff

Obě strany předchozí revize Předchozí verze
Následující verze
Předchozí verze
it:iot:baxi:arduino-wemos-d1-r2-uno-esp8266 [2023/12/03 20:00] Petr Nosekit:iot:baxi:arduino-wemos-d1-r2-uno-esp8266 [2024/01/01 21:19] (aktuální) Petr Nosek
Řádek 53: Řádek 53:
 Na [[https://www.arduino.cc/en/software|této adrese]] jsem stáhnul vývojové prostředí Arduino IDE 2.2.1. Zvolil jsem ZIP file pro Linux.  Na [[https://www.arduino.cc/en/software|této adrese]] jsem stáhnul vývojové prostředí Arduino IDE 2.2.1. Zvolil jsem ZIP file pro Linux. 
  
-Po rozbalení stačí spustit+Uživatel, který spouští Arduino IDE musí být ve skupině **dialout**. Po rozbalení stačí spustit
  
 <code bash> <code bash>
Řádek 326: Řádek 326:
 </code> </code>
  
 +
 +
 +==== 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:iot:self-signed-certificate|]]. Dále je nakonfigurován MQTT server Mosquitto [[it:iot:mosquitto|pro použití uživatelského jména a hesla]]. Čip ESP8266 nezvládá ověření self signed certifikátu, proto jsem to musel v kódu vypnout. V článku o [[it:iot:self-signed-certificate|Self signed certifikátech]] mám vysvětleno, jak jsem je dostal do kódu pro Arduino.
 +  * 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 <ESP8266WiFi.h>
 +#include <WiFiClientSecure.h>
 +#include <PubSubClient.h>
 +
 +
 +#include "client_crt.h"  // client certificate
 +#include "client_key.h"  // client key
 +#include "ca_crt.h"  // CA
 +
 +
 +const char* WiFiName = "Wifi_IoT";
 +const char* WiFiPassword = "supersecurepassword";
 +
 +
 +// MQTT connection
 +const char* clientName = "wemos";
 +const char* mqttBroker = "192.168.0.50";
 +const int mqttPort = 8883;
 +const char* mqttUser = "user";
 +const char* mqttPassword = "users_password";
 +const char* mqttTopic = "building/room/wemos";
 +
 +
 +#define LED_SCK 14
 +
 +WiFiClientSecure espClient;
 +PubSubClient mqttClient(espClient);
 +
 +
 +void setupWiFi() {
 +  
 +  
 +  WiFi.mode(WIFI_STA);
 +  WiFi.begin(WiFiName, WiFiPassword);
 +  
 +  // connect to WiFi
 +  Serial.print("Connecting to WiFi...");
 +  
 +  // 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("Connected to WiFi ");
 +  Serial.println(WiFiName);
 +  Serial.print("IP address: ");
 +  Serial.println(WiFi.localIP());
 +
 +  WiFi.setAutoReconnect(true);
 +  WiFi.persistent(true);
 +
 +}
 +
 +
 +
 +
 +void reconnectMQTT() {
 +
 +  int retryCount = 0;
 +
 +  while (!mqttClient.connected()) {
 +
 +    Serial.println("Attempting to connect to MQTT server...");
 +
 +    // SSL/TLS certificate and key settings
 +    // The BearSSL::X509List and BearSSL::PrivateKey objects must be in scope (alive)
 +    // 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::X509List cert(cert_der, cert_der_len);
 +    BearSSL::PrivateKey key(key_der, key_der_len);
 +    BearSSL::X509List caCert(ca_der, ca_der_len);
 +
 +
 +    espClient.setInsecure();
 +    //espClient.setTrustAnchors(&caCert);  // Setting the CA certification
 +    espClient.setClientRSACert(&cert, &key);  // Setting the client's cert and key
 +
 +    mqttClient.setServer(mqttBroker, mqttPort);
 +
 +
 +    if (mqttClient.connect(clientName, mqttUser, mqttPassword)) {
 +      Serial.println("Connected");
 +      // Subscribe to topics here if necessary
 +      // mqttClient.subscribe("some/topic");
 +    } else {
 +      Serial.print("Failed, rc=");
 +      Serial.print(mqttClient.state());
 +      Serial.println(" trying again in 5 seconds");
 +      delay(5000); // 5-second delay between attempts
 +
 +      retryCount++;
 +
 +      if (retryCount > 5) {  // Restart the ESP if it fails to connect 5 times
 +        //ESP.restart();
 +      }
 +
 +    }
 +  }
 +
 +}
 +
 +
 +
 +void setup() {
 +
 +  // put your setup code here, to run once:
 +
 +  // set LED diode as OUTPUT a turn on the diode
 +  pinMode(LED_SCK, HIGH);
 +
 +  // 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, message);
 +  }
 + 
 +
 +}
 +
 +</code>
  
  • it/iot/baxi/arduino-wemos-d1-r2-uno-esp8266.1701633607.txt.gz
  • Poslední úprava: 2023/12/03 20:00
  • autor: Petr Nosek