ESPNow send two types of messages to two destinations simulationsly

27 Views Asked by At

What I'm trying to achieve is to send packets from an ESP32 let's call it Master to another ESP32 with connection to wifi to send data to Blynk platform and in the same time that ESP32 Master will send another message to an ESP8266. I tried to put a delay timer between the two sending events but mostly I'm getting one as successful while the other fails. Here is the code for the ESP32 Master:

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

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
bool connected = false;
char incomingChar =' ';
//set ports
// state pin
#define rx 0 // => HC05: tx
#define tx 2 // => HC05: rx
// GND pin
// 5V pin
#define cmd 4 //EN pin
// bluetooth configuration
BluetoothSerial SerialBT;
// 98:da:50:01:e7:89
String myName = "ESP32-BT-Master";
uint8_t address[] = {0x98,0xda,0x50,0x01,0x5a,0x2d}; 
// esp now to address the blynk receiver
// A8:42:E3:48:E5:88
uint8_t blynkAddress[] = {0xA8, 0x42, 0xE3, 0x48, 0xE5, 0x88};
// 8C:CE:4E:CC:3A:BB
uint8_t ESPAddress1[] = {0x8C, 0xCE, 0x4E, 0xCC, 0x3A, 0xBB};
// C4:5B:BE:63:68:5C
uint8_t ESPAddress0[] = {0xC4, 0x5B, 0xBE, 0x63, 0x68, 0x5C};
// Broadcast
uint8_t ESPAddress2[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

// Define variables to store DHT readings to be sent
int Data;
int i=0;
// Define variables to store incoming readings
int incomingdata1,incomingdata2;
float incomingvar;
int incomingpriority;
int8_t incomingrssi;
unsigned long currentMillis;
unsigned long lastWakeup = 0;
const int maxRuntime = 180; // Maximum runtime in seconds (3 minutes)
unsigned long FirstWakeUpTime = millis();
#define NUM_READINGS 5
int Readings1[NUM_READINGS],Readings2[NUM_READINGS];
int readingsIndex1,readingsIndex2 = 0;
float StandardDeviation[]={0,0};
int Variation=0;
char *parameter;
// Variable to store if sending data was successful
String success;

typedef struct struct_message {
  int id;
  int data1,data2;
  int priority;
  int8_t rssi;
} struct_message;
typedef struct struct_blynk {
  int id;
  int battery;
} struct_blynk;
// Create a struct_message to hold sensor readings
int sleeptime[2];
int pastsleeptime[2]={10000,10000};
int sleepcount[2]={0,0};
//Data to send (you are here)
struct_message sentData;
struct_blynk blynkdata;
// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;
esp_now_peer_info_t peerInfo,peerInfo2,peerInfo3;
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");
  if (status ==0){
    success = "Delivery Success :)";
  }
  else{
    success = "Delivery Fail :(";
  }
}
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  
  
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print(" received from slave: ");
  i = incomingReadings.id;
  incomingdata1 = incomingReadings.data1;
  incomingdata2 = incomingReadings.data2;
  incomingpriority = incomingReadings.priority;
  incomingrssi = incomingReadings.rssi;
  if(i==0){
    Readings1[readingsIndex1] = incomingdata1;
    readingsIndex1 = (readingsIndex1 + 1) % NUM_READINGS;
    //calculate std deviation
  if (readingsIndex1 == 0) {
      // Calculate standard deviation of the last 5 readings
      StandardDeviation[i] = calculateStandardDeviation(Readings1, NUM_READINGS);
    }}
  else{
    Readings2[readingsIndex2] = incomingdata1;
    readingsIndex2 = (readingsIndex2 + 1) % NUM_READINGS;
    if (readingsIndex2 == 0) {
      // Calculate standard deviation of the last 5 readings
      StandardDeviation[i] = calculateStandardDeviation(Readings2, NUM_READINGS);
    }
  }
  (StandardDeviation[i]>0) ? Variation=1:Variation=0;
  // Print incoming readings
  Serial.print("Sensor Data:  ");
  Serial.println(incomingdata1);
  Serial.print("Battery level:  ");
  Serial.println(incomingdata2);
  Serial.print("Sleep count before this one:  ");
  Serial.println(sleepcount[i]);
  Serial.print("Variation of info:  ");
  Serial.println(StandardDeviation[i]);
  Serial.print("Priority of info: ");
  Serial.println(incomingpriority);
  Serial.print("RSSI: ");
  Serial.println(incomingrssi);
  sleeptime[i]=(pastsleeptime[i]+(((0.002*(100-incomingdata2)-0.04*incomingpriority-0.06*Variation-0.0012*incomingrssi)*pastsleeptime[i])))*(1+0.02*sleepcount[i]);
  sentData.data1 = sleeptime[i];
  pastsleeptime[i] = sleeptime[i];
  blynkdata.id=i;
  blynkdata.battery=incomingdata2;
  esp_err_t result;
  if(i==0){
   result = esp_now_send(ESPAddress0, (uint8_t *) &sentData, sizeof(sentData));
   delay(5000);
   result = esp_now_send(blynkAddress, (uint8_t *) &blynkdata, sizeof(blynkdata));}
  if(i==1){
   result = esp_now_send(ESPAddress1, (uint8_t *) &sentData, sizeof(sentData));
   delay(5000);
   result = esp_now_send(blynkAddress, (uint8_t *) &blynkdata, sizeof(blynkdata));
  }
  if (result == ESP_OK) {
    Serial.print("Sent with success to the slave: ");
    Serial.println(sentData.data1);
    Serial.println("--Data sent to Blynk server--");
    currentMillis = millis();  
    Serial.print("Time left: ");
    Serial.println(currentMillis-FirstWakeUpTime);
    if(currentMillis-FirstWakeUpTime < maxRuntime*1000){
      sleepcount[i]++;}
    else{
      sleepcount[i]=0;
      FirstWakeUpTime = millis();
    }
  }
  else {
    Serial.println("Error sending the data");
  }

}



void setup() {
  // put your setup code here, to run once:
// Init Serial Monitor
  
  Serial.begin(9600);
  
  
  
  Serial.println("The device master started, now you can pair it with bluetooth!");
 // Set device as a Wi-Fi Station 
  WiFi.mode(WIFI_STA);
  WiFi.printDiag(Serial); // Uncomment to verify channel number before
  esp_wifi_set_promiscuous(true);
  esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE);
  esp_wifi_set_promiscuous(false);
  WiFi.printDiag(Serial); // Uncomment to verify channel change after
   // 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, ESPAddress1, 6);
  memcpy(peerInfo2.peer_addr, blynkAddress, 6);
  memcpy(peerInfo3.peer_addr, ESPAddress0, 6);
  peerInfo.channel = 0; 
  peerInfo2.channel = 0; 
  peerInfo3.channel = 0;  
  peerInfo.encrypt = false;
  peerInfo2.encrypt = false;
  peerInfo3.encrypt = false;
  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer2");
    return;
  }
  if (esp_now_add_peer(&peerInfo2) != ESP_OK){
    Serial.println("Failed to add peer Blynk");
    return;
  }
  if (esp_now_add_peer(&peerInfo3) != ESP_OK){
    Serial.println("Failed to add peer1");
    return;
  }
  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);
  // set input through EN pin
  pinMode(cmd, OUTPUT);
  digitalWrite(cmd, HIGH);
 
}

void loop() {
  
 
  delay(100);

}
float calculateStandardDeviation(int data[], int n) {
  float sum = 0.0, mean, standardDeviation = 0.0;

  for (int i = 0; i < n; ++i) {
    sum += data[i];
  }

  mean = sum / n;

  for (int i = 0; i < n; ++i) {
    standardDeviation += pow(data[i] - mean, 2);
  }

  return sqrt(standardDeviation / n);
}

Is there any suggestion to how to send two packets simultaneously from the same ESP using the same ESP-Now with the same channel?

0

There are 0 best solutions below