|
| 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 */ |
0 commit comments