Unable to connect 2 ESP with ESP now and send data to Thingspeak over Wifi simultaneously

Hi,
I'm trying to connect 2 ESP (1 as Tx and other as Rx) with the communication between the ESP being ESP Now and then at the Rx I need to send the received data via ESP Now protocol via Wi Fi to Thingspeak. But both tasks happen separately but not simultaneously. I get an error 301 while trying to send the received data from Tx ESP to Thingspeak. Please let me know any kind of solutions regarding this.

Tx Code


#include <esp_now.h>
#include <esp_wifi.h>
#include <WiFi.h>

// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xE1, 0x67, 0x60, 0x9A, 0x4E, 0x84};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  char a[32];
  int b;
  float c;
  bool d;
} struct_message;

// Create a struct_message called myData
struct_message myData;
constexpr char wifi_SSID[] = "SSID";

esp_now_peer_info_t peerInfo;

int32_t obtain_wifi(const char *ssid) {
  if (int32_t n = WiFi.scanNetworks()) {
      for (uint8_t i=0; i<n; i++) {
          if (!strcmp(ssid, WiFi.SSID(i).c_str())) {
              return WiFi.channel(i);
          }
      }
  }
  return 0;
}

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
 
void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
 
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);
int32_t channel = obtain_wifi(wifi_SSID);
  WiFi.printDiag(Serial); 
  esp_wifi_set_promiscuous(true);
  esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
  esp_wifi_set_promiscuous(false);
  WiFi.printDiag(Serial);
  
  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  //peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  
  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
}
 
void loop() {
  // Set values to send
  strcpy(myData.a, "THIS IS A CHAR");
  myData.b = random(1,20);
  myData.c = random(1.0,3.0);
  myData.d = false;
  
  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(2000);
}

Rx Code with Thingspeak


#include <esp_now.h>
#include <WiFi.h>
#include "ThingSpeak.h"

const char* ssid     = "Ssid";
const char* password = "Password";

bool rda=false;
int y=0;

WiFiClient  client;

unsigned long myChannelNumber = 1;
const char * myWriteAPIKey = "KEYS";

// Structure example to receive data 
typedef struct struct_message {
    char a[32];
    int b;
    float c;
    bool d;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("Char: ");
  Serial.println(myData.a);
  Serial.print("Int: ");
  Serial.println(myData.b);
  y=myData.b;
  Serial.print("Float: ");
  Serial.println(myData.c);
  Serial.print("Bool: ");
  Serial.println(myData.d);
  Serial.println();
  //ThingSpeak.setField(1, y);
  //int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  
  int x = ThingSpeak.writeField(myChannelNumber, 1, y, myWriteAPIKey);
   if(x == 200){
      Serial.println("Channel update successful.");
    }
    else{
      Serial.println("Problem updating channel. HTTP error code " + String(x));
    }

}
 
void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  
  WiFi.mode(WIFI_AP_STA);
  ThingSpeak.begin(client);
  WiFi.begin(ssid, password);
    
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Setting as a Wi-Fi Station..");
  }
  Serial.print("Station IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.print("Wi-Fi Channel: ");
  Serial.println(WiFi.channel());
   
  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;

    
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {
delay(15000);
}

The output on serial monitor with received values from Tx and error

As far as I know the ESP32 cannot do ESP Now simultaneously while holding a connection a WiFi access point.

Why do you think that the communication between the two ESPs should be ESP Now?

have a look at esp32-esp-now-wi-fi-web-server it may give you some ideas

@horace Thanks for the response. I did try this but it seems this is only able to connect to a webserver via internet, but gives an error while replacing the webserver part with the part to upload data to Thingspeak. I might be placing the code to upload data to Thingspeak at the wrong position though. Currently I'm trying to upload the sensor data right after data is received from esp now in the call back function, and I get an error 301 from Thingspeak( not able to connect to Thingspeak server). Do you know if this is right?The parameters like API key and server address of Thingspeak are correct as they work if I try to upload the data without connecting to Esp Wroom . Also I think even though WiFi shows it's connected (via WiFi.connect(SSID PASSWORD)), its really not, as even a simple ping to Google returns as Fail