Closed bytesseur closed 4 years ago
There's also a strange thing happening:
11:59:18.130 -> IPv4: 0.0.0.0
11:59:28.229 -> MQTT: -2
It showed IP on beginning and now there's 0?
Hi, this connects
#include <WiFiEspAT.h>
#include <PubSubClient.h>
const char* server = "hairdresser.cloudmqtt.com";
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
WiFiClient ethClient;
PubSubClient client(ethClient);
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic","hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(115200);
client.setServer(server, 16068);
client.setCallback(callback);
Serial1.begin(115200);
WiFi.init(Serial1);
if (WiFi.status() == WL_NO_MODULE) {
Serial.println();
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// waiting for connection to Wifi network set with the SetupWiFiConnection sketch
Serial.println("Waiting for connection to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print('.');
}
Serial.println();
Serial.println("Connected to WiFi network.");
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}
do you do something with Serial used for esp8266? do you have interrupts disabled? do you call WiFiEspAT from interrupt?
Hi, thank you for your response. I can confirm that ESP connects to MQTT using the code above.
do you do something with Serial used for esp8266?
Serial for ESP is separated from Serial used for logs. Both work on different baud rate.
To make this easier to understand I will send you the Sketch. ;)
Hi Juraj,
you were right... I didn't call ESP from interrupt. Adding the sei()
fixed all problems.
Everything works perfectly - thank you for creating such a useful library for ESP8266! :)
~ Daniel
Hi Juraj,
For starter... ESP8266 firmware π
PLATFORM: Arduino Uno/Mega + ESP8266 (UART)
ESP connects to the WiFi network correctly:
PROBLEM:
I can't connect to the MQTT cloud broker... I have no idea why it's happening because I have tested it few times without your library - just Arduino console. Here's the example of what I did:
However I saw that WiFiEspAT sets up the AT+CIPMUX=1 in the beginning.
What can cause such a stupid... bug? I use PubSubClient library by knolleary.
I would be very thankful for your help. :)
~ Daniel