用 ESP-NOW 用 Blynk 控制 esp8266,有可能吗?

问题描述

我试图使用blynk通过互联网控制我的锁,同时,我需要另一种方法来紧急打开它。所以在接收器上,我使用 example>blynk>boards_wifi>esp8266_standalone。对于blynk部分没问题,可以从android控制,但ESP Now的部分似乎不起作用。发件人说,投递成功,但收件人根本没有收到任何数据。可以帮忙吗?

这是接收器的代码

#define BLYNK_PRINT 串行

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <espNow.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " ";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = " ";
char pass[] = " ";


// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
  char a[32];
  int b;
  float c;
  String d;
  bool e;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac,uint8_t *incomingData,uint8_t 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);
  Serial.print("Float: ");
  Serial.println(myData.c);
  Serial.print("String: ");
  Serial.println(myData.d);
  Serial.print("Bool: ");
  Serial.println(myData.e);
  Serial.println();
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  Serial.println();
  Serial.print("ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
  WiFi.mode(WIFI_AP_STA);

  Blynk.begin(auth,ssid,pass);

  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() != 0) {
    Serial.println("Error initializing ESP-Now");
    return;
  }
  // Once ESPNow is successfully Init,we will register for recv CB to
  // get recv packer info
  esp_Now_set_self_role(ESP_Now_ROLE_SLAVE);
  esp_Now_register_recv_cb(OnDataRecv);

}
void loop()
{
  Blynk.run();
}

对于发件人

#include <ESP8266WiFi.h>
#include <espNow.h>

// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x84,0xCC,0xA8,0xA9,0x70,0xAB};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  char a[32];
  int b;
  float c;
  String d;
  bool e;
} struct_message;

// Create a struct_message called myData
struct_message myData;

unsigned long lastTime = 0;  
unsigned long timerDelay = 2000;  // send readings timer

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr,uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0){
    Serial.println("Delivery success");
  }
  else{
    Serial.println("Delivery fail");
  }
}
 
void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
 
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-Now
  if (esp_Now_init() != 0) {
    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_set_self_role(ESP_Now_ROLE_CONTROLLER);
  esp_Now_register_send_cb(OnDataSent);
  
  // Register peer
  esp_Now_add_peer(broadcastAddress,ESP_Now_ROLE_SLAVE,1,NULL,0);
}
 
void loop() {
  if ((millis() - lastTime) > timerDelay) {
    // Set values to send
    strcpy(myData.a,"THIS IS A CHAR");
    myData.b = random(1,20);
    myData.c = 1.2;
    myData.d = "Hello";
    myData.e = false;

    // Send message via ESP-Now
    esp_Now_send(broadcastAddress,(uint8_t *) &myData,sizeof(myData));

    lastTime = millis();
  }
}

都使用wemos d1 mini。 mydata 只是为了查看通信是否正确

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)