Connecting mqtt to Greengrass with Beta 4 C SDK APIS / Greengrass demo

TJirsch wrote on September 23, 2019:


I am trying to move code connecting to a thing shadow on a greengrass host from the old “AGENT” Apis to the V4 C SDK Ones under the current master branch.

I have a working Greengrass Host (V1.93) and Discovery, Thing and IoT Setup.
I have validated:

  1. The Greengrass Demo code (old style APIs) is working with the GG host and the Things certificates.
  2. My Code connects to the cloud IoT Endpoint
  3. The Discovery finds the correct Greengrass Connection info (
  4. My old code uses the same APIs as the Greengrass demo and also works

When calling the function below with uConnectToGreenGrass = true (Discovery done ok with correct info in pxHostAdrData) I get a Network Error from IotMqtt_Connect. The error occurs somewhere in FreeRTOS_connect in FreeRTOS_Sockets.c.
I think I am setting up everything ok, could you provide some help where to look or provide a greengrass demo that uses the current APIs ?

void vShadowConnect(GGD_HostAddressData_t *pxHostAdrData, uint8_t uConnectToGreenGrass)
  uint8_t            	 nRetries    = 0;

  IotMqttError_t       xReturn     = IOT_MQTT_STATUS_PENDING;
  IotMqttNetworkInfo_t networkInfo = IOT_MQTT_NETWORK_INFO_INITIALIZER;
  IotMqttConnectInfo_t connectInfo = IOT_MQTT_CONNECT_INFO_INITIALIZER;

  IotNetworkServerInfo_t  iotSrvInfo;

  iotCreds.pClientCert       = keyCLIENT_CERTIFICATE_PEM;
  iotCreds.clientCertSize    = sizeof(keyCLIENT_CERTIFICATE_PEM);

  iotCreds.pPrivateKey       = keyCLIENT_PRIVATE_KEY_PEM;
  iotCreds.privateKeySize    = sizeof(keyCLIENT_PRIVATE_KEY_PEM);

    iotSrvInfo.pHostName     = pxHostAdrData->pcHostAddress;
    iotSrvInfo.port          = pxHostAdrData->usPort;

    iotCreds.pRootCa         = pxHostAdrData->pcCertificate;
    iotCreds.rootCaSize      = strlen(pxHostAdrData->pcCertificate);

    ESP_LOGI(TAG, "%s rootca cert
%s",__FUNCTION__,iotCreds.pRootCa    );
    iotSrvInfo.pHostName     = clientcredentialMQTT_BROKER_ENDPOINT;
    iotSrvInfo.port          = clientcredentialMQTT_BROKER_PORT;

    iotCreds.pRootCa         = NULL;
    iotCreds.rootCaSize      = 0;
  ESP_LOGI(TAG, "%s Shadow connecting to %s endpoint %s:%d",__FUNCTION__,
           (uConnectToGreenGrass ? "Greengrass":"IoT Cloud"),
           iotSrvInfo.pHostName, iotSrvInfo.port);

  ESP_LOGI(TAG, "%s client cert

  networkInfo.createNetworkConnection        = true;
  networkInfo.u.setup.pNetworkServerInfo     = &iotSrvInfo;
  networkInfo.u.setup.pNetworkCredentialInfo = &iotCreds;
  networkInfo.pNetworkInterface              = IOT_NETWORK_INTERFACE_AFR;

  connectInfo.awsIotMqttMode                 = true;
  connectInfo.cleanSession                   = true;
  connectInfo.keepAliveSeconds               = 50;
  connectInfo.pClientIdentifier              = clientcredentialIOT_THING_NAME;
  connectInfo.clientIdentifierLength         = ( uint16_t ) strlen(clientcredentialIOT_THING_NAME);

  xReturn = IotMqtt_Connect( &networkInfo,&connectInfo,20000,&xMQTTShadow);  

Edited by: TJirsch on Sep 23, 2019 1:23 AM
Discovery was successfull, is the greengrass host.
What stands out is the eCONNECT_SYN: flags 0012 expected, not 0010 line.
Any hints ?
I turned DEBUG Logs on in iot_config.h and FreeRTOSIPConfig.h this gets me the following:

FreeRTOS_connect: 24324 to c0a85624ip:8883
Socket 24324 -> c0a85624ip:8883 State eCLOSED->eCONNECT_SYN
prvSocketSetMSS: 1420 bytes for c0a85624ip:8883
prvWinScaleFactor: uxRxWinSize 1 MSS 1420 Factor 0
Connect[c0a85624ip:8883]: next timeout 1: 3000 ms
eCONNECT_SYN: flags 0012 expected, not 0010
Socket 24324 -> c0a85624ip:8883 State eCONNECT_SYN->eCLOSE_WAIT
[ERROR][NET][25870] Failed to establish new connection.
FreeRTOS_closesocket[24324 to c0a85624ip:8883]: buffers 60 socks 0
[ERROR][MQTT][25870] Failed to establish new MQTT connection, error NETWORK ERROR.
I (26083) [SHADOW   ]: vShadowConnect espLogger01 shadow MQTT connect to try#1 NETWORK ERROR

Edited by: TJirsch on Sep 23, 2019 3:20 AM

Edited by: TJirsch on Sep 23, 2019 3:25 AM

Gaurav-Aggarwal-AWS wrote on September 24, 2019:

What it means is that Greengrass Discovery is successful and the Greengrass Core is The logs you shared indicate that the TCP connection to is not successful (the server is not responding with a SYNACK).

Can you check that port number 8883 on is reachable using some other machine on the same network? You can use the following command to do that:

nc -zv 8883


TJirsch wrote on September 24, 2019:

I looked at the “old” MQTT_AGENT call, which translates the old parameters to the new ones and in the end it was (again !) my own stupidity:

iotCreds.rootCaSize  = strlen(pxHostAdrData->pcCertificate);
needs to be:
iotCreds.rootCaSize  = pxHostAdrData->ulCertificateSize;

Everything worked fine after that, sorry for that.
As there seems to be some change with QOS it will be interesting to see if OTA now works over the connection and if I can use the same MQTT Connection for both shadow and normal mqtt to AWS Topics.

Gaurav-Aggarwal-AWS wrote on September 24, 2019:

I am glad that it worked for you. You should be able to share connection for MQTT and Shadow - let us know if you face any problem there.

Also, see my response here where I was able to do OTA via Greengrass: