Created
September 16, 2020 21:56
M5ATOMlite_ESP-NOW_SPP-Module Input ASCII+\n\r data Serial2 Pin:RX23 TX22
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 <BluetoothSerial.h> | |
#include <esp_now.h> | |
#include <WiFi.h> | |
int startPin=19; | |
int t,t_1; | |
int x=1; | |
int i=0; | |
int stime; | |
int rt1; | |
//uint8_t data; | |
String sdata; | |
//============================================================================================ | |
//=========================ESP-NOW Functions================================================== | |
//==============slave:PC master:SCP,AD7194================================================================= | |
// マルチキャスト用 MAC登録 | |
// PC M5ATOM COM7=D8:A0:1D:50:54:E8 | |
// SCP 1ch M5Stick COM13=D8:A0:1D:58:56:CC | |
// AD7194 M5ATOM COM9=24:0A:C4:A5:30:DC | |
esp_now_peer_info_t slave; //slave peer定義 | |
uint8_t data[100];//12byte 4*value0,value1,value2,value3 //送信データ配列 | |
char cdata[100]; | |
//char *cdata;//シリアル受信したstring | |
//uint8_t *data;//シリアル受信stiringを文字列配列にしてそれをuint8_t配列に変換する、ポインタで定義しておく 配列数が決まってからnewで再定義 | |
int dnum;//data配列の要素数 | |
volatile int comst=0; | |
volatile int comtr=0; | |
// 送信コールバック | |
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]); | |
if (data[0] == 0x73 && data[1] == 0x74) //when data="ST" data_sending_STart | |
{ | |
digitalWrite(startPin,HIGH); //Timig RiseEdge | |
comst=1; | |
stime=data[3]|data[4]<<8|data[5]<<16|data[6]<<24; //Left Shift OR | |
rt1=millis(); | |
// Serial.print("********stRcv="); | |
//Serial.println(millis()); | |
} | |
/* | |
if (data[0] == 0x54 && data[1] == 0x52) //when data="TR" TimerReset | |
{ | |
comtr=1; | |
} | |
// Serial.print( macStr); | |
//Serial.print(","); | |
//Serial.print(data_len); | |
// for ( int i = 0 ; i < data_len ; i++ ) { | |
// Serial.print(data[i]); | |
// Serial.print(" "); | |
// } | |
//Serial.println(""); | |
*/ | |
} | |
//+++++++++++++++i_to_char+++++++++++++++++ | |
// i=IntegerValueData,*d=Array pointer, n=Array start No | |
void i_to_char(int i, uint8_t *d, int n) | |
{ | |
d[n] = i & 0x000000ff; | |
d[n + 1] = (i & 0x0000ff00) >> 8; | |
d[n + 2] = (i & 0x00ff0000) >> 16; | |
d[n + 3] = (i & 0xff000000) >> 24; | |
} | |
//======================================================================== | |
void setup() { | |
M5.begin(); | |
pinMode(startPin,OUTPUT);//startPin Start DataSending | |
Serial2.begin(115200,SERIAL_8N1,23,22);//*******(baudrate,config,RX,TX)******* | |
t_1=millis(); | |
//------------------------------------------------ | |
//--------- 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<pnのM5ATOM> address 50:02:91:86:9B:CC | |
memset(&slave, 0, sizeof(slave)); | |
//============M5Stack Slave AP Mac====================== | |
slave.peer_addr[0] = (uint8_t)0x50; | |
slave.peer_addr[1] = (uint8_t)0x02; | |
slave.peer_addr[2] = (uint8_t)0x91; | |
slave.peer_addr[3] = (uint8_t)0x86; | |
slave.peer_addr[4] = (uint8_t)0x9B; | |
slave.peer_addr[5] = (uint8_t)0xCC; | |
esp_err_t addStatus = esp_now_add_peer(&slave); | |
// ESP-NOWコールバック登録 | |
esp_now_register_send_cb(OnDataSent); | |
esp_now_register_recv_cb(OnDataRecv); | |
//-------------------------------------------------- | |
//-------------------------------------------------- | |
} | |
void loop() | |
{ | |
t=millis(); | |
/* if(t-t_1>1000) | |
{ | |
// digitalWrite(startPin,HIGH); //Timig RiseEdge | |
t_1=t; | |
Serial.println("====Reset====="); | |
} | |
*/ | |
//===============Serial2 REceiving================================== | |
if(Serial2.available()>0) | |
{ | |
sdata=Serial2.readStringUntil('\r'); | |
Serial.println(sdata); | |
//digitalWrite(startPin,LOW);//Timing FallEdge | |
//============sdata Trans========================================= | |
dnum=sdata.length(); | |
//Serial.print("dnum="); | |
//Serial.println(dnum); | |
//==========Str to int(uin8_t)========= | |
//data = new uint8_t[dnum+1]; | |
//cdata= new char[dnum]; | |
sdata.toCharArray(cdata,dnum); | |
//========== char to uint8_t atoi============ | |
for (i=0;i<dnum;i++) | |
{ | |
data[i]=(uint8_t)cdata[i]; | |
//Serial.print(char(data[i])); | |
} | |
//data[dnum+1]=0x00;// Array end Null | |
// Serial.println(""); | |
//===================================================================== | |
//=================ESP-NOW Send============================== | |
esp_err_t result = esp_now_send(slave.peer_addr, data, dnum); | |
digitalWrite(startPin,LOW);//Timing FallEdge | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment