Ajout du wrapper orienté objet afin de faciliter l'utilisation du module LoRa. Ce wrapper utilise LMIC en interne
This commit is contained in:
parent
62019634c6
commit
4f452fc2c1
64
lib/LoRaRadio/LoRaRadio.cpp
Normal file
64
lib/LoRaRadio/LoRaRadio.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
#include "LoRaRadio.h"
|
||||
|
||||
lmic_pinmap lmic_pins = {0};
|
||||
|
||||
LoRaRadio::LoRaRadio(PinMap pinMap, dr_t dataRate, s1_t txPower) :_pinMap(pinMap), _dataRate(dataRate), _txPower(txPower)
|
||||
{
|
||||
lmic_pins.nss = pinMap._nss;
|
||||
lmic_pins.rxtx = pinMap._rxtx;
|
||||
lmic_pins.rst = pinMap._rst;
|
||||
lmic_pins.dio[0] = pinMap._dio[0];
|
||||
lmic_pins.dio[1] = pinMap._dio[1];
|
||||
lmic_pins.dio[2] = pinMap._dio[2];
|
||||
}
|
||||
|
||||
void LoRaRadio::init()
|
||||
{
|
||||
os_init();
|
||||
LMIC_reset();
|
||||
LMIC_setDrTxpow(_dataRate, _txPower);
|
||||
}
|
||||
|
||||
void LoRaRadio::setMCUClockError(u2_t percent)
|
||||
{
|
||||
LMIC_setClockError(MAX_CLOCK_ERROR * percent / 100);
|
||||
}
|
||||
|
||||
void LoRaRadio::setTTNSession(u4_t channelId, devaddr_t deviceAddress, xref2u1_t networkSystemKey, xref2u1_t applicationSystemKey, bit_t linkCheckMode)
|
||||
{
|
||||
LMIC_setSession(channelId, deviceAddress, networkSystemKey, applicationSystemKey);
|
||||
LMIC_setLinkCheckMode(linkCheckMode);
|
||||
}
|
||||
|
||||
void LoRaRadio::setRadioEUChannels()
|
||||
{
|
||||
LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
|
||||
LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI); // g-band
|
||||
LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
|
||||
LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
|
||||
LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
|
||||
LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
|
||||
LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
|
||||
LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
|
||||
LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK, DR_FSK), BAND_MILLI); // g2-band
|
||||
LMIC.dn2Dr = DR_SF9;
|
||||
}
|
||||
|
||||
void LoRaRadio::send(u1_t port, uint8_t *data, uint8_t length, u1_t confirmed)
|
||||
{
|
||||
if (LMIC.opmode & OP_TXRXPEND)
|
||||
{
|
||||
//Serial.println(F("OP_TXRXPEND, not sending"));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Prepare upstream data transmission at the next possible time.
|
||||
LMIC_setTxData2(port, data, length, confirmed);
|
||||
Serial.println("Packet queued");
|
||||
}
|
||||
}
|
||||
|
||||
void LoRaRadio::run()
|
||||
{
|
||||
os_runloop_once();
|
||||
}
|
42
lib/LoRaRadio/LoRaRadio.h
Normal file
42
lib/LoRaRadio/LoRaRadio.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef LORARADIO_H
|
||||
#define LORARADIO_H
|
||||
|
||||
#include <lmic.h>
|
||||
#include <hal/hal.h>
|
||||
#include <SPI.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
class PinMap
|
||||
{
|
||||
friend class LoRaRadio;
|
||||
public:
|
||||
PinMap(u1_t nss, u1_t rxtx, u1_t rst, const u1_t dio[3]) : _nss(nss), _rxtx(rxtx), _rst(rst)
|
||||
{
|
||||
_dio[0] = dio[0];
|
||||
_dio[1] = dio[1];
|
||||
_dio[2] = dio[2];
|
||||
}
|
||||
protected:
|
||||
private:
|
||||
u1_t _nss, _rxtx, _rst, _dio[3];
|
||||
};
|
||||
|
||||
class LoRaRadio
|
||||
{
|
||||
public:
|
||||
LoRaRadio(PinMap pinMap, dr_t dataRate = DR_SF7, s1_t txPower = 23);
|
||||
|
||||
void init();
|
||||
void setTTNSession(u4_t channelId, devaddr_t deviceAddress, xref2u1_t networkSystemKey, xref2u1_t applicationSystemKey, bit_t linkCheckMode = 0);
|
||||
void setRadioEUChannels();
|
||||
void setMCUClockError(u2_t percent = 30);
|
||||
void send(u1_t port, uint8_t *data, uint8_t length, u1_t confirmed = false);
|
||||
void run();
|
||||
protected:
|
||||
private:
|
||||
dr_t _dataRate;
|
||||
s1_t _txPower;
|
||||
PinMap _pinMap;
|
||||
};
|
||||
|
||||
#endif //LORARADIO_H
|
54
lib/LoRaRadio/LoRaRadio.ino
Normal file
54
lib/LoRaRadio/LoRaRadio.ino
Normal file
@ -0,0 +1,54 @@
|
||||
#include <Arduino.h>
|
||||
#include "LoRaRadio.h"
|
||||
#include "PayloadFormatter.h"
|
||||
|
||||
u1_t dio[3] = {26,33,32};
|
||||
|
||||
void os_getArtEui (u1_t* buf) { }
|
||||
void os_getDevEui (u1_t* buf) { }
|
||||
void os_getDevKey (u1_t* buf) { }
|
||||
void onEvent(ev_t ev)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static u1_t NWKSKEY[16] = { 0x1F, 0x9E, 0xE2, 0x7A, 0xC8, 0xBA, 0xE8, 0xEA, 0xF5, 0xC2, 0x5E, 0x47, 0x5D, 0xE0, 0x77, 0x55 };
|
||||
static u1_t APPSKEY[16] = { 0x3B, 0x89, 0x86, 0x96, 0xBB, 0xAA, 0x38, 0x1E, 0x1F, 0xC4, 0xAD, 0x03, 0xEF, 0x3F, 0x56, 0x12 };
|
||||
static u4_t DEVADDR = 0x260113D3;//0x03FF0001 ; // <-- Change this address for every node!
|
||||
|
||||
unsigned long lapse(0);
|
||||
|
||||
PinMap pinMap(18, LMIC_UNUSED_PIN, 14, dio);
|
||||
LoRaRadio radio(pinMap);
|
||||
|
||||
PayloadFormatter pf(1,4);
|
||||
DateTime payloadDate(2020,12,26,8,42);
|
||||
double temps[4] = {21.31, 20.35, 21.25, 21.36};
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("Starting setup");
|
||||
radio.init();
|
||||
radio.setTTNSession(0x1, DEVADDR, NWKSKEY, APPSKEY);
|
||||
radio.setRadioEUChannels();
|
||||
radio.setMCUClockError();
|
||||
lapse = millis();
|
||||
Serial.println("Ending setup");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
radio.run();
|
||||
if(millis() - lapse > 30000)
|
||||
{
|
||||
Serial.printf("Building payload\n");
|
||||
pf.startSession(1);
|
||||
uint8_t *p(NULL);
|
||||
uint8_t size = pf.buildPayload(&p, &payloadDate,temps);
|
||||
pf.endSession();
|
||||
Serial.printf("Sending data\n");
|
||||
radio.send(1, p, size);
|
||||
lapse = millis();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user