-
Notifications
You must be signed in to change notification settings - Fork 135
feature/lora: Enable LoraWAN supported hardware #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kallisti5
wants to merge
2
commits into
OpenEVSE:master
Choose a base branch
from
kallisti5:lora
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+286
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,6 +102,16 @@ build_flags = | |
| build_partitions = min_spiffs.csv | ||
| build_partitions_debug = min_spiffs_debug.csv | ||
|
|
||
| # Used for LoRaWAN builds | ||
| # https://github.com/mcci-catena/arduino-lmic#selecting-the-lorawan-region-configuration | ||
| lora_lib = | ||
| MCCI LoRaWAN LMIC library | ||
| aparcar/CayenneLPP@^1.3.0 | ||
| lora_build_flags = | ||
| -D ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS | ||
| -D hal_init=LMICHAL_init ; Workaround mcci arduino-lmic bug 714 on esp32 | ||
| -D CFG_us915 ; USA 915Mhz | ||
|
|
||
| neopixel_lib = adafruit/Adafruit [email protected] | ||
|
|
||
|
|
||
|
|
@@ -308,9 +318,14 @@ upload_speed = 921600 | |
|
|
||
| [env:openevse_esp32-heltec-wifi-lora-v2] | ||
| board = heltec_wifi_lora_32_V2 | ||
| lib_deps = | ||
| ${common.lib_deps} | ||
| ${common.lora_lib} | ||
| build_flags = | ||
| ${common.build_flags} | ||
| ${common.src_build_flags} | ||
| ${common.lora_build_flags} | ||
| ${common.version}.dev | ||
| -D DEBUG_PORT=Serial | ||
| -D WIFI_LED=25 | ||
| -D WIFI_LED_ON_STATE=HIGH | ||
|
|
@@ -319,3 +334,10 @@ build_flags = | |
| -D RAPI_PORT=Serial1 | ||
| -D RX1=25 | ||
| -D TX1=27 | ||
| -D CFG_sx1276_radio ; SX1275 radio | ||
| -D ENABLE_LORA=1 | ||
| -D LORA_NSS=18 | ||
| -D LORA_RST=14 | ||
| -D LORA_DIO0=26 | ||
| -D LORA_DIO1=35 | ||
| -D LORA_DIO2=34 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| /* | ||
kallisti5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * Copyright (c) 2019-2020 Alexander von Gluck IV for OpenEVSE | ||
| * | ||
| * ------------------------------------------------------------------- | ||
| * | ||
| * Additional Adaptation of OpenEVSE ESP Wifi | ||
| * by Trystan Lea, Glyn Hudson, OpenEnergyMonitor | ||
| * All adaptation GNU General Public License as below. | ||
| * | ||
| * ------------------------------------------------------------------- | ||
| * | ||
| * This file is part of Open EVSE. | ||
| * Open EVSE is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 3, or (at your option) | ||
| * any later version. | ||
| * Open EVSE is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with Open EVSE; see the file COPYING. If not, write to the | ||
| * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
| * Boston, MA 02111-1307, USA. | ||
| */ | ||
| #ifdef ENABLE_LORA | ||
|
|
||
| #include <lmic.h> | ||
| #include <hal/hal.h> | ||
| #include <SPI.h> | ||
|
|
||
| #include "emonesp.h" | ||
| #include "input.h" | ||
| #include "lora.h" | ||
|
|
||
| #include "app_config.h" | ||
|
|
||
|
|
||
| #define LORA_HTOI(c) ((c<='9')?(c-'0'):((c<='F')?(c-'A'+10):((c<='f')?(c-'a'+10):(0)))) | ||
| #define LORA_TWO_HTOI(h, l) ((LORA_HTOI(h) << 4) + LORA_HTOI(l)) | ||
| #define LORA_HEX_TO_BYTE(a, h, n) { for (int i = 0; i < n; i++) (a)[i] = LORA_TWO_HTOI(h[2*i], h[2*i + 1]); } | ||
| #define LORA_DEVADDR(a) (uint32_t) ((uint32_t) (a)[3] | (uint32_t) (a)[2] << 8 | (uint32_t) (a)[1] << 16 | (uint32_t) (a)[0] << 24) | ||
|
|
||
| // Announce every 15 minutes | ||
| #define ANNOUNCE_INTERVAL 900 * 1000 // (In Milliseconds) | ||
|
|
||
|
|
||
| // LoRa module pin mapping | ||
| const lmic_pinmap lmic_pins = { | ||
| .nss = LORA_NSS, | ||
| .rxtx = LMIC_UNUSED_PIN, | ||
| .rst = LORA_RST, | ||
| .dio = {LORA_DIO0, LORA_DIO1, LORA_DIO2}, | ||
| }; | ||
|
|
||
| // Used for OTAA, not used (yet) | ||
| void os_getArtEui (u1_t* buf) { } | ||
| void os_getDevEui (u1_t* buf) { } | ||
| void os_getDevKey (u1_t* buf) { } | ||
|
|
||
| void | ||
| create_rapi_cayennelpp(EvseManager* _evse, CayenneLPP* lpp) | ||
| { | ||
| if (_evse == NULL) { | ||
| DBUGF("Corrupt EvseManager!") | ||
| return; | ||
| } | ||
| if (lpp == NULL) { | ||
| DBUGF("Corrupt CayenneLPP buffer!") | ||
| return; | ||
| } | ||
|
|
||
| lpp->reset(); | ||
| lpp->addDigitalInput(0, _evse->getEvseState()); | ||
| lpp->addAnalogInput(1, _evse->getVoltage()); | ||
| lpp->addAnalogInput(2, _evse->getAmps()); | ||
| lpp->addAnalogInput(3, _evse->getChargeCurrent()); | ||
| lpp->addDigitalInput(4, _evse->getSessionElapsed() / 60); | ||
| if(evse.isTemperatureValid(EVSE_MONITOR_TEMP_MONITOR)) | ||
| lpp->addTemperature(5, _evse->getTemperature(EVSE_MONITOR_TEMP_MONITOR) * TEMP_SCALE_FACTOR); | ||
| if(evse.isTemperatureValid(EVSE_MONITOR_TEMP_MAX)) | ||
| lpp->addTemperature(6, _evse->getTemperature(EVSE_MONITOR_TEMP_MAX) * TEMP_SCALE_FACTOR); | ||
| } | ||
|
|
||
| /// Reset LoRa modem. Reload LoRaWAN keys | ||
| void onEvent(ev_t ev) { | ||
| switch (ev) { | ||
| case EV_TXCOMPLETE: | ||
| DBUGF("LoRa: TX Complete."); | ||
| // LoRaWAN transmission complete | ||
| if (LMIC.txrxFlags & TXRX_ACK) { | ||
| // Received ack | ||
| DBUGF("LoRa: TX ack."); | ||
| } | ||
| break; | ||
| case EV_TXSTART: | ||
| DBUGF("LoRa: TX Begin."); | ||
| break; | ||
| default: | ||
| // Ignore anything else for now | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| LoraTask::LoraTask() | ||
| : | ||
| MicroTasks::Task() | ||
| { | ||
| } | ||
|
|
||
| void | ||
| LoraTask::begin(EvseManager &evse) | ||
| { | ||
| _evse = &evse; | ||
| MicroTask.startTask(this); | ||
| } | ||
|
|
||
| /// Initial setup of LoRa modem. | ||
| void | ||
| LoraTask::setup() | ||
| { | ||
| Profile_Start(LoraTask::setup); | ||
|
|
||
| os_init(); | ||
| modem_reset(); | ||
|
|
||
| Profile_End(LoraTask::setup, 1); | ||
| } | ||
|
|
||
| /// Reset LoRa modem. Reload LoRaWAN keys | ||
| void | ||
| LoraTask::modem_reset() | ||
| { | ||
| Profile_Start(LoraTask::modem_reset); | ||
| // LoRaWAN credentials to use | ||
| uint8_t DEVADDR[4]; | ||
| uint8_t NWKSKEY[16]; | ||
| uint8_t APPSKEY[16]; | ||
|
|
||
| LORA_HEX_TO_BYTE(DEVADDR, lora_deveui.c_str(), 4); | ||
| LORA_HEX_TO_BYTE(NWKSKEY, lora_appeui.c_str(), 16); | ||
| LORA_HEX_TO_BYTE(APPSKEY, lora_appkey.c_str(), 16); | ||
|
|
||
| LMIC_reset(); | ||
| LMIC_setSession (0x13, LORA_DEVADDR(DEVADDR), NWKSKEY, APPSKEY); | ||
| LMIC_setAdrMode(0); | ||
| LMIC_setClockError(MAX_CLOCK_ERROR * 10 / 100); | ||
| LMIC_selectSubBand(1); | ||
| LMIC_setLinkCheckMode(0); | ||
| LMIC.dn2Dr = DR_SF7; | ||
| Profile_End(LoraTask::modem_reset, 1); | ||
| } | ||
|
|
||
| /// Announce our status to LoraWAN if it's time | ||
| void | ||
| LoraTask::publish(CayenneLPP* lpp) | ||
| { | ||
| Profile_Start(LoraTask::publish); | ||
| DBUGF("LoRa: Starting LoRaWAN broadcast..."); | ||
| // Check if there is not a current TX/RX job running | ||
| if (LMIC.opmode & OP_TXRXPEND) { | ||
| DBUGF("LoRa: Modem busy. Retry later"); | ||
| return; | ||
| } | ||
| LMIC_setTxData2(1, lpp->getBuffer(), lpp->getSize(), false); | ||
| Profile_End(LoraTask::publish, 1); | ||
| } | ||
|
|
||
| unsigned long | ||
| LoraTask::loop(MicroTasks::WakeReason reason) | ||
| { | ||
| if (!config_lora_enabled()) | ||
| return MicroTask.Infinate; | ||
|
|
||
| CayenneLPP lpp(24); | ||
| create_rapi_cayennelpp(_evse, &lpp); | ||
| lora.publish(&lpp); | ||
| return ANNOUNCE_INTERVAL; | ||
| } | ||
|
|
||
| LoraTask lora; | ||
|
|
||
| #endif /* ENABLE_LORA */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright (c) 2019-2023 Alexander von Gluck IV for OpenEVSE | ||
| * | ||
| * ------------------------------------------------------------------- | ||
| * | ||
| * Additional Adaptation of OpenEVSE ESP Wifi | ||
| * by Trystan Lea, Glyn Hudson, OpenEnergyMonitor | ||
| * All adaptation GNU General Public License as below. | ||
| * | ||
| * ------------------------------------------------------------------- | ||
| * | ||
| * This file is part of Open EVSE. | ||
| * Open EVSE is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 3, or (at your option) | ||
| * any later version. | ||
| * Open EVSE is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with Open EVSE; see the file COPYING. If not, write to the | ||
| * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
| * Boston, MA 02111-1307, USA. | ||
| */ | ||
| #ifdef ENABLE_LORA | ||
| #ifndef _LORA_H | ||
| #define _LORA_H | ||
|
|
||
| #include <CayenneLPP.h> | ||
| #include <MicroTasks.h> | ||
|
|
||
| #include "evse_man.h" | ||
|
|
||
| void create_rapi_cayennelpp(EvseManager* _evse, CayenneLPP* lpp); | ||
|
|
||
| class LoraTask : public MicroTasks::Task { | ||
| private: | ||
| EvseManager* _evse; | ||
|
|
||
| protected: | ||
| void setup(); | ||
| unsigned long loop(MicroTasks::WakeReason reason); | ||
| void publish(CayenneLPP* lpp); | ||
| void modem_reset(); | ||
|
|
||
| public: | ||
| LoraTask(); | ||
| void begin(EvseManager &evse); | ||
| }; | ||
|
|
||
| extern LoraTask lora; | ||
|
|
||
| #endif // _LORA_H | ||
| #endif /* ENABLE_LORA */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.