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:esp8266:vyroba-wifi-teplomeru [2024/01/02 17:09] – [Výroba Wi-Fi teploměru s ESP8266] Petr Nosek | it:iot:esp8266:vyroba-wifi-teplomeru [2024/01/02 17:17] (aktuální) – Petr Nosek | ||
|---|---|---|---|
| Řádek 631: | Řádek 631: | ||
| + | |||
| + | ===== Kód pro teploměr s ESP8266 napsaný v C++ ===== | ||
| + | |||
| + | I když jsem začal stránku s tím, že budu používat MicroPython, | ||
| + | |||
| + | <code cpp> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #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 = "/ | ||
| + | |||
| + | Adafruit_BME280 bme; | ||
| + | |||
| + | |||
| + | |||
| + | 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:: | ||
| + | |||
| + | |||
| + | |||
| + | espClient.setInsecure(); | ||
| + | espClient.setClientRSACert(& | ||
| + | |||
| + | mqttClient.setServer(mqttBroker, | ||
| + | |||
| + | |||
| + | if (mqttClient.connect(clientName, | ||
| + | | ||
| + | Serial.println(" | ||
| + | | ||
| + | } 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: | ||
| + | |||
| + | | ||
| + | // start communication on serial line | ||
| + | Serial.begin(9600); | ||
| + | |||
| + | |||
| + | // Inicializace BME280 | ||
| + | if (!bme.begin(0x76)) { // BME280 I2C sensor address | ||
| + | Serial.println(" | ||
| + | while (1); | ||
| + | } | ||
| + | | ||
| + | setupWiFi(); | ||
| + | | ||
| + | reconnectMQTT(); | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | void loop() { | ||
| + | // put your main code here, to run repeatedly: | ||
| + | |||
| + | if (!mqttClient.connected()) { | ||
| + | reconnectMQTT(); | ||
| + | } | ||
| + | mqttClient.loop(); | ||
| + | |||
| + | |||
| + | // send message each 5 sec | ||
| + | static unsigned long lastMsg = 0; | ||
| + | unsigned long now = millis(); | ||
| + | |||
| + | if (now - lastMsg > 5000) { | ||
| + | lastMsg = now; | ||
| + | |||
| + | // read data from BME280 | ||
| + | float humidity = bme.readHumidity(); | ||
| + | float temperature = bme.readTemperature(); | ||
| + | float pressure = bme.readPressure() / 100.0F; | ||
| + | |||
| + | |||
| + | // Vytvoření JSON objektu | ||
| + | StaticJsonDocument< | ||
| + | | ||
| + | doc[" | ||
| + | doc[" | ||
| + | doc[" | ||
| + | |||
| + | // Serializace JSON objektu do řetězce | ||
| + | char jsonBuffer[200]; | ||
| + | serializeJson(doc, | ||
| + | |||
| + | // Odeslání JSON řetězce na MQTT server | ||
| + | mqttClient.publish(mqttTopic, | ||
| + | | ||
| + | } | ||
| + | |||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <adm warning> | ||