Feo
New member
Здравствуйте!
Помогите новичку. Пытаюсь отправлять данные с ESP32 в Yandex Cloud. MQTT с аутентификацией SSL. У них есть пример работы, но для ESP8266. Под мою плату ESP32 он не компилируется: "ESP8266WiFi.h: No such file or directory"
Подскажите, как правильно его адаптировать. Пробовал вместо библиотеки ESP8266WiFi.h включать в проект библиотеки WiFi.h и WiFiClientSecure.h. Тогда возникает ошибка с командой "BearSSL::X509List x509(test_root_ca);"
Помогите новичку. Пытаюсь отправлять данные с ESP32 в Yandex Cloud. MQTT с аутентификацией SSL. У них есть пример работы, но для ESP8266. Под мою плату ESP32 он не компилируется: "ESP8266WiFi.h: No such file or directory"
Подскажите, как правильно его адаптировать. Пробовал вместо библиотеки ESP8266WiFi.h включать в проект библиотеки WiFi.h и WiFiClientSecure.h. Тогда возникает ошибка с командой "BearSSL::X509List x509(test_root_ca);"
C:
#include <ESP8266WiFi.h>
#include <PubSubClient.h> // MQTT Client
//#include <WiFi.h>
//#include "src/dependencies/WiFiClientSecure/WiFiClientSecure.h" //using older WiFiClientSecure
//#include "uptime_formatter.h"
const char* ssid = ".....";
const char* password = "....";
const char* mqttserver = "130.193.44.244"; //mqtt.cloud.yandex.net
const char* yandexIoTCoredeviceId = "....";
const char* mqttpassword = "....."; //<Yandex IoT Core Device pawssword>
const int mqttport = 8883;
String topicCommands = String("$devices/") + String(yandexIoTCoredeviceId) + String("/commands/#");
String topicEvents = String("$devices/") + String(yandexIoTCoredeviceId) + String("/events/");
const char* test_root_ca = \
"-----BEGIN CERTIFICATE-----\n \
-----END CERTIFICATE-----\n";
WiFiClientSecure net;
PubSubClient client(net);
BearSSL::X509List x509(test_root_ca);
#define DEBUG_SERIAL Serial
#define DEBUG_SERIAL_BAUDRATE 115200
void connect() {
delay(5000);
DEBUG_SERIAL.print("Conecting to wifi ...");
while (WiFi.status() != WL_CONNECTED) {
DEBUG_SERIAL.print(".");
delay(1000);
}
DEBUG_SERIAL.println(" Connected");
net.setInsecure();
DEBUG_SERIAL.print("Connecting to Yandex IoT Core as");
DEBUG_SERIAL.print(yandexIoTCoredeviceId);
DEBUG_SERIAL.print(" ...");
while (!client.connect("Esp32Client", yandexIoTCoredeviceId, mqttpassword)) {
DEBUG_SERIAL.print(".");
delay(1000);
}
DEBUG_SERIAL.println(" Connected");
if (client.publish(topicEvents.c_str(), yandexIoTCoredeviceId)) {
DEBUG_SERIAL.println("Publish ok");
}
else {
DEBUG_SERIAL.println("Publish failed");
}
DEBUG_SERIAL.println("Subscribe to: ");
DEBUG_SERIAL.print(topicCommands.c_str());
client.subscribe(topicCommands.c_str());
}
void setup() {
DEBUG_SERIAL.begin(DEBUG_SERIAL_BAUDRATE);
delay(10);
DEBUG_SERIAL.println("Device started");
WiFi.begin(ssid, password);
client.setServer(mqttserver, mqttport);
client.setCallback(messageReceived);
client.setBufferSize(1024);
client.setKeepAlive(15);
connect();
}
void messageReceived(char* topic, byte* payload, unsigned int length) {
String topicString = String(topic);
DEBUG_SERIAL.print("Message received. Topic: ");
DEBUG_SERIAL.println(topicString.c_str());
String payloadStr = "";
for (int i = 0; i < length; i++) {
payloadStr += (char)payload[i];
}
DEBUG_SERIAL.print("Payload: ");
DEBUG_SERIAL.println(payloadStr);
}
void loop() {
// put your main code here, to run repeatedly:
client.loop();
if (!client.connected()) {
connect();
}
}