I’m trying to send a data in a struct using ESP NOW and am stuck because the variable is saying it is not declared in the scope of the function. I’m just struck on how to fix this. I modified the original example code from the ESP NOW two way communication to call the send data function outside the loop and dont understand why it is throwing this error in this other case.
The code is trying to send the clock value of the main sensor to the connected peers to calculate the latency currently. The function giving me trouble is the sensorSync function when trying to send the struct.
The function itself is:
void sensorSync(){
const uint8_t *peer_addr = peers[peerIndex].peer_addr; //saving peer address to local var
if(!syncStatus[peerIndex]){ //is the current peer synced?
sendData.mSentClk = millis(); //saving the current main sensor clock
esp_err_t result = esp_now_send(peer_addr, (uint8_t*) $sendData, sizeof(sendData)); //sending data
if (result == ESP_OK){
Serial.println("Success");
}
}
else{
if(sendData.packet == 10){
esp_err_t result = esp_now_send(peer_addr, (uint8_t*) $sendData, sizeof(sendData)); //sending data
if (result == ESP_OK){
Serial.println("Success sending...");
}
}
else{
Serial.println("Something went wrong, sensor set as synced not at 10 packets.");
Serial.print("Peer Count: ");
Serial.println(peerCnt);
Serial.print("Current Index: ");
Serial.println(peerIndex);
Serial.print("Current Packet: ");
Serial.println(sendData.packet);
}
}
}
The entire program is this:
//Wifi Lib
#include <WiFi.h>
#include <esp_now.h>
#define NUMPEERS 20 //max number of paired devices at one time
#define CHANNEL 1 //channel to send info
#define PRINTSCANRESULTS 0
//the two different states for the syncing process, using a enum for readability
enum syncingState : uint8_t {
SCANNING, SYNCING
};
//The struct of the data being sent to each esp for the syncing process
typedef struct sensorInfo {
unsigned long int mSentClk; //the clock value that is sent from the main sensor
unsigned long int pClk; //the peer clock
uint8_t packet = 0; //the number of packets sent
bool synced = false; //the sync status of the peer
} sensorInfo; //refer to the struct as sensorInfo
//defining vars
syncingState currSyncStatus = SCANNING; //setting Sync Status state
esp_now_peer_info_t peers[NUMPEERS] = {}; //esp now struct to save registered peer info
int peerCnt = 0; //Number of current peers found
//syncing loop vars
uint8_t peerIndex = 0, paired = 0, fpaired = 0; //index of current peer, # of paired and # of failed pairs
bool pairing = false; //is a peer being paired currently?
unsigned long int startPairingTime; //start of the pairing time
sensorInfo recvData; //where the data recived is stored
sensorInfo sendData; //where the data sent is stored
bool syncStatus[NUMPEERS] = {1}; //sync status of the peers
unsigned long int latency[NUMPEERS][10]; //latency of the pings
//start of program
void setup(){
Serial.begin(115200); //start the serial port comm
WiFi.mode(WIFI_STA); //setting device into station mode
configDeviceAP(); //configure device
InitESPNow(); //initialize ESP NOW
esp_now_register_send_cb(onDataSent); //Registering callback function when data is sent
esp_now_register_recv_cb(onDataRecv); //Registering callback funtion when data is recv
}
void loop(){
switch(currSyncStatus){
case SCANNING :
scanForPeers();
if(peerCnt > 0){
managePeer();
currSyncStatus = SYNCING;
}
delay(1000);
break;
case SYNCING :
if(peerCnt > peerIndex){
if(pairing){
if(syncStatus[peerIndex]){
paired++;
peerIndex++;
pairing = false;
}
if((millis() - startPairingTime) > 15000){
fpaired++;
peerIndex++;
pairing = false;
}
}
else{
pairing = true;
startPairingTime = millis();
if (peerIndex == 0){
Serial.println("Starting to Pair");
}
sensorSync();
}
}
else{
Serial.println("Pairing is complete.");
Serial.print("Attempted Pairs: ");
Serial.println(paired + fpaired);
Serial.print("Successful Pairs: ");
Serial.println(paired);
Serial.print("Failed Pairs: ");
Serial.println(fpaired);
//resetting everything (maybe a memory reset)
peerIndex = 0;
paired = 0;
fpaired = 0;
for(int i = 0; i<peerCnt; i++){
syncStatus[i] = true;
}
}
break;
}
}
void configDeviceAP() {
String Prefix = "Main:"; //setting the prefix of the device
String Mac = WiFi.macAddress();
String SSID = Prefix + Mac;
String Password = "123456789";
bool result = WiFi.softAP(SSID.c_str(), Password.c_str(), CHANNEL, 0);
if (!result) {
Serial.println("AP Config failed.");
} else {
Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID));
}
}
void InitESPNow() {
WiFi.disconnect();
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
else {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
}
void sensorSync(){
const uint8_t *peer_addr = peers[peerIndex].peer_addr; //saving peer address to local var
if(!syncStatus[peerIndex]){ //is the current peer synced?
sendData.mSentClk = millis(); //saving the current main sensor clock
esp_err_t result = esp_now_send(peer_addr, (uint8_t*) $sendData, sizeof(sendData)); //sending data
if (result == ESP_OK){
Serial.println("Success");
}
}
else{
if(sendData.packet == 10){
esp_err_t result = esp_now_send(peer_addr, (uint8_t*) $sendData, sizeof(sendData)); //sending data
if (result == ESP_OK){
Serial.println("Success sending...");
}
}
else{
Serial.println("Something went wrong, sensor set as synced not at 10 packets.");
Serial.print("Peer Count: ");
Serial.println(peerCnt);
Serial.print("Current Index: ");
Serial.println(peerIndex);
Serial.print("Current Packet: ");
Serial.println(sendData.packet);
}
}
}
void onDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int data_len){
unsigned long int recTime = millis();
memcpy(&recvData, incomingData, sizeof(recvData));
if(sendData.packet == recvData.packet + 1 && sendData.mSentClk == recvData.mSentClk){
latency[peerIndex][recvData.packet] = recTime - sendData.mSentClk;
Serial.print("Ping number ");
Serial.print(sendData.packet);
Serial.print(" : ");
Serial.print(latency[peerIndex][recvData.packet]);
Serial.println(" milliseconds");
if(sendData.packet == 10){
sendData.synced = true;
}
sensorSync();
}
else{
Serial.println("Something went wrong!");
}
}
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status){
if(status == ESP_NOW_SEND_SUCCESS){
sendData.packet++;
syncStatus[peerIndex] = true;
}
}
void managePeer() {
if (peerCnt > 0) {
for (int i = 0; i < peerCnt; i++) {
Serial.print("Processing: ");
for (int ii = 0; ii < 6; ++ii ) {
Serial.print((uint8_t) peers[i].peer_addr[ii], HEX);
if (ii != 5) Serial.print(":");
}
Serial.print(" Status: ");
// check if the peer exists
bool exists = esp_now_is_peer_exist(peers[i].peer_addr);
if (exists) {
// Peer already paired.
Serial.println("Already Paired");
} else {
// Peer not paired, attempt pair
esp_err_t addStatus = esp_now_add_peer(&peers[i]);
if (addStatus == ESP_OK) {
// Pair success
Serial.println("Pair success");
//create sensorSync struct
} else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) {
// How did we get so far!!
Serial.println("ESPNOW Not Init");
} else if (addStatus == ESP_ERR_ESPNOW_ARG) {
Serial.println("Add Peer - Invalid Argument");
} else if (addStatus == ESP_ERR_ESPNOW_FULL) {
Serial.println("Peer list full");
} else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) {
Serial.println("Out of memory");
} else if (addStatus == ESP_ERR_ESPNOW_EXIST) {
Serial.println("Peer Exists");
} else {
Serial.println("Not sure what happened");
}
delay(100);
}
}
} else {
// No peer found to process
Serial.println("No Peers found to process");
}
}
void scanForPeers() {
int8_t scanResults = WiFi.scanNetworks();
//reset peers
memset(peers, 0, sizeof(peers));
peerCnt = 0;
Serial.println("");
if (scanResults == 0) {
Serial.println("No WiFi devices in AP Mode found");
} else {
Serial.print("Found "); Serial.print(scanResults); Serial.println(" devices ");
for (int i = 0; i < scanResults; ++i) {
// Print SSID and RSSI for each device found
String SSID = WiFi.SSID(i);
int32_t RSSI = WiFi.RSSI(i);
String BSSIDstr = WiFi.BSSIDstr(i);
if (PRINTSCANRESULTS) {
Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println("");
}
delay(10);
// Check if the current device starts with `Peer`
if (SSID.indexOf("Peer") == 0) {
// SSID of interest
Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println("");
// Get BSSID => Mac Address of the Slave
int mac[6];
if ( 6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) {
for (int ii = 0; ii < 6; ++ii ) {
peers[peerCnt].peer_addr[ii] = (uint8_t) mac[ii];
}
}
peers[peerCnt].channel = CHANNEL; // pick a channel
peers[peerCnt].encrypt = 0; // no encryption
syncStatus[peerCnt] = false;
peerCnt++;
}
}
}
}
This is resulting in an error:
In function 'void sensorSync()': Test:133:59: error: '$sendData' was not declared in this scope esp_err_t result = esp_now_send(peer_addr, (uint8_t*) $sendData, sizeof(sendData)); //sending data ^ Test:140:61: error: '$sendData' was not declared in this scope esp_err_t result = esp_now_send(peer_addr, (uint8_t*) $sendData, sizeof(sendData)); //sending data ^ exit status 1 '$sendData' was not declared in this scope
I’ve tried passing in a variable in sensorSync and creating a temp variable to send but it still says it is not declared in the scope. I feel like this has to do with ‘$’ symbol but couldn’t find too much googling it.
Using the sample code from the ESP NOW course allows me to create a function that calls the send data function