Reorganize project in multiple files for readability
This commit is contained in:
parent
bfb177c0c8
commit
01289ec357
4 changed files with 87 additions and 58 deletions
22
src/config.h
Normal file
22
src/config.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#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
|
||||
|
||||
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
|
||||
49
src/lora.cpp
Normal file
49
src/lora.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include <config.h>
|
||||
|
||||
#include <SPI.h>
|
||||
#include <RadioLib.h>
|
||||
|
||||
SX1276 radio = new Module(LORA_NSS, LORA_DIO0, LORA_RST, LORA_DIO1);
|
||||
LoRaWANNode node(&radio, &EU868);
|
||||
|
||||
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("[LoRa] Init... ");
|
||||
int state = radio.begin();
|
||||
if (state != RADIOLIB_ERR_NONE) {
|
||||
Serial.printf("ERREUR %d\n", state);
|
||||
while (true) { delay(1000); }
|
||||
}
|
||||
Serial.println("OK");
|
||||
}
|
||||
|
||||
void loraJoin(){
|
||||
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
|
||||
|
||||
Serial.print("[LoRaWAN] Join... ");
|
||||
int state = node.activateOTAA();
|
||||
if (state != RADIOLIB_LORAWAN_NEW_SESSION) {
|
||||
Serial.printf("Echec : %d\n", state);
|
||||
while (true) { delay(1000); }
|
||||
}
|
||||
Serial.println("Join OK !");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
11
src/lora.h
Normal file
11
src/lora.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef LORA_H
|
||||
#define LORA_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
void loraReset();
|
||||
void loraInit();
|
||||
void loraJoin();
|
||||
void loraSend(uint8_t payload[], size_t size);
|
||||
|
||||
#endif
|
||||
63
src/main.cpp
63
src/main.cpp
|
|
@ -1,68 +1,15 @@
|
|||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
#include <RadioLib.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
|
||||
|
||||
void loraReset() {
|
||||
pinMode(LORA_RST, OUTPUT);
|
||||
digitalWrite(LORA_RST, LOW);
|
||||
delay(10);
|
||||
digitalWrite(LORA_RST, HIGH);
|
||||
delay(10);
|
||||
}
|
||||
|
||||
SX1276 radio = new Module(LORA_NSS, LORA_DIO0, LORA_RST, LORA_DIO1);
|
||||
|
||||
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;
|
||||
|
||||
LoRaWANNode node(&radio, &EU868);
|
||||
#include <lora.h>
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(2000);
|
||||
|
||||
loraReset();
|
||||
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_NSS);
|
||||
|
||||
Serial.print("[LoRa] Init... ");
|
||||
int state = radio.begin();
|
||||
if (state != RADIOLIB_ERR_NONE) {
|
||||
Serial.printf("ERREUR %d\n", state);
|
||||
while (true) { delay(1000); }
|
||||
}
|
||||
Serial.println("OK");
|
||||
|
||||
node.beginOTAA(joinEUI, devEUI, nwkKey, appKey);
|
||||
|
||||
Serial.print("[LoRaWAN] Join... ");
|
||||
state = node.activateOTAA();
|
||||
if (state != RADIOLIB_LORAWAN_NEW_SESSION) {
|
||||
Serial.printf("Echec : %d\n", state);
|
||||
while (true) { delay(1000); }
|
||||
}
|
||||
Serial.println("Join OK !");
|
||||
|
||||
loraInit();
|
||||
loraJoin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
uint8_t payload[] = { 0x01, 0x02, 0x03 };
|
||||
int state = node.sendReceive(payload, sizeof(payload), 1);
|
||||
if (state == RADIOLIB_ERR_NONE || state == RADIOLIB_LORAWAN_NO_DOWNLINK) {
|
||||
Serial.println("[TX] OK");
|
||||
} else {
|
||||
Serial.printf("[TX] Erreur : %d\n", state);
|
||||
}
|
||||
loraSend(payload, sizeof(payload));
|
||||
delay(60000);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue