123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- void serialEvent() {
- //bool doEval = false;
- while (Serial.available()) {
- char ch = Serial.read();
- cmdBuffer[cmdBufferCount] = ch;
- if ((ch == 13 || ch == 10)) { // ASCII code 13 = "CR", 10 = "LF"
- cmdBuffer[cmdBufferCount] = 0; // nullterminator
- //Serial.print("CR_ ");
- //Serial.print(cmdBuffer);
- //evalCmd();
- if (cmdBufferCount >= 1) evalCmd();
- else {
- cmdBufferCount = 0;
- for ( int i = 0; i < sizeof(cmdBuffer); ++i ) cmdBuffer[i] = (char)0;
- }
- }
- // else if (ch == 10 && cmdBufferCount > 1) { // ASCII code 10 = "LF"
- // cmdBuffer[cmdBufferCount] = 0; // nullterminator
- // //Serial.print("LF__ ");
- // //Serial.print(cmdBuffer);
- // //evalCmd();
- // doEval = true;
- // }
- else {
- cmdBufferCount++;
- }
- }
- }
- void evalCmd() {
- if (debug) {
- Serial.print("cmd: '");
- Serial.print(cmdBuffer);
- Serial.println("'");
- //Serial.println(strlen(cmdBuffer));
- }
- // begin commands
- if (strncmp(cmdBuffer, "ID", 2) == 0 && strlen(cmdBuffer) == 2) {
- Serial.print(PROJECT_NAME);
- Serial.print(" v");
- Serial.print(PROJECT_VERSION);
- Serial.println();
- }
- else if (strncmp(cmdBuffer, "get eeprom", 10) == 0 && strlen(cmdBuffer) == 10) {
- getEEPROMContent();
- }
- else if (strncmp(cmdBuffer, "save data", 9) == 0 && strlen(cmdBuffer) == 9) {
- saveAllCurrentData(false);
- }
- else if (strncmp(cmdBuffer, "debug 1", 7) == 0 && strlen(cmdBuffer) == 7) {
- debug = true;
- debuglevel = 1;
- byte tmpval = 1;
- Serial.println(F("DEBUG ENABLED"));
- EEPROM.put(eeprom_addr_debug, tmpval);
- }
- else if (strncmp(cmdBuffer, "debug 0", 7) == 0 && strlen(cmdBuffer) == 7) {
- debug = false;
- debuglevel = 0;
- byte tmpval = 0;
- Serial.println(F("DEBUG DISABLED"));
- EEPROM.put(eeprom_addr_debug, tmpval);
- }
- else if (strncmp(cmdBuffer, "format all", 10) == 0 && strlen(cmdBuffer) == 10) {
- formatEEPROM_AllData();
- }
- else if (strncmp(cmdBuffer, "format c", 8) == 0 && strlen(cmdBuffer) >= 8) {
- if (strlen(cmdBuffer) == 8) {
- Serial.println(F("missing counter number"));
- }
- else if (strlen(cmdBuffer) == 9) {
- char _tmpcNum[2];
- _tmpcNum[0] = cmdBuffer[8];
- _tmpcNum[1] = '\0';
- byte _cNum = atoi(_tmpcNum);
- //Serial.println(_cNum);
- if (_cNum > 0 && _cNum <= COUNTERS_COUNT) {
- formatEEPROMAreaOfCounter(_cNum - 1);
- }
- else {
- Serial.println(F("invalid counter number"));
- }
- }
- }
- else if (strncmp(cmdBuffer, "get readings", 12) == 0 && strlen(cmdBuffer) == 12) {
- printCurrentReadings();
- }
- else if (strncmp(cmdBuffer, "get imp c", 9) == 0 && strlen(cmdBuffer) >= 9) {
- if (strlen(cmdBuffer) == 9) {
- Serial.println(F("missing counter number"));
- }
- else if (strlen(cmdBuffer) == 10) {
- char _tmpcNum[2];
- _tmpcNum[0] = cmdBuffer[9];
- _tmpcNum[1] = '\0';
- byte _cNum = atoi(_tmpcNum);
- //Serial.println(_cNum);
- if (_cNum > 0 && _cNum <= COUNTERS_COUNT) {
- restoreLastSavedImpulseCount(_cNum - 1, true);
- }
- else {
- Serial.println(F("invalid counter number"));
- }
- }
- }
- else if (strncmp(cmdBuffer, "get pwrgood", 11) == 0 && strlen(cmdBuffer) == 11) {
- printPowerValue();
- }
- else if (strncmp(cmdBuffer, "set pwrgood", 11) == 0 && strlen(cmdBuffer) >= 13) {
- uint16_t tmpvalue = 0;
- char offset = 13;
- char len = strlen(cmdBuffer) - offset;
- if (strlen(cmdBuffer) >= offset) {
- for (char i = 0; i < len; i++) {
- if (i < (sizeof(cmdDataBuffer) - 1)) cmdDataBuffer[i] = cmdBuffer[i + offset];
- }
- cmdDataBuffer[len] = '\0';
- //Serial.println();
- //Serial.print("'");
- //Serial.print(cmdDataBuffer);
- //Serial.println("'");
- char _value1[6];
- uint8_t _ivalue1;
- byte _i2 = 0;
- for (byte i = offset; i < strlen(cmdBuffer); i++) {
- _value1[_i2] = cmdBuffer[i];
- _i2++;
- }
- _value1[_i2] = '\0';
- _ivalue1 = atoi(_value1);
- if (_ivalue1 >= 0 && _ivalue1 <= 1023) {
- Serial.print(F("RES: set pwrGood="));
- Serial.print(_ivalue1);
- Serial.println();
- powerGoodMinValue = _ivalue1;
- }
- saveGlobalConfig();
- }
- }
- else if (strncmp(cmdBuffer, "set json 0", 10) == 0 && strlen(cmdBuffer) == 10) {
- output_json = false;
- byte tmpval = 0;
- EEPROM.put(eeprom_addr_output_json, tmpval);
- }
- else if (strncmp(cmdBuffer, "set json 1", 10) == 0 && strlen(cmdBuffer) == 10) {
- output_json = true;
- byte tmpval = 1;
- EEPROM.put(eeprom_addr_output_json, tmpval);
- }
- else if (strncmp(cmdBuffer, "get conf", 8) == 0 && strlen(cmdBuffer) >= 8) {
- // if (strlen(cmdBuffer) > 8 && cmdBuffer[8] == ' ' && cmdBuffer[9] == 'c') {
- // // is "get conf c#"
- // if (strlen(cmdBuffer) == 10) {
- // Serial.println(F("missing counter number"));
- // }
- // else {
- // uint16_t tmpvalue = 0;
- // char offset = 10;
- // char len = strlen(cmdBuffer) - offset;
- // if (strlen(cmdBuffer) >= offset) {
- // for (char i = 0; i < len; i++) {
- // if (i < (sizeof(cmdDataBuffer) - 1)) cmdDataBuffer[i] = cmdBuffer[i + offset];
- // }
- // cmdDataBuffer[len] = '\0';
- // //Serial.println(cmdDataBuffer);
- // tmpvalue = atoi(cmdDataBuffer);
- // if (tmpvalue > 0 && tmpvalue <= COUNTERS_COUNT) {
- // //Serial.print("conf counter ");
- // //Serial.println(tmpvalue);
- // printCounterConf(tmpvalue - 1);
- // }
- // else {
- // Serial.println(F("counter number invalid"));
- // }
- //
- // }
- // }
- // }
- // else {
- // // is "get conf" - so show all
- printGlobalConf();
- for (byte i = 0; i < COUNTERS_COUNT; i++) {
- printCounterConf(i);
- }
- // }
- }
- else if (strncmp(cmdBuffer, "set debounce", 12) == 0 && strlen(cmdBuffer) >= 14) {
- uint16_t tmpvalue = 0;
- char offset = 13;
- char len = strlen(cmdBuffer) - offset;
- if (strlen(cmdBuffer) >= offset) {
- for (char i = 0; i < len; i++) {
- if (i < (sizeof(cmdDataBuffer) - 1)) cmdDataBuffer[i] = cmdBuffer[i + offset];
- }
- cmdDataBuffer[len] = '\0';
- //Serial.println();
- //Serial.print("'");
- //Serial.print(cmdDataBuffer);
- //Serial.println("'");
- char _value1[6];
- char _value2[6];
- uint8_t _ivalue1, _ivalue2;
- bool _withValue2;
- byte _sepOffset = 0;
- byte _i2 = 0;
- for (byte i = offset; i < strlen(cmdBuffer); i++) {
- if (cmdBuffer[i] == ',') {
- //Serial.print("found , at offset ");
- //Serial.println(i);
- _sepOffset = i;
- }
- else if (_sepOffset == 0) {
- _value1[_i2] = cmdBuffer[i];
- _i2++;
- }
- }
- _value1[_i2] = '\0';
- if (_sepOffset > 0) {
- _i2 = 0;
- for (byte i = _sepOffset + 1; i < strlen(cmdBuffer); i++) {
- _value2[_i2] = cmdBuffer[i];
- _i2++;
- }
- _value2[_i2] = '\0';
- _withValue2 = true;
- }
- else {
- _value2[0] = '\0'; // no _value2 was given
- _withValue2 = false;
- }
- _ivalue1 = atoi(_value1);
- _ivalue2 = atoi(_value2);
- if (_ivalue1 >= 0 && _ivalue1 <= 200) {
- Serial.print(F("RES: set debounce="));
- Serial.print(_ivalue1);
- debounceDelay = _ivalue1;
- }
- if (_withValue2 && _ivalue2 >= 0 && _ivalue2 <= 500) {
- Serial.print(F(", debounceRecov="));
- Serial.print(_ivalue2);
- debounceRecoveryDelay = _ivalue2;
- }
- Serial.println();
- saveGlobalConfig();
- }
- }
- else if (strncmp(cmdBuffer, "get debounce", 12) == 0 && strlen(cmdBuffer) == 12) {
- Serial.print(F("debounce: "));
- Serial.print(debounceDelay);
- Serial.print(", ");
- Serial.println(debounceRecoveryDelay);
- }
- else if (strncmp(cmdBuffer, "set conf c", 10) == 0 && strlen(cmdBuffer) >= 10) {
- if (strlen(cmdBuffer) == 10) {
- Serial.println(F("missing counter number"));
- }
- else if (strlen(cmdBuffer) >= 15) {
- uint16_t tmpvalue = 0;
- char offset = 10;
- char len = strlen(cmdBuffer) - offset;
- if (strlen(cmdBuffer) >= offset) {
- char _cNumBuf[2];
- char _commandBuffer[11];
- char _valueBuffer[11];
- //byte _delimitersFound = 0;
- byte _cNum;
- boolean _cNumOK = false;
- //
- for (byte i = 0; i < len; i++) {
- if (i < (sizeof(cmdDataBuffer) - 1)) cmdDataBuffer[i] = cmdBuffer[i + offset];
- }
- cmdDataBuffer[len] = '\0';
- Serial.print("cmdDataBuffer: '");
- Serial.print(cmdDataBuffer);
- Serial.println("'");
- // get counter number, expect it on 1st place and 1 char long
- _cNumBuf[0] = cmdDataBuffer[0];
- _cNumBuf[1] = '\0';
- _cNum = atoi(_cNumBuf);
- if (_cNum > 0 && _cNum <= COUNTERS_COUNT) {
- Serial.print("_cNum=");
- Serial.println(_cNum);
- _cNumOK = true;
- }
- else if (cmdDataBuffer[1] != ' ') {
- Serial.println(F("ERROR - invalid syntax"));
- }
- else {
- Serial.print("_cNum invalid: ");
- Serial.println(_cNum);
- }
- // Serial.println();
- // Serial.print("cmdDataBuffer[1]='");
- // Serial.print(cmdDataBuffer[1]);
- // Serial.println("'");
- if (_cNumOK && strlen(cmdDataBuffer) > 4) {
- // https://arduino.stackexchange.com/questions/1013/how-do-i-split-an-incoming-string
- // Read each command pair
- char* _param = strtok(cmdDataBuffer, " ");
- while (_param != 0)
- {
- // Split the command in two values
- char* _value = strchr(_param, '=');
- if (_value != 0)
- {
- // Actually split the string in 2: replace ':' with 0
- *_value = 0;
- strlwr(_param);
- // Serial.print("_param='");
- // Serial.print(_param);
- ++_value;
- // Serial.print("' ");
- // Serial.print("_value='");
- // Serial.print(_value);
- // Serial.println("'");
- if (strcmp(_param, "impperunit") == 0) {
- Serial.print("impPerUnit=");
- Serial.println(atoi(_value));
- meter_impPerUnit[_cNum - 1] = atoi(_value);
- }
- else if (strcmp(_param, "noimptout") == 0) {
- Serial.print("noImpTout=");
- Serial.println(atoi(_value));
- meter_noImpulseTimeout_seconds[_cNum - 1] = atoi(_value);
- }
- else if (strcmp(_param, "saveint") == 0) {
- Serial.print("saveInt=");
- Serial.println(atoi(_value));
- meter_savePulsesOnInterval_mins[_cNum - 1] = atoi(_value);
- }
- else {
- Serial.print(F("unknown parameter '"));
- Serial.print(_param);
- Serial.println("'");
- }
- printCounterConf(_cNum - 1);
- }
- // Find the next command in input string
- _param = strtok(0, ";");
- }
- }
- else {
- Serial.println(F("invalid command"));
- }
- }
- }
- }
- else if (strncmp(cmdBuffer, "save conf", 9) == 0 && strlen(cmdBuffer) == 9) {
- Serial.println(F("SAVING CONFIG"));
- saveCountersConfig();
- saveGlobalConfig();
- }
- #ifdef INCLUDE_TESTFUNCTIONS
- else if (strncmp(cmdBuffer, "test init c2", 12) == 0 && strlen(cmdBuffer) == 12) {
- Serial.println("test init c2");
-
- formatEEPROMAreaOfCounter(1);
-
- eeprom_writecounter_currentReadingImpulses[1] = 65530;
- eeprom_nextoffset_currentReadingImpulses[1] = 0;
- }
-
- else if (strncmp(cmdBuffer, "test c2", 7) == 0 && strlen(cmdBuffer) == 7) {
- increaseCounter(1);
- storeCurrentImpulses(1, false);
- }
- #endif
- else if (strncmp(cmdBuffer, "set reading", 11) == 0 && strlen(cmdBuffer) >= 11) {
- uint16_t tmpvalue = 0;
- char tmpdata_1[10];
- char tmpdata_2[10];
- unsigned long newReading;
- uint16_t newImpulses;
- if (strlen(cmdBuffer) <= 13) {
- Serial.println(F("ERROR: missing valid counter number"));
- }
- else if ( strlen(cmdBuffer) > 13 && cmdBuffer[11] == ' ' && cmdBuffer[12] == 'c') {
- byte _cNum;
- char tmp_cNum[2];
- tmp_cNum[0] = cmdBuffer[13]; // atoi needs char array to work...
- tmp_cNum[1] = 0;
- _cNum = atoi(tmp_cNum);
- if (_cNum < 1 || _cNum > COUNTERS_COUNT) {
- Serial.println(F("ERROR: invalid counter number"));
- }
- else if (_cNum > 0 && _cNum <= COUNTERS_COUNT) {
- Serial.print(F("got valid counter number: "));
- Serial.println(_cNum);
- _cNum = _cNum - 1; // we actually need the array index, not the pretty number...
- //char offset = 12;
- byte offset = 15;
- byte len = strlen(cmdBuffer) - offset;
- Serial.println("SET READING");
- if (strlen(cmdBuffer) > offset) {
- //for (char i = 0; i < len; i++) {
- // if (i < (sizeof(cmdDataBuffer) - 1)) cmdDataBuffer[i] = cmdBuffer[i + offset];
- //}
- bool value_1_finished = false;
- bool value_2_finished = false;
- bool commaGiven = false;
- bool impulsesGiven = false;
- uint8_t i1 = 0;
- uint8_t i2 = 0;
- //Serial.println("vor val1");
- while (!value_1_finished) {
- if (isdigit(cmdBuffer[i1 + offset])) {
- if (i1 < (sizeof(tmpdata_1) - 1)) tmpdata_1[i1] = cmdBuffer[i1 + offset];
- //Serial.print(" ");
- //Serial.print(i1);
- i1++;
- i2++;
- }
- else if (cmdBuffer[i1 + offset] == '.' || cmdBuffer[i1 + offset] == ',' || cmdBuffer[i1 + offset] == ' ') {
- value_1_finished = true;
- if (cmdBuffer[i1 + offset] == '.' || cmdBuffer[i1 + offset] == ',') commaGiven = true;
- //Serial.println("value_1_finished=true");
- i1++;
- }
- else if (cmdBuffer[i1 + offset] == '\0') {
- //Serial.println("value_[1|2]_finished=true");
- value_1_finished = true;
- value_2_finished = true; // end of string reached - no value 2 was given
- }
- }
- tmpdata_1[i2] = '\0';
- newReading = atol(tmpdata_1);
- //Serial.print(F("value 1: "));
- //Serial.println(tmpdata_1);
- if (!value_2_finished) {
- impulsesGiven = true;
- i2 = 0;
- while (!value_2_finished) {
- if (isdigit(cmdBuffer[i1 + offset])) {
- if (i2 < (sizeof(tmpdata_2) - 1)) tmpdata_2[i2] = cmdBuffer[i1 + offset];
- i1++;
- i2++;
- }
- else if (cmdBuffer[i1 + offset] == ' ' || cmdBuffer[i1 + offset] == '\0') {
- value_2_finished = true; // end of string reached
- }
- }
- tmpdata_2[i2] = '\0';
- newImpulses = atoi(tmpdata_2);
- //Serial.print(F("value 2: "));
- //Serial.println(tmpdata_2);
- }
- if (commaGiven && impulsesGiven && tmpdata_2 >= 0) {
- if ((meter_impPerUnit[_cNum] == 10 && newImpulses < 10) || (meter_impPerUnit[_cNum] == 100 && newImpulses < 100) || (meter_impPerUnit[_cNum] == 1000 && newImpulses < 1000) || (meter_impPerUnit[_cNum] == 10000 && newImpulses < 10000)) {
- Serial.print(F("INFO: set new reading: "));
- Serial.print(newReading);
- Serial.print(".");
- if (meter_impPerUnit[_cNum] == 100 && newImpulses < 10) Serial.print("0");
- else if (meter_impPerUnit[_cNum] == 1000 && newImpulses < 10) Serial.print("00");
- else if (meter_impPerUnit[_cNum] == 1000 && newImpulses < 100) Serial.print("0");
- Serial.println(newImpulses);
- currentReading[_cNum] = newReading;
- currentReadingImpulses[_cNum] = newImpulses;
- formatEEPROMAreaOfCounter(_cNum);
- storeCurrentReading(_cNum, true);
- storeCurrentImpulses(_cNum, true);
- }
- else {
- Serial.print(F("ERROR: invalid decimals for set impPerUnit="));
- Serial.print(meter_impPerUnit[_cNum]);
- Serial.println(" !!!");
- }
- }
- else if (!commaGiven && impulsesGiven) {
- Serial.print(F("INFO: set new reading: "));
- Serial.println(newReading);
- Serial.print(F("INFO: set new impulses: "));
- Serial.println(newImpulses);
- currentReading[_cNum] = newReading;
- currentReadingImpulses[_cNum] = newImpulses;
- formatEEPROMAreaOfCounter(_cNum);
- storeCurrentReading(_cNum, true);
- storeCurrentImpulses(_cNum, true);
- }
- else if (!commaGiven && !impulsesGiven) {
- Serial.print(F("INFO: set new reading: "));
- Serial.println(newReading);
- currentReading[_cNum] = newReading;
- currentReadingImpulses[_cNum] = 0;
- formatEEPROMAreaOfCounter(_cNum);
- storeCurrentReading(_cNum, true);
- storeCurrentImpulses(_cNum, true);
- }
- }
- }
- }
- }
- else if (strncmp(cmdBuffer, "get wcount", 10) == 0 && strlen(cmdBuffer) == 10) {
- for (int i = 0; i < COUNTERS_COUNT; i++) {
- Serial.print(F("wCount C"));
- Serial.print(i + 1);
- Serial.print("=");
- Serial.println(eeprom_writecounter_currentReadingImpulses[i]);
- }
- }
- else if (strncmp(cmdBuffer, "reset", 5) == 0 && strlen(cmdBuffer) >= 5) {
- if (strlen(cmdBuffer) == 5) {
- resetForFlashing(0);
- }
- else {
- uint16_t tmpvalue = 0;
- char offset = 6;
- char len = strlen(cmdBuffer) - offset;
- if (strlen(cmdBuffer) >= offset) {
- for (char i = 0; i < len; i++) {
- if (i < (sizeof(cmdDataBuffer) - 1)) cmdDataBuffer[i] = cmdBuffer[i + offset];
- }
- cmdDataBuffer[len] = '\0';
- Serial.println(cmdDataBuffer);
- tmpvalue = atoi(cmdDataBuffer);
- if (tmpvalue >= 0 && tmpvalue <= 15000) {
- resetForFlashing(tmpvalue);
- }
- else Serial.println(F("out of range 0-15000"));
- }
- }
- }
- else {
- Serial.print(F("UNKNOWN COMMAND: '"));
- Serial.print(cmdBuffer);
- Serial.println("'");
- }
- cmdBufferCount = 0;
- for ( int i = 0; i < sizeof(cmdBuffer); ++i ) cmdBuffer[i] = (char)0;
- }
|