#include "FS.h" |
#include "SD.h" |
#include "SPI.h" |
#include<esp_now.h> |
#include<WiFi.h> |
float incomingAccX, incomingAccY, incomingAccZ; |
void accSD(int16_t,int16_t,int16_t); |
|
typedef struct struct_message{ |
float AccX; |
float AccY; |
float AccZ; |
} struct_message; |
|
struct_message incomingReadings; |
|
// Callback when data is received |
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { |
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings)); |
incomingAccX=incomingReadings.AccX; |
incomingAccY=incomingReadings.AccY; |
incomingAccZ=incomingReadings.AccZ; |
} |
void accSD(const float x , const float y,const float z){ |
appendFile(SD, "/AccX_Wifi.txt",x); |
appendFile_char(SD, "/AccX_Wifi.txt", ","); |
|
appendFile(SD, "/AccY_Wifi.txt", y); |
appendFile_char(SD, "/AccY_Wifi.txt", ","); |
|
appendFile(SD, "/AccZ_Wifi.txt", x); |
appendFile_char(SD, "/AccZ_Wifi.txt", ","); |
|
} |
//void accSD(int16_t x,int16_t y,int16_t z){ |
// appendFile(SD, "/AccX_Wifi.txt",x); |
// appendFile_char(SD, "/AccX_Wifi.txt", ","); |
// |
// appendFile(SD, "/AccY_Wifi.txt", y); |
// appendFile_char(SD, "/AccY_Wifi.txt", ","); |
// |
// appendFile(SD, "/AccZ_Wifi.txt", x); |
// appendFile_char(SD, "/AccZ_Wifi.txt", ","); |
// |
//} |
|
void setup(){ |
Serial.begin(115200); |
WiFi.mode(WIFI_STA); |
// Init ESP-NOW |
if (esp_now_init() != ESP_OK) { |
Serial.println("Error initializing ESP-NOW"); |
return; |
} |
// Register for a callback function that will be called when data is received |
esp_now_register_recv_cb(OnDataRecv); |
if(!SD.begin()){ |
Serial.println("Card Mount Failed"); |
return; |
} |
} |
void loop(){ |
uint8_t cardType = SD.cardType(); |
if(cardType == CARD_NONE){ |
Serial.println("No SD card attached"); |
return; |
} |
appendFile(SD, "/AccX_Wifi.txt",incomingAccX); |
appendFile_char(SD, "/AccX_Wifi.txt", ","); |
|
appendFile(SD, "/AccY_Wifi.txt", incomingAccY); |
appendFile_char(SD, "/AccY_Wifi.txt", ","); |
|
appendFile(SD, "/AccZ_Wifi.txt", incomingAccZ); |
appendFile_char(SD, "/AccZ_Wifi.txt", ","); |
} |
|
|
void appendFile(fs::FS &fs, const char * path, float message){ // changed the argument type to int16_t |
|
File file = fs.open(path, FILE_APPEND); |
if(!file){ |
Serial.println("Failed to open file for appending"); |
return; |
} |
if(file.print(message)){ |
Serial.println("Message appended"); |
} else { |
Serial.println("Append failed"); |
} |
file.close(); |
} |
void appendFile_char(fs::FS &fs, const char * path, const char * message){ // added an 'appendFile_char' function to append ',' as data separators |
Serial.printf("Appending to file: %s\n", path); |
|
File file = fs.open(path, FILE_APPEND); |
if(!file){ |
Serial.println("Failed to open file for appending"); |
return; |
} |
if(file.print(message)){ |
Serial.println("Message appended"); |
} else { |
Serial.println("Append failed"); |
} |
file.close(); |
#include "FS.h" |
#include "SD.h" |
#include "SPI.h" |
#include<esp_now.h> |
#include<WiFi.h> |
#include<Wire.h> |
const int MPU=0x68;//Device address |
float AccX, AccY, AccZ; |
int16_t AccX_temp, AccY_temp, AccZ_temp; |
String success; |
uint8_t broadcastAddress[] = {0xFC, 0xF5, 0xC4, 0x26, 0xF7, 0x7C}; // Receiver's mac address |
|
typedef struct struct_message{ |
float AccX; |
float AccY; |
float AccZ; |
} struct_message; |
|
struct_message MPU6050Readings; |
|
void setup(){ |
Wire.begin(); //To redefine the I2C pins: Wire.begin(SDA,SCL) or Wire.begin(SDA, SCL, Bus_Speed). |
// Wake up the sensor (reset) |
Wire.beginTransmission(MPU); |
Wire.write(0x6B);//Wake up the MPU chip |
Wire.write(0x00); |
Wire.endTransmission(true); |
Wire.beginTransmission(MPU); |
Wire.write(0x1C);//Talk to the ACCEL_CONFIG register (1C hex) |
Wire.write(0x08);//Set the register bits as 00001000 (+/- 4g full scale range) |
Wire.endTransmission(true); |
Serial.begin(115200); // It's fine to use a higher speed other than 9600 but remember to change the rate in your serial monitor |
WiFi.mode(WIFI_STA); |
// Init ESP-NOW |
if (esp_now_init() != ESP_OK) { |
Serial.println("Error initializing ESP-NOW"); |
return; |
} |
// Once ESPNow is successfully Init, we will register for Send CB to get the status of Trasnmitted packet |
// esp_now_register_send_cb(OnDataSent); |
// Register peer |
esp_now_peer_info_t peerInfo; |
memcpy(peerInfo.peer_addr, broadcastAddress, 6); |
peerInfo.channel = 0; |
peerInfo.encrypt = false; |
// Add peer |
if (esp_now_add_peer(&peerInfo) != ESP_OK){ |
Serial.println("Failed to add peer"); |
return; |
} |
} |
void loop(){ |
accRead(); |
MPU6050Readings.AccX=AccX; |
MPU6050Readings.AccY=AccY; |
MPU6050Readings.AccZ=AccZ; |
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &MPU6050Readings, sizeof(MPU6050Readings)); //Send the data. &MPU6050Readings is just a 8-bit character pointer to store the data |
if (result == ESP_OK) { // For debug purpose |
Serial.println("Sent with success"); |
} |
else { |
Serial.println("Error sending the data"); |
} |
Serial.print(MPU6050Readings.AccX); |
Serial.print(" "); |
Serial.print(MPU6050Readings.AccY); |
Serial.print(" "); |
Serial.print(MPU6050Readings.AccY); |
Serial.println(" "); |
} |
void accRead(){ // read the MPU data to ESP32 |
Wire.beginTransmission(MPU); |
Wire.write(0x3B);//Start with register 0x3B |
Wire.endTransmission(false); |
Wire.requestFrom(MPU,6,true);// Read 6 registers total |
AccX_temp=Wire.read()<<8 | Wire.read(); |
AccX=AccX_temp; |
AccY_temp=Wire.read()<<8 | Wire.read(); |
AccY=AccY_temp; |
AccZ_temp=Wire.read()<<8 | Wire.read(); |
AccZ=AccZ_temp; |
Wire.endTransmission(true); |
} |
// Callback when data is sent, for debug purpose |
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { |
Serial.print("\r\nLast Packet Send Status:\t"); |
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); |
if (status ==0){ |
success = "Delivery Success :)"; |
} |
else{ |
success = "Delivery Fail :("; |
} |
//Clean code fore ESP32 SD card slave |
#include "FS.h" |
#include "SD.h" |
#include "SPI.h" |
#include<esp_now.h> |
#include<WiFi.h> |
float incomingAccX, incomingAccY, incomingAccZ; |
void accSD(int16_t,int16_t,int16_t); |
|
typedef struct struct_message{ |
float AccX; |
float AccY; |
float AccZ; |
} struct_message; |
|
struct_message incomingReadings; |
|
// Callback when data is received |
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { |
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings)); |
incomingAccX=incomingReadings.AccX; |
incomingAccY=incomingReadings.AccY; |
incomingAccZ=incomingReadings.AccZ; |
} |
void accSD(const float x , const float y,const float z){ |
appendFile(SD, "/AccX_Wifi.txt",x); |
appendFile_char(SD, "/AccX_Wifi.txt", ","); |
|
appendFile(SD, "/AccY_Wifi.txt", y); |
appendFile_char(SD, "/AccY_Wifi.txt", ","); |
|
appendFile(SD, "/AccZ_Wifi.txt", x); |
appendFile_char(SD, "/AccZ_Wifi.txt", ","); |
|
} |
|
void setup(){ |
WiFi.mode(WIFI_STA); |
// Init ESP-NOW |
if (esp_now_init() != ESP_OK) { |
return; |
} |
// Register for a callback function that will be called when data is received |
esp_now_register_recv_cb(OnDataRecv); |
|
} |
void loop(){ |
uint8_t cardType = SD.cardType(); |
|
appendFile(SD, "/AccX_Wifi.txt",incomingAccX); |
appendFile_char(SD, "/AccX_Wifi.txt", ","); |
|
appendFile(SD, "/AccY_Wifi.txt", incomingAccY); |
appendFile_char(SD, "/AccY_Wifi.txt", ","); |
|
appendFile(SD, "/AccZ_Wifi.txt", incomingAccZ); |
appendFile_char(SD, "/AccZ_Wifi.txt", ","); |
} |
|
|
void appendFile(fs::FS &fs, const char * path, float message){ // changed the argument type to int16_t |
|
File file = fs.open(path, FILE_APPEND); |
|
file.print(message); |
|
file.close(); |
} |
void appendFile_char(fs::FS &fs, const char * path, const char * message){ // added an 'appendFile_char' function to append ',' as data separators |
|
File file = fs.open(path, FILE_APPEND); |
|
file.print(message); |
|
file.close(); |
//Clean code fore ESP32 SD card slave |
#include "FS.h" |
#include "SD.h" |
#include "SPI.h" |
#include<esp_now.h> |
#include<WiFi.h> |
float incomingAccX, incomingAccY, incomingAccZ; |
void accSD(int16_t,int16_t,int16_t); |
|
typedef struct struct_message{ |
float AccX; |
float AccY; |
float AccZ; |
} struct_message; |
|
struct_message incomingReadings; |
|
// Callback when data is received |
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { |
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings)); |
incomingAccX=incomingReadings.AccX; |
incomingAccY=incomingReadings.AccY; |
incomingAccZ=incomingReadings.AccZ; |
} |
void accSD(const float x , const float y,const float z){ |
appendFile(SD, "/AccX_Wifi.txt",x); |
appendFile_char(SD, "/AccX_Wifi.txt", ","); |
|
appendFile(SD, "/AccY_Wifi.txt", y); |
appendFile_char(SD, "/AccY_Wifi.txt", ","); |
|
appendFile(SD, "/AccZ_Wifi.txt", x); |
appendFile_char(SD, "/AccZ_Wifi.txt", ","); |
|
} |
|
void setup(){ |
WiFi.mode(WIFI_STA); |
// Init ESP-NOW |
if (esp_now_init() != ESP_OK) { |
return; |
} |
// Register for a callback function that will be called when data is received |
esp_now_register_recv_cb(OnDataRecv); |
|
} |
void loop(){ |
uint8_t cardType = SD.cardType(); |
|
appendFile(SD, "/AccX_Wifi.txt",incomingAccX); |
appendFile_char(SD, "/AccX_Wifi.txt", ","); |
|
appendFile(SD, "/AccY_Wifi.txt", incomingAccY); |
appendFile_char(SD, "/AccY_Wifi.txt", ","); |
|
appendFile(SD, "/AccZ_Wifi.txt", incomingAccZ); |
appendFile_char(SD, "/AccZ_Wifi.txt", ","); |
} |
|
|
void appendFile(fs::FS &fs, const char * path, float message){ // changed the argument type to int16_t |
|
File file = fs.open(path, FILE_APPEND); |
|
file.print(message); |
|
file.close(); |
} |
void appendFile_char(fs::FS &fs, const char * path, const char * message){ // added an 'appendFile_char' function to append ',' as data separators |
|
File file = fs.open(path, FILE_APPEND); |
|
file.print(message); |
|
file.close(); |