Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
paragshar opened this issue Dec 11, 2016 · 0 comments
Open

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

paragshar opened this issue Dec 11, 2016 · 0 comments

Comments

@paragshar
Copy link

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:::<device_id>";
char *IOT_HOST = ".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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant