I'm doing a project using an ESP32 TTGO with LoRa, the Max485 and a WELLPRO ADAM 8082 for Modbus communication. I'm trying to use the ESP32 as a master to and the ADAM as a slave. I'm also using Node-RED to send the input signals (reading the digital inputs on the ADAM or sending signals to the digital outputs). Whenever I send a signal there's no problem, the ADAM receives it correctly and sends the answer. But when my ESP32 receives the answer, I get the following error:
Stack smashing protect failure!
abort() was called at PC 0x400dc860 on core 1
I tried debugging my program and the error happens when my program comes back from the function that has my reading modbus loop. I'm pretty new to Arduino, so I don't really understand what could be causing this stack overflow.
Here's my code.
My global variables:
unsigned char ucSt[N_MAX];
int delayTime;
HardwareSerial modbusData(2);
WiFiClientSecure espClient;
PubSubClient client(espClient);
boolean bWifiConnected = false;
const byte button = PRG_BUTTON;
Modbus reading with loop:
void vReadingModBus()
{
int i, nCmpt = 0;
unsigned int uiCRC;
while (modbusData.available() > 0)
{
Serial.print("nCmpt: ");
Serial.println(nCmpt);
ucSt[nCmpt] = (unsigned char)modbusData.read();
nCmpt++;
delay(delayTime - 2);
}
if (nCmpt)
{
Serial.print("He llegit aquests ");
Serial.print(nCmpt);
Serial.println(" bytes: ");
char szResposta[N_MAX] = "", szByte[3];
for (i = 0; i < nCmpt; i++)
{
sprintf(szByte, "%02X", ucSt[i]);
strcat(szResposta, szByte);
Serial.print(ucSt[i], HEX);
Serial.print(" ");
}
Serial.println();
client.publish(TEMA_RESPOSTA_MODBUS, szResposta);
uiCRC = uiModRTU_CRC(ucSt, nCmpt - 2);
if ((byte)(uiCRC >> 8) == ucSt[nCmpt - 1] && (byte)(uiCRC & 0xFF) == ucSt[nCmpt - 2])
{
Serial.println("Trama amb CRC correcte");
}
else
{
Serial.println("Trama amb CRC incorrecte");
}
}
nCmpt = 0;
}
Modbus transmission:
void vModbusTx(unsigned char *uc, int nLen)
{
int i;
Serial.print("Enviat: ");
for (i = 0; i < nLen; i++)
{
Serial.print(uc[i], HEX);
Serial.print(" ");
}
Serial.println();
vModeTxRxRS485(RS485Transmit);
delay(1);
for (i = 0; i < nLen; i++)
{
modbusData.write(uc[i]);
}
delay(delayTime);
vModeTxRxRS485(RS485Receive);
}
My setup:
void setup() {
Serial.begin(115200); // U0_RXD:GPIO3, U0_TXD:GPIO1 (UART0)
modbusData.begin(9600); // U2_RXD:GPIO16, U2_TXD:GPIO17 (UART2)
pinMode(PRG_BUTTON, INPUT_PULLUP);
pinMode(GPIO_R2, INPUT); // 39
pinMode(SSerialTxControl, OUTPUT); // hardwired to GPIO_R2
pinMode(LED, OUTPUT);
vModeTxRxRS485(RS485Receive); // RS485Receive is a 0, and vModeTxRxRS485 controls the DE/RE bits for the Max485
delayTime = 0;
//Wire.begin(I2C_SCL_OLED);
delay(1000); // give me time to bring up serial monitor
}
My loop:
void loop() {
boolean bButtonState = !digitalRead(button);
static boolean bLastButtonState = bButtonState;
// MODBUS READING LOOP
vReadingModBus();
if (!client.connected())
{
reconnect();
}
// BUTTON STATE CHECK
if (bButtonState != bLastButtonState)
{
bLastButtonState = bButtonState;
vDelayESP(50);
if (!bButtonState)
client.publish(TEMA_PUBLICA_ESTAT_BOTO, "UNPRESSED");
else
client.publish(TEMA_PUBLICA_ESTAT_BOTO, "PRESSED");
}
client.loop();
}
And finally here's an example of my program running and giving the error mentioned:
Connecting to WiFi network
Waiting for WIFI connection...
..............................................
Attempting MQTTS connection...connected
Message arrived [/clot/dam/biel/esp32/escriuModbus] 010F000000080111
Petició a ModBus:
010F000000080111
Mida: 8
ucSt[0] = 1
ucSt[1] = F
ucSt[2] = 0
ucSt[3] = 0
ucSt[4] = 0
ucSt[5] = 8
ucSt[6] = 1
ucSt[7] = 11
Enviat: 1 F 0 0 0 8 1 11 3E 99
He llegit aquests 8 bytes:
1 F 0 0 0 8 54 D
Trama amb CRC correcte
Stack smashing protect failure!
abort() was called at PC 0x400dc7d8 on core 1
Backtrace: 0x4008c800:0x3ffb1ef0 0x4008ca31:0x3ffb1f10 0x400dc7d8:0x3ffb1f30 0x400d1eed:0x3ffb1f50 0x400d23c0:0x3ffb1f90 0x400d513d:0x3ffb1fb0 0x4008879d:0x3ffb1fd0
Rebooting...
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:8424
ho 0 tail 12 room 4
load:0x40080400,len:5868
entry 0x4008069c