B524 and M2 som board showing red light

The code was successfully uploaded initially

// Include Particle Device OS APIs
#include "Particle.h"
#include "ModbusMaster-Particle.h"
#include "JsonParserGeneratorRK.h"

// Test code
SYSTEM_MODE(AUTOMATIC);
SYSTEM_THREAD(ENABLED);

// Show system, cloud connectivity, and application logs over USB

SerialLogHandler logHandler(9600, LOG_LEVEL_WARN, {
    {"app", LOG_LEVEL_TRACE},
    {"system", LOG_LEVEL_INFO}
});

ModbusMaster slave1;

void preTransmission() {
    digitalWrite(D3, 1);
    digitalWrite(D2, 1);
}

void postTransmission() {
    digitalWrite(D3, 0);
    digitalWrite(D2, 0);
}

void idle() {
    delay(100); // in case slave only replies after 10ms
    Particle.process(); // avoids letting the connection close if open
}

void setup() {
    pinMode(D3, OUTPUT);
    pinMode(D2, OUTPUT);
    slave1.begin(7, Serial1); // Use slave ID 7
    slave1.setSpeed(9600, SERIAL_8N1);
    slave1.enableDebug(); // Enable debug logs
    slave1.preTransmission(preTransmission);
    slave1.postTransmission(postTransmission);
    slave1.idle(idle);
}

float conv(int i) {
    uint32_t rawValue = (slave1.getResponseBuffer(i) << 16) | slave1.getResponseBuffer(i + 1);
    float floatValue = *(float*)&rawValue; // Convert raw value to IEEE 754 float
    return floatValue;
}

float readFloatsFromSlave(uint16_t startAddress) {
    uint8_t result = slave1.readInputRegisters(startAddress, 2);

    if (!result) {
        return conv(startAddress);
    } else {
        Log.warn("Modbus read error at address %d: %d", startAddress, result);
        return -1; // Return a default error value if read fails
    }
}

void loop() {
    struct Parameter {
        const char* CorrelationId;
        const char* DisplayName;
        const char* Address;
        float Value;
    };

    // Define parameters with addresses and correlation IDs
    Parameter parameters[] = {
        {"Plants_RVoltage", "L-F1-RV", "30001", readFloatsFromSlave(0)},
        {"PlantLifetimeEnergy", "L-F1-E", "30073", readFloatsFromSlave(74)},
        {"Plants_Frequency", "L-F1-F", "30071", readFloatsFromSlave(70)},
        {"Plants_RPower", "L-F1-RP", "30013", readFloatsFromSlave(12)}        // Add more parameters as needed
    };

// Parameter parameters[] = {
//     {"DG_Run_Hour", "SG-BIO-RH", "30227", 154.28},
//     {"Plants_DG_Power", "SG-BIO-P", "30053", 0},
//     {"Load_MinCurrent", "L-F1-MIA", "30143", 0},
//     {"Plants_DG_Voltage", "SG-BIO-ACV", "30043", 0},
//     {"MPPTS1_Run_Hour_Ch1", "MP1-RH-CH1", "30057", 0},
//     {"Plants_DG_LifeTimeEnergy", "SG-BIO-E", "30073", 2983.95},
//     {"MPPTS1_Current_Ch2", "MP1-DCA-CH2", "30005", 0},
//     {"Plants_Inverter_MPPTS1DCPower", "PG-DD-1P", "30019", 0},
//     {"Plants_BPowerFactor", "L-F1-BPF", "30035", 0.95},
//     {"Plants_RVoltage", "L-F1-RV", "30001", 240.45},
//     {"MPPTS1_Current_Ch3", "MP1-DCA-CH3", "30007", 0},
//     {"MPPTS1_Run_Hour_Ch3", "MP1-RH-CH3", "30061", 0},
//     {"MPPTS1_Energy_Exp_Ch1", "MP1-EE-CH1", "30031", 0},
//     {"MPPTS1_Power_Ch3", "MP1-DCP-CH3", "30015", 0},
//     {"Plants_Inverter_MPPTS1DCVoltage", "PG-DD-1DCV", "30001", 0},
//     {"Plants_BPower", "L-F1-BP", "30017", 0.44},
//     {"Plants_DG_RVoltage", "SG-BIO-RV", "30001", 0},
//     {"Plants_UPS_BattDisEnergy", "ES-PB-DE", "30039", 23234},
//     {"MPPTS1_Current_Ch4", "MP1-DCA-CH4", "30009", 0},
//     {"Plants_UPS_BattCurrent", "ES-PB-DCA", "30003", 8.25},
//     {"Plants_DG_BVoltage", "SG-BIO-BV", "30005", 0},
//     {"MPPTS1_Run_Hour_Ch2", "MP1-RH-CH2", "30059", 79009},
//     {"Battery power", "PG-S-SR", "30011", 2.08},
//     {"Plants_DG_OutputPowerFactor", "SG-BIO-PF", "30063", 1},
//     {"DG_On_Hour", "SG-BIO-ONH", "30229", 15139.38},
//     {"Load_MaxVoltage", "L-F1-MAV", "30133", 271.84},
//     {"Load_MinVoltage", "L-F1-MIV", "30135", 0},
//     {"Batt_No_of_Interuptions", "ES-PB-INT", "30213", 289},
//     {"Batt_Run_Hour", "ES-PB-RH", "30205", 14844.16},
//     {"Batt_On_Hour", "ES-PB-ONH", "30203", 15189.18},
//     {"DG_No_of_Interuptions", "SG-BIO-INT", "30231", 281},
//     {"MPPTS1_Current_Ch1", "MP1-DCA-CH1", "30003", 0},
//     {"PlantLifetimeEnergy", "L-F1-E", "30073", 25155.52},
//     {"Load_On_Hour", "L-F1-RH", "30229", 15130.1},
//     {"Plants_YVoltage", "L-F1-YV", "30003", 240.63},
//     {"Load_Run_Hour", "L-F1-ONH", "30227", 14161.07},
//     {"Load_MaxCurrent", "L-F1-MAA", "30141", 128.99},
//     {"Load_No_of_Interrupts", "L-F1-INT", "30231", 282},
//     {"MPPTS1_Energy_Exp_Ch2", "MP1-EE-CH2", "30033", 0},
//     {"Plants_RPowerFactor", "L-F1-RPF", "30031", 1},
//     {"MPPTS1_Energy_Imp_Ch2", "MP1-IE-CH2", "30025", 0},
//     {"MPPTS1_Power_Ch2", "MP1-DCP-CH2", "30013", 0},
//     {"MPPTS1_Run_Hour_Ch1", "MP1-RH-CH1", "30057", 0},
//     {"Plants_YPower", "L-F1-YP", "30015", 0.51},
//     {"Plants_Frequency", "L-F1-F", "30071", 49.98},
//     {"MPPTS1_Power_Ch4", "MP1-DCP-CH4", "30017", 0},
//     {"MPPTS1_Energy_Imp_Ch1", "MP1-IE-CH1", "30023", 40639},
//     {"Plants_Inverter_MPPTS1Energy", "PG-DD-1E", "30039", 40639},
//     {"Plants_DG_OutputFrequency", "SG-BIO-F", "30071", 0},
//     {"MPPTS1_Power_Ch1", "MP1-DCP-CH1", "30011", 0},
//     {"Plants_DG_YVoltage", "SG-BIO-YV", "30003", 0},
//     {"MPPTS1_Run_Hour_Ch4", "MP1-RH-CH4", "30063", 0},
//     {"Plants_UPS_BattVoltage", "ES-PB-DCV", "30001", 251.94},
//     {"PlantLifetimeExportEnergy", "L-F1-EE", "30075", 0.02},
//     {"Plants_UPS_BattChaEnergy", "ES-PB-CE", "30041", 21966},
//     {"Plants_RPower", "L-F1-RP", "30013", 0}
// };

    // Create JSON object
    JsonWriterStatic<4096> jsonWriter;
    jsonWriter.startObject();
    jsonWriter.insertKeyValue("PublishTimestamp", Time.format(Time.now(), "%Y-%m-%d %H:%M:%S"));

    // Insert Content key and start array
    jsonWriter.insertKeyArray("Content");
    
    jsonWriter.startObject();
    jsonWriter.insertKeyValue("HwId", "Statcon-RTU");

    // Insert Data key and start array
    jsonWriter.insertKeyArray("Data");

    for (auto& param : parameters) {
        // jsonWriter.startObject();
        // jsonWriter.insertKeyValue("HwId", "Statcon-RTU");

        // // Insert Data key and start array
        // jsonWriter.insertKeyArray("Data");
        
        jsonWriter.startObject();
        jsonWriter.insertKeyValue("CorrelationId", param.CorrelationId);
        jsonWriter.insertKeyValue("SourceTimestamp", Time.format(Time.now(), "%Y-%m-%D %H:%M:%S"));

        // Insert Values key and start array
        jsonWriter.insertKeyArray("Values");
        
        jsonWriter.startObject();
        jsonWriter.insertKeyValue("DisplayName", param.DisplayName);
        jsonWriter.insertKeyValue("Address", param.Address);
        jsonWriter.insertKeyValue("Value", String::format("%.2f", param.Value));

        jsonWriter.finishObjectOrArray();  // Close Values object
        jsonWriter.finishObjectOrArray();   // Close Values array
        
        jsonWriter.finishObjectOrArray();  // Close Data object
    }
    jsonWriter.finishObjectOrArray();  // Close individual parameter object

    jsonWriter.finishObjectOrArray();   // Close Content array
    jsonWriter.finishObjectOrArray();  // Close main object
    jsonWriter.finishObjectOrArray();  // Close main object

    // Retrieve the JSON data
    String jsonData = String(jsonWriter.getBuffer());

    // Publish JSON data to the Particle Cloud
    Particle.publish("ModbusData", jsonData, PRIVATE);

    // Delay for the next reading
    delay(30000); // Adjust delay as needed
}

but after making small changes to the values in the code, it is no longer uploading. The device is showing the following error


and device is also following error

. Your assistance is needed to resolve this issue quickly.

By red light, do you mean the status LED is blinking red? What is the LED pattern?

The most common reason for that is a bug in your code that causes a hard fault. In that link, look for Safe Mode which will prevent your code from running and should allow you to flash new code. DFU mode (blinking yellow) can also be used if you are flashing locally from Workbench.

@rickkas7 yes it is the RGB LED. I have also attached the link to the video showcasing the blinking pattern. I think it is Heap error from the document could you confirm from your side. (https://drive.google.com/file/d/1ve4Tz0dB_1LMbTocnMStEXfm9xOJ-gFw/view?usp=drive_link)

I think that's 1 blink, hard fault. But in any case it doesn't really matter as long as you can put the device in safe mode you can flash new code to it.

However you do need to fix the underlying bug in your code that is causing the hard fault, typically using an uninitialized or delete pointer, or overwriting the end of a memory block.

@rickkas7 i have steps mentioned in above docs to shift to safe mode. but currently no led is blinking . I am attaching screen shots for reference. please your swift response is appreciated.


and also when i am flashing basic code


// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);

// Run the application and system concurrently in separate threads
SYSTEM_THREAD(ENABLED);

// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);

// setup() runs once, when the device is first turned on
void setup() {
  // Put initialization like pinMode and begin functions here
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  // The core of your code will likely live here.

  // Example: Publish event to cloud every 10 seconds. Uncomment the next 3 lines to try it!
  // Log.info("Sending Hello World to the cloud!");
  Particle.publish("Hello world!");
  // delay( 10 * 1000 ); // milliseconds and blocking - see docs for more info!
}

it is displaying following msg

If you cannot get the device into safe mode, you can try putting it in DFU mode (blinking yellow) and do Particle: Flash application and Device OS (local) from Workbench instead.

@rickkas7 , I followed your instructions and successfully put the B524 into DFU mode. I have also successfully uploaded the following code to the B524.

/* 
 * Project myProject
 * Author: Your Name
 * Date: 
 * For comprehensive documentation and examples, please visit:
 * https://docs.particle.io/firmware/best-practices/firmware-template/
 */

// Include Particle Device OS APIs
#include "Particle.h"

// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);

// Run the application and system concurrently in separate threads
SYSTEM_THREAD(ENABLED);

// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);

// setup() runs once, when the device is first turned on
void setup() {
  // Put initialization like pinMode and begin functions here
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  // The core of your code will likely live here.

  // Example: Publish event to cloud every 10 seconds. Uncomment the next 3 lines to try it!
  Log.info("Sending Hello World to the cloud!");
  Particle.publish("Hello world!");
  delay( 10 * 1000 ); // milliseconds and blocking - see docs for more info!
}


However, after that, it stopped blinking, and now I'm unable to upload any code to it. How should we proceed from here.

Do you have a battery connected to the eval board? There does not appear to be one in the picture.

The B524 cannot be powered by USB alone and use cellular reliably. It requires a LiPo battery or an external 2A 9-12V DC power supply connected to the barrel connector.

@rickkas7 , when I provided 12V power and attempted to upload the code again, it started blinking green, but data wasn’t reaching the Particle Cloud. After some time, it began blinking red again. Now, when I try to change the mode, it keeps switching back to red

When the device is blinking green it's still connecting to the Particle cloud, so there is no expectation you'd see any data yet.

If you're still going into SOS mode you haven't fixed the bug in your code that is causing it yet.

@rickkas its working thanks for your support. its working now. i still have doubts i have this code

#include "Particle.h"
#include "ModbusMaster-Particle.h"
#include "JsonParserGeneratorRK.h"

// Test code
SYSTEM_MODE(AUTOMATIC);
SYSTEM_THREAD(ENABLED);

SerialLogHandler logHandler(9600, LOG_LEVEL_WARN, {
    {"app", LOG_LEVEL_TRACE},
    {"system", LOG_LEVEL_INFO}
});

// Define Modbus slave instances
ModbusMaster sl[7];

void pr() {
    digitalWrite(D3, 1);
    digitalWrite(D2, 1);
}

void po() {
    digitalWrite(D3, 0);
    digitalWrite(D2, 0);
}

void id() {
    delay(100);
    Particle.process();
}

void setup() {
    pinMode(D3, OUTPUT);
    pinMode(D2, OUTPUT);

    int si[] = {1, 2, 3, 4, 7, 8, 9};

    for (int i = 0; i < 7; i++) {
        sl[i].begin(si[i], Serial1);
        sl[i].setSpeed(9600, SERIAL_8N1);
        sl[i].enableDebug();
        sl[i].preTransmission(pr);
        sl[i].postTransmission(po);
        sl[i].idle(id);
    }
}

float rd(uint16_t Ad, int si) {
    uint8_t rs = sl[si].readInputRegisters(Ad, 2);

    if (!rs) {
      uint32_t rw = (sl[si].getResponseBuffer(0) << 16) | sl[si].getResponseBuffer(1);
      float f = *(float*)&rw; 
      return f;
    } else {
        return -1; 
    }
    delay(500);
}

void loop() {
    struct pm {
        const char* DN;
        float Vl;
    };

    // Define parameters with addresses and correlation IDs
    pm pm[] = {
        //Loadenergymeter
        {"L-LE", rd(74,4)},
        {"L-EE", rd(72, 4)},
        {"L-F",  rd(70, 4)},
        {"L-RV", rd(0, 4)},
        {"L-RP", rd(12, 4)},
        {"L-RPF", rd(30, 4)},
        {"L-YV", rd(2, 4)},
        {"L-YP", rd(14, 4)},
        {"L-YPF", rd(32, 4)},
        {"L-BV", rd(4, 4)},
        {"L-BP", rd(16, 4)},
        {"L-BPF", rd(34, 4)},
        {"L-MIV", rd(134, 4)},
        {"L-MAV", rd(132, 4)},
        {"L-MIA", rd(142, 4)},
        {"L-MAA", rd(140, 4)}, 
        {"L-RH", rd(226, 4)},
        {"L-OH", rd(228, 4)}, 
        {"L-NI", rd(230, 4)},
        // {"MP1-E", rd(38, 0)},
        // {"MP1-DCV", rd(0, 0)},
        // {"MP1-P", rd(18, 0)},
        // {"MP1-DCA-CH1", rd(2, 0)},
        // {"MP1-DCA-CH2", rd(4, 0)},
        // {"MP1-DCA-CH3", rd(6, 0)},
        // {"MP1-DCA-CH4", rd(8, 0)},
        // {"MP1-DCP-CH1", rd(10, 0)},
        // {"MP1-DCP-CH2", rd(12, 0)},
        // {"MP1-DCP-CH3", rd(14, 0)},
        // {"MP1-DCP-CH4", rd(16, 0)},
        // {"MP1-IE-CH1", rd(22, 0)},
        // {"MP1-IE-CH2", rd(24, 0)},
        // {"MP1-IE-CH3", rd(26, 0)},
        // {"MP1-IE-CH4", rd(28, 0)},
        // {"MP1-EE-CH1", rd(140, 0)}, 
        // {"MP1-EE-CH2", rd(226, 0)},
        // {"MP1-EE-CH3", rd(228, 0)}, 
        // {"MP1-EE-CH4", rd(230, 0)},
        // {"MP1-RH-CH1", rd(956, 0)}, 
        // {"MP1-RH-CH2", rd(958, 0)},
        // {"MP1-RH-CH3", rd(960, 0)}, 
        // {"MP1-RH-CH4", rd(962, 0)},
        // {"Plants_Inverter_MPPTS2Energy", rd(38, 5)},
        // {"Plants_Inverter_MPPTS2DCVoltage", rd(0, 5)},
        // {"Plants_Inverter_MPPTS2DCPower", rd(18, 5)},
        // {"MPPTS2_Current_Ch1", rd(2, 5)},
        // {"MPPTS2_Current_Ch2", rd(4, 5)},
        // {"MPPTS2_Current_Ch3", rd(6, 5)},
        // {"MPPTS2_Current_Ch4", rd(8, 5)},
        // {"MPPTS2_Power_Ch1", rd(10, 5)},
        // {"MPPTS2_Power_Ch2", rd(12, 5)},
        // {"MPPTS2_Power_Ch3", rd(14, 5)},
        // {"MPPTS2_Power_Ch4", rd(16, 5)},
        // {"MPPTS2_Energy_Imp_Ch1", rd(22, 5)},
        // {"MPPTS2_Energy_Imp_Ch2", rd(24, 5)},
        // {"MPPTS2_Energy_Imp_Ch3", rd(26, 5)},
        // {"MPPTS2_Energy_Imp_Ch4", rd(28, 5)},
        // {"MPPTS2_Energy_Exp_Ch1", rd(140, 5)}, 
        // {"MPPTS2_Energy_Exp_Ch2", rd(226, 5)},
        // {"MPPTS2_Energy_Exp_Ch3", rd(228, 5)}, 
        // {"MPPTS2_Energy_Exp_Ch4", rd(230, 5)},
        // {"MPPTS2_Run_Hour_Ch1", rd(956, 5)}, 
        // {"MPPTS2_Run_Hour_Ch2", rd(958, 5)},
        // {"MPPTS2_Run_Hour_Ch3", rd(960, 5)}, 
        // {"MPPTS2_Run_Hour_Ch4", rd(962, 5)},
        // {"Plants_Inverter_MPPTS3Energy", rd(38, 6)},
        // {"Plants_Inverter_MPPTS3DCVoltage", rd(0, 6)},
        // {"Plants_Inverter_MPPTS3DCPower", rd(18, 6)},
        // {"MPPTS3_Current_Ch1", rd(2, 6)},
        // {"MPPTS3_Current_Ch2", rd(4, 6)},
        // {"MPPTS3_Current_Ch3", rd(6, 6)},
        // {"MPPTS3_Current_Ch4", rd(8, 6)},
        // {"MPPTS3_Power_Ch1", rd(10, 6)},
        // {"MPPTS3_Power_Ch2", rd(12, 6)},
        // {"MPPTS3_Power_Ch3", rd(14, 6)},
        // {"MPPTS3_Power_Ch4", rd(16, 6)},
        // {"MPPTS3_Energy_Imp_Ch1", rd(22, 6)},
        // {"MPPTS3_Energy_Imp_Ch2", rd(24, 6)},
        // {"MPPTS3_Energy_Imp_Ch3", rd(26, 6)},
        // {"MPPTS3_Energy_Imp_Ch4", rd(28, 6)},
        // {"MPPTS3_Energy_Exp_Ch1", rd(140, 6)}, 
        // {"MPPTS3_Energy_Exp_Ch2", rd(226, 6)},
        // {"MPPTS3_Energy_Exp_Ch3", rd(228, 6)}, 
        // {"MPPTS3_Energy_Exp_Ch4", rd(230, 6)},
        // {"MPPTS3_Run_Hour_Ch1", rd(956, 6)}, 
        // {"MPPTS3_Run_Hour_Ch2", rd(958, 6)},
        // {"MPPTS3_Run_Hour_Ch3", rd(960, 6)}, 
        // {"MPPTS3_Run_Hour_Ch4", rd(962, 6)},
        // {"ES-DCV", rd(0, 2)},
        // {"ES-DCA", rd(2, 2)},
        // {"ES-DE", rd(38, 2)},
        // {"ES-CE", rd(40, 2)},
        // {"ES-T", rd(58, 2)},
        // {"ES-RH", rd(66, 2)},
        // {"ES-SR", rd(10, 2)},
        // {"ES-ONH", rd(202, 2)},
        // {"ES-RH", rd(204, 2)}, 
        // {"ES-INT", rd(212, 2)},
        // {"SG-E", rd(72, 3)},
        // {"SG-ACV", rd(42, 3)},
        // {"SG-F", rd(70, 3)},
        // {"SG-P", rd(52, 3)},
        // {"SG-RV", rd(0, 3)},
        // {"SG-YV", rd(2, 3)},
        // {"SG-BV", rd(4, 3)},
        {"SG-RH", rd(226, 3)},
        {"SG-ONH", rd(228, 3)}, 
        {"SG-INT", rd(230, 3)},
        {"SG-PF", rd(62, 3)}
    };

    JsonWriterStatic<4096> jw;
    jw.startObject();
    jw.insertKeyValue("PublishTime", Time.format(Time.now(), "%Y-%m-%d %H:%M:%S"));
    // jw.insertKeyValue("HwId", "Statcon-RTU");

    for (auto& pm : pm) {
        jw.insertKeyValue(pm.DN, String::format("%.2f", pm.Vl));        
    }
    
    jw.finishObjectOrArray();  
    String data = String(jw.getBuffer());
    Particle.publish("ModbusData", data, PRIVATE);
    delay(10000);
}

when i upload this code there isn't a problem but if i uncomment even one more pm pm then the rgb led is blinking red color. could you help me through this problem it would be a great help

You've allocated this on the stack, and the stack is only 6K. This will almost certainly not work.

JsonWriterStatic<4096> jw;

You could create as a global variable instead, but make sure you call

jw.clear()

where the allocation used to be.