Global variable not updating from function

I have a master esp8266 that sends data in a structure "dataPacket" to this slave esp8266.

when the slave receives an instruction it creates a local structure (packet) and calls method "dataReceived" but when I try to update global variables

bool openWindow;
bool closeWindow;

inside the function, using code

openWindow = packet.openWindow;
closeWindow = packet.closeWindow;

it fails to update. Any ideas why?

#include<ESP8266WiFi.h>
#include<espnow.h>

#define MY_NAME   "SLAVE_BED_WINDOW"

//define global variables 
bool openWindow;
bool closeWindow;

//define structures 
struct __attribute__((packed)) dataPacket {
  bool openWindow;
  bool closeWindow;
};

//Define methods
void dataReceived(uint8_t *senderMac, uint8_t *data, uint8_t dataLength) {
  char macStr[18];
  dataPacket packet;  

  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", senderMac[0], senderMac[1], senderMac[2], senderMac[3], senderMac[4], senderMac[5]);

  openWindow = packet.openWindow;
  closeWindow = packet.closeWindow;
  
  Serial.println();
  Serial.print("Received data from: ");
  Serial.println(macStr);

  memcpy(&packet, data, sizeof(packet));
  Serial.print("Sent Open Window: ");
  Serial.println(packet.openWindow);
  Serial.print("Sent Close Window: ");
  Serial.println(packet.closeWindow);
  Serial.print("Open Window: ");
  Serial.println(openWindow);
  Serial.print("Close Window: ");
  Serial.println(closeWindow);
}

//Setup program 
void setup() {
  pinMode(D4, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(16, OUTPUT);
  digitalWrite(2, LOW);
  Serial.begin(9600);     // initialize serial port

  Serial.println();
  Serial.println();
  Serial.println();
  Serial.print("Initializing...");
  Serial.println(MY_NAME);
  Serial.print("My MAC address is: ");
  Serial.println(WiFi.macAddress());

  WiFi.mode(WIFI_STA);
  WiFi.disconnect();        // we do not want to connect to a WiFi network

  if(esp_now_init() != 0) {
    Serial.println("ESP-NOW initialization failed");
    return;
  }

  esp_now_register_recv_cb(dataReceived);   // this function will get called once all data is sent

  Serial.println("Initialized.");
}

//define program 
void loop() {
 delay(1000);
  
 Serial.println(openWindow);
 if(openWindow == 1) {
    digitalWrite(2, HIGH);
    digitalWrite(16, LOW);
    digitalWrite(D2, LOW);
    Serial.println("opening window");
  } else if(closeWindow == 1) {
    digitalWrite(16, HIGH);
    digitalWrite(2, LOW);
    Serial.println("closing window");
  } else {
    //Serial.println("no change ");
  } 
}

Either use the globally declared variables

or the variables from the struct

in the code, not both. I recommend using the struct as that makes handling the payload easier.

1 Like

Didn't work and I don't think it matters because one is defined struct.openWindow and the other is openWindow

anyways I changed to the following with no success.

//define global variables 
bool openWindow;
bool closeWindow;

//define structures 
struct __attribute__((packed)) dataPacket {
  bool structOpenWindow;
  bool structCloseWindow;
};
dataPacket packet;  

openWindow = packet.structOpenWindow;
closeWindow = packet.structCloseWindow;

was working fine last night

output
image

Move these lines:

1 Like

What on earth do you expect that code to do? You create the packet variable, assign it NO value, so it is filled with whatever random garbage is on the stack, then you attempt to copy the values of two non-existant members (packetOpenWindow and packetCloseWindow) of that struct to the global variables openWindow and closeWindow. I fail to see how that code even compiles, much less how it would do anything useful if it did.

Because it's syntactically correct.

thanks so much. so data had to be in memory first? do you know why?

How could it be any other way? You have to put data into the struct before you copy the struct elements to another variable.

Not when it references non-existant members of a struct...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.