Hi,
Trying to use the MQTT library with bluemix on Photon. I am able to send ( publish) the mqtt message to bluemix. And I can see the message being parsed on bluemix, and output from bluemix.
I do not receive the message on Photon, as the code never executes the callback.
Should I have a mosquito server running on port 1883? I have an AT&T modem, on which I have opened the port 1883 ( or so I think!). How can I verify that the messages are indeed reaching the modem but not my photon which is wireless-ly attached to the AT&T modem.
Any help appreciated, thanks in advance, my code below
// This #include statement was automatically added by the Particle IDE.
#include "SparkJson/SparkJson.h"
// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"
#define LEDON HIGH
#define LEDOFF LOW
int MYLED = D7;
char server[] = "quickstart.messaging.internetofthings.ibmcloud.com";
char topic[] = "iot-2/evt/status/fmt/json";
char clientId[] = "d:quickstart:Photon:00FFBBCCDE10";
char authMethod[] = "use-token-auth"
char token[] ="my_unique_token"
char Ctrl = '0';
MQTT mqttClient(server, 1883, callback);
void BlinkMYLED() { digitalWrite(MYLED, LEDON); delay(250); digitalWrite(MYLED,
LEDOFF); delay(250); }
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("Called back.. do some processing!!");
BlinkMYLED();
Ctrl=0;
}
void setup() {
pinMode(MYLED, OUTPUT);
digitalWrite(MYLED, LEDOFF);
Serial1.begin(9600);
RGB.control(true);
RGB.color(255,0,0);
delay(2000);
RGB.color(0,255,0);
delay(2000);
RGB.color(0,0,255);
delay(2000);
RGB.control(false);
Serial.println(" "); // start on a new line
mqttClient.connect(server);
Serial.println("Done Setup & mqttsetup");
}
void sendToIOTF(String payload) {
while (!mqttClient.isConnected()) { // If the Mqtt Client is not connected, so try to connect
Serial.print("MQTT connection...");
if (mqttClient.connect(clientId, authMethod, token)) { // Try to connect the Mqtt Client
Serial.println("connected");
} else {
Serial.println("failed, MQTT connection");
Serial.println(" try again in 1 second");
delay(1000);
}
}
mqttClient.publish(topic, (char*) payload.c_str()); // Send a message
mqttClient.subscribe(topic);
}
void loop() {
Ctrl= Serial.read();
if ( Ctrl == 'P') {
sendToIOTF("Yo! Am I there yet?");
delay(100);
}
}