Skip to content

Commit

Permalink
feature/lora: Enable LoraWAN supported hardware
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kallisti5 committed Jan 31, 2023
1 parent 751d837 commit 41eb837
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 1 deletion.
14 changes: 14 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ build_flags =
-D AO_DBG_LEVEL=AO_DL_INFO
-D AO_MG_VERSION_614 ; use MG v6.14
-D AO_CA_CERT_LOCAL ; manage certificate locally
-D ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS
-D hal_init=LMICHAL_init ; Workaround mcci arduino-lmic bug 714 on esp32
#-D ENABLE_DEBUG
#-D ENABLE_DEBUG_MONGOOSE_HTTP_CLIENT
-D RAPI_MAX_COMMANDS=20
Expand Down Expand Up @@ -316,6 +318,10 @@ upload_speed = 921600

[env:openevse_esp32-heltec-wifi-lora-v2]
board = heltec_wifi_lora_32_V2
lib_deps =
${common.lib_deps}
MCCI LoRaWAN LMIC library
# https://github.com/mcci-catena/arduino-lmic#selecting-the-lorawan-region-configuration
build_flags =
${common.build_flags}
${common.src_build_flags}
Expand All @@ -328,3 +334,11 @@ build_flags =
-D RAPI_PORT=Serial1
-D RX1=25
-D TX1=27
-D CFG_us915 ; USA 915Mhz
-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
5 changes: 5 additions & 0 deletions src/app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ String mqtt_vehicle_range;
String mqtt_vehicle_eta;
String mqtt_announce_topic;

// LoraWAN network settings
String lora_deveui;
String lora_appeui;
String lora_appkey;

// OCPP 1.6 Settings
String ocpp_server;
String ocpp_chargeBoxId;
Expand Down
10 changes: 10 additions & 0 deletions src/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ extern String mqtt_vehicle_range;
extern String mqtt_vehicle_eta;
extern String mqtt_announce_topic;

// LoraWAN Settings
extern String lora_deveui;
extern String lora_appeui;
extern String lora_appkey;

// OCPP 1.6 Settings
extern String ocpp_server;
extern String ocpp_chargeBoxId;
Expand Down Expand Up @@ -97,6 +102,7 @@ extern uint32_t flags;
#define CONFIG_OCPP_AUTO_AUTH (1 << 22)
#define CONFIG_OCPP_OFFLINE_AUTH (1 << 23)
#define CONFIG_THREEPHASE (1 << 24)
#define CONFIG_SERVICE_LORA (1 << 25)


inline bool config_emoncms_enabled() {
Expand Down Expand Up @@ -127,6 +133,10 @@ inline bool config_mqtt_reject_unauthorized() {
return 0 == (flags & CONFIG_MQTT_ALLOW_ANY_CERT);
}

inline bool config_lora_enabled() {
return CONFIG_SERVICE_LORA == (flags & CONFIG_SERVICE_LORA);
}

inline bool config_ocpp_enabled() {
return CONFIG_SERVICE_OCPP == (flags & CONFIG_SERVICE_OCPP);
}
Expand Down
23 changes: 23 additions & 0 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,29 @@ void create_rapi_json(JsonDocument &doc)
doc["elapsed"] = evse.getSessionElapsed();
}

/// A small packed report of unit's status
void create_rapi_packed(uint8_t *data)
{
if (sizeof(data[0]) / sizeof(data) != 8) {
DBUGF("create_rapi_packed: Incorrect data size passed!");
return;
}
// Values with potential > 255 are reduced via a
// conversion factor.
data[0] = evse.getEvseState();
data[1] = evse.getVoltage() / 2; // CF * 2
data[2] = evse.getAmps();
data[3] = evse.getChargeCurrent();
data[4] = evse.getSessionElapsed() / 60; // CF * 60

if(evse.isTemperatureValid(EVSE_MONITOR_TEMP_MONITOR))
data[5] = evse.getTemperature(EVSE_MONITOR_TEMP_MONITOR) * TEMP_SCALE_FACTOR;
if(evse.isTemperatureValid(EVSE_MONITOR_TEMP_MAX))
data[6] = evse.getTemperature(EVSE_MONITOR_TEMP_MAX) * TEMP_SCALE_FACTOR;

// data[7] what?
}

void
handleRapiRead()
{
Expand Down
1 change: 1 addition & 0 deletions src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern String ohm_hour;

extern void handleRapiRead();
extern void create_rapi_json(JsonDocument &data);
extern void create_rapi_packed(uint8_t* data);

extern void input_setup();

Expand Down
152 changes: 152 additions & 0 deletions src/lora.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* 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 "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)

#define ANNOUNCE_INTERVAL 30 * 1000 // (In Milliseconds)


// TODO: Store these via WebUI? We're doing (simple) ABP activation for now
//const char *devAddr = "00000000";
//const char *nwkSKey = "00000000000000000000000000000000";
//const char *appSKey = "00000000000000000000000000000000";


// LoRaWAN credentials to use
static uint8_t DEVADDR[4];
static uint8_t NWKSKEY[16];
static uint8_t APPSKEY[16];

// Next LoRaWAN announcement
unsigned long nextAnnounce;

// 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 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;
}
}

/// Reset LoRa modem. Reload LoRaWAN keys
void lora_reset()
{
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;
}

/// Initial setup of LoRa modem.
void lora_setup()
{
Profile_Start(lora_setup);

os_init();
lora_reset();

// Set us up for an immeadiate announcement
nextAnnounce = millis();

Profile_End(lora_setup, 1);
}

/// Announce our status to LoraWAN if it's time
void lora_publish(uint8_t *dataPacket)
{
if (millis() < nextAnnounce)
return;

Profile_Start(lora_loop);
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, dataPacket, sizeof(dataPacket), true);
nextAnnounce = millis() + ANNOUNCE_INTERVAL;

Profile_End(lora_loop, 1);
}

#else /* !ENABLE_LORA */

#include "emonesp.h"
#include "app_config.h"

void lora_setup() { /*NOP*/ }
void lora_reset() { /*NOP*/ }
void lora_publish(uint8_t *dataPacket) { /*NOP*/ }

#endif
10 changes: 10 additions & 0 deletions src/lora.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _LORA_H
#define _LORA_H


void lora_setup();
void lora_reset();
void lora_publish(uint8_t *dataPacket);


#endif // _LORA_H
15 changes: 14 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "divert.h"
#include "ota.h"
#include "lcd.h"
#include "lora.h"
#include "openevse.h"
#include "root_ca.h"
#include "espal.h"
Expand Down Expand Up @@ -139,6 +140,12 @@ void setup()
net_setup();
DBUGF("After net_setup: %d", ESPAL.getFreeHeap());

#ifdef ENABLE_LORA
// initialise LoRA if supported
lora_setup();
DBUGF("After lora_setup: %d" ESPAL.getFreeHeap());
#endif

// Initialise Mongoose networking library
Mongoose.begin();
Mongoose.setRootCa(root_ca);
Expand Down Expand Up @@ -248,6 +255,12 @@ loop() {
}
} // end WiFi connected

#ifdef ENABLE_LORA
uint8_t loraPacket[8];
create_rapi_packed(loraPacket);
lora_publish(loraPacket);
#endif

if(DEBUG_PORT.available()) {
handle_serial();
}
Expand Down Expand Up @@ -335,4 +348,4 @@ void handle_serial()
DEBUG_PORT.printf("{\"code\":200,\"msg\":\"%s\"}\n", config_modified ? "done" : "no change");
}
}
}
}

0 comments on commit 41eb837

Please sign in to comment.