Skip to content

Commit 4018d74

Browse files
committed
lora: Initial work at LoRaWAN support.
1 parent 1309b4e commit 4018d74

File tree

6 files changed

+207
-1
lines changed

6 files changed

+207
-1
lines changed

platformio.ini

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,20 @@ board_build.extra_flags = "-DARDUINO_ESP32_GATEWAY=\'E\'"
298298
platform = ${common.platform_esp32}
299299
board = heltec_wifi_lora_32_V2
300300
framework = arduino
301-
lib_deps = ${common.lib_deps}
301+
lib_deps =
302+
${common.lib_deps}
303+
MCCI LoRaWAN LMIC library
302304
src_build_flags =
303305
${common.version}.dev
304306
${common.src_build_flags}
305307
${common.src_build_flags_esp32}
306308
${common.debug_flags_esp32}
309+
-DENABLE_LORA=1
310+
-DLORA_NSS=18
311+
-DLORA_RST=14
312+
-DLORA_DIO0=26
313+
-DLORA_DIO1=35
314+
-DLORA_DIO2=34
307315
-DWIFI_LED=25
308316
-DWIFI_BUTTON=2
309317
-DWIFI_LED_ON_STATE=HIGH

src/input.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,27 @@ void create_rapi_json(String &data)
9999
//DEBUG.print(emoncms_server.c_str() + String(url));
100100
}
101101

102+
103+
/// A small packed report of unit's status
104+
void create_rapi_packed(uint8_t *data)
105+
{
106+
if (sizeof(data[0]) / sizeof(data) != 8) {
107+
DBUGF("create_rapi_packed: Incorrect data size passed!");
108+
return;
109+
}
110+
// Values with potential > 255 are reduced via a
111+
// conversion factor.
112+
data[0] = state;
113+
data[1] = volt / 2; // CF * 2
114+
data[2] = amp;
115+
data[3] = pilot;
116+
data[4] = elapsed / 60; // CF * 60
117+
data[5] = temp1 / 10; // CF * 10
118+
data[6] = temp2 / 10; // CF * 10
119+
data[7] = temp3 / 10; // CF * 10
120+
}
121+
122+
102123
// -------------------------------------------------------------------
103124
// OpenEVSE Request
104125
//

src/input.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ extern String ohm_hour;
6767
extern void handleRapiRead();
6868
extern void update_rapi_values();
6969
extern void create_rapi_json(String &data);
70+
extern void create_rapi_packed(uint8_t *data);
7071

7172
extern void input_setup();
7273

src/lora.cpp

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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 "lora.h"
34+
35+
#include "app_config.h"
36+
37+
38+
#define LORA_HTOI(c) ((c<='9')?(c-'0'):((c<='F')?(c-'A'+10):((c<='f')?(c-'a'+10):(0))))
39+
#define LORA_TWO_HTOI(h, l) ((LORA_HTOI(h) << 4) + LORA_HTOI(l))
40+
#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]); }
41+
#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)
42+
43+
#define ANNOUNCE_INTERVAL 30 * 1000 // (In Milliseconds)
44+
45+
46+
// TODO: Store these via WebUI? We're doing (simple) ABP activation for now
47+
const char *devAddr = "00000000";
48+
const char *nwkSKey = "00000000000000000000000000000000";
49+
const char *appSKey = "00000000000000000000000000000000";
50+
51+
52+
// LoRaWAN credentials to use
53+
static uint8_t DEVADDR[4];
54+
static uint8_t NWKSKEY[16];
55+
static uint8_t APPSKEY[16];
56+
57+
// Next LoRaWAN announcement
58+
unsigned long nextAnnounce;
59+
60+
// LoRa module pin mapping
61+
const lmic_pinmap lmic_pins = {
62+
.nss = LORA_NSS,
63+
.rxtx = LMIC_UNUSED_PIN,
64+
.rst = LORA_RST,
65+
.dio = {LORA_DIO0, LORA_DIO1, LORA_DIO2},
66+
};
67+
68+
// Used for OTAA, not used (yet)
69+
void os_getArtEui (u1_t* buf) { }
70+
void os_getDevEui (u1_t* buf) { }
71+
void os_getDevKey (u1_t* buf) { }
72+
73+
74+
void onEvent(ev_t ev) {
75+
switch (ev) {
76+
case EV_TXCOMPLETE:
77+
DBUGF("LoRa: TX Complete.");
78+
// LoRaWAN transmission complete
79+
if (LMIC.txrxFlags & TXRX_ACK) {
80+
// Received ack
81+
DBUGF("LoRa: TX ack.");
82+
}
83+
break;
84+
case EV_TXSTART:
85+
DBUGF("LoRa: TX Begin.");
86+
break;
87+
default:
88+
// Ignore anything else for now
89+
break;
90+
}
91+
}
92+
93+
/// Reset LoRa modem. Reload LoRaWAN keys
94+
void lora_reset()
95+
{
96+
LORA_HEX_TO_BYTE(DEVADDR, devAddr, 4);
97+
LORA_HEX_TO_BYTE(NWKSKEY, nwkSKey, 16);
98+
LORA_HEX_TO_BYTE(APPSKEY, appSKey, 16);
99+
100+
LMIC_reset();
101+
LMIC_setSession (0x13, LORA_DEVADDR(DEVADDR), NWKSKEY, APPSKEY);
102+
LMIC_setAdrMode(0);
103+
LMIC_setClockError(MAX_CLOCK_ERROR * 10 / 100);
104+
LMIC_selectSubBand(1);
105+
LMIC_setLinkCheckMode(0);
106+
LMIC.dn2Dr = DR_SF7;
107+
}
108+
109+
110+
/// Initial setup of LoRa modem.
111+
void lora_setup()
112+
{
113+
Profile_Start(lora_setup);
114+
115+
os_init();
116+
lora_reset();
117+
118+
// Set us up for an immeadiate announcement
119+
nextAnnounce = millis();
120+
121+
Profile_End(lora_setup, 1);
122+
}
123+
124+
125+
void lora_publish(uint8_t *dataPacket)
126+
{
127+
if (millis() < nextAnnounce)
128+
return;
129+
130+
Profile_Start(lora_loop);
131+
DBUGF("LoRa: Starting LoRaWAN broadcast...");
132+
// Check if there is not a current TX/RX job running
133+
if (LMIC.opmode & OP_TXRXPEND) {
134+
DBUGF("LoRa: Modem busy. Retry later");
135+
return;
136+
}
137+
138+
LMIC_setTxData2(1, dataPacket, sizeof(dataPacket), true);
139+
nextAnnounce = millis() + ANNOUNCE_INTERVAL;
140+
141+
Profile_End(lora_loop, 1);
142+
}
143+
144+
#else /* !ENABLE_LORA */
145+
146+
#include "emonesp.h"
147+
#include "app_config.h"
148+
149+
void lora_setup() { /*NOP*/ }
150+
void lora_reset() { /*NOP*/ }
151+
void lora_publish(uint8_t *dataPacket) { /*NOP*/ }
152+
153+
#endif

src/lora.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef _LORA_H
2+
#define _LORA_H
3+
4+
5+
void lora_setup();
6+
void lora_reset();
7+
void lora_publish(uint8_t *dataPacket);
8+
9+
10+
#endif // _LORA_H

src/main.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "divert.h"
4141
#include "ota.h"
4242
#include "lcd.h"
43+
#include "lora.h"
4344
#include "openevse.h"
4445
#include "root_ca.h"
4546
#include "hal.h"
@@ -78,6 +79,12 @@ void setup()
7879
net_setup();
7980
DBUGF("After net_setup: %d", HAL.getFreeHeap());
8081

82+
#ifdef ENABLE_LORA
83+
// Initialise LoRa if supported
84+
lora_setup();
85+
DBUGF("After lora_setup: %d", HAL.getFreeHeap());
86+
#endif
87+
8188
// Initialise Mongoose networking library
8289
Mongoose.begin();
8390
Mongoose.setRootCa(root_ca);
@@ -200,6 +207,12 @@ loop() {
200207
}
201208
} // end WiFi connected
202209

210+
#ifdef ENABLE_LORA
211+
uint8_t loraPacket[8];
212+
create_rapi_packed(loraPacket);
213+
lora_publish(loraPacket);
214+
#endif
215+
203216
Profile_End(loop, 10);
204217
} // end loop
205218

0 commit comments

Comments
 (0)