I have a Mega + ESP8266 (Huzzah flashed with AT command set) set up and communicating with a Mosquitto MQTT broker running on a server within my network. My wifi communications seem to work just fine and I can publish to MQTT to the network server (across wifi) without issue.
However, I am not receiving anything on the Mega. The topic I am subscribed to works from a broker perspective (I've got a service monitoring the MQTT traffic and have verified messages). I'm using the following code (mostly snipets found in examples). I assume if I see anything in the incoming buffer I would print the 'Payload' message at the end of the callback function but I never get to this line. I've seen some similar topics on the forum but not one that has a solution.
#include <WiFiEspClient.h>
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>
#include <PubSubClient.h>
#define WIFI_AP "mywifi"
#define WIFI_PASSWORD "mypassword"
#define TOKEN "DEMO_TOKEN"
char MQTT_SERVER[] = "10.0.0.28"; // MQTT server inside network
// Initialize the Ethernet client object
WiFiEspClient espClient;
PubSubClient client(espClient);
HardwareSerial & soft = Serial1; // Use hardware serial for MEGA
int status = WL_IDLE_STATUS;
unsigned long lastSend;
char message_buff[100];
float send_data = 68.0;
void setup() {
Serial.begin(9600);
InitWiFi();
client.setServer( MQTT_SERVER, 1883 );
client.setCallback(callback);
lastSend = 0;
}
void loop() {
status = WiFi.status();
// attempt to connect to network if not connected
if ( status != WL_CONNECTED) {
Serial.println("In !WL_CONNECTED process");
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(WIFI_AP);
// Connect to WPA/WPA2 network
status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
delay(500);
}
Serial.println("Connected to AP");
}
// attempt to connect to MQTT SERVER if not connected
if ( !client.connected() ) {
Serial.println((millis()-lastSend)/1000); // print time since last reconnect
reconnect();
lastSend = millis();
}
if ( millis() - lastSend > 5000 ) { // send every xx seconds
sendData();
lastSend = millis();
}
// maintain connection to MQTT SERVER and process incoming messages
client.loop();
delay(100);
}
void sendData()
{
send_data += 0.1;
String payload = String(send_data);
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
client.publish( "bedroom/temperature", attributes );
}
// handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
int i = 0;
//Serial.println("Message arrived: topic: " + String(topic));
//Serial.println("Length: " + String(length,DEC));
// create character buffer with ending null terminator (string)
for(i=0; i<length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
Serial.println("Payload: " + msgString);
}
void InitWiFi()
{
// initialize serial for ESP module
soft.begin(9600);
// initialize ESP module
WiFi.init(&soft);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(WIFI_AP);
// Connect to WPA/WPA2 network
status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
delay(500);
}
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected to MQTT Server
while (!client.connected()) {
Serial.print("Connecting to MQTT Server ...");
// Attempt to connect (clientId, username, password)
if ( client.connect("Arduino Device", TOKEN, NULL) ) {
Serial.println( "[DONE]" );
//client.subscribe( "bedroom/temperature" );
client.subscribe( "arduinoTest" );
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.print( client.state() );
Serial.println( " : retrying in 5 seconds]" );
// Wait 5 seconds before retrying
delay( 5000 );
}
}
}