Skip to content

Instantly share code, notes, and snippets.

@mongonta0716
Created April 6, 2022 06:44
Show Gist options
  • Save mongonta0716/a8bba91cc984d685153d37c856237a4b to your computer and use it in GitHub Desktop.
Save mongonta0716/a8bba91cc984d685153d37c856237a4b to your computer and use it in GitHub Desktop.
UARTから受信したデータをESP-NOWで複数のM5Stackに流す(要Arduino-esp32 2.0.3RC1)
// UARTで受信したデータをESP-NOWで送信するサンプル
// 受信はESP-NOW MONITORが使えます。https://gist.github.com/mongonta0716/6a749f3f5a3e119d47a13b5c5c9c69db
#include <Arduino.h>
#include <M5Unified.h>
#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;
// ESP-NOWで使用するWiFiチャンネルを固定(※ルーターの設定も必要かも)
#define WIFI_DEFAULT_CHANNEL 1
#define WIFI_SECONDORY_CHANNEL 2
char *json_buffer;
#define MAX_CLIENT 2 // 接続先のESP32の台数
uint8_t mac[][6] = {
{ 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }, // 接続先1のMACアドレス
{ 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB }, // 接続先2のMACアドレス
// { 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC }, // 接続先の端末分だけ配列を用意する
};
void peerClient(int no) {
for (int j=0; j<6; j++) {
esp_ap.peer_addr[j] = (uint8_t)mac[no][j];
}
if (esp_now_add_peer(peer) != ESP_OK){
M5.Display.println("Failed to add peer");
} else {
M5.Display.println("Success Peer");
}
}
void UART_RX_IRQ() {
if (Serial.available()) {
String uart_data = Serial.readStringUntil('\n');
int data_len = uart_data.length();
json_buffer = (char *)(uart_data.c_str());
for (int i=0; i< MAX_CLIENT; i++) {
peerClient(i);
esp_now_send(peer_addr, (uint8_t*)json_buffer, data_len);
}
M5.Display.println(uart_data);
}
}
void setup() {
auto cfg = M5.config();
M5.begin(cfg);
M5.Display.init();
M5.Display.setRotation(3);
M5.Display.setTextSize(2);
M5.Display.println("UART Test");
M5.Display.setTextScroll(true);
M5.Display.setScrollRect(0, 15, M5.Display.width(), M5.Display.height() - 15);
Serial.onReceive(UART_RX_IRQ);
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 init");
} else {
Serial.println("esp now init failed");
M5.Display.println("esp now init failed");
}
}
void loop() {
// put your main code here, to run repeatedly:
}
; 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
[env:m5stick-c]
board = m5stick-c
platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream
platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.3-RC1
framework = arduino
lib_deps =
m5stack/M5Unified
# PC(Linux) or ラズパイからUARTでテキストデータを送信するサンプル
import serial
import json
data = [{"aqtalk": "konnichiwa"}, {"motion": "m1"}, {"expression":1}]
ser = serial.Serial('/dev/ttyUSB0', 115200)
send_data = json.dumps(data)
ser.write(send_data.encode('utf-8') + "\n".encode('utf-8'))
ser.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment