I've written some Arduino Code which kept crashing and i couldn't find out why. Then i reduced the code as much as i could in order to see the problem. The reduced code currently doesn't do anything extraordinary except printing some strings over the serial line.
This is the reduced code:
void setup()
{
Serial.begin(9600);
Serial.print("Begin\r\n");
send_wifi_beacon();
}
void loop()
{
delay(1000);
}
void send_wifi_beacon()
{
send_wlan_chip_command("AT+CIFSR");
Serial.print("notreached"); // this part is never reached
send_data_over_network(); // can't remove this line otherwise the crash won't happen which is weird since this is never called
}
void read_response_from_serial()
{
delay(2000);
Serial.print("==-1\r\n"); // crashes here
}
void send_wlan_chip_command(char* command)
{
Serial.print("Sending command to wifi module: ");
Serial.print(command);
read_response_from_serial();
Serial.print("Command response: ");
}
// this function is never called, since the crash happens before the call, but it can't be removed otherwise the crash won't happen
// also it can't be altered then, the crash also will not happen.
void send_data_over_network()
{
char number[3] = "12";
//char number[3] = "";
char length_of_payload[20] = "0123456789012345678"; // 20 digits if assuming 64 bit integer is the maximum this code will ever run on
// char length_of_payload[20] = "";
char *command;
command = malloc(26);
command = "AT+CIPSSEND";
strcat(command, number);
strcat(command, ",");
strcat(command, length_of_payload);
send_wlan_chip_command(command);
free(command);
}
This is the according output:
Begin Sending command to wifi module: AT+CIFSR==Begin
Sending command to wifi module: AT+CIFSR==Begin
Sending command to wifi module: AT+CIFSR==Begin
Sending command to wifi module: AT+CIFSR==Begin
Which means that the arduino keeps crashing and resetting. This goes on forever.
The weird thing is, if I change the following two lines to: (in the"send_data_over_network" function as in example above)
char number[3] = "1"; // changed from => char number[3] = "";
char length_of_payload[20] = "123"; // changed from => char length_of_payload[20] = "0123456789012345678";
The output now changes to :
I call this behaviour weird since it shouldn't have any influence on the result because this function(send_data_over_network) is never called in the crash example from before.
Troubleshooting options i already tried:
- Reinstalling Arduino IDE , i tried with mutliple versions 1.8 , 1.6, 1.4. 1.0
- Different Arduino Boards (Arduino Uno and Arduino Mega)
- Different Laptops (since i thought maybe it's related to power issues of my laptop)
Maybe it's something obvious and i just can't see it, but i already spent +16h debugging, so help would be really appreciated.
