Hello, i've bought from AliExpress Arduino Mega 2560 (32M) with built-in ESP8266. Flashed it with
AiThinker_ESP8266_DIO_8M_8M_20160615_V1.5.4 (dont know why 32M version doesnt want to be uploaded). Opened Arduino IDE and used this sketch. The sketch is working, but mqtt stays connected for several seconds and then it reconnects ( withmessage : [WiFiEsp] Disconnecting 3
) . also received messages are not displayed in serial, but the sketch is publishing OK.
is it just too much for esp or what? I am trying to make a wifi mqtt controlled vehicle and it surprises me it cant handle it?
it's also my first time working with Mega 2560 + wifi, so it might be my fault...
but with normal Nodemcu V3 mqtt work flawless...
#include "WiFiEsp.h"
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif
#include <PubSubClient.h>
char* wifi_ssid = "**";
char* wifi_password = "**";
char* mqtt_server = "**";
int mqtt_port = 1883;
char* mqtt_clientID = "ESP8266_1503489186_1";
char* mqtt_username = "**";
char* mqtt_password = "**";
char* mqtt_publish_topic = "test";
char* mqtt_subscribe_topic = "kosilnica/pogon";
WiFiEspClient espClient;
PubSubClient client(espClient);
int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
reconnect();
}
void setup_wifi() {
// initialize serial for ESP module
Serial3.begin(115200);
// initialize ESP module
WiFi.init(&Serial3);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_clientID,mqtt_username,mqtt_password)) {
Serial.println("connected");
client.subscribe(mqtt_subscribe_topic);
Serial.println("Subscribed to test");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
//client.publish("test", "test");
//Serial.println("Publishing test");
}
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0'; // Null terminator used to terminate the char array
String message = (char*)payload;
String strTopic = String((char*)topic);
Serial.println(message);
}
