Created
December 28, 2020 19:42
-
-
Save dj1711572002/8d78245a12a14b09b62e1ec0145feb8d to your computer and use it in GitHub Desktop.
M5Stack-SKI_TURN_RTK_MovingBase_Logger_Pgm_Nowdebugging
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
//M5StackBasic_SKI_F9P-MB_PC-Slave_Rcv_rev0111.ino | |
#include <M5Stack.h> | |
//#include <M5StickC.h> | |
#include <esp_now.h> | |
#include <WiFi.h> | |
#include "time.h" | |
//===DEF:Receive data ======= | |
int idata[172];//uint8_t=>int | |
//char hexdata[1000];//int =>HEX strings | |
String datahex[172]; | |
String sdstr;//HEXStrings Array=>String | |
char buf[1000]; | |
//===DEF:WiFi==================== | |
const char* ssid = "e"; | |
const char* password = "21311610"; | |
//const char* ssid = "30F772C87B3E-2G"; | |
//const char* password = "2215094594340"; | |
//===DEF:NTP Def==================== | |
const char* ntpServer = "ntp.jst.mfeed.ad.jp"; | |
const long gmtOffset_sec = 9 * 3600; | |
const int daylightOffset_sec = 0; | |
static uint8_t conv2d(const char* p); // Forward declaration needed for IDE 1.6.x | |
uint8_t hh = conv2d(__TIME__), mm = conv2d(__TIME__ + 3), ss = conv2d(__TIME__ + 6); // Get H, M, S from compile time | |
byte xcolon = 0, xsecs = 0; | |
//===DEF:SD FILE============ | |
File file; | |
char* fname;//保存するファイル名 | |
String timeStr; | |
int logFlag=0; | |
//===DEF:SD CARD WRITE=================== | |
void writeData(char *paramStr) { | |
// SDカードへの書き込み処理(ファイル追加モード) | |
// SD.beginはM5.begin内で処理されているので不要 | |
file = SD.open(fname, FILE_APPEND); | |
file.println(paramStr); | |
//file.close(); | |
} | |
esp_now_peer_info_t slave; | |
// 送信コールバック | |
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"); | |
/* | |
// 画面にも描画 | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.setCursor(0, 0); | |
M5.Lcd.print("Last Packet Sent to: \n "); | |
M5.Lcd.println(macStr); | |
M5.Lcd.print("Last Packet Send Status: \n "); | |
M5.Lcd.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 Time=%d,Datalength=%d \n\r", millis(),data_len); | |
M5.Lcd.setCursor(1, 50); | |
sdstr=""; | |
for ( int i = 0 ; i < data_len ; i++ ) {//------------------------------------------- | |
datahex[i]=String(data[i],HEX); | |
//sdstr=sdstr+","+datahex[i]; NG? | |
if (logFlag==1) | |
{ | |
sdstr=","+datahex[i];//OK | |
file.print(sdstr); | |
} | |
Serial.printf("datahex[%d]=%s,data=%x,sdstr=%s\n\r",i,datahex[i],data[i],sdstr); | |
//Serial.write(data[i]); | |
//Serial.print(data[i],HEX); | |
// int len=sprintf(buf,"%x",data[i]);//uint8_t => char Array | |
// Serial.printf("data=%x,hexdata=%s\n\r",data[i],buf); | |
// if (i < data_len-1) | |
// { | |
//Serial.print(','); | |
//} | |
//---172byte finished------------ | |
// if((i+1) % 172 ==0) | |
// { | |
// Serial.println(); | |
//int len=sprintf(buf,"%x",data[i]);//uint8_t => char Array | |
// | |
// M5.Lcd.setTextSize(2); | |
//M5.Lcd.print(data[i],HEX); | |
//M5.Lcd.fillScreen(BLACK); | |
//M5.Lcd.setCursor(1, 50); | |
// } | |
//Serial.printf("data[%d]=%x\n\r",i,data[i]); | |
} //172 byte Ascii Transform-------------------------------------------------------- | |
//strhex=buf; | |
// Serial.printf("strhex=%s\n\r",strhex); | |
if (logFlag==1) | |
{ | |
file.println(); | |
} | |
//===data Log to SD Card============================================= | |
// file.println(sdStr); | |
}//Ond Data Rcv | |
//===FUNCTION:NTP GET========================== | |
void NTPget() | |
{ | |
M5.Lcd.setCursor(1, 1); | |
M5.Lcd.print("***Set Pocket WIFI***"); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println("CONNECTED"); | |
//init and get the time | |
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); | |
struct tm timeinfo; | |
if (!getLocalTime(&timeinfo)) | |
{ | |
Serial.println("Failed to obtain time"); | |
return; | |
} | |
hh = timeinfo.tm_hour; | |
mm = timeinfo.tm_min; | |
ss = timeinfo.tm_sec; | |
timeStr="/"+String(timeinfo.tm_mon)+String(timeinfo.tm_mday)+"_"+String(hh)+"_"+String(mm)+"_"+String(ss)+".csv"; | |
Serial.println(timeStr); | |
M5.Lcd.setCursor(1,20); | |
M5.Lcd.print("LogFile="); | |
M5.Lcd.print(timeStr+"\n"); | |
//disconnect WiFi as it's no longer needed | |
WiFi.disconnect(true); | |
WiFi.mode(WIFI_OFF); | |
WiFi.disconnect(); | |
//delay(3000); | |
} | |
void setup() { | |
M5.begin(); | |
M5.Lcd.setTextSize(2); | |
M5.Lcd.setTextColor(YELLOW); | |
//NTP get for FileName--------------------- | |
NTPget();//NTP Access Time Stamp Get | |
//*******************SD FILE OPEN ********************************************************* | |
file = SD.open(timeStr, FILE_APPEND); | |
// ESP-NOW初期化 | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
if (esp_now_init() == ESP_OK) { | |
Serial.println("ESPNow Init Success"); | |
M5.Lcd.print("ESPNow Init Success\n"); | |
} else { | |
Serial.println("ESPNow Init Failed"); | |
M5.Lcd.print("ESPNow Init Failed\n"); | |
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); | |
M5.Lcd.setCursor(230, 200);//C Button | |
M5.Lcd.print("LOG"); | |
} | |
void loop() { | |
M5.update(); | |
//button C LOG START/STOP Toggling | |
if ( M5.BtnC.wasPressed() ) | |
{// buttonA=>M5StickC buttonB=>M5Atom lite | |
if (logFlag==0) | |
{ | |
logFlag=1;//Logging Start | |
M5.Lcd.setCursor(50, 160);//C Button | |
M5.Lcd.setTextSize(3); | |
M5.Lcd.print("---START---"); | |
} | |
else if (logFlag==1) | |
{ | |
logFlag=0;//LOgging Stop | |
M5.Lcd.setCursor(50, 160);//C Button | |
M5.Lcd.setTextSize(3); | |
M5.Lcd.print("---STOP---"); | |
file.close(); | |
} | |
} | |
/* | |
// ボタンを押したら送信 | |
if ( M5.BtnB.wasPressed() ) {// buttonA=>M5StickC buttonB=>M5Atom lite | |
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); | |
*/ | |
}// end loop | |
// ======================================================= | |
//===Function to extract numbers from compile time string | |
//======================================================== | |
static uint8_t conv2d(const char* p) { | |
uint8_t v = 0; | |
if ('0' <= *p && *p <= '9') | |
v = *p - '0'; | |
return 10 * v + *++p - '0'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment