Hi All,
Need your help once more.
I had a perfect working sketch, everything working.
Made a bad connection and fried my ESP
I bought a new one, is a esp32-s2-saola-1.
I think this problem is something else than mqtt, but can't figure what!!
The sketch below is working fine, but only because the server is local. Any server I try outside my network doesn't work, just keep trying.
It's driving me crazy ... again
Here's the code:
////////////////////////////
//INCLUDE
#include "libs.h"
///////////////////////////
// Add your MQTT Broker IP address, example:
//=======================================================================
// START MQTT SERVER
//=======================================================================
const char* mqtt_server = "192.168.121.22";
const char* mqtt_name = "WeatherStation";
const char* mqtt_weather = "weather/#";
const char* mqtt_tasmota = "stat/tasmota_xxxxx/POWER";
const char* mqttuser = "xxxxxxxx";
const char* mqttpass = "xxxxxxxxxxx";
const int mqttPort = 1883;
WiFiClient wifiClient;
PubSubClient client(mqtt_server, mqttPort, wifiClient);
long timeToReconnect = 0;
String rxString = "";
String mqttComando;
//long timerRequest;
String mqttReqst;
//Mqtt
const long intervalleitura = 4000; //4 Seg entre leituras e envio ao mqtt e LCD
unsigned long previousMillisleitura = 0;
String mqttmess = "";
//=======================================================================
// MQTT CALLBACK
//=======================================================================
void callback(char* mqtt_topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(mqtt_topic);
Serial.print("] ");
for (int i = 0; i < length; i++) rxString += (char)payload[i];
Serial.print("(");
Serial.print(rxString);
Serial.println(")");
rxString = "";
mqttComando = mqtt_topic;
}
void connectMosquitto() {
client.setServer(mqtt_server, mqttPort);
client.setCallback(callback);
Serial.println("");
Serial.println("Conecting MOSQUITTO Broker...");
if (client.connect(mqtt_name, mqttuser, mqttpass)) {
Serial.println("Connected to MOSQUITTO Broker!");
Serial.println("");
Serial.print("I have subscribe ");
Serial.print(mqtt_weather); Serial.print(" e "); Serial.print(mqtt_tasmota);
client.subscribe(mqtt_weather);
client.subscribe(mqtt_tasmota);
client.publish("weather", "ola do esp32");
} else {
Serial.println("Connection to MOSQUITTO Broker failed. , i will try again...");
timeToReconnect = millis() + 5000;
}
}
//////////////////// SETUP ////////////////
void setup() {
//Serial
Serial.begin(115200);
///////////////////////////
//Wifi
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) Serial.println("STA Failed to configure");
WiFi.begin(ssid, password);
delay(1000);
//BROKER
connectMosquitto();
//OTA
#include "ota.h"
///////////////////////////
}
//////////////////// LOOP ////////////////
void loop() {
///////////////////////////////////////
//MOSQUITTO
client.loop();
if (client.state() != 0 && timeToReconnect < millis() && WiFi.status() == WL_CONNECTED) {
connectMosquitto();
}
client.loop();
//OTA
ArduinoOTA.handle();
}
More info: I can ping the ESP, OTA is working fine.
I'll apreciate any help.