unable to authenticate bluemix connection [message #1770494] |
Sat, 12 August 2017 13:24  |
Eclipse User |
|
|
|
Hello all,
I am using the eclipse paho mqtt c library to connect to bluemix. Have done my part on the bluemix cloud and created an IoT device and registered the same, have generated API keys.
I am writing my raspberry pi cpu temperature on bluemix cloud, But when i try to connect to the device using the below code, my code exits with an rc state of 5.
And i see log messages on the cloud saying "Closed connection, unable to authenticate" .
I have tried a lot, but rc is always 5.
Appreciate your assistance.
#include <iostream>
#include <sstream>
#include <fstream>
#include <string.h>
#include "MQTTClient.h"
#define CPU_TEMP "/sys/class/thermal/thermal_zone0/temp"
using namespace std;
#define ADDRESS "tcp://<org_ID>.messaging.internetofthings.ibmcloud.com:1883" //have used organization ID
#define CLIENTID "d:<org_ID>:<Device_type>:<space_ID>" //have used organization ID, Device type field and my space ID.
#define AUTHMETHOD "use-token-auth"
#define AUTHTOKEN "<authentication_token>"
#define TOPIC "iot-2/evt/status/fmt/json"
#define QOS 1
#define TIMEOUT 10000L
float getCPUTemperature(){
int CPUTemp;
fstream fs;
fs.open(CPU_TEMP, fstream::in);
fs >> CPUTemp;
fs.close();
return (((float)CPUTemp)/1000);
}
int main(int argc, char* argv[]) {
MQTTClient client;
MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
opts.keepAliveInterval = 20;
opts.cleansession = 1;
opts.username = AUTHMETHOD;
opts.password = AUTHTOKEN;
int rc;
if ((rc = MQTTClient_connect(client, &opts)) != MQTTCLIENT_SUCCESS)
{
cout<<"Failed to connect "<<rc<<endl; // Here my code exits with rc=5
return -1;
}
stringstream message;
message <<"{\"d\":{\"Temp\":"<<getCPUTemperature()<<"}}";
pubmsg.payload = (char*) message.str().c_str();
pubmsg.payloadlen = message.str().length();
pubmsg.qos = QOS;
pubmsg.retained = 0;
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
cout<< "Waiting for " << (int) (TIMEOUT/1000) << "seconds for pub of"<<message.str()<<"\non topic"<<TOPIC<<"for ClientID:"<<CLIENTID<<endl;
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
cout<<"Message with token"<< (int)token<<"delivered"<<endl;
MQTTClient_disconnect(client,10000);
MQTTClient_destroy(&client);
return rc;
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04669 seconds