Go Down

Topic: ESP8266 with ESP-NOW as an alternative to nRF24L01+ (Read 20739 times) previous topic - next topic

who_took_my_nick

Ok, here is the sender INO.

Code: [Select]

#include <ESP8266WiFi.h>
extern "C" {
    #include <espnow.h>
}
uint8_t remoteMac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33};
#define WIFI_CHANNEL 4
struct __attribute__((packed)) DataStruct {
    int integer;
};
DataStruct myData;
unsigned long lastSentMillis;
unsigned long sendIntervalMillis = 1000;
unsigned long sentMicros;
unsigned long ackMicros;
unsigned long lastBlinkMillis;
unsigned long fastBlinkMillis = 200;
unsigned long slowBlinkMillis = 700;
unsigned long blinkIntervalMillis = slowBlinkMillis;

long startTimeTrigger = 0;
unsigned long currentTimeTrigger = millis();
long elapsedTimeTrigger = currentTimeTrigger-startTimeTrigger;
long cycleTimeTrigger = 5000;

byte ledPin = 14;
void setup() {
    Serial.begin(9600);
    WiFi.mode(WIFI_STA); // Station mode for esp-now controller
    WiFi.disconnect();
    if (esp_now_init() != 0) {
        Serial.println("*** ESP_Now init failed");
        while(true) {};
    }
    esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
    esp_now_add_peer(remoteMac, ESP_NOW_ROLE_SLAVE, WIFI_CHANNEL, NULL, 0);
    esp_now_register_send_cb(sendCallBackFunction);
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, HIGH);
}
void loop() {
  currentTimeTrigger = millis();
  elapsedTimeTrigger = currentTimeTrigger-startTimeTrigger;
  if(elapsedTimeTrigger > cycleTimeTrigger){
    startTimeTrigger = currentTimeTrigger;
    if(elapsedTimeTrigger > 0 && elapsedTimeTrigger < 1000)
      myData.integer = 1;
      Serial.println("1");
   
    if(elapsedTimeTrigger > 1000 && elapsedTimeTrigger < 2000)
      myData.integer = 2;
      Serial.println("2");
   
    if(elapsedTimeTrigger > 2000 && elapsedTimeTrigger < 3000)
      myData.integer = 5;
      Serial.println("5");
   
    }
    Serial.println(elapsedTimeTrigger);
    //myData.integer = 5;
    sendData();
    blinkLed();
}
void sendData() {
    if (millis() - lastSentMillis >= sendIntervalMillis) {
        lastSentMillis += sendIntervalMillis;
        uint8_t bs[sizeof(myData)];
        memcpy(bs, &myData, sizeof(myData));
        sentMicros = micros();
        esp_now_send(NULL, bs, sizeof(myData)); // NULL means send to all peers
    }
}
void sendCallBackFunction(uint8_t* mac, uint8_t sendStatus) {
    Serial.printf("Send status = %i", sendStatus);
    Serial.println();
    if (sendStatus == 0) {
        blinkIntervalMillis = fastBlinkMillis;
    }
    else {
        blinkIntervalMillis = slowBlinkMillis;
    }
}
void blinkLed() {
    if (millis() - lastBlinkMillis >= blinkIntervalMillis) {
        lastBlinkMillis += blinkIntervalMillis;
        digitalWrite(ledPin, ! digitalRead(ledPin));
    }
}


And here is receiver INO.

Code: [Select]

#include <ESP8266WiFi.h>
extern "C" {
    #include <espnow.h>
    #include <user_interface.h>
}
uint8_t mac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33};
void initVariant() {
  WiFi.mode(WIFI_AP);
  wifi_set_macaddr(SOFTAP_IF, &mac[0]);
}
#define WIFI_CHANNEL 4
struct __attribute__((packed)) DataStruct {
    int integer;
};
DataStruct myData;
void setup() {
    Serial.begin(9600);
    if (esp_now_init()!=0) {
        Serial.println("*** ESP_Now init failed");
        while(true) {};
    }
    esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
    esp_now_register_recv_cb(receiveCallBackFunction);
    pinMode(12, OUTPUT);
    digitalWrite(12,HIGH);
}
void loop() {
  if(myData.integer == 5){
    digitalWrite(12,LOW);
    }else{
      digitalWrite(12,HIGH);
      }

}
void receiveCallBackFunction(uint8_t *senderMac, uint8_t *incomingData, uint8_t len) {
    memcpy(&myData, incomingData, sizeof(myData));
    Serial.print("  integer  ");
    Serial.print(myData.integer);
    Serial.println();
}


What I get with these sketches...
When I remove this millis() part and assign myData.integer=5; sketch pass it to sendData() function and I can get it on the other ESP. When I include millis() part to send different integer after 1 second all I get on the other ESP is 0. On the first ESP Serial doesn't show any number, just as it doesn't match the condition.

My brain just get stuck. Any help would be nice.


who_took_my_nick

My mistake.

It passes all three integers one by one in a second to Serial. But not to sendData(); function.

who_took_my_nick

Wrong if condition. :(

Eventually, it works.

Sender INO file. If someone needs it. It sends different values every second from one ESP to the other. The receiver is from the above post.

Code: [Select]
#include <ESP8266WiFi.h>
extern "C" {
    #include <espnow.h>
}
uint8_t remoteMac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33};
#define WIFI_CHANNEL 4
struct __attribute__((packed)) DataStruct {
    int integer;
};
DataStruct myData;
unsigned long lastSentMillis;
unsigned long sendIntervalMillis = 1000;
unsigned long sentMicros;
unsigned long ackMicros;
unsigned long lastBlinkMillis;
unsigned long fastBlinkMillis = 200;
unsigned long slowBlinkMillis = 700;
unsigned long blinkIntervalMillis = slowBlinkMillis;

long startTimeTrigger = 0;
unsigned long currentTimeTrigger = millis();
long elapsedTimeTrigger = currentTimeTrigger-startTimeTrigger;
long cycleTimeTrigger = 5000;

byte ledPin = 14;
void setup() {
    Serial.begin(9600);
    WiFi.mode(WIFI_STA); // Station mode for esp-now controller
    WiFi.disconnect();
    if (esp_now_init() != 0) {
        Serial.println("*** ESP_Now init failed");
        while(true) {};
    }
    esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
    esp_now_add_peer(remoteMac, ESP_NOW_ROLE_SLAVE, WIFI_CHANNEL, NULL, 0);
    esp_now_register_send_cb(sendCallBackFunction);
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, HIGH);
}
void loop() {
  currentTimeTrigger = millis();
  elapsedTimeTrigger = currentTimeTrigger-startTimeTrigger;
  if(elapsedTimeTrigger > cycleTimeTrigger){
    startTimeTrigger = currentTimeTrigger;
  }
    if(elapsedTimeTrigger > 0 && elapsedTimeTrigger < 1000){
      myData.integer = 1;
    }
   if(elapsedTimeTrigger > 1000 && elapsedTimeTrigger < 2000){
      myData.integer = 2;
   }
   if(elapsedTimeTrigger > 2000 && elapsedTimeTrigger < 3000){
      myData.integer = 3;
   }
   if(elapsedTimeTrigger > 3000 && elapsedTimeTrigger < 4000){
      myData.integer = 4;
   }
   if(elapsedTimeTrigger > 4000 && elapsedTimeTrigger < 5000){
      myData.integer = 5;
   }
    sendData();
    blinkLed();
}
void sendData() {
  //myData.integer = 5;
    if (millis() - lastSentMillis >= sendIntervalMillis) {
        lastSentMillis += sendIntervalMillis;
        uint8_t bs[sizeof(myData)];
        memcpy(bs, &myData, sizeof(myData));
       // sentMicros = micros();
        esp_now_send(NULL, bs, sizeof(myData)); // NULL means send to all peers
    }
}
void sendCallBackFunction(uint8_t* mac, uint8_t sendStatus) {
    Serial.printf("Send status = %i", sendStatus);
    Serial.println();
    if (sendStatus == 0) {
        blinkIntervalMillis = fastBlinkMillis;
    }
    else {
        blinkIntervalMillis = slowBlinkMillis;
    }
}
void blinkLed() {
    if (millis() - lastBlinkMillis >= blinkIntervalMillis) {
        lastBlinkMillis += blinkIntervalMillis;
        digitalWrite(ledPin, ! digitalRead(ledPin));
    }
}

Go Up