Created
September 16, 2020 22:02
-
-
Save dj1711572002/08e9bb5ec0362c630f2706e6d4ee1b4a to your computer and use it in GitHub Desktop.
M5Atomlite ESP-NOW Receive Controler
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 <M5StickC.h> | |
//#include <M5Stack.h> | |
#include <esp_now.h> | |
#include <WiFi.h> | |
esp_now_peer_info_t slave; | |
uint8_t cdata[2]; | |
int t,t_1; | |
int stime; | |
//uint8_t cdata[6]={}; | |
// 送信コールバック | |
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { | |
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 Sent to: "); | |
//Serial.println(macStr); | |
//Serial.print("Last Packet Send Status: "); | |
//Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); | |
} | |
// 受信コールバック | |
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { | |
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.printf("Last Packet Recv from: %s\n", macStr); | |
//Serial.printf("Last Packet Recv Data(%d): ", data_len); | |
//Serial.print("dataLen="); | |
//Serial.println(data_len); | |
for ( int i = 0 ; i < data_len ; i++ ) { | |
Serial.print(char(data[i])); | |
} | |
Serial.println(""); | |
} | |
void setup() { | |
M5.begin(); | |
int t,t_1 ; | |
// ESP-NOW初期化 | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
if (esp_now_init() == ESP_OK) { | |
Serial.println("ESPNow Init Success"); | |
} else { | |
Serial.println("ESPNow Init Failed"); | |
ESP.restart(); | |
} | |
// マルチキャスト用Slave登録 | |
memset(&slave, 0, sizeof(slave)); | |
for (int i = 0; i < 6; ++i) { | |
slave.peer_addr[i] = (uint8_t)0xff; | |
} | |
esp_err_t addStatus = esp_now_add_peer(&slave); | |
if (addStatus == ESP_OK) { | |
// Pair success | |
//Serial.println("Pair success"); | |
} | |
// ESP-NOWコールバック登録 | |
esp_now_register_send_cb(OnDataSent); | |
esp_now_register_recv_cb(OnDataRecv); | |
} | |
void loop() { | |
M5.update(); | |
t=millis(); | |
if(t-t_1>1000) | |
{ | |
t_1=t; | |
stime++; | |
cdata[0] =0x73;//"s" | |
cdata[1] =0x74;//"t" //start command | |
//i_to_char(stime,cdata,3);//n=>cdata[3][4][5][6] | |
//rt0=millis(); | |
esp_err_t result = esp_now_send(slave.peer_addr, cdata, sizeof(cdata)); | |
Serial.printf("============stime=%d===================\n\r",stime); | |
} | |
delay(1); | |
// ボタンを押したら送信 | |
if ( M5.BtnB.wasPressed() ) {//For Atom HomeButton=GPIO39=> M5.BtnB.wasPressed() For M5StickC GPIO37=> M5.BtnA.wasPressed() | |
uint8_t data[2] = {123, 234}; | |
esp_err_t result = esp_now_send(slave.peer_addr, data, sizeof(data)); | |
Serial.print("Send Status: "); | |
if (result == ESP_OK) { | |
Serial.println("Success"); | |
} else if (result == ESP_ERR_ESPNOW_NOT_INIT) { | |
Serial.println("ESPNOW not Init."); | |
} else if (result == ESP_ERR_ESPNOW_ARG) { | |
Serial.println("Invalid Argument"); | |
} else if (result == ESP_ERR_ESPNOW_INTERNAL) { | |
Serial.println("Internal Error"); | |
} else if (result == ESP_ERR_ESPNOW_NO_MEM) { | |
Serial.println("ESP_ERR_ESPNOW_NO_MEM"); | |
} else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { | |
Serial.println("Peer not found."); | |
} else { | |
Serial.println("Not sure what happened"); | |
} | |
} | |
delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment