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.