Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SCP System_M5StickC-LT1167_ESP-NOW_dataSend
#include <M5StickC.h>
#include <BluetoothSerial.h>
#include <esp_now.h>
#include <WiFi.h>
//====IMU=============
float accX = 0.0F;
float accY = 0.0F;
float accZ = 0.0F;
int PINZ = 36;
int mV[100];
int mVMAsum=0;
int MA=10;
int mVz;
float Fz=0,Fzt=0;;
String iFz ;
float mVsum=0,mVave=0,Fzave=0;
//============================================================================================
//=========================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 Variables==================
esp_now_peer_info_t slave; //slave peer定義
uint8_t data[100];//100byte 4*value0,value1,value2,value3 //送信データ配列
char cdata[100];//100byte 変換用文字配列
String head;
String str0,str1,str2,str3,str4,str5,str6,str7,strall;//変換用STRING
volatile int comst=0;
volatile int comtr=0;
volatile int rt1=0;
volatile int mtime;
volatile int stime=0;
int i,j;
int t,t_1,t0;
int dnum=0;
volatile int inum=0;//1sec data couter
volatile int nnum=0;// Total data counter
//=============================================
// 送信コールバック
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
inum=0;
rt1=micros();
//Serial.print("********stRcv=");
//Serial.println(millis());
}
if (data[0] == 0x54 && data[1] == 0x52) //when data="TR" TareReset
{
}
// 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 taring()
{
mVsum=0;
for (i=0;i<10;i++)
{
mVsum=mVsum+analogRead(PINZ);
}
mVave=mVsum/10;
Fzave==5.540337*mVave-10190.2;
Serial.print("TATRED mVave,Fzave=");
Serial.print(mVave);
Serial.print(",");
Serial.println(Fzave);
}
//===========================================================================================
//===========================================================================================
//===========================================================================================
void setup() {
M5.begin();
//------------------------------------------------
//--------- 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;
//for (int i = 0; i < 6; ++i) {//for broad cast
// 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.IMU.Init();
M5.Lcd.setRotation( 3 );
M5.Lcd.setTextSize(3);
M5.Lcd.setTextColor(0xFFE0,BLACK);//Yellow
pinMode(PINZ, ANALOG);
mVave=0;
mVsum=0;
taring();
}
void loop() {
i++;
//==== Measurement===================================
//================IMU======================
M5.IMU.getAccelData(&accX,&accY,&accZ);
//=========================================
mVz=analogRead(PINZ);
mV[i%MA]=mVz;
//==========Moving Average ================
mVMAsum=0;
for(j=0;j<MA;j++)
{
mVMAsum=mVMAsum+mV[j];
}
mVz=int(mVMAsum/MA);
//=======================================
Fz=5.540337*mVz-10190.2 ;
Fzt=5.540337*(mVz-mVave);
//==================================================
if(comst==1 )
{
M5.Lcd.fillScreen(BLACK);
iFz=String(int(Fz))+"gf";
M5.Lcd.setCursor(0, 10);
M5.Lcd.println(iFz);
}
//===============10msec ESP-Now SEND==============
t=micros();
if(t-t_1>12500)
{
inum++;//1sec data counter
nnum++;//Total data counter
t_1=t;
t0=micros();
//=====Send Data prepare========
head="B,";
str0=String(inum);
str1=String(Fzt);
str2=String(accX*1000);
str3=String(accY*1000);
str4=String(accZ*1000);
str5=String( t0-rt1);
strall=head+str0+","+str1 + "," + str2 + "," + str3 + "," + str4 + "," + str5 +"\n\r";
dnum=strall.length();
Serial.print(strall);
strall.toCharArray(cdata,dnum);//String strall to CharArray cdata[dnum]
for (j=0;j<dnum;j++)
{
data[j]=(uint8_t)cdata[j];
}
esp_err_t result = esp_now_send(slave.peer_addr, data, dnum);
comst=0;
}
//======================================================================
//======================================================================
// 電源ボタンの状態取得(一度しか0以外のステータスは取得できない)
int axpButton = M5.Axp.GetBtnPress();
if ( axpButton == 1 ) {
// 1秒以上電源ボタンを押している
Serial.println("M5.Axp.GetBtnPress() == 1");
}
if ( axpButton == 2 ) {
// 1秒未満電源ボタンを押して離した TARED
//
mVsum=0;
for (i=0;i<10;i++)
{
mVsum=mVsum+analogRead(PINZ);
}
mVave=mVsum/10;
Fzave==5.540337*mVave-10190.2;
Serial.print("TATRED mVave,Fzave=");
Serial.print(mVave);
Serial.print(",");
Serial.println(Fzave);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.