add : ajoute la bibliothèque LoRaTransmitter pour envoyer les données
This commit is contained in:
parent
4fd89ecfad
commit
c022aa8a4a
3 changed files with 131 additions and 0 deletions
76
lib/Transmitter/LoRaTransmitter.cpp
Normal file
76
lib/Transmitter/LoRaTransmitter.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#include <LoRaTransmitter.h>
|
||||
|
||||
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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue