Hello, everybody,
First, I want to thank you, the experts, for supporting us in our struggle with our projects.
In my project a single server collects data through its local resources and distributes it to some peripheral units. The requests for the data are coming from every perihheral unit on its own time. There is no need to register the remote units. This is not a typical implementation of ESP NOW protocol, and I had to construct a dedicated protocol based on tested examples. The included code represents a shrunk version of the original code where the “wifiConnect” method is the original one.
The wifi station is working, and I prove it by getting UTP time. However, “esp_now_send” method returns error 0x3069 (?), and the callback method “OnDataSent” is not beeing triggered. This means that the test message is not transmitted. The monitor output is as designed. I assume that I missed something on my way, but I don’t see what it is. I’d appreciate any clue that you may send.
I’d further appreciate pointing to a source of explanation on the callback mechanism, especially on how it is triggered (interrupt? Polling?) and where the registered methods get their parameters from (“OnDataRecv“ gets 3 parameters, “OnDataSend” gets 2 parameters – none of them is indicated during the registration).
Project details: Seeed ESP32C3, Arduino IDE Version: 2.3.5, Date: 2025-04-02T13:16:49.885Z, CLI Version: 1.2.0, Windows 10 Pro build 19045.5737.
The monitor output is:
Loop #0; status = NO ERROR
Loop #1; status = WL IDLE STATUS
Loop #2; status = WL IDLE STATUS
Final status - WL CONNECTED
Server mac address: ff:ff:ff:ff:ff:ff
Server SOFT AP MAC Address: 64:E8:33:84:1C:11
Station IP Address: 10.0.0.34; Wi-Fi Channel: 2
wireless setting status - NO ERROR
Synchronizing RTC with NTP server...
Time synchronized!
Total setup time - 12357 milliseconds
Begin main loop
Tx result - NOK (error 0x3069)
The code:
> #include <Arduino.h>
> #include <WiFi.h>
> #include <esp_now.h>
> #include <esp_wifi.h>
>
> #include "rtc.h"
>
> #define VERBOSE
>
> uint8_t macAddress[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
> const char *ssid = "Oferco";
> const char *password = "AslanGY82";
>
> enum wifiStatusEnum { NO_ERROR,
> WIFI_NOT_CONNECTED,
> ESP_NOW_INIT_ERR,
> NO_MAC_ADDRESS };
> const String wifiErrors[] = { "NO ERROR", "WIFI NOT CONNECTED", "ESP NOW INIT ERR", "NO MAC ADDRESS" };
> const String wifiStatusCodes[6] = { "WL IDLE STATUS", "WL NO SSID AVAIL", "WL SCAN COMPLETED",
> "WL CONNECTED", "WL CONNECT FAILED", "WL CONNECTION LOST" };
>
> /*****************************************/
> void OnDataSent(const uint8_t *macAddress, esp_now_send_status_t status) {
> #ifdef VERBOSE
> Serial.print("Packet Send Status:\tDelivery ");
> Serial.println((status == ESP_NOW_SEND_SUCCESS) ? "Success" : "Fail");
> #endif
> }
> /*****************************************/
> wifiStatusEnum wifiConnect() {
> WiFi.persistent(false);
> WiFi.mode(WIFI_AP_STA);
> WiFi.begin(ssid, password);
> uint8_t loops = 0;
> int st = WiFi.status();
> while (st != WL_CONNECTED) {
> #ifdef VERBOSE
> Serial.println("Loop #" + String(loops) + "; status = " + wifiStatusCodes[(uint8_t)st]);
> #endif
> if (++loops > 120) {
> #ifdef VERBOSE
> Serial.println("Sync error. Processing halted.");
> #endif
> return WIFI_NOT_CONNECTED;
> }
> delay(500);
> st = WiFi.status();
> }
> #ifdef VERBOSE
> Serial.println("Final status - " + wifiStatusCodes[(uint8_t)st]);
> Serial.print("Server mac address: ");
> Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
> macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]);
> Serial.print("Server SOFT AP MAC Address: ");
> Serial.println(WiFi.softAPmacAddress());
> Serial.print("Station IP Address: ");
> Serial.print(WiFi.localIP());
> Serial.print("; Wi-Fi Channel: ");
> Serial.println(WiFi.channel());
> #endif
>
> // ESPNOW
> if (esp_now_init() != ESP_OK) {
> #ifdef VERBOSE
> Serial.println("Error initializing ESP-NOW");
> #endif
> return ESP_NOW_INIT_ERR;
> }
> if (!esp_wifi_get_mac(WIFI_IF_STA, macAddress) == ESP_OK) {
> #ifdef VERBOSE
> Serial.println("Error reading mac address");
> #endif
> return NO_MAC_ADDRESS;
> }
> esp_now_register_send_cb(OnDataSent);
> esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
> return NO_ERROR;
> }
> /*****************************************/
> void setup() {
> unsigned long setupTime = millis();
> Serial.begin(9600);
> while (!Serial) ;
> Serial.println("wireless init result - " + wifiErrors[(uint8_t)wifiConnect()]);
>
> Serial.println();
> rtc.syncTime(); // Time sync bewtween local RTC and NTP
> Serial.flush();
> Serial.println("Total setup run time - " + String(millis() - setupTime) + " milliseconds");
> }
> /*****************************************/
> void loop() {
> Serial.println("\nBegin main loop ");
> char testData[] = "test tx";
> esp_err_t er = esp_now_send(macAddress, (uint8_t *)&testData, sizeof(testData));
> Serial.print("Tx result - ");
> Serial.println((er == ESP_OK) ? "OK" : "NOK (error 0x" + String(er, HEX) + ")");
> while (1)
> ;