Я пытаюсь установить sh безопасное соединение (tls & open ssl сертификаты) от ESP32 к брокеру MQTT на Raspberry Pi с использованием pubsubclient & Wificlientsecure, но получаю ошибку сокета, как показано ниже:
* Консоль 1002 * MQTT:
1582138400: New connection from 192.168.4.9 on port 8883.
1582138405: Socket error on client <unknown>, disconnecting.
1582138413: New connection from 192.168.4.9 on port 8883.
1582138405: Socket error on client <unknown>, disconnecting.
Вот файл конфигурации - /etc/mosquitto/mosquitto.conf:
persistence true
persistence_location /var/lib/mosquitto/
#log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
port 8883
allow_anonymous true
cafile /etc/mosquitto/certs/ca.crt
keyfile /etc/mosquitto/certs/server.key
certfile /etc/mosquitto/certs/server.crt
#tls_version tlsv1.2
Код Arduino:
#include <PubSubClient.h>
#include <SPIFFS.h>
#include <WiFiClientSecure.h>
#include "DHT.h"
#include <Wire.h>
#include <Adafruit_BMP085.h>
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
#define wifi_ssid "MGK-RP-1" //wifi ssid
#define wifi_password "naht" //wifi password
const char* ca_cert =
"-----BEGIN CERTIFICATE-----\n"
"MIIEAjCCAuqgAwIBAgIJAM0HTvv4E6FaMA0GCSqGSIb3DQEBCwUAMIGVMQswCQYD\n"
"VQQGEwJpbjESMBAGA1UECAwJdGFtaWxuYWR1MRMwEQYDVQQHDApjb2ltYmF0b3Jl\n"
"MQwwCgYDVQQKDANtZ2sxETAPBgNVBAsMCGJ1aWxkaW90MRQwEgYDVQQDDAsxOTIu\n"
"MTY4LjQuMTEmMCQGCSqGSIb3DQEJARYXcmFta3VtYXIubWdrMUBnbWFpbC5jb20w\n"
"HhcNMjAwMjI4MDY1NTIxWhcNMjUwMjI3MDY1NTIxWjCBlTELMAkGA1UEBhMCaW4x\n"
"EjAQBgNVBAgMCXRhbWlsbmFkdTETMBEGA1UEBwwKY29pbWJhdG9yZTEMMAoGA1UE\n"
"JjAkBgkqhkiG9w0BCQEWF3JhbWt1bWFyLm1nazFAZ21haWwuY29tMIIBIjANBgkq\n"
"hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtmbO3kNK6lmYAGhAt8n8yziPNYZ1wLhs\n"
"3W/VkIm5A2veyHz8PivnknVdATx9/fRJ1cy1LKD/Wo5cwaH8dyKiVq/iRYZ4zAxq\n"
"s8va5xUkWlXoiGcB87wCviAGsdNXLa54/qPPa04T5vlaBr+Ht5gsQvB1OeRyTo8U\n"
"mD7VSw9eCBMlBpTZprlHReXLcnBOvNQ/9n0vN/KmdJXXu2ZNGj8P/itbFlRpKBAP\n"
"2AxUJAiNHAB7No7WZ4mMLPxp6iJbDb6KUVH0vwMgVHq4tkWNEcMcnUPRemkiA321\n"
"1oJRxBQY26xd7AXbTass9UqIRF1R1kb29pZqkwarfVAgqalegd8W6wIDAQABo1Mw\n"
"UTAdBgNVHQ4EFgQUHZBuhBeUW9UhnQQ5AWdmQffukPowHwYDVR0jBBgwFoAUHZBu\n"
"hBeUW9UhnQQ5AWdmQffukPowDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsF\n"
"AAOCAQEAC6buT/r7oJiLhgWnU5QrSPFbuX6vViN6//wk1Jz3SZLP/qgfv1UgHXmX\n"
"hGHtse45S9V3ke7isI6IQ2QQpLaCg2NpQoEqXy2E3zUForElRuB2NWy6OT2+fglL\n"
"wWJ2f7STaGJcTUAxwB4R5pfNycTR+jg3l3pNuosnq85E0zSUnvMPPNilM7JooWQ5\n"
"bFixSJUx7rknyRjvD+5X7umyB+rWhv/VdogcKvln3dL70yS3ujWatTVYkUKirdL/\n"
"8MiM2wxi5pSV9PRmiJxbCVRryKMJ05ZELndmjRLtU/0kwKthENkV0pL1n81UqeIu\n"
"PtkIMHm89cGiKmM+pZpCDVcqAN8bWg==\n"
"-----END CERTIFICATE-----\n";
#define mqtt_server "192.168.4.1" // server name or IP
#define temperature_topic "temphum" //Topic temperature
#define atmos_pre_topic "atmos_pre"
#define light_lux_topic "light_lux"
//#define humidity_topic "humid1" //Topic humidity
#define debug_topic "debug"
bool debug = true;
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
int sensorPin = 36; //
float rawRange = 4096;
float logRange = 5.0;
/* create an instance of WiFiClientSecure */
WiFiClientSecure espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(9600);
Serial.println(ca_cert);
setup_wifi(); //Connect to Wifi network
espClient.setCACert(ca_cert);
client.setServer(mqtt_server, 8883); // Configure MQTT connection, change port if needed.
if (!client.connected()) {
reconnect();
}
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
while (1) {}
}
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
String tempe = String(t).c_str();
String hum = String(h).c_str();
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
String heat = String(hic).c_str();
String temphum = tempe+","+hum+","+heat;
float P = bmp.readPressure();
float temp = bmp.readTemperature();
String pre = String(P).c_str();
String atmos_pre = pre+","+String(temp).c_str();
int rawValue = analogRead(sensorPin);
String light_lux = String(RawToLux(rawValue)).c_str();
Serial.println();
delay(500);
if ( debug ) {
Serial.print("Temperature : ");
Serial.print(t);
Serial.print(" | Humidity : ");
Serial.println(h);
}
// Publish values to MQTT topics
client.publish(temperature_topic, String(temphum).c_str(), false);
client.publish(atmos_pre_topic, String(atmos_pre).c_str(), false);
client.publish(light_lux_topic, String(light_lux).c_str(), false);
if ( debug ) {
Serial.println("data sent to MQTT.");
}
}
float RawToLux(int raw)
{
float logLux = raw * logRange / rawRange;
Serial.print("loglux");
Serial.print(logLux);
return pow(10, logLux);
}
//Setup connection to wifi
void setup_wifi() {
delay(20);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
//WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi is OK ");
Serial.print("=> ESP32 new IP address is: ");
Serial.print(WiFi.localIP());
Serial.println("");
}
//Reconnect to wifi if connection is lost
void reconnect() {
while (!client.connected()) {
Serial.print("Connecting to MQTT broker ...");
if (client.connect("ESP32Client",0,2,0,0)) {
Serial.println("OK");
} else {
Serial.print("[Error] Not connected: ");
Serial.println(client.state());
Serial.println("Wait 5 seconds before retry.");
delay(5000);
}
}
}
Pubsubclient подключается к сети, но я получаю код ошибки -2 из функции client.state (). Можете ли вы помочь мне исправить эту ошибку и установить sh безопасное соединение.