现在使用 esp 将数据从 NodeMcu(ESP266)发送到 ESP32?

Send data from NodeMcu(ESP266) to ESP32 using esp now?

我正在尝试将一些数据从 Nodemcu(esp8266) 发送到 ESP32。我正在尝试为此目的使用 espnow,但我真的被卡住了,我无法为两个板合并主从,我发现代码大不相同我尝试了一些修改并且我可以从 Nodemcu 发送数据但是可以在 ESP32 上接收它。我正在尝试为手势控制汽车发送两个模拟值。

下面给出Nodemcu上的主码或控制器码运行

#include <ESP8266WiFi.h>
#include <espnow.h>

#define MUX_A D4
#define MUX_B D3
#define MUX_C D2
#define ANALOG_INPUT A0
#define CHANNEL 4

extern "C" {
}
uint8_t remoteMac[] = {0x24, 0x6F, 0x28, 0xB6, 0x24, 0x49};

struct __attribute__((packed)) DataStruct {
    //char text[32];
    int x;
    int y;
    unsigned long time;
};

DataStruct myData;

unsigned long lastSentMillis;
unsigned long sendIntervalMillis = 1000;
unsigned long sentMicros;
unsigned long ackMicros;
int xAxis;
int yAxis;
int zAxis;

void InitESPNow() {
  WiFi.disconnect();
  if (esp_now_init()==0) {
    Serial.println("ESPNow Init Success");
  }
  else {
    Serial.println("ESPNow Init Failed");
    // Retry InitESPNow, add a counte and then restart?
    // InitESPNow();
    // or Simply Restart
    ESP.restart();
  }
}


void sendData() {
  if (millis() - lastSentMillis >= sendIntervalMillis) {
        lastSentMillis += sendIntervalMillis;
        myData.time = millis();
        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
        Serial.println("sent data");
        Serial.println(myData.x);
        Serial.println(myData.y);
    }
}
void sendCallBackFunction(uint8_t* mac, uint8_t sendStatus) {
    ackMicros = micros();
    Serial.print("Trip micros "); Serial.println(ackMicros - sentMicros);
    Serial.printf("Send status = %i", sendStatus);
    Serial.println();
    Serial.println();
}
void setup() {
    Serial.begin(115200); Serial.println();
    Serial.println("Starting EspnowController.ino");

    WiFi.mode(WIFI_STA); // Station mode for esp-now controller
    WiFi.disconnect();

    Serial.printf("This mac: %s, ", WiFi.macAddress().c_str());
    Serial.printf("slave mac: %02x%02x%02x%02x%02x%02x", remoteMac[0], remoteMac[1], remoteMac[2], remoteMac[3], remoteMac[4], remoteMac[5]);

    Serial.printf(", channel: %i\n",CHANNEL);
    InitESPNow();
    esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
    esp_now_add_peer(remoteMac, ESP_NOW_ROLE_SLAVE, CHANNEL, NULL, 0);

    esp_now_register_send_cb(sendCallBackFunction);

    Serial.print("Message "); 

    pinMode(MUX_A, OUTPUT);
    pinMode(MUX_B, OUTPUT);     
    pinMode(MUX_C, OUTPUT);
    Serial.println("Setup finished");

    }

void changeMux(int c, int b, int a) {
  digitalWrite(MUX_A, a);
  digitalWrite(MUX_B, b);
  digitalWrite(MUX_C, c);
}
void loop() {

    changeMux(LOW, LOW, LOW);
    xAxis = analogRead(ANALOG_INPUT); //Value of the sensor connected to  pin 0 of IC
    changeMux(LOW, LOW, HIGH);
    yAxis = analogRead(ANALOG_INPUT); //Value of the sensor connected to  pin 1 of IC
    changeMux(LOW, HIGH, LOW);
    zAxis = analogRead(ANALOG_INPUT); //Value of the sensor connected to  pin 2 of IC
    changeMux(LOW, HIGH, LOW);

    myData.x= xAxis;
    myData.y= yAxis;
    sendData();
    delay(500);
}

ESP32上的slave代码运行如下

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

#define CHANNEL 4

uint8_t mac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33};

struct __attribute__((packed)) DataStruct {
    //char text[32];
    int x;
    int y;
    unsigned long time;
};

DataStruct myData;
// Init ESP Now with fallback


void setup() {
  Serial.begin(115200);
  Serial.println("ESPNow/Basic/Slave Example");
  //Set device in AP mode to begin with
  WiFi.mode(WIFI_AP);
  // configure device AP mode
  // This is the mac address of the Slave in AP Mode
  esp_wifi_set_mac(ESP_IF_WIFI_STA, &mac[0]);


  Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress());
  // Init ESPNow with a fallback logic
  if (esp_now_init()!=0) {
        Serial.println("*** ESP_Now init failed");
        while(true) {};
    }

  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info.
  esp_now_register_recv_cb(OnDataRecv);
   Serial.print("Aheloiioi");
}

// callback when data is recv from Master
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
  memcpy(&myData, data, sizeof(myData));
  char macStr[18];
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  Serial.print("Last Packet Recv from: "); Serial.println(macStr);
  Serial.print("Last Packet Recv Data: "); Serial.println(myData.x); Serial.println(myData.y);
  Serial.println("");
}

void loop() {
  // Chill
}

这是我在 ESP32 上获得的唯一输出

ESPNow/Basic/Slave Example
AP MAC: 24:6F:28:B6:24:49
Aheloiioi

虽然这是 Nodemcu 上的输出

Starting EspnowController.ino
This mac: BC:DD:C2:B5:E3:2B, slave mac: 246f28b62449, channel: 4
ESPNow Init Success
Message Setup finished
sent data
10
8
Trip micros 7320
Send status = 1

sent data
9
8
Trip micros 6817
Send status = 1

sent data
10
9
Trip micros 6731
Send status = 1

并继续

如果有其他发送数据的方法,请务必注明

我以前没用过esp_now所以没有自己测试,不过我觉得这跟库和esp32没有关系,只是c++用法的一个小错误

在你的 esp8266 的 sendData() 函数中,你这样做了:

        uint8_t bs[sizeof(myData)];
        memcpy(bs, &myData, sizeof(myData));
        sentMicros = micros();
        esp_now_send(NULL, bs, sizeof(myData));

bs 的类型为 uint8_t 并且是一个数组,您尝试将结构类型为 myData 的数据复制到数组中。然后您尝试将数组传递到 esp_now_send()。赶紧看了一下esp_now_send()函数原型定义,esp_now_send()需要传入地址(里面有一个uint8_t的类型)您的数据结构 myData

我不知道你为什么需要做memcpy,但我认为直接将myData的指针传入函数调用会更容易和简单。

void sendData() {
  if (millis() - lastSentMillis >= sendIntervalMillis) {
        lastSentMillis += sendIntervalMillis;
        myData.time = millis();
        esp_now_send(NULL, (uint8_t *)&myData, sizeof(myData)); // NULL means send to all peers
        Serial.println("sent data");
        Serial.println(myData.x);
        Serial.println(myData.y);
    }
}

请告诉我这是否有效?