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
