Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Unable to authenticate bluemix connection(When trying to access bluemix device through c++ code, authentication fails.)
Unable to authenticate bluemix connection [message #1770614] Tue, 15 August 2017 07:40 Go to next message
Mohammed Ubaid is currently offline Mohammed UbaidFriend
Messages: 4
Registered: August 2017
Junior Member
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;
}
Re: Unable to authenticate bluemix connection [message #1770818 is a reply to message #1770614] Thu, 17 August 2017 11:03 Go to previous message
Mohammed Ubaid is currently offline Mohammed UbaidFriend
Messages: 4
Registered: August 2017
Junior Member
It's solved, supposedly i was accessing a TLS connection.
Now it is authenticating.
Previous Topic:MQTT SSL Implementation on Android
Next Topic:MQTT PAHO C Library SSL
Goto Forum:
  


Current Time: Tue Sep 25 23:25:59 GMT 2018

Powered by FUDForum. Page generated in 0.02043 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top