■ESP-NOWの実験■
手前左がスレーブ(受信)、奥の4つがサーバ(送信)です。スレーブには4台分のMACアドレスを登録し、サーバには受信側のMACアドレスを登録してあります。送信側:
起動してからsetup()でdigitalWrite(13,LOW)が実行されるまでの間は左側LEDがぼんやり点灯しています(deep sleep中はoff)。右側のシリアルTx LEDの2回目の点灯が送信です(deep sleepからの解除時のシステムメッセージで1回光り、送信時にもう一回光ります)。
受信側:
受信時に光ります。ご覧のとおり、送信側の2回目の点灯と受信側のシリアルの点灯が同期しています。よかったよかった。と思ったら1回明らかに抜けているところがあるなぁ。
フレームのレイヤで誤り検出をしているので、よその電波とぶつかって信号が乱れたらいさぎよく破棄されるのが電波通信の習わしです。きっと他のWiFiなどとの輻輳でデータが壊れたのでパケットが破棄されたのでしょう。
以下ソースは受信側(スレーブ)です。複数のサーバを相手にするために「氾濫原」さんのものに少し変更しました。ありがとうございます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ESP_NOW_Sleve(Receiver) | |
// | |
// thanks: | |
// http://lowreal.net/2016/01/14/2 | |
// ESP8266 の低消費電力の限界をさぐる (ESP-NOWを使ってみる) | |
// | |
// Arranged by koichi kurahsahi 2016-03-31 | |
// 相手サーバを4つに増やしましたw | |
// | |
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
extern "C" { | |
#include <espnow.h> | |
#include <user_interface.h> | |
} | |
#define WIFI_DEFAULT_CHANNEL 1 | |
uint8_t mac[][6] = { | |
{0x18, 0xFE, 0x34, 0xEF, 0x5C, 0x76}, | |
{0x18, 0xFE, 0x34, 0xEE, 0x6D, 0xFC}, | |
{0x5C, 0xCF, 0x7F, 0x08, 0x36, 0x9A}, | |
{0x5C, 0xCF, 0x7F, 0x08, 0x36, 0xA1} | |
}; | |
void printMacAddress(uint8_t* macaddr) { | |
Serial.print("{"); | |
for (int i = 0; i < 6; i++) { | |
Serial.print("0x"); | |
Serial.print(macaddr[i], HEX); | |
if (i < 5) Serial.print(','); | |
} | |
Serial.println("}"); | |
} | |
void setup() { | |
pinMode(13, OUTPUT); | |
Serial.begin(115200); | |
Serial.println("Initializing..."); | |
WiFi.mode(WIFI_AP); | |
WiFi.softAP("foobar", "12345678", 1, 0); | |
uint8_t macaddr[6]; | |
wifi_get_macaddr(STATION_IF, macaddr); | |
Serial.print("mac address (STATION_IF): "); | |
printMacAddress(macaddr); | |
wifi_get_macaddr(SOFTAP_IF, macaddr); | |
Serial.print("mac address (SOFTAP_IF): "); | |
printMacAddress(macaddr); | |
if (esp_now_init() == 0) { | |
Serial.println("init"); | |
} else { | |
Serial.println("init failed"); | |
ESP.restart(); | |
return; | |
} | |
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE); | |
esp_now_register_recv_cb([](uint8_t *macaddr, uint8_t *data, uint8_t len) { | |
Serial.println("recv_cb"); | |
Serial.print("mac address: "); | |
printMacAddress(macaddr); | |
Serial.print("data: "); | |
for (int i = 0; i < len; i++) { | |
Serial.print(" 0x"); | |
Serial.print(data[i], HEX); | |
} | |
Serial.println(""); | |
}); | |
esp_now_register_send_cb([](uint8_t* macaddr, uint8_t status) { | |
Serial.println("send_cb"); | |
Serial.print("mac address: "); | |
printMacAddress(macaddr); | |
Serial.print("status = "); Serial.println(status); | |
}); | |
int l = sizeof(mac) / sizeof(mac[0]); | |
for (int i = 0; i < l; i++) { | |
Serial.print("ch="); | |
Serial.print(i); | |
Serial.println(esp_now_add_peer(mac[i], (uint8_t)ESP_NOW_ROLE_CONTROLLER, (uint8_t)WIFI_DEFAULT_CHANNEL, NULL, 0)); | |
} | |
} | |
void loop() { | |
delay(500); | |
} |
0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。