立即使用esp将数据从NodeMcu(ESP266)发送到ESP32吗?

问题描述 投票:0回答:1

我正在尝试从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上运行的从站代码如下

#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

并且继续

如果还有其他发送数据的方法,请提及

esp8266 arduino-ide nodemcu esp32
1个回答
0
投票

我以前从未使用过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_taddress(类型为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);
    }
}

请让我知道是否可行?

© www.soinside.com 2019 - 2024. All rights reserved.