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

**URL:** <https://forums.freertos.org/t/connecting-mqtt-to-greengrass-with-beta-4-c-sdk-apis-greengrass-demo/8950>\
**Category:** AWS\
**Created:** [September 23, 2019, 8:22am UTC](https://forums.freertos.org/t/connecting-mqtt-to-greengrass-with-beta-4-c-sdk-apis-greengrass-demo/8950 "2019-09-23T08:22:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 23, 2019, 8:22am UTC](https://forums.freertos.org/t/connecting-mqtt-to-greengrass-with-beta-4-c-sdk-apis-greengrass-demo/8950/1 "2019-09-23T08:22:55Z")

</div>

**[TJirsch](https://forums.aws.amazon.com/profile.jspa?userID=431883)** wrote on September 23, 2019:

Hi,

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 (192.168.86.36:8883)
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 ?

```auto

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;
  IotNetworkCredentials_t iotCreds = AWS_IOT_NETWORK_CREDENTIALS_AFR_INITIALIZER;

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

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

  if(uConnectToGreenGrass)
  {
    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 );
  }
  else
  {
    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
%s", __FUNCTION__ ,iotCreds.pClientCert);

  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, 192.168.86.36:8883 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:

```auto

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 192.168.86.36:8883 try#1 NETWORK ERROR

```

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

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

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 24, 2019, 4:38am UTC](https://forums.freertos.org/t/connecting-mqtt-to-greengrass-with-beta-4-c-sdk-apis-greengrass-demo/8950/2 "2019-09-24T04:38:41Z")

</div>

**[Gaurav-Aggarwal-AWS](https://forums.aws.amazon.com/profile.jspa?userID=447386)** wrote on September 24, 2019:

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

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

```auto

nc -zv 192.168.86.36 8883

```

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 24, 2019, 11:58am UTC](https://forums.freertos.org/t/connecting-mqtt-to-greengrass-with-beta-4-c-sdk-apis-greengrass-demo/8950/3 "2019-09-24T11:58:49Z")

</div>

**[TJirsch](https://forums.aws.amazon.com/profile.jspa?userID=431883)** 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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 24, 2019, 6:13pm UTC](https://forums.freertos.org/t/connecting-mqtt-to-greengrass-with-beta-4-c-sdk-apis-greengrass-demo/8950/4 "2019-09-24T18:13:55Z")

</div>

**[Gaurav-Aggarwal-AWS](https://forums.aws.amazon.com/profile.jspa?userID=447386)** 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: [https://forums.aws.amazon.com/thread.jspa?threadID=310051&tstart=0](https://forums.aws.amazon.com/thread.jspa?threadID=310051&tstart=0)

Thanks.
