diff --git a/firmware/platformio.ini b/firmware/platformio.ini index c097c2c..b68b4a6 100644 --- a/firmware/platformio.ini +++ b/firmware/platformio.ini @@ -20,6 +20,7 @@ board = bluepill_f103c8_128k upload_protocol = serial monitor_speed = 115200 +#upload_port = COM4 # To upload the code uncomment this line and put the port corresponding to your module on you PC # Disable some warning flags for libraries build_flags = diff --git a/firmware/src/credit.hpp b/firmware/src/credit.hpp index 921843f..fbe395c 100644 --- a/firmware/src/credit.hpp +++ b/firmware/src/credit.hpp @@ -11,7 +11,7 @@ #include "relay.hpp" float billing = 0.0; -unsigned int tariff = 70; +unsigned int tariff = 1; float creditt = 0.0; byte fault = 0; diff --git a/firmware/src/helpers.hpp b/firmware/src/helpers.hpp index 4251033..bed6863 100644 --- a/firmware/src/helpers.hpp +++ b/firmware/src/helpers.hpp @@ -38,7 +38,9 @@ void urgeent() { credit_formular(); } - STS_keypad(); + if (Mode_select != 2) { + STS_keypad(); + } warn_now = millis(); if (c_chek == 1) { if (warn_now - warntime > 1500) { diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index b1a0793..4daaf77 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -168,26 +168,37 @@ void setup() { lcd.clear(); lcd.setCursor(0, 0); lcd.println("No Configuration! "); - while (Mode_select == 0) // wait for mode configuration + while (Mode_select == 0) // wait for configuration mode { STS_keypad(); delay(20); } break; - case 1: + case 1: // Module in STS Mode + printf("Welcome to the STS Energy-based\n"); MyTim->attachInterrupt(urgeent); MyTim->resume(); break; - case 2: + case 2: // Module in OpenPaygo Energy-based /*OpenPayGo Token initializing code; */ - printf("Welcome to the OPAYGO Device\n"); + printf("Welcome to the OpenPaygo Energy-based\n"); printf( "We're waiting for the * character to start recording the key " "presses.\n(Press the '#' key to see the device activation " "status)\n\n"); - LoadActivationVariables(); // We load the activation variableS + LoadActivationVariables(); // We load the activation variables + break; + + case 3: // Module in OpenPaygo Time-based + /*OpenPaygo Time-based initializing code; */ + printf("Welcome to the OpenPaygo Time-based\n"); + printf( + "We're waiting for the * character to start recording the key " + "presses.\n(Press the '#' key to see the device activation " + "status)\n\n"); + LoadActivationVariables(); // We load the activation variables break; } @@ -195,7 +206,7 @@ void setup() { } void loop() { - if (Mode_select == 1) { + if (Mode_select == 1) { // Module in STS Mode mesure(); if ((mains_input_value > 50)) { credit_reminder(); @@ -210,7 +221,9 @@ void loop() { } } - if (Mode_select == 2) { + if (Mode_select == 2 || + Mode_select == 3) { // 2 = Module in OpenPaygo Energy-based; 3 = Module + // in OpenPaygo Time-based // We wait for a token to be entered InputToken = WaitForTokenEntry(); // We get the activation value from the token diff --git a/firmware/src/opaygo_functions.hpp b/firmware/src/opaygo_functions.hpp index c998313..5d3529f 100644 --- a/firmware/src/opaygo_functions.hpp +++ b/firmware/src/opaygo_functions.hpp @@ -57,12 +57,27 @@ void LoadActivationVariables() { // UsedTokens if needed) UsedTokens = mem.readInt(UsedTokens_eeprom_location); // We load UsedTokens if needed - PAYGEnabled = mem.read( + switch (mem.read(PAYGEnabled_eeprom_location)) { + case 0: + PAYGEnabled = false; + break; + case 1: + PAYGEnabled = true; + break; + } + + mem.read( PAYGEnabled_eeprom_location); // We load PAYGEnabled //Verify this syntax - ActiveUntil = - mem.readLong(ActiveUntil_eeprom_location); // We load ActiveUntil TokenEntryLockedUntil = mem.readLong( TokenEntryLockedUntil_eeprom_location); // We load TokenEntryLockedUntil + if (Mode_select == 2) { + creditt = + mem.readLong(credit_eeprom_location); // We load creditt in case of + // OpenPaygo Energy-based + } else { + ActiveUntil = + mem.readLong(ActiveUntil_eeprom_location); // We load ActiveUntil + } } void StoreActivationVariables() { @@ -70,12 +85,22 @@ void StoreActivationVariables() { TokenCount); // We store TokenCount (& UsedTokens if needed) mem.writeInt(UsedTokens_eeprom_location, UsedTokens); // We store UsedTokens if needed - mem.write(PAYGEnabled_eeprom_location, - PAYGEnabled); // We store PAYGEnabled //Verify this syntax - mem.writeLong(ActiveUntil_eeprom_location, - ActiveUntil); // We store ActiveUntil + if (PAYGEnabled) { + mem.writeInt(PAYGEnabled_eeprom_location, 1); // We store PAYGEnabled + } else { + mem.writeInt(PAYGEnabled_eeprom_location, 0); // We store PAYGEnabled + } + mem.writeLong(TokenEntryLockedUntil_eeprom_location, TokenEntryLockedUntil); // We store TokenEntryLockedUntil + if (Mode_select == 2) { + mem.writeLong( + credit_eeprom_location, + creditt); // We Store creditt in case of OpenPaygo Energy-based + } else { + mem.writeLong(ActiveUntil_eeprom_location, + ActiveUntil); // We store ActiveUntil + } } void storeTimeStampEEPROM(uint32_t timeStampInSeconds) { @@ -202,7 +227,29 @@ void SetTime(int ActivationValue) { // seconds for to compare to our RTC time) } +void AddCreditt(int ActivationValue) { + mem.writeLong(credit_eeprom_location, creditt); // write present credit + creditt = mem.readLong(credit_eeprom_location); // fetch previous credit // + // add new if any to old credit // + creditt += ActivationValue; + mem.writeLong(credit_eeprom_location, creditt); + get_credit = 1; +} + +void SetCreditt(int ActivationValue) { + creditt = ActivationValue; + mem.writeLong(credit_eeprom_location, creditt); // write present credit + creditt = mem.readLong(credit_eeprom_location); // fetch previous credit // + // add new if any to old credit // + creditt = ActivationValue; + mem.writeLong(credit_eeprom_location, creditt); + get_credit = 1; +} + void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) { + // TokenValue in case of OpenPaygo Energy-based is consider directly as a + // credit to add or set and in case of OpenPaygo Time-based it is consider as + // a number of day to add or to set if (TokenValue == -1) { InvalidTokenCount++; UpdateInvalidTokenWaitingPeriod(); @@ -223,9 +270,17 @@ void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) { } else { if (ActivationCount % 2) { PAYGEnabled = true; - SetTime(TokenValue); + if (Mode_select == 2) { // Module in OpenPaygo Energy-based + SetCreditt(TokenValue); + } else { // Module in OpenPaygo Time-based + SetTime(TokenValue); + } } else { - AddTime(TokenValue); + if (Mode_select == 2) { // Module in OpenPaygo Energy-based + AddCreditt(TokenValue); + } else { // Module in OpenPaygo Time-based + AddTime(TokenValue); + } } BlinkGreenLED( 2, @@ -237,14 +292,23 @@ void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) { bool IsActive() { if (PAYGEnabled) { - if (ActiveUntil > GetTimeInSeconds()) { - return true; - } else { - return false; + if (Mode_select == 2) { + if (creditt < 1 || fault == 1) { + return false; + } else { + return true; + } + } + + if (Mode_select == 3) { + if (ActiveUntil > GetTimeInSeconds()) { + return true; + } else { + return false; + } } - } else { - return true; } + return true; } uint64_t WaitForTokenEntry() { @@ -268,8 +332,22 @@ uint64_t WaitForTokenEntry() { if (IsActive()) { BlinkGreenLED(1, BLINK_PERIOD); #ifdef DEBUG - printf("\nTime Left: %" PRIu32 "seconds", - ActiveUntil - GetTimeInSeconds()); + if (Mode_select == 2) { // Module in OpenPaygo Energy-based + mesure(); + if ((mains_input_value > 50)) { + credit_reminder(); + } + if ((mains_input_value < 50)) { + digitalWrite(red_led, LOW); + digitalWrite(green_led, LOW); + } + urgeent(); + printf("\nEnergy Left: %f KWH", ENERGY); + } else { // Module in OpenPaygo Time-based + printf("\nTime Left: %" PRIu32 "seconds", + ActiveUntil - GetTimeInSeconds()); + } + #endif } else { BlinkRedLED(1, BLINK_PERIOD); diff --git a/firmware/src/sts_token.hpp b/firmware/src/sts_token.hpp index 5dc00c9..84ecb1c 100644 --- a/firmware/src/sts_token.hpp +++ b/firmware/src/sts_token.hpp @@ -176,7 +176,7 @@ void STStoken_decode() { mem.writeLong(credit_eeprom_location, creditt); // write present credit creditt = mem.readLong(credit_eeprom_location); // fetch previous credit // - creditt = creditt + topup; // add new if any to old credit // + creditt = creditt + topup; // add new if any to old credit // mem.writeLong(credit_eeprom_location, creditt); get_credit = 1; } diff --git a/firmware/src/token_management.hpp b/firmware/src/token_management.hpp index 0d26a33..98feae0 100644 --- a/firmware/src/token_management.hpp +++ b/firmware/src/token_management.hpp @@ -14,7 +14,6 @@ #include "sts_token.hpp" char customKey; -bool is_STSmode = false; // by default on OpenPAYGO Token // keypad const byte ROWS = 4; @@ -158,11 +157,15 @@ void STS_keypad() { dt = 0; } if (sts_data == "112") { - Mode_select = 1; + Mode_select = 1; // For STS Mode (Energy-based) } if (sts_data == "122") { - Mode_select = 2; + Mode_select = 2; // For OpenPaygo Energy-based + } + + if (sts_data == "132") { + Mode_select = 3; // For OpenPaygo Time-based } } }