Skip to content

Commit 9802797

Browse files
committed
feature/lora: Add LoraWAN communications on supported hardware
* Allows OpenEVSE units (with a LoraWAN compatible modem) to announce their status up to several miles to TheThingsNetwork or Helium IoT gateways. * Must be using a ESP32 with a built-in LoRa modem, or by manually attaching a LoRA modem to a stock ESP32 * Needs gui changes too.. however holding on that until better tested
1 parent 751d837 commit 9802797

File tree

8 files changed

+299
-1
lines changed

8 files changed

+299
-1
lines changed

platformio.ini

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ build_flags =
9797
build_partitions = min_spiffs.csv
9898
build_partitions_debug = min_spiffs_debug.csv
9999

100+
# Used for LoRaWAN builds
101+
# https://github.com/mcci-catena/arduino-lmic#selecting-the-lorawan-region-configuration
102+
lora_lib = MCCI LoRaWAN LMIC library
103+
lora_build_flags =
104+
-D ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS
105+
-D hal_init=LMICHAL_init ; Workaround mcci arduino-lmic bug 714 on esp32
106+
-D CFG_us915 ; USA 915Mhz
107+
100108
neopixel_lib = adafruit/Adafruit NeoPixel@1.7.0
101109

102110

@@ -316,9 +324,13 @@ upload_speed = 921600
316324

317325
[env:openevse_esp32-heltec-wifi-lora-v2]
318326
board = heltec_wifi_lora_32_V2
327+
lib_deps =
328+
${common.lib_deps}
329+
${common.lora_lib}
319330
build_flags =
320331
${common.build_flags}
321332
${common.src_build_flags}
333+
${common.lora_build_flags}
322334
${common.version}.dev
323335
-D DEBUG_PORT=Serial
324336
-D WIFI_LED=25
@@ -328,3 +340,10 @@ build_flags =
328340
-D RAPI_PORT=Serial1
329341
-D RX1=25
330342
-D TX1=27
343+
-D CFG_sx1276_radio ; SX1275 radio
344+
-D ENABLE_LORA=1
345+
-D LORA_NSS=18
346+
-D LORA_RST=14
347+
-D LORA_DIO0=26
348+
-D LORA_DIO1=35
349+
-D LORA_DIO2=34

src/app_config.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ String mqtt_vehicle_range;
6262
String mqtt_vehicle_eta;
6363
String mqtt_announce_topic;
6464

65+
// LoraWAN network settings
66+
String lora_deveui;
67+
String lora_appeui;
68+
String lora_appkey;
69+
6570
// OCPP 1.6 Settings
6671
String ocpp_server;
6772
String ocpp_chargeBoxId;
@@ -181,6 +186,11 @@ ConfigOpt *opts[] =
181186
// RFID storage
182187
new ConfigOptDefenition<String>(rfid_storage, "", "rfid_storage", "rs"),
183188

189+
// Lora settings
190+
new ConfigOptDefenition<String>(lora_deveui, "", "lora_deveui", "lde"),
191+
new ConfigOptDefenition<String>(lora_appeui, "", "lora_appeui", "lae"),
192+
new ConfigOptDefenition<String>(lora_appeui, "", "lora_appkey", "lak"),
193+
184194
#if RGB_LED
185195
// LED brightness
186196
new ConfigOptDefenition<uint8_t>(led_brightness, LED_DEFAULT_BRIGHTNESS, "led_brightness", "lb"),
@@ -439,6 +449,19 @@ config_save_ohm(bool enable, String qohm)
439449
user_config.commit();
440450
}
441451

452+
void
453+
config_save_lora(bool enable, String devEui, String appEui, String appKey)
454+
{
455+
uint32_t newflags = flags & ~CONFIG_LORA;
456+
if(enable)
457+
newflags |= CONFIG_LORA;
458+
459+
user_config.set("flags", newflags);
460+
user_config.set("lora_deveui", devEui);
461+
user_config.set("lora_appeui", appEui);
462+
user_config.set("lora_appkey", appKey);
463+
}
464+
442465
void
443466
config_save_rfid(bool enable, String storage){
444467
uint32_t newflags = flags & ~CONFIG_RFID;

src/app_config.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ extern String mqtt_vehicle_range;
4949
extern String mqtt_vehicle_eta;
5050
extern String mqtt_announce_topic;
5151

52+
// LoraWAN Settings
53+
extern String lora_deveui;
54+
extern String lora_appeui;
55+
extern String lora_appkey;
56+
5257
// OCPP 1.6 Settings
5358
extern String ocpp_server;
5459
extern String ocpp_chargeBoxId;
@@ -97,6 +102,7 @@ extern uint32_t flags;
97102
#define CONFIG_OCPP_AUTO_AUTH (1 << 22)
98103
#define CONFIG_OCPP_OFFLINE_AUTH (1 << 23)
99104
#define CONFIG_THREEPHASE (1 << 24)
105+
#define CONFIG_LORA (1 << 25)
100106

101107

102108
inline bool config_emoncms_enabled() {
@@ -127,6 +133,10 @@ inline bool config_mqtt_reject_unauthorized() {
127133
return 0 == (flags & CONFIG_MQTT_ALLOW_ANY_CERT);
128134
}
129135

136+
inline bool config_lora_enabled() {
137+
return CONFIG_LORA == (flags & CONFIG_LORA);
138+
}
139+
130140
inline bool config_ocpp_enabled() {
131141
return CONFIG_SERVICE_OCPP == (flags & CONFIG_SERVICE_OCPP);
132142
}
@@ -232,6 +242,11 @@ extern void config_save_ohm(bool enable, String qohm);
232242
// -------------------------------------------------------------------
233243
extern void config_save_rfid(bool enable, String storage);
234244

245+
// -------------------------------------------------------------------
246+
// Save Lora settings
247+
// -------------------------------------------------------------------
248+
extern void config_save_lora(bool enable, String devEui, String appEui, String appKey);
249+
235250
// -------------------------------------------------------------------
236251
// Save the flags
237252
// -------------------------------------------------------------------

src/input.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,29 @@ void create_rapi_json(JsonDocument &doc)
155155
doc["elapsed"] = evse.getSessionElapsed();
156156
}
157157

158+
/// A small packed report of unit's status
159+
void create_rapi_packed(uint8_t *data)
160+
{
161+
if (sizeof(data[0]) / sizeof(data) != 8) {
162+
DBUGF("create_rapi_packed: Incorrect data size passed!");
163+
return;
164+
}
165+
// Values with potential > 255 are reduced via a
166+
// conversion factor.
167+
data[0] = evse.getEvseState();
168+
data[1] = evse.getVoltage() / 2; // CF * 2
169+
data[2] = evse.getAmps();
170+
data[3] = evse.getChargeCurrent();
171+
data[4] = evse.getSessionElapsed() / 60; // CF * 60
172+
173+
if(evse.isTemperatureValid(EVSE_MONITOR_TEMP_MONITOR))
174+
data[5] = evse.getTemperature(EVSE_MONITOR_TEMP_MONITOR) * TEMP_SCALE_FACTOR;
175+
if(evse.isTemperatureValid(EVSE_MONITOR_TEMP_MAX))
176+
data[6] = evse.getTemperature(EVSE_MONITOR_TEMP_MAX) * TEMP_SCALE_FACTOR;
177+
178+
// data[7] what?
179+
}
180+
158181
void
159182
handleRapiRead()
160183
{

src/input.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern String ohm_hour;
1717

1818
extern void handleRapiRead();
1919
extern void create_rapi_json(JsonDocument &data);
20+
extern void create_rapi_packed(uint8_t* data);
2021

2122
extern void input_setup();
2223

src/lora.cpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright (c) 2019-2020 Alexander von Gluck IV for OpenEVSE
3+
*
4+
* -------------------------------------------------------------------
5+
*
6+
* Additional Adaptation of OpenEVSE ESP Wifi
7+
* by Trystan Lea, Glyn Hudson, OpenEnergyMonitor
8+
* All adaptation GNU General Public License as below.
9+
*
10+
* -------------------------------------------------------------------
11+
*
12+
* This file is part of Open EVSE.
13+
* Open EVSE is free software; you can redistribute it and/or modify
14+
* it under the terms of the GNU General Public License as published by
15+
* the Free Software Foundation; either version 3, or (at your option)
16+
* any later version.
17+
* Open EVSE is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU General Public License for more details.
21+
* You should have received a copy of the GNU General Public License
22+
* along with Open EVSE; see the file COPYING. If not, write to the
23+
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24+
* Boston, MA 02111-1307, USA.
25+
*/
26+
#ifdef ENABLE_LORA
27+
28+
#include <lmic.h>
29+
#include <hal/hal.h>
30+
#include <SPI.h>
31+
32+
#include "emonesp.h"
33+
#include "input.h"
34+
#include "lora.h"
35+
36+
#include "app_config.h"
37+
38+
39+
#define LORA_HTOI(c) ((c<='9')?(c-'0'):((c<='F')?(c-'A'+10):((c<='f')?(c-'a'+10):(0))))
40+
#define LORA_TWO_HTOI(h, l) ((LORA_HTOI(h) << 4) + LORA_HTOI(l))
41+
#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]); }
42+
#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)
43+
44+
#define ANNOUNCE_INTERVAL 30 * 1000 // (In Milliseconds)
45+
46+
47+
// LoRa module pin mapping
48+
const lmic_pinmap lmic_pins = {
49+
.nss = LORA_NSS,
50+
.rxtx = LMIC_UNUSED_PIN,
51+
.rst = LORA_RST,
52+
.dio = {LORA_DIO0, LORA_DIO1, LORA_DIO2},
53+
};
54+
55+
// Used for OTAA, not used (yet)
56+
void os_getArtEui (u1_t* buf) { }
57+
void os_getDevEui (u1_t* buf) { }
58+
void os_getDevKey (u1_t* buf) { }
59+
60+
/// Reset LoRa modem. Reload LoRaWAN keys
61+
void onEvent(ev_t ev) {
62+
switch (ev) {
63+
case EV_TXCOMPLETE:
64+
DBUGF("LoRa: TX Complete.");
65+
// LoRaWAN transmission complete
66+
if (LMIC.txrxFlags & TXRX_ACK) {
67+
// Received ack
68+
DBUGF("LoRa: TX ack.");
69+
}
70+
break;
71+
case EV_TXSTART:
72+
DBUGF("LoRa: TX Begin.");
73+
break;
74+
default:
75+
// Ignore anything else for now
76+
break;
77+
}
78+
}
79+
80+
LoraTask::LoraTask()
81+
:
82+
MicroTasks::Task()
83+
{
84+
}
85+
86+
void
87+
LoraTask::begin(EvseManager &evse)
88+
{
89+
_evse = &evse;
90+
MicroTask.startTask(this);
91+
}
92+
93+
/// Initial setup of LoRa modem.
94+
void
95+
LoraTask::setup()
96+
{
97+
Profile_Start(LoraTask::setup);
98+
99+
os_init();
100+
modem_reset();
101+
102+
Profile_End(LoraTask::setup, 1);
103+
}
104+
105+
/// Reset LoRa modem. Reload LoRaWAN keys
106+
void
107+
LoraTask::modem_reset()
108+
{
109+
Profile_Start(LoraTask::modem_reset);
110+
// LoRaWAN credentials to use
111+
uint8_t DEVADDR[4];
112+
uint8_t NWKSKEY[16];
113+
uint8_t APPSKEY[16];
114+
115+
LORA_HEX_TO_BYTE(DEVADDR, lora_deveui.c_str(), 4);
116+
LORA_HEX_TO_BYTE(NWKSKEY, lora_appeui.c_str(), 16);
117+
LORA_HEX_TO_BYTE(APPSKEY, lora_appkey.c_str(), 16);
118+
119+
LMIC_reset();
120+
LMIC_setSession (0x13, LORA_DEVADDR(DEVADDR), NWKSKEY, APPSKEY);
121+
LMIC_setAdrMode(0);
122+
LMIC_setClockError(MAX_CLOCK_ERROR * 10 / 100);
123+
LMIC_selectSubBand(1);
124+
LMIC_setLinkCheckMode(0);
125+
LMIC.dn2Dr = DR_SF7;
126+
Profile_End(LoraTask::modem_reset, 1);
127+
}
128+
129+
/// Announce our status to LoraWAN if it's time
130+
void
131+
LoraTask::publish(uint8_t* dataPacket)
132+
{
133+
Profile_Start(LoraTask::publish);
134+
DBUGF("LoRa: Starting LoRaWAN broadcast...");
135+
// Check if there is not a current TX/RX job running
136+
if (LMIC.opmode & OP_TXRXPEND) {
137+
DBUGF("LoRa: Modem busy. Retry later");
138+
return;
139+
}
140+
LMIC_setTxData2(1, dataPacket, sizeof(dataPacket), true);
141+
Profile_End(LoraTask::publish, 1);
142+
}
143+
144+
unsigned long
145+
LoraTask::loop(MicroTasks::WakeReason reason)
146+
{
147+
if (!config_lora_enabled())
148+
return 1000;
149+
150+
uint8_t loraPacket[8];
151+
create_rapi_packed(loraPacket);
152+
lora.publish(loraPacket);
153+
return ANNOUNCE_INTERVAL;
154+
}
155+
156+
LoraTask lora;
157+
158+
#endif /* ENABLE_LORA */

src/lora.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2019-2023 Alexander von Gluck IV for OpenEVSE
3+
*
4+
* -------------------------------------------------------------------
5+
*
6+
* Additional Adaptation of OpenEVSE ESP Wifi
7+
* by Trystan Lea, Glyn Hudson, OpenEnergyMonitor
8+
* All adaptation GNU General Public License as below.
9+
*
10+
* -------------------------------------------------------------------
11+
*
12+
* This file is part of Open EVSE.
13+
* Open EVSE is free software; you can redistribute it and/or modify
14+
* it under the terms of the GNU General Public License as published by
15+
* the Free Software Foundation; either version 3, or (at your option)
16+
* any later version.
17+
* Open EVSE is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU General Public License for more details.
21+
* You should have received a copy of the GNU General Public License
22+
* along with Open EVSE; see the file COPYING. If not, write to the
23+
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24+
* Boston, MA 02111-1307, USA.
25+
*/
26+
#ifdef ENABLE_LORA
27+
#ifndef _LORA_H
28+
#define _LORA_H
29+
30+
#include <MicroTasks.h>
31+
32+
#include "evse_man.h"
33+
34+
35+
class LoraTask : public MicroTasks::Task {
36+
private:
37+
EvseManager* _evse;
38+
39+
protected:
40+
void setup();
41+
unsigned long loop(MicroTasks::WakeReason reason);
42+
void publish(uint8_t* dataPacket);
43+
void modem_reset();
44+
45+
public:
46+
LoraTask();
47+
void begin(EvseManager &evse);
48+
};
49+
50+
extern LoraTask lora;
51+
52+
#endif // _LORA_H
53+
#endif /* ENABLE_LORA */

0 commit comments

Comments
 (0)