Page 1 of 2

BUG/ Workaround: ESP Mesh and ESP Now interoperability issues

Posted: Fri Feb 15, 2019 8:25 pm
by leenowell
Hi All,

I have an ESP 32 which receives an ESP Now message from an ESP 8266 and sends the data up the mesh to the root. If the node that happens to receive the message is not root, it fails when calling esp_mesh_send. I have done a lot of debugging to try and find out the issue and have some weird results.

1. The error I receive seems to depend on the tos parameter I set as follows

MESH_TOS_P2P; I get ESP_ERR_MESH_TIMEOUT
MESH_TOS_DEF; I get ESP_ERR_MESH_ARGUMENT
MESH_TOS_E2E; I get ESP_ERR_NOT_SUPPORT

2. Sending the message using the same code outside the espnow receive call back seems to work fine.

The mesh_data_t looks like this

Code: Select all

	mesh_data_t dataToRoot = {0};
	dataToRoot.data 	= (void*) &homeiotSendMessage;
	dataToRoot.size 	= sizeof(homeiotSendMessage);
	dataToRoot.proto 	= MESH_PROTO_BIN;
//	dataToRoot.tos		= MESH_TOS_P2P;	// ESP_ERR_MESH_TIMEOUT
	dataToRoot.tos 		= MESH_TOS_DEF; // ESP_ERR_MESH_ARGUMENT
//	dataToRoot.tos 		= MESH_TOS_E2E; // ESP_ERR_NOT_SUPPORT
and the call to send the message is as follows where nFlag = NULL when sending to ROOT.

Code: Select all

	err = esp_mesh_send(dest_addr, data, nFlag, NULL, 0);
Anyone have any ideas? Is ESP Now and ESP Mesh compatible?

thanks

Lee.

Re: ESP Mesh and ESP Now interoperability issues

Posted: Mon Feb 18, 2019 7:55 am
by ESP_yudong
Hi Lee,
ESP Now and ESP Mesh should be able to work together.
Have you tried with data.tos=MESH_TOS_P2P and flag=MESH_DATA_P2P ?
And can you provide all parameters and return values if esp_mesh_send failed?

Re: ESP Mesh and ESP Now interoperability issues

Posted: Mon Feb 18, 2019 8:22 am
by leenowell
Hi,

Thanks for getting back to me. The actual send line is

Code: Select all

err = esp_mesh_send(dest_addr, data, MESH_DATA_P2P, NULL, 0);
In the code above, I have tried all the possible tos values and the returned error code is shown as a comment beside each as they differ depending upon which tos I set.

Hope this helps.
Thanks

Lee.

Re: ESP Mesh and ESP Now interoperability issues

Posted: Mon Feb 18, 2019 7:39 pm
by leenowell
Hi,

To try and simplify the scenario, I have simplified the code in the esp now receive call back to the following....

Code: Select all

static void cbOnESPNowReceive(const uint8_t *mac_addr, const uint8_t *data, int len)
{
	esp_err_t err = ESP_OK;
	char sMessage[100] = "";

	ESP_LOGI(HOMEIOT_TAG, "OnESPNowRec: Received data from ["MACSTR"] Message [%s]", MAC2STR(mac_addr), (char *) data);

	sprintf(sMessage, "Test Message");
	mesh_data_t dataMesh = {0};
	dataMesh.data = (void*) sMessage;
	dataMesh.size = strlen((char*) dataMesh.data)+1;
	dataMesh.proto = MESH_PROTO_BIN;
	dataMesh.tos = MESH_TOS_P2P;
	err = esp_mesh_send(NULL, &dataMesh, 0, NULL, 0);

	if (err !=ESP_OK)
		ESP_LOGE(HOMEIOT_TAG, "OnESPNowRec: Failed to send ESPNow data Error[%d = %s] - ESPNow node ["MACSTR"] Message [%s]", err ,esp_err_to_name(err),  MAC2STR(mac_addr), (char *) data);
}
The ESP now message is broadcast and both nodes pick it up.. For the root node, the send returns ESP_Ok. For the leaf node, I get ESP_ERR_MESH_TIMEOUT.

Changing the send line to

Code: Select all

		err = esp_mesh_send(NULL, &dataMesh, MESH_DATA_P2P, NULL, 0);

I also get a return of ESP_ERR_MESH_TIMEOUT.

After a period of time, the error then changes to ESP_ERR_MESH_NO_MEMORY.

Hope this helps

thanks

Lee.

Re: ESP Mesh and ESP Now interoperability issues

Posted: Thu Feb 21, 2019 8:20 am
by leenowell
Hi

Has anyone had a chance to look at this yet? Given the simplified code below that reproduces the problem wonder if I should raise as a bug on git?

Thanks

Lee.

Re: ESP Mesh and ESP Now interoperability issues

Posted: Thu Feb 21, 2019 1:10 pm
by ESP_yudong
Hi Lee,
I have some guesses.

Maybe you should move esp_mesh_send from esp_now callback to another task, since esp_now callback belong to wifi task so it can't handle events from other task such as mesh task. That's why esp_mesh_send never succeed.

Re: ESP Mesh and ESP Now interoperability issues

Posted: Thu Feb 21, 2019 1:33 pm
by leenowell
Hi

Thanks for getting back to me. Do you have an example of how to do this? I assume I will need to somehow create an esp-mesh-send task and have a shared queue between it and the espnow thread . Then the espnow call back adds the message to the queue and the mesh send takes it off?

Sounds a bit complicated . Also, not sure this explains why we get different behaviour if the node happens to be root or not?

Thanks

Lee.

Re: BUG/ Workaround: ESP Mesh and ESP Now interoperability issues

Posted: Thu Feb 21, 2019 9:05 pm
by leenowell
Hi @ESP_yudong

I have tried putting the esp_mesh_send into another task and sending the data between the espnow_receive call back to the new task via a queue.

The new task looks like this

Code: Select all

void DoMeshSend()
{
	esp_err_t err = ESP_OK;

	homeiot_message_t message;

	while(1)
	{
		if (xQueueReceive( meshQueue, &message, 0 ))
		{
			mesh_data_t dataMesh = {0};
			dataMesh.data = (void*) &message;
			dataMesh.size = sizeof(message);
			dataMesh.proto = MESH_PROTO_BIN;
			dataMesh.tos = MESH_TOS_P2P;

			ESP_LOGI(HOMEIOT_TAG,"DoMeshSend: Read [%s] from the queue", (char*) message.data.sMessage);
			err = esp_mesh_send(NULL, &dataMesh, MESH_DATA_P2P, NULL, 0);
		}
		vTaskDelay(1 * 1000 / portTICK_RATE_MS);

	}

}

and the espnow_receive callback looks like this

Code: Select all

	homeiot_message_t myMessage = {0};
	myMessage.event = NODE_DATA;
	strcpy(myMessage.data.sMessage, (char*) data);

	if (xQueueSend( meshQueue, &myMessage, 0 ) != pdPASS )
		ESP_LOGE(HOMEIOT_TAG, "OnESPNowRec: Failed to send ESPNow data Error[%d = %s] - ESPNow node ["MACSTR"] Message [%s]", err ,esp_err_to_name(err),  MAC2STR(mac_addr), (char *) myMessage.data.sMessage);
	else
		ESP_LOGI(HOMEIOT_TAG,"OnESPNowRec: Added [%s] to the queue", (char*) myMessage.data.sMessage);
Whilst this seems to workaround the problem, I don't think this workaround will work in my scenario as the send is now asynchronous via the queue so the calling function doesn't know whether the send has happened or not.

Should I formally raise this as a bug on git? As mentioned on my other thread about smart config, the 2 issues may well be related?

thanks

Lee.

Re: BUG/ Workaround: ESP Mesh and ESP Now interoperability issues

Posted: Fri Feb 22, 2019 2:26 am
by ESP_yudong
Hi Lee,
Yes, queue and esp_mesh_send task are also used in MDF.
It only uses memcpy if esp_mesh_send send packet to itself, that's why root succeed.

These two are different issues, yuo can raise them on github as well.
This issue is esp_mesh_send can't be called anywhere, and the issue about smart config is most likely some smart config behavier break the mesh state machine.

By the way, Did you disable mesh self organized before you use smart config?

Re: BUG/ Workaround: ESP Mesh and ESP Now interoperability issues

Posted: Fri Feb 22, 2019 8:08 am
by leenowell
Ah that makes sense thanks.

Yes I disabled self organising before kicking off the smart config task. I can double check the code when I get back but believe it was with parameters false, false. Will raise on git too as not at PC now

I will be working on this again tomorrow so if you want me to run any tests possible let me know. Also, to be clear I am using IDF rather than MDF if that matters.

My setup is the same as I specified in this bug report if that helps

https://github.com/espressif/esp-idf/issues/3047