Created
December 26, 2020 01:16
-
-
Save dj1711572002/cd03cba975709b7a56958723d394f78b to your computer and use it in GitHub Desktop.
M5Atom lite F9P MovingBase Output and trasmit by ESP-NOW to PC connected another M5Atom
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
//M5Atom-F9P_MovingBaseSend_Serial2_ESP-NOW_Add_Serial1_rev01.ino | |
#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; | |
String pdata;//NAV-PVT data | |
//============================================================================================ | |
//=========================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,25,21);//*******M5 Atom1-Atom2 EXT(baudrate,config,RX25,TX21)******* | |
Serial2.begin(115200,SERIAL_8N1,33,23);//*******M5 Atom lite-F9P EXT(baudrate,config,RX33,TX23)******* | |
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(Serial1.available()>0)//Recieving NAV-PVT from M5Atom1 | |
{ | |
Serial.print("Serial-1_Recive:"); | |
for(i=0;i<172;i++) | |
{ | |
pdata=Serial1.read(); | |
Serial.print(pdata); | |
Serial.print(","); | |
} | |
*/ | |
Serial.println(); | |
} | |
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(','); | |
// Serial1.print(f9pdata[ci],HEX); | |
// Serial1.print(','); | |
} | |
Serial.println(); | |
//Serial1.println(); | |
t_1=t; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment