Last active
July 5, 2022 00:25
-
-
Save mongonta0716/6a749f3f5a3e119d47a13b5c5c9c69db to your computer and use it in GitHub Desktop.
ESP-NOW Monitor ESP-NOWで受け取ったデータを表示し続けるアプリ
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
#include <Arduino.h> | |
#ifdef ARDUINO_M5STACK_Core2 | |
#include <M5Core2.h> | |
#elif defined(ARDUINO_M5STACK_FIRE) || defined(ARDUINO_M5Stack_Core_ESP32) | |
#include <M5Stack.h> | |
#endif | |
#define LGFX_AUTODETECT | |
#include <LovyanGFX.hpp> | |
#include <esp_now.h> | |
#include <WiFi.h> | |
#include <esp_wifi.h> | |
esp_now_peer_info_t esp_ap; | |
const uint8_t *peer_addr = esp_ap.peer_addr; | |
const esp_now_peer_info_t *peer = &esp_ap; | |
// 同報したい端末数と端末のMACアドレスを指定します。 | |
// 受信専門の場合は設定は不要 | |
#define MAX_CLIENT 2 | |
uint8_t mac[][6] = { | |
{0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}, // 端末1 | |
{0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb}, // 端末2 | |
// {0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc}, // 端末3 | |
}; | |
// 時刻同期用のWiFi設定 | |
const char *ssid = "YOUR_SSID"; | |
const char *pass = "YOUR_PASS"; | |
#define WIFI_DEFAULT_CHANNEL 1 | |
static LGFX gfx; | |
uint8_t text_size = 1; | |
// NTP同期用 | |
boolean time_stamp_mode = false; | |
struct tm time_info; | |
uint8_t send_data[250]; // ESP-NOWの送信バッファ(250Byteまで) | |
// コールバック関数実行時に排他制御をする。 | |
SemaphoreHandle_t xMutex = NULL; | |
void onRecvData(const uint8_t *mac_addr, const uint8_t *data, int data_len) { | |
// ログを画面表示したい場合は排他をかけないと失敗する場合あり | |
Serial.println("onRecvData"); | |
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]); | |
char time_stamp[9] = ""; | |
if (time_stamp_mode) { | |
// データの先頭に時刻を表示する。 | |
getLocalTime(&time_info, 10); | |
sprintf(time_stamp, "%02d:%02d:%02d,", | |
time_info.tm_hour, time_info.tm_min, time_info.tm_sec); | |
} | |
char buf[250]; | |
memcpy(&buf, data, data_len); | |
BaseType_t xStatus; | |
const TickType_t xTicksToWait = 1000UL; | |
xSemaphoreGive(xMutex); | |
xStatus = xSemaphoreTake(xMutex, xTicksToWait); | |
if (xStatus == pdTRUE) { | |
// gfx.printf("Recv:%s", macStr); | |
gfx.printf("%s%s\n", time_stamp, buf); | |
} | |
xSemaphoreGive(xMutex); | |
} | |
void onSentData(const uint8_t *mac_addr, esp_now_send_status_t status) { | |
// ログを画面表示したい場合は排他をかけないと失敗する場合あり | |
Serial.println("onSent"); | |
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail"); | |
BaseType_t xStatus; | |
const TickType_t xTicksToWait = 1000U; | |
xStatus = xSemaphoreTake(xMutex, xTicksToWait); | |
if (xStatus == pdTRUE) { | |
gfx.print("onSent:"); | |
gfx.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail"); | |
} | |
xSemaphoreGive(xMutex); | |
} | |
void sendData(const uint8_t *data) { | |
for (int i=0; i< MAX_CLIENT; i++) { | |
for (int j=0; j<6; j++) { | |
esp_ap.peer_addr[j] = (uint8_t)mac[i][j]; | |
} | |
BaseType_t xStatus; | |
const TickType_t xTicksToWait = 1000U; | |
xStatus = xSemaphoreTake(xMutex, xTicksToWait); | |
if (xStatus == pdTRUE) { | |
gfx.println("Send Data"); | |
} | |
xSemaphoreGive(xMutex); | |
// gfx.printf("%02X:%02X:%02X:%02X:%02X:%02X\n", peer_addr[0], peer_addr[1], peer_addr[2], peer_addr[3], peer_addr[4], peer_addr[5]); | |
esp_now_send(peer_addr, send_data, sizeof(send_data)); | |
} | |
} | |
void peerClients() { | |
for (int i=0; i< MAX_CLIENT; i++) { | |
for (int j=0; j<6; j++) { | |
esp_ap.peer_addr[j] = (uint8_t)mac[i][j]; | |
} | |
if (esp_now_add_peer(peer) != ESP_OK){ | |
gfx.println("Failed to add peer"); | |
} else { | |
gfx.println("Success Peer"); | |
} | |
} | |
} | |
void syncTime() { | |
WiFi.begin(ssid, pass); | |
while (WiFi.status() != WL_CONNECTED) { | |
gfx.print("."); | |
delay(500); | |
} | |
configTime(9*3600L, 0, "ntp.nict.jp"); | |
getLocalTime(&time_info); | |
gfx.printf("%02d:%02d:%02d\n", | |
time_info.tm_hour, time_info.tm_min, time_info.tm_sec); | |
delay(1000); | |
WiFi.disconnect(); | |
delay(1000); | |
WiFi.mode(WIFI_STA); | |
ESP_ERROR_CHECK(esp_wifi_set_channel(WIFI_DEFAULT_CHANNEL, WIFI_SECOND_CHAN_ABOVE)); | |
if (esp_now_init() == 0) { | |
Serial.println("esp now reinit"); | |
} | |
esp_now_register_recv_cb(onRecvData); | |
esp_now_register_send_cb(onSentData); | |
} | |
void changeTextSize() { | |
text_size++; | |
if (text_size > 2) { | |
text_size = 1; | |
} | |
gfx.setTextSize(text_size); | |
gfx.println("text size changed"); | |
} | |
void setup() { | |
gfx.init(); | |
M5.begin(); | |
gfx.fillScreen(TFT_BLACK); | |
gfx.setTextColor(TFT_WHITE, TFT_BLACK); | |
gfx.setFont(&fonts::lgfxJapanGothic_16); | |
gfx.setBrightness(50); | |
uint8_t mac_addr[6]; | |
esp_read_mac(mac_addr, ESP_MAC_WIFI_STA); | |
// gfx.printf("%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.printf("%02X:%02X:%02X:%02X:%02X:%02X\n", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); | |
WiFi.mode(WIFI_STA); | |
if (esp_now_init() == 0) { | |
Serial.println("esp now init"); | |
} else { | |
Serial.println("esp now init failed"); | |
gfx.println("esp now init failed"); | |
} | |
xMutex = xSemaphoreCreateMutex(); | |
esp_now_register_recv_cb(onRecvData); | |
esp_now_register_send_cb(onSentData); | |
gfx.setTextSize(1); | |
gfx.println("ESP-NOW Monitor: BtnC Help"); | |
gfx.setCursor(0, 20); | |
gfx.setTextScroll(true); | |
gfx.setScrollRect(0, 20, gfx.width(), gfx.height()-20); | |
} | |
void loop() { | |
M5.update(); | |
BaseType_t xStatus; | |
const TickType_t xTicksToWait = 1000U; | |
if (M5.BtnB.wasPressed()) { | |
syncTime(); | |
} | |
if (M5.BtnB.pressedFor(2000)) { | |
xStatus = xSemaphoreTake(xMutex, xTicksToWait); | |
if (xStatus == pdTRUE) { | |
gfx.setCursor(0,20); | |
gfx.fillRect(0, 20, gfx.width(), gfx.height() - 20, TFT_BLACK); | |
} | |
xSemaphoreGive(xMutex); | |
} | |
if (M5.BtnA.pressedFor(2000)) { | |
changeTextSize(); | |
delay(2000); | |
} | |
if (M5.BtnA.wasPressed()) { | |
uint8_t mac_addr[6]; | |
esp_read_mac(mac_addr, ESP_MAC_WIFI_STA); | |
xStatus = xSemaphoreTake(xMutex, xTicksToWait); | |
if (xStatus == pdTRUE) { | |
gfx.printf("%02X:%02X:%02X:%02X:%02X:%02X\n", | |
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); | |
Serial.printf("%02X:%02X:%02X:%02X:%02X:%02X\n", | |
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); | |
} | |
xSemaphoreGive(xMutex); | |
} | |
if (M5.BtnC.pressedFor(2000)) { | |
time_stamp_mode = not(time_stamp_mode); | |
delay(2000); | |
} | |
if (M5.BtnC.wasPressed()) { | |
String help_msg = "BtnA:DisplayMACAddr\n" | |
"BtnA Long:Change font size\n" | |
"BtnB:NTP sync\n" | |
"BtnB Long:Clear\n" | |
"BtnC:Help\n" | |
"BtnC Long:Change time stamp mode\n"; | |
xStatus = xSemaphoreTake(xMutex, xTicksToWait); | |
if (xStatus == pdTRUE) { | |
gfx.setTextSize(1); | |
gfx.print(help_msg); | |
Serial.print(help_msg); | |
gfx.setTextSize(text_size); | |
} | |
xSemaphoreGive(xMutex); | |
} | |
} | |
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
; PlatformIO Project Configuration File | |
; | |
; Build options: build flags, source filter | |
; Upload options: custom upload port, speed and extra flags | |
; Library options: dependencies, extra library storages | |
; Advanced options: extra scripting | |
; | |
; Please visit documentation for the other options and examples | |
; https://docs.platformio.org/page/projectconf.html | |
[platformio] | |
default_envs = m5stack-core2 | |
[env:m5stack-fire] | |
platform = espressif32 | |
board = m5stack-fire | |
framework = arduino | |
upload_speed = 1500000 | |
monitor_speed = 115200 | |
board_build.flash_mode = qio | |
board_build.f_flash = 80000000L | |
lib_deps = | |
lovyan03/LovyanGFX@^0.3.10 | |
m5stack/M5Stack@^0.3.1 | |
bblanchon/ArduinoJson@^6.17.3 | |
[env:m5stack-core2] | |
platform = espressif32 | |
board = m5stack-core2 | |
framework = arduino | |
upload_speed = 1500000 | |
monitor_speed = 115200 | |
board_build.flash_mode = qio | |
board_build.f_flash = 80000000L | |
lib_deps = | |
lovyan03/LovyanGFX@^0.3.10 | |
m5stack/M5Core2@^0.0.2 | |
bblanchon/ArduinoJson@^6.17.3 | |
[env:m5stack-grey] | |
platform = espressif32 | |
board = m5stack-grey | |
framework = arduino | |
upload_speed = 1500000 | |
monitor_speed = 115200 | |
board_build.flash_mode = qio | |
board_build.f_flash = 80000000L | |
lib_deps = | |
lovyan03/LovyanGFX@^0.3.10 | |
m5stack/M5Stack@^0.3.1 | |
bblanchon/ArduinoJson@^6.17.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment