Created
December 18, 2020 08:42
-
-
Save dj1711572002/05decbecff9160b01f000a5b710197dc to your computer and use it in GitHub Desktop.
M5Atom-F9P_MovingBasemode_172ByteOUTPUT Recieved and ESP-NOW Send toSlave M5Atom connected PC
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 j,k; | |
int stime; | |
int rt1; | |
uint8_t f9pHead[4]; | |
uint8_t f9pdata[172]; | |
int count,ci ; | |
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 | |
{ | |
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,32,26);//*******M5 Atom lite Grove(baudrate,config,RX32,TX26)******* | |
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)0x87; | |
slave.peer_addr[4] = (uint8_t)0x21; | |
slave.peer_addr[5] = (uint8_t)0x84; | |
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(); | |
//===============Serial2 REceiving================================== | |
if(Serial2.available()>0) | |
{ | |
for (j=0;j<4;j++) | |
{ | |
f9pdata[j]= Serial2.read(); | |
} | |
if (f9pdata[0]==0xB5 && f9pdata[1]==0x62 && f9pdata[2]==0x01 && f9pdata[3]==0x07) | |
{ | |
//for (j=4;j<172;j++) | |
// { | |
k=4; | |
while(k<172) | |
{ | |
if(Serial2.available()>0 ) | |
{ | |
f9pdata[k]=Serial2.read(); | |
//Serial.printf("f9pdata[%d]=%x\n\r",k,f9pdata[k]); | |
k++; | |
} | |
} | |
} | |
if (k>171) | |
{ | |
k=0; | |
Serial.print(t-t_1,DEC); | |
Serial.print("msec-"); | |
esp_err_t result = esp_now_send(slave.peer_addr, f9pdata, 172); | |
for (ci=0;ci<172;ci++) | |
{ | |
Serial.print(f9pdata[ci],HEX); | |
Serial.print(','); | |
} | |
Serial.println(); | |
t_1=t; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment