salve a tutti, sto giocando da un po con l'esp32 e espnow, ho scoperto da poco la possibilita di poter scegliere su quale core (0 o 1) far eseguire il (o una parte di) codice..
nel mio codice misuro con un hc-sr04, faccio dei calcoli per tirare fuori la misura in metrico o imperiale, la visualizzo in locale e la invio tramite espnow ad un altro esp32 che la visualizza in remoto.
ora, per cercare di velocizzare il piu possibile il tutto, mi chiedo:
è possibile far gestire per esempio la parte di invio e ricezione ad un core diverso da quello che gestisce misura, calcolo e visualizzazione? ha senso?
questi i codici spolpati del superfluo:
tx:
#include <M5Stack.h>
#include <esp_now.h>
#include <WiFi.h>
#define WIFI_CHANNEL 1
esp_now_peer_info_t slave;
uint8_t remoteMac[] = {0x3C, 0x71, 0xBF, 0x45, 0xFD, 0x52};
const uint8_t maxDataFrameSize = 200;
// must match the controller struct
struct __attribute__((packed)) DataStruct {
unsigned long time;
};
DataStruct myData;
const esp_now_peer_info_t *peer = &slave;
uint8_t dataToSend[maxDataFrameSize];
const int trigPin = 22; //GIALLO GROVE
const int echoPin = 21; // BIANCO GROVE
// defines variables
long duration;
int cm;
int feet;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200);
M5.begin();
if (WIFI == 1) {
Serial.print("\r\n\r\n");
WiFi.mode(WIFI_STA);
Serial.println( WiFi.softAPmacAddress() );
WiFi.disconnect();
if (esp_now_init() == ESP_OK)
{
Serial.println("ESP NOW INIT!");
}
else
{
Serial.println("ESP NOW INIT FAILED....");
}
memcpy( &slave.peer_addr, &remoteMac, 6 );
slave.channel = WIFI_CHANNEL;
slave.encrypt = 0;
if ( esp_now_add_peer(peer) == ESP_OK)
{
Serial.println("Added Peer!");
Serial.println();
}
esp_now_register_send_cb(OnDataSent);
}
}
void loop() {
// put your main code here, to run repeatedly:
misura();
M5.update();
visualizza();
M5.update();
sendData();
M5.update();
}
void misura() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
delay(50);
}
void visualizza() {
Serial.print("duration = ");
Serial.println(duration);
}
void sendData() {
if (millis() - lastSentMillis >= sendIntervalMillis) {
lastSentMillis += sendIntervalMillis;
// myData.time = echoTime;
myData.time = duration;
uint8_t bs[sizeof(myData)];
memcpy(bs, &myData, sizeof(myData));
sentMicros = micros();
esp_now_send(NULL, bs, sizeof(myData)); // NULL means send to all peers
Serial.println(" sent data");
// Serial.println();
}
}
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
if (status == ESP_NOW_SEND_SUCCESS) {
Serial.println("RX ACCESO");
StatoRX = 1;
} else {
Serial.println("RX SPENTO");
StatoRX = 0;
}
Serial.println();
}
rx:
#include <M5Stack.h>
#include <esp_now.h>
#include <WiFi.h>
#define WIFI_CHANNEL 1
uint8_t localCustomMac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33};
const byte maxDataFrameSize = 200;
// must match the controller struct
struct __attribute__((packed)) DataStruct {
unsigned long time;
};
DataStruct myData;
void setup() {
M5.begin();
Serial.begin(115200);
Serial.print("\r\n\r\n");
WiFi.mode(WIFI_AP);
Serial.println( WiFi.softAPmacAddress() );
WiFi.disconnect();
if (esp_now_init() == ESP_OK)
{
Serial.println("ESPNow Init Success!");
}
else
{
Serial.println("ESPNow Init Failed....");
}
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
cm = (myData.time) / 29 / 2;
Serial.println(cm);
}
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int data_len)
{
memcpy(&myData, incomingData, sizeof(myData));
Serial.print ("MAC ADDRES = ");
for (byte n = 0; n < 6; n++) {
Serial.print (mac_addr[n], HEX);
}
Serial.println ();
}
grazie