refacto: change lora to class with ITransmitter interface
This commit is contained in:
parent
2d87381903
commit
244d1eea21
7 changed files with 225 additions and 179 deletions
26
src/config.h
26
src/config.h
|
|
@ -1,22 +1,8 @@
|
|||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#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
|
||||
#pragma once
|
||||
|
||||
uint64_t joinEUI = 0x35f48318e1324e2e;
|
||||
uint64_t devEUI = 0x0586fe41112d83d9;
|
||||
uint8_t appKey[] = {
|
||||
0x0b, 0x84, 0xb4, 0x04, 0x0f, 0xd5, 0x56, 0x1b,
|
||||
0x00, 0x53, 0x94, 0x22, 0xc1, 0xf1, 0x4f, 0xd6
|
||||
};
|
||||
uint8_t *nwkKey = appKey;
|
||||
|
||||
#endif
|
||||
uint64_t devEUI = 0x0586fe41112d83d9;
|
||||
uint8_t appKey[] = {
|
||||
0x0b, 0x84, 0xb4, 0x04, 0x0f, 0xd5, 0x56, 0x1b,
|
||||
0x00, 0x53, 0x94, 0x22, 0xc1, 0xf1, 0x4f, 0xd6};
|
||||
uint8_t *nwkKey = appKey;
|
||||
140
src/lora.cpp
140
src/lora.cpp
|
|
@ -1,140 +0,0 @@
|
|||
#include <config.h>
|
||||
|
||||
#include <SPI.h>
|
||||
#include <RadioLib.h>
|
||||
#include <Preferences.h>
|
||||
|
||||
Preferences prefs;
|
||||
SX1276 radio = new Module(LORA_NSS, LORA_DIO0, LORA_RST, LORA_DIO1);
|
||||
LoRaWANNode node(&radio, &EU868);
|
||||
|
||||
uint8_t noncesBuffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE];
|
||||
RTC_DATA_ATTR uint8_t LWsession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE];
|
||||
|
||||
void loraReset()
|
||||
{
|
||||
pinMode(LORA_RST, OUTPUT);
|
||||
digitalWrite(LORA_RST, LOW);
|
||||
delay(10);
|
||||
digitalWrite(LORA_RST, HIGH);
|
||||
delay(10);
|
||||
}
|
||||
|
||||
void loraInit()
|
||||
{
|
||||
loraReset();
|
||||
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);
|
||||
while (true)
|
||||
{
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
Serial.println("OK");
|
||||
}
|
||||
|
||||
void loraRestoreDevNonce()
|
||||
{
|
||||
prefs.begin("lorawan");
|
||||
if (prefs.isKey("nonces"))
|
||||
{
|
||||
prefs.getBytes("nonces", noncesBuffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("[NVS] Aucune donnée trouvée, buffer initialisé à zéro");
|
||||
memset(noncesBuffer, 0, RADIOLIB_LORAWAN_NONCES_BUF_SIZE);
|
||||
}
|
||||
prefs.end();
|
||||
|
||||
node.setBufferNonces(noncesBuffer);
|
||||
}
|
||||
|
||||
void loraPersistDevNonce()
|
||||
{
|
||||
prefs.begin("lorawan");
|
||||
uint8_t buffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE];
|
||||
uint8_t *persist = node.getBufferNonces();
|
||||
memcpy(buffer, persist, RADIOLIB_LORAWAN_NONCES_BUF_SIZE);
|
||||
|
||||
size_t result = prefs.putBytes("nonces", buffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE);
|
||||
|
||||
prefs.end();
|
||||
}
|
||||
|
||||
int16_t loraRestoreSession()
|
||||
{
|
||||
Serial.print("[LoRaWAN] Tentative de restaurer la session...");
|
||||
|
||||
int16_t state = node.setBufferSession(LWsession);
|
||||
if (state == RADIOLIB_ERR_NONE)
|
||||
{
|
||||
state = node.activateOTAA();
|
||||
}
|
||||
else {
|
||||
Serial.println("Echec");
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
void loraSaveSession()
|
||||
{
|
||||
uint8_t *session = node.getBufferSession();
|
||||
memcpy(LWsession, session, RADIOLIB_LORAWAN_SESSION_BUF_SIZE);
|
||||
Serial.println("[RTC] Session sauvegardée");
|
||||
}
|
||||
|
||||
int16_t loraFullJoin()
|
||||
{
|
||||
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
|
||||
loraRestoreDevNonce();
|
||||
|
||||
int16_t state = node.activateOTAA();
|
||||
|
||||
loraPersistDevNonce();
|
||||
return state;
|
||||
}
|
||||
|
||||
void loraJoin()
|
||||
{
|
||||
int16_t state = RADIOLIB_ERR_NETWORK_NOT_JOINED;
|
||||
|
||||
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
|
||||
loraRestoreDevNonce();
|
||||
if (loraRestoreSession() != RADIOLIB_LORAWAN_SESSION_RESTORED)
|
||||
{
|
||||
Serial.print("[LoRaWAN] Join complet...");
|
||||
int16_t state = loraFullJoin();
|
||||
|
||||
if (state != RADIOLIB_LORAWAN_NEW_SESSION)
|
||||
{
|
||||
Serial.printf("Echec : %d\n", state);
|
||||
while (true)
|
||||
{
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println("OK");
|
||||
loraSaveSession();
|
||||
}
|
||||
|
||||
void loraSend(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");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.printf("[TX] Erreur : %d\n", state);
|
||||
}
|
||||
loraSaveSession();
|
||||
}
|
||||
15
src/lora.h
15
src/lora.h
|
|
@ -1,15 +0,0 @@
|
|||
#ifndef LORA_H
|
||||
#define LORA_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
void loraReset();
|
||||
void loraInit();
|
||||
void loraJoin();
|
||||
void loraSend(uint8_t payload[], size_t size);
|
||||
void loraPersistDevNonce();
|
||||
void loraSaveDevNonce();
|
||||
void loraRestoreSession();
|
||||
void loraSaveSession();
|
||||
|
||||
#endif
|
||||
18
src/main.cpp
18
src/main.cpp
|
|
@ -1,16 +1,26 @@
|
|||
#include <lora.h>
|
||||
#include <Arduino.h>
|
||||
#include "LoRaTransmitter.h"
|
||||
#include "config.h"
|
||||
|
||||
ITransmitter* transmitter = new LoRaTransmitter(
|
||||
joinEUI,
|
||||
devEUI,
|
||||
nwkKey,
|
||||
appKey
|
||||
);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(2000);
|
||||
|
||||
loraInit();
|
||||
transmitter->init();
|
||||
transmitter->join();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
loraJoin();
|
||||
uint8_t payload[] = { 0x01, 0x02, 0x03 };
|
||||
loraSend(payload, sizeof(payload));
|
||||
transmitter->send(payload, sizeof(payload));
|
||||
|
||||
Serial.flush();
|
||||
esp_deep_sleep(60000000);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue