Other Parts Discussed in Thread: UNIFLASH
Hi all,
I'm trying to connect to the Google iot Core with MQTT but so far it's not working.
I have verified that the needed credentials such as the clientid and password (in this case the JWT token needed for google's iot core) are correct with a local mqtt client. I was able to connect to my registered device and able to publish messages.
However when I try to connect it with the CC3220SF I get the following error message on the serial terminal:
I checked the certificate and verified that it actually is a GlobalSign certificate so I'm really confused here. This is the most common error I get while occasionally also getting -468, -458, -457 or -2005 depending on the root certificate that I upload. All the root certificates that I have tried I have downloaded directly from google, either from their certificates repository (pki.goog/repository/) or from their MQTT-bridge example page cloud.google.com/iot/docs/how-tos/mqtt-bridge.
I upload them as such via Uniflash :
I followed the empty MQTT example from the SDK and was able to connect to the eclipse broker with a secured connection. I expanded the code and added the needed credentials for the google iot core but I must be doing something wrong as I'm not able to connect to their MQTT broker.
Here is part of my code :
#define ClientId "projects/myProjectId/locations/europe-west1/registries/myRegistry/devices/qdevice"
#define MQTT_CLIENT_PASSWORD "myJWTtoken"
#define MQTT_CLIENT_USERNAME "unused"
#define MQTT_CLIENT_KEEPALIVE 60
#define MQTT_CLIENT_CLEAN_CONNECT true
#define MQTT_CLIENT_MQTT_V3_1 false
#define MQTT_CLIENT_BLOCKING_SEND true
char *MQTTClient_secureFiles[1] = {"gsr2.pem"};
#define MQTT_CONNECTION_FLAGS MQTTCLIENT_NETCONN_URL | MQTTCLIENT_NETCONN_SEC
#define MQTT_CONNECTION_ADDRESS "mqtt.googleapis.com"
#define MQTT_CONNECTION_PORT_NUMBER 8883
#define SL_TASKSTACKSIZE 2048
#define SPAWN_TASK_PRIORITY 9
MQTT_IF_InitParams_t mqttInitParams =
{
MQTT_MODULE_TASK_STACK_SIZE, // stack size for mqtt module
MQTT_MODULE_TASK_PRIORITY // thread priority for MQTT
};
MQTT_IF_ClientParams_t mqttClientParams =
{
ClientId, // client ID
MQTT_CLIENT_USERNAME, // user name
MQTT_CLIENT_PASSWORD, // password
MQTT_CLIENT_KEEPALIVE, // keep-alive time
MQTT_CLIENT_CLEAN_CONNECT, // clean connect flag
MQTT_CLIENT_MQTT_V3_1, // true = 3.1, false = 3.1.1
MQTT_CLIENT_BLOCKING_SEND, // blocking send flag
};
MQTTClient_ConnParams mqttConnParams =
{
MQTT_CONNECTION_FLAGS, // connection flags
MQTT_CONNECTION_ADDRESS, // server address
MQTT_CONNECTION_PORT_NUMBER, // port number of MQTT server
SLNETSOCK_SEC_METHOD_TLSV1_2, // method for secure socket
SLNETSOCK_SEC_CIPHER_FULL_LIST, // cipher for secure socket
1, // number of files for secure connection
MQTTClient_secureFiles // secure files
};
/* When MQTT_if_handler is called use is expected to provide event callback.
* This callback will be used by the MQTT client module to notify the main application when certain MQTT events have occured (e.g. connect, disconnect, etc) */
void MQTT_EventCallback(int32_t event)
{
switch(event){
case MQTT_EVENT_CONNACK:
{
LOG_INFO("MQTT_EVENT_CONNACK\r\n");
break;
}
case MQTT_EVENT_CLIENT_DISCONNECT:
{
LOG_INFO("MQTT_EVENT_CLIENT_DISCONNECT\r\n");
break;
}
case MQTT_EVENT_SERVER_DISCONNECT:
{
LOG_INFO("MQTT_EVENT_SERVER_DISCONNECT\r\n");
break;
}
case MQTT_EVENT_DESTROY:
{
LOG_INFO("MQTT_EVENT_DESTROY\r\n");
break;
}
default:
{
LOG_INFO("Unknown MQTT event\r\n");
break;
}
}
}
/* WiFi initialiser */
int WifiInit(){
int32_t ret;
SlWlanSecParams_t security_params;
pthread_t spawn_thread = (pthread_t) NULL;
pthread_attr_t pattrs_spawn;
struct sched_param pri_param;
pthread_attr_init(&pattrs_spawn);
pri_param.sched_priority = SPAWN_TASK_PRIORITY;
ret = pthread_attr_setschedparam(&pattrs_spawn, &pri_param);
ret |= pthread_attr_setstacksize(&pattrs_spawn, SL_TASKSTACKSIZE);
ret |= pthread_attr_setdetachstate(&pattrs_spawn, PTHREAD_CREATE_DETACHED);
ret = pthread_create(&spawn_thread, &pattrs_spawn, sl_Task, NULL);
if(ret != 0){
LOG_ERROR("could not create simplelink task\n\r");
while(1);
}
Network_IF_ResetMCUStateMachine();
Network_IF_DeInitDriver();
ret = Network_IF_InitDriver(ROLE_STA);
if(ret < 0){
LOG_ERROR("Failed to start SimpleLink Device\n\r");
while(1);
}
// SetClientIdNamefromMacAddress();
security_params.Key = (signed char*)SECURITY_KEY;
security_params.KeyLen = strlen(SECURITY_KEY);
security_params.Type = SECURITY_TYPE;
ret = Network_IF_ConnectAP(SSID_NAME, security_params);
if(ret < 0){
LOG_ERROR("Connection to an AP failed\n\r");
}
else{
ret = sl_WlanProfileAdd((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &security_params, NULL, 7, 0);
if(ret < 0){
LOG_ERROR("failed to add profile %s\r\n", SSID_NAME);
}
else{
LOG_INFO("profile added %s\r\n", SSID_NAME);
}
}
return ret;
}
void MQTTDemo(char* topic, char* payload, uint8_t qos)
{
LOG_INFO("TOPIC: %s PAYLOAD: %s QOS: %d\r\n", topic, payload, qos);
}
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
/* 1 second delay */
uint32_t time = 1;
/* MQTT variables */
int32_t ret;
UART_Handle uartHandle;
MQTTClient_Handle mqttClientHandle;
GPIO_init();
SPI_init();
/* Open UART instance to enable application printing */
uartHandle = InitTerm();
UART_control(uartHandle, UART_CMD_RXDISABLE, NULL);
/* Initialize the IP network services layer for the MQTT client module */
ret = ti_net_SlNet_initConfig();
if(0 != ret)
{
LOG_ERROR("Failed to initialize SlNetSock\n\r");
}
/* Call WifiInit() to start the network processor and connect to an Access Point */
ret = WifiInit();
if(ret < 0){
while(1);
}
setTime();
/* Initialize MQTT client module */
ret = MQTT_IF_Init(mqttInitParams);
if(ret < 0){
while(1);
}
ret = MQTT_IF_Subscribe(mqttClientHandle, "/devices/mydevice/events", MQTT_QOS_1, MQTTDemo); //Google doesnt support QOS 2
if(ret < 0){
while(1);
}
else{
LOG_INFO("Subscribed to all topics successfully\r\n");
}
/* Connect MQTT broker and register event call back */
mqttClientHandle = MQTT_IF_Connect(mqttClientParams, mqttConnParams, MQTT_EventCallback);
if(mqttClientHandle < 0){
while(1);
}
while (1) {
sleep(time);
MQTT_IF_Publish(mqttClientHandle,
"/devices/qdevice/events",
"hello\r\n",
strlen("hello\r\n"),
MQTT_QOS_0);
}
}