Problem with greengrass discovery on esp32 wrover kit v3

salkaleidoscope wrote on October 11, 2019:

Hi, I have been trying to build a project off of the greengrass discovery demo on a esp32 wrover devkit v3. In my project I plan to use a ring buffer and some peripherals including GPIOs, UART, and SPI. The demo works fine right out of the box, but when I started adding my own setup code for either UART, SPI, or creating a ring buffer, the demo fails. I have tried increasing the stack size of the greengrass demo to something much larger like 80,000 bytes and that still didn’t help. Does anyone know what kind of complication initializing a ringbuffer or UART, SPI, and their respective configuration structs could create? Whats also interesting is even if my code to setup a ringbuffer is AFTER the greengrass discovery demo function it still causes the greengrass discovery to fail. Here are the logs when the failure happens and also the code I added to the function vStartGreenGrassDiscoveryTask():

3 828 [IP-task] vDHCPProcess: offer a000058ip
I (8728) event: sta ip:, mask:, gw:
4 838 [IP-task] vDHCPProcess: offer a000058ip
5 839 [iot_thread] [INFO ][DEMO][8390] Successfully initialized the demo. Network type for the demo: 1
6 839 [iot_thread] Attempting automated selection of Greengrass device
7 1038 [iot_thread] ERROR: Handshake failed with error code -16
8 1038 [iot_thread] About to close socket.
9 1039 [iot_thread] Socket closed.
10 1039 [iot_thread] Stack high watermark for discovery helper task: 5488.
11 1039 [iot_thread] JSON request could not connect to end point
12 1039 [iot_thread] Auto-connect: Failed to retrieve Greengrass address and certificate.
13 1039 [iot_thread] ----Demo finished----

RingbufHandle_t ring_buffer;

int vStartGreenGrassDiscoveryTask(bool awsIotMqttMode,
                                  const char *pIdentifier,
                                  void *pNetworkServerInfo,
                                  void *pNetworkCredentialInfo,
                                  const IotNetworkInterface_t *pNetworkInterface)
    /* Unused parameters */


    ring_buffer = xRingbufferCreate(RING_BUFFER_SIZE, RINGBUF_TYPE_ALLOWSPLIT);

    return 0;

SarenaAtAws wrote on October 14, 2019:

Yes you are right because xHostAddressData references directly strings stored in pcJSONFile. Thanks for catching that.

Is there anything you need? Is it sufficient enough that you can lower some memory usage?

salkaleidoscope wrote on October 14, 2019:

Thank you very much for the insight! I had some large statically allocated global variables that I had brought in from other code. Once I made them a lot smaller my code to initialize peripherals and ring buffer worked.

Although in the code you posted, I believe that the vPortFree() needs to be after the call to prvSendMessageToGGC() because I think xHostAddressData is using memory that gets freed otherwise.I was getting errors like this:

13 1448 [iot_thread] [ERROR][NET][14480] Failed to resolve ��EXX٦�?Į�?.
14 1448 [iot_thread] [ERROR][MQTT][14480] Failed to establish new MQTT connection, error NETWORK ERROR. 15 1448 [iot_thread] ERROR: Could not connect to the Broker.
16 1448 [iot_thread] failed to connect to MQTT Agent
17 1448 [iot_thread] Heap low watermark: 9140. Stack high watermark: 5084.

salkaleidoscope wrote on October 15, 2019:

I have a follow up question, do you need to use the demo routine to use greengrass discovery functions? For example, now the project works if I add my setup code in the vStartGreengrassDiscoveryTask() function but I am trying to just use an initialization function for all my setup and tasks from app_main() and then connect to the greengrass core. In the initialization function, I first initialize the wifi in station mode with my network credentials and then start greengrass discovery but eventually prvMQTTConnect() fails with the logs below. I already traced the NETWORK_ERROR back to pdFREERTOS_ERRNO_ETIMEDOUT in FreeRTOS_Connect().

5 1392 [main] About to close socket.
6 1397 [main] Socket closed.
7 1397 [main] Stack high watermark for discovery helper task: 5324.
8 1397 [main] Greengrass device discovered.
9 1397 [main] Establishing MQTT communication to Greengrass...
10 1398 [main] Connecting to Host Adrress:, Port: 8883
I (14319) prvSendMessageToGGC: Free Heap Size: 92284

11 1899 [main] [ERROR][NET][18990] Failed to establish new connection.
12 1899 [main] [ERROR][MQTT][18990] Failed to establish new MQTT connection, error NETWORK ERROR.
13 1899 [main] ERROR: Could not connect to the Broker.
14 1899 [main] failed to connect to MQTT Agent
15 1900 [main] Heap low watermark: 30284. Stack high watermark: 5324.
16 1901 [main] ----Demo finished----

So I’m wondering if there is anything in the _initialize() function in iot_demo_freertos.c or the rest of the AWS demo framework that I need to do for the greengrass discovery to succeed. I even changed the task stack size for the main_task to democonfigGREENGRASS_DISCOVERY_TASK_STACK_SIZE but that didn’t help.

SarenaAtAws wrote on October 21, 2019:


Could you please turn on the FreeRTOS+TCP stack logging in vendors\espressif\boards\esp32\aws_demos\config_files\FreeRTOSIPConfig.h by setting ipconfigHAS_DEBUG_PRINTF to 1.:

#define ipconfigHAS_DEBUG_PRINTF    1

Then run again to see any issues in the the TCP/IP stack that may cause a timeout.

salkaleidoscope wrote on October 25, 2019:

So it looks like the combination of two things fixed this issue, all I had was this a wifi initialization function before that connected to the AP in station mode with my credentials. But then I added IoTSdk_Init() before that. At that point I noticed that if I debug and pause the ESP32 when it is stuck in prvMQTTConnect() that it was stuck in function vApplicationStackOverflowHook. So then I increased the task stack size of the app_main() to the same stack size as democonfigGREENGRASS_DISCOVERY_TASK_STACK_SIZE and then the ggd demo was able to complete.