Hi everyone, I’m new on this stuff, so I’ve been reading a lot of forums that talk about ESP32 connection to web clusters and I’ve addapted the default example to my board, the thing is when I tried to connect my ESP32 DEVKITV1 board to my web Cluster I had this error when I uploaded the code on my board:
Intentando conexión MQTT
[ 2040][E][ssl_client.cpp:37] _handle_error(): [start_ssl_client():187]: (-4396) PEM - PEM string is not as expected : BASE64 - Invalid character in input
[ 2054][E][WiFiClientSecure.cpp:144] connect(): start_ssl_client: -4396
Comunicación fallida, rc= -2
Intente de nuevo en 5 segundos
Here is my code:
#include "definitions.h"
#include "PubSubClient.h"
#include "WiFiClientSecure.h"
WiFiClientSecure espClient;
PubSubClient client(espClient);
// WiFi functions
void connectWifiTask();
// MQTT functions
void publishMessage(const char*, String);
void callback(char*, byte*, unsigned int);
void reconnect();
const char* sensor1_topic = "Señal_Flujo1";
const char* sensor2_topic = "Señal_Flujo2";
const char* command1_topic = "Señal_Valvula1";
const char* command2_topic = "Señal_Valvula2";
void setup() {
Serial.begin(115200);
connectWifiTask();
randomSeed(micros());
while (!Serial) delay(1);
espClient.setCACert(root_ca);
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
pinMode(SENSOR_FLUJO_1, INPUT);
pinMode(SENSOR_FLUJO_2, INPUT);
pinMode(ELECTROVALVULA_1, OUTPUT);
pinMode(ELECTROVALVULA_2, OUTPUT);
attachInterrupt(SENSOR_FLUJO_1, ContarPulsos1, RISING); //(Interrupcion 0(#Pin,funcion,Flanco de subida))
attachInterrupt(SENSOR_FLUJO_2, ContarPulsos2, RISING);
}
void loop() {
if (!client.connected()){
reconnect();
}
client.loop();
float frecuencia[2];
ObtenerFrecuencias(frecuencia);
float caudal1_L_m = frecuencia[0]/factor_conversion;
float caudal2_L_m = frecuencia[1]/factor_conversion;
publishMessage(sensor1_topic, String(caudal1_L_m));
publishMessage(sensor2_topic, String(caudal2_L_m));
}
void connectWifiTask(){
Serial.print("\nConectando a: ");
Serial.println(WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("\nConectado al WiFi: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
while (!client.connected()) {
Serial.println("Intentando conexión MQTT");
String clientId = "ESP32Client";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Conectado");
client.subscribe(command1_topic);
}else {
Serial.print("Comunicación fallida, rc= ");
Serial.println(client.state());
Serial.println("Intente de nuevo en 5 segundos");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String incommingMessage = "";
for (int i = 0; i < length; i++) incommingMessage+=(char)payload[i];
Serial.println("Mensaje recibido [" + String(topic) + "]" + incommingMessage);
/*// check for other commands
else if( strcmp(topic,command2_topic) == 0){
if (incommingMessage.equals(“1”)) { } // do something else
}
*/
}
void publishMessage(const char* topic, String payload){
if (client.publish(topic, payload.c_str(), true))
Serial.println("Mensaje publicado ["+String(topic)+"]: "+payload);
}
I have all the constants defined in another file called definitions, but here are the most relevants:
#include <Arduino.h>
//---- WiFi settings
const char WIFI_SSID[] = "My red";
const char WIFI_PASSWORD[] = "My password";
//---- MQTT Broker settings
const char* mqtt_server = "dba7ffda2f3e406daf4b090dfdbf85c4.s1.eu.hivemq.cloud";
const char* mqtt_username = "My username";
const char* mqtt_password = "My password";
const int mqtt_port = 8883;
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
//---- MQTT Broker certificate
static const char *root_ca PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNM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