What a coincidence (or isn't it?), I was just finishing up and testing the improved MQTT examples. Quick look at your Sketch shows that you are missing the client.loop() in the loop function. This call will service any outstanding messages that came in and call your callback. Thank you for providing detailed information. This helps a lot with not having to guess or ask about details!
Below is a example Sketch that I just finished up:
/*
Basic MQTT example
- connects to an MQTT server
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic"
*/
#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>
// your network name also called SSID
char ssid[] = "energia1";
// your network password
char password[] = "launch pad";
// MQTTServer to use
char server[] = "iot.eclipse.org";
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Received message for topic ");
Serial.print(topic);
Serial.print("with length ");
Serial.println(length);
Serial.println("Message:");
Serial.write(payload, length);
Serial.println();
}
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);
void setup()
{
Serial.begin(115200);
// Start Ethernet with the build in MAC Address
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to Network named: ");
// print the network name (SSID);
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED) {
// print dots while we wait to connect
Serial.print(".");
delay(300);
}
Serial.println("\nYou're connected to the network");
Serial.println("Waiting for an ip address");
while (WiFi.localIP() == INADDR_NONE) {
// print dots while we wait for an ip addresss
Serial.print(".");
delay(300);
}
Serial.println("\nIP Address obtained");
// We are connected and have an IP address.
// Print the WiFi status.
printWifiStatus();
}
void loop()
{
// Reconnect if the connection was lost
if (!client.connected()) {
Serial.println("Disconnected. Reconnecting....");
if(!client.connect("energiaClient")) {
Serial.println("Connection failed");
} else {
Serial.println("Connection success");
if(client.subscribe("inTopic")) {
Serial.println("Subscription successfull");
}
}
}
if(client.publish("outTopic","hello world")) {
Serial.println("Publish success");
} else {
Serial.println("Publish failed");
}
// Check if any message were received
// on the topic we subsrcived to
client.loop();
delay(1000);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}