hirotakaster / MQTT

MQTT for Photon, Spark Core
Other
217 stars 118 forks source link

Receiving message on callback does not work on Redbear Duo #44

Open paragshar opened 7 years ago

paragshar commented 7 years ago

I am using redbear duo as my device to receive messages from the IBM IOT platform. I am able to send messages to the platform, but not able to receive messages. The callback function is never invoked.

The MQTT broker seems fine without any issues. Is there any problem with the MQTT client in receiving the messages that it has earlier subscribed for?

My code is here

#include <MQTT.h>

char *IOT_CLIENT = "d:<orgid>:<type>:<device_id>";  
char *IOT_HOST = "<orgid>.messaging.internetofthings.ibmcloud.com";  

char *IOT_PUBLISH = "iot-2/evt/count/fmt/json"; 
char *IOT_SUBSCRIBE = "iot-2/cmd/channel/fmt/json";

char *IOT_USERNAME = "xxx";
char *IOT_PASSWORD = "yyyy";

#if defined(ARDUINO) 
SYSTEM_MODE(SEMI_AUTOMATIC); 
#endif

// your network name also called SSID
//char ssid[] = "TP-LINK_1CE8";
char ssid[] = "D-Link_DIR-524";
// your network password (key)
char password[] = "xxxx";

void printCurrentNet();
void printWifiData();

int count = 0;

void callback(char* topic, byte* payload, unsigned int length);

MQTT client(IOT_HOST, 1883, callback);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  Serial.println(PLATFORM_ID);
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  Serial.println(ssid);
  WiFi.on();
  WiFi.setCredentials(ssid,password);
  WiFi.connect();

  while ( WiFi.connecting()) {
    // 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");

  IPAddress localIP = WiFi.localIP();
  while (localIP[0] == 0) {
    localIP = WiFi.localIP();
    Serial.println("waiting for an IP address");
    delay(1000);
  }

  Serial.println("\nIP Address obtained");

  printCurrentNet();
  printWifiData();

  client.connect(IOT_CLIENT, IOT_USERNAME, IOT_PASSWORD);

  if( client.isConnected() ) {
    Serial.println( "Connected." );
    client.subscribe(IOT_SUBSCRIBE);
  }
}

void loop() {
  if( !client.isConnected() ) {
   Serial.println("Reconnecting...");
   client.connect(IOT_CLIENT, IOT_USERNAME, IOT_PASSWORD);
   client.subscribe(IOT_SUBSCRIBE);
 }
// 
  count = count + 1;
  client.publish(IOT_PUBLISH, getJson(count));            
  client.loop();

  Serial.print( "Count: " );
  Serial.println( count );
  delay( 10000 );
}

void printWifiData() {
  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println(ip);

  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  Serial.print(mac[5], HEX);
  Serial.print(":");
  Serial.print(mac[4], HEX);
  Serial.print(":");
  Serial.print(mac[3], HEX);
  Serial.print(":");
  Serial.print(mac[2], HEX);
  Serial.print(":");
  Serial.print(mac[1], HEX);
  Serial.print(":");
  Serial.println(mac[0], HEX);
}

void printCurrentNet() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

  Serial.print("Encryption Type:");
  Serial.println(WEP, HEX);
  Serial.println();
}

String getJson(int no){
  String message = "{";
  message += "\"no\":";
  message += no;
  message += "}";
  return message;
}

void callback( char* topic, byte* payload, unsigned int length ) {  
  Serial.println("Message Received by Device");
  char p[length + 1];

  memcpy(p, payload, length);
  p[length] = NULL;

  String message(p);
  Serial.println(message);
}
hirotakaster commented 7 years ago

Hi, I think this program have no problem, do you try to publish the topic message with mosquitto_pub command? I use RedBear duo with my mosquitto server now, pub/sub works very well.