#define DEBUG true //set to true for debug output, false for no debug output
#define DEB \
if (DEBUG) Serial
/*void parseresult(const char *buffer){
struct statusEntry{
char str[8+1];
union{
uint8_t byte;
struct{
bool acChargingStatus:1;
bool sccChargingStatus:1;
bool chargingStatus:1;
bool reserved_b3:1;
bool loadStatus:1;
bool sscVersion:1;
bool configStatus:1;
bool reserved_b7:1;
} bits;
} status;
};
char gridVoltage[6];
char gridFreq[6];
char outputVoltage[6];
char outputFreq[6];
char outputPowerApparent[6];
char outputPowerActive[6];
char loadPercent[6];
char busVoltage[6];
char batteryVoltage[6];
char batteryCurrent[6];
char batteryCapacity[6];
char temperature[6];
char solarCurrent[6];
char solarVoltage[6];
char batteryVoltageSCC[6];
char batteryDischargeCurrent[6];
statusEntry status;
/*char fanoffset[6];
char eepromversion[6];
char temp[6];
char solarPower[6];
buffer++; /* Skip start byte '('
#define strncpyTerminated(destination, source, num) \
do{ \
strncpy(destination, source, num); \
destination[num] = 0; \
}while(0)
#define copyAndAdvance(dest, src, size) strncpyTerminated(dest, src, size); buffer += size+1
copyAndAdvance(gridVoltage, buffer, 5);
copyAndAdvance(gridFreq, buffer, 4);
copyAndAdvance(outputVoltage, buffer, 5);
copyAndAdvance(outputFreq, buffer, 4);
copyAndAdvance(outputPowerApparent, buffer, 4);
copyAndAdvance(outputPowerActive, buffer, 4);
copyAndAdvance(loadPercent, buffer, 3);
copyAndAdvance(busVoltage, buffer, 3);
copyAndAdvance(batteryVoltage, buffer, 5);
copyAndAdvance(batteryCurrent, buffer, 3);
copyAndAdvance(batteryCapacity, buffer, 3);
copyAndAdvance(temperature, buffer, 4);
copyAndAdvance(solarCurrent, buffer, 4);
copyAndAdvance(solarVoltage, buffer, 5);
copyAndAdvance(batteryVoltageSCC, buffer, 5);
copyAndAdvance(batteryDischargeCurrent, buffer, 5);
copyAndAdvance(status.str, buffer, 8);
/*copyAndAdvance(fanoffset, buffer, 2);
copyAndAdvance(eepromversion, buffer, 2);
copyAndAdvance(temp, buffer, 5);
copyAndAdvance(solarPower, buffer, 5);
#undef copyAndAdvance
#undef strncpyTerminated
status.status.byte = 0;
for(int i=0; i<8; i++){ status.status.byte |= (status.str[7-i] - '0')<<i;}
store("/gridVoltage",gridVoltage);
DEB.printf("gridVoltage: %s\n", gridVoltage);
store("/gridFreq",gridFreq);
DEB.printf("gridFreq: %s\n", gridFreq);
store("/outputVoltage",outputVoltage);
DEB.printf("outputVoltage: %s\n", outputVoltage);
store("/outputFreq",outputFreq);
DEB.printf("outputFreq: %s\n", outputFreq);
store("/outputPowerApparent",outputPowerApparent);
DEB.printf("outputPowerApparent: %s\n", outputPowerApparent);
store("/outputPowerActive",outputPowerActive);
DEB.printf("outputPowerActive: %s\n", outputPowerActive);
store("/loadPercent",loadPercent);
DEB.printf("loadPercent: %s\n", loadPercent);
store("/busVoltage",busVoltage);
DEB.printf("busVoltage: %s\n", busVoltage);
store("/batteryVoltage",batteryVoltage);
DEB.printf("batteryVoltage: %s\n", batteryVoltage);
store("/batteryCurrent",batteryCurrent);
DEB.printf("batteryCurrent: %s\n", batteryCurrent);
store("/batteryCapacity",batteryCapacity);
DEB.printf("batteryCapacity: %s\n", batteryCapacity);
store("/temperature",temperature);
DEB.printf("temperature: %s\n", temperature);
store("/solarCurrent",solarCurrent);
DEB.printf("solarCurrent: %s\n", solarCurrent);
store("/solarVoltage",solarVoltage);
DEB.printf("solarVoltage: %s\n", solarVoltage);
store("/batteryVoltageSCC",batteryVoltageSCC);
DEB.printf("batteryVoltageSCC: %s\n", batteryVoltageSCC);
store("/batteryDischargeCurrent",batteryDischargeCurrent);
DEB.printf("batteryDischargeCurrent: %s\n", batteryDischargeCurrent);
/*DEB.printf("fanoffset: %s\n", fanoffset);
DEB.printf("eepromversion: %s\n", eepromversion);
store("/solarPower",solarPower);
DEB.printf("solarPower: %s\n", solarPower);
}*/
void setup() {
DEB.begin(115200);
DEB.println("Insert string to be parsed");
}
void loop() {
//String ht = " err :0, desc : ERR_NONE , dat : dat : 28 30 30 30 2E 30 20 30 30 2E 30 20 32 32 39 2E 39 20 35 30 2E 30 20 30 35 37 34 20 30 35 31 32 20 30 30 39 20 34 32 35 20 35 36 2E 32 30 20 30 32 34 20 31 30 30 20 30 30 35 34 20 31 32 2E 30 20 31 36 38 2E 35 20 30 30 2E 30 30 20 30 30 30 30 30 20 30 30 30 31 30 31 31 30 20 30 30 20 30 30 20 30 32 30 33 34 20 30 31 30 1C CA 0D ";
String ht = " err :0, desc : ERR_NONE , dat : dat : 05 03 1A 00 00 00 00 00 00 01 00 0B 02 64 00 00 00 02 00 FA 08 F4 01 B7 00 46 00 02 00 BD 85 ";
String test;
while ( test == "") {
test = Serial.readStringUntil('\n');
}
//const char *depr = hexToAscii(ht);
//const char *depr = hexToAscii(ht.substring(41, (ht.length() - 12)).c_str());
//parseresult(ht.c_str());
//parseresult(ht.substring(50, (ht.length() - 10))/*.c_str()*/);
//String test ="03 00 00 00 00 00 7E 06 80 00 06 02 64 00 00 00 00 00 FA 08 F3 01 B7 00 60 00 02 00 02 00 00 00 B8 01 ED 1C 00 00 02 00 38 18 38 18 E6 00 1A 00 E0 01 E6 00 F4 01 1A 00 00 00 00 00 9F 15 9F 15 9F 15 37 00 9C 01 02 00 02 00 01 00 03 00 00 00 32 00 E6 00 02 00 E0 01 F4 01 48 02 3C 02 90 01 48 02 3C 00 78 00 1E 00 46 14 07 00 0B 00 0B 00 1F 00 10 04 01 00 01 00 00 00 ";
int reg = 4501;
for (int i=0; i<=(test.length()-6); i+=6 )
{
Serial.print(reg);
Serial.print( " ");
Serial.print(test.substring(i, i + 6));
Serial.print( " ");
Serial.println(swapBytes(test.substring(i, i + 6)));
reg = reg +1 ;
//Serial.println(i);
}
//const char *depr = ht.substring(50, (ht.length() - 10)).c_str();
//parseresult(depr);
//DEB.println(depr);
delay(10000);
}
/*const char *hexToAscii( String hex ){
uint16_t len = hex.length();
String ascii = "";
for ( uint16_t i = 0; i < len; i += 3 )
ascii += (char)strtol( hex.substring( i, i+2 ).c_str(), NULL, 16 );
return ascii.c_str();
}*/
/*void store(String path, String message){
//ht = rtc.getTime("%H:%M:%S") + " " + ht.substring(44, (ht.length() - 12)) + "\n";
//String azi = "/" + rtc.getTime("%F") + ".txt";
//DEB.println(azi);
//writeF(SD, azi.c_str(), ht.c_str());
//DEB.println(ht);
String anul = "/2021";
String luna = anul + "/August";
path = luna + path;
if (!SD.exists(anul)) { SD.mkdir(anul); }
if (!SD.exists(luna)) { SD.mkdir(luna); }
if (!SD.exists(path)) { SD.mkdir(path); }
String azi = "/212220.txt";
//DEB.println(path + azi);
File file = SD.open(path + azi , FILE_WRITE);
message = "22:10:10" + message;
//rtc.getTime("%H:%M:%S") + " " + message ;
file.print(message);
file.close();
/*File file1 = SD.open(luna + path + azi);
if (!file1) {
DEB.println("Failed to open file for reading");
return;
}
DEB.print("Read from file: ");
while (file1.available()) {
DEB.write(file1.read());
}
file1.close();/
}*/
//float swapBytes(String hex, uint16_t divizor)
uint16_t swapBytes(String hex)
//float swapBytes(String hex)
{
hex = hex.substring(0, 2) + hex.substring(3, 5);
uint16_t intValue = strtol(hex.c_str(), NULL, 16);
intValue = (intValue << 8) | (intValue >> 8);
//DEB.printf("Valoarea int %u\n", intValue);
//float fvalue = (intValue << 8) | (intValue >> 8);
//float fvalue = intValue;// / divizor;
//fvalue = fvalue / divizor;
//DEB.println(fvalue);
//return fvalue;
return intValue ;
//return (intValue << 8) | (intValue >> 8);
}