diff --git a/lib/Transmitter/ITransmitter.h b/lib/Transmitter/ITransmitter.h new file mode 100644 index 0000000..ac24ad0 --- /dev/null +++ b/lib/Transmitter/ITransmitter.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include + +enum class TransmitError : int16_t { + OK = 0, + INIT_FAILED = -1, + JOIN_FAILED = -2, + SEND_FAILED = -3, +}; + +class ITransmitter { +public: + virtual ~ITransmitter() = default; + virtual TransmitError init() = 0; + virtual TransmitError join() = 0; + virtual TransmitError send(uint8_t* payload, size_t size) = 0; +}; \ No newline at end of file diff --git a/lib/Transmitter/LoRaTransmitter.cpp b/lib/Transmitter/LoRaTransmitter.cpp new file mode 100644 index 0000000..d241869 --- /dev/null +++ b/lib/Transmitter/LoRaTransmitter.cpp @@ -0,0 +1,76 @@ +#include + +RTC_DATA_ATTR uint8_t LWsession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE]; + +LoRaTransmitter::LoRaTransmitter( + uint64_t joinEUI, + uint64_t devEUI, + uint8_t* nwkKey, + uint8_t* appKey +) + : _joinEUI(joinEUI), + _devEUI(devEUI), + _radio(new Module(LORA_NSS, LORA_DIO0, LORA_RST, LORA_DIO1)), + _node(&_radio, &EU868) +{ + memcpy(_nwkKey, nwkKey, 16); + memcpy(_appKey, appKey, 16); +} + +TransmitError LoRaTransmitter::init() +{ + _reset(); + SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_NSS); + + Serial.print("[LoRaWAN] Init... "); + int state = _radio.begin(); + if (state != RADIOLIB_ERR_NONE) + { + Serial.printf("ERREUR %d\n", state); + return TransmitError::INIT_FAILED; + } + Serial.println("OK"); + return TransmitError::OK; +} + +TransmitError LoRaTransmitter::join() +{ + int16_t state = RADIOLIB_ERR_NETWORK_NOT_JOINED; + + _node.beginOTAA(_joinEUI, _devEUI, _nwkKey, _appKey); + state = _node.activateOTAA(); + + if (state != RADIOLIB_LORAWAN_NEW_SESSION) + { + Serial.printf("Echec : %d\n", state); + return TransmitError::JOIN_FAILED; + } + + Serial.println("OK"); + return TransmitError::OK; +} + +TransmitError LoRaTransmitter::send(uint8_t payload[], size_t size) +{ + int state = _node.sendReceive(payload, size, 1); + if (state == RADIOLIB_ERR_NONE || state == RADIOLIB_LORAWAN_NO_DOWNLINK) + { + Serial.println("[TX] OK"); + return TransmitError::OK; + } + else + { + Serial.printf("[TX] Erreur : %d\n", state); + return TransmitError::SEND_FAILED; + } + +} + +void LoRaTransmitter::_reset() +{ + pinMode(LORA_RST, OUTPUT); + digitalWrite(LORA_RST, LOW); + delay(10); + digitalWrite(LORA_RST, HIGH); + delay(10); +} diff --git a/lib/Transmitter/LoRaTransmitter.h b/lib/Transmitter/LoRaTransmitter.h new file mode 100644 index 0000000..4fba9dc --- /dev/null +++ b/lib/Transmitter/LoRaTransmitter.h @@ -0,0 +1,37 @@ +#pragma once +#include +#include +#include + +#define LORA_SCK 5 +#define LORA_MISO 19 +#define LORA_MOSI 27 +#define LORA_NSS 18 +#define LORA_RST 23 +#define LORA_DIO0 26 +#define LORA_DIO1 33 + +class LoRaTransmitter : public ITransmitter { +public: + LoRaTransmitter( + uint64_t joinEUI, + uint64_t devEUI, + uint8_t* nwkKey, + uint8_t* appKey + ); + + TransmitError init() override; + TransmitError join() override; + TransmitError send(uint8_t* payload, size_t size) override; + +private: + uint64_t _joinEUI; + uint64_t _devEUI; + uint8_t _nwkKey[16]; + uint8_t _appKey[16]; + + SX1276 _radio; + LoRaWANNode _node; + + void _reset(); +}; \ No newline at end of file