I am trying to use the library to communicate with TN. Also, I am trying to receive a package with the LoRa.h library. I’m new in general with the library. But, I already tried with a github example and managed to send a hello world. My node is a lora32u4 ii from bsfrance. I share my code. stay tuned
I am new in general with the library. my node is a lora32u4 ii by bsfrance. I share my code. stay tuned
`#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <Arduino.h>
#include <LoRa.h>
//Lora
//LoR32u4II 868MHz or 915MHz (black board)
#define SCK 15
#define MISO 14
#define MOSI 16
#define SS 8
#define RST 4
#define DI0 7
#define BAND 915E6
static const u1_t PROGMEM APPEUI[8]= { };
static const u1_t PROGMEM DEVEUI[8] = { };
static const u1_t PROGMEM APPKEY[16] = { };
void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8); }
void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8); }
void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16); }
static uint8_t mydata[] = “Hello, world!”;
static osjob_t sendjob;
bool loraData = true;
int i = 0;
const unsigned TX_INTERVAL = 5;
const lmic_pinmap lmic_pins = {
.nss = 8,
.rxtx = LMIC_UNUSED_PIN,
.rst = 4,
.dio = {7, 5, LMIC_UNUSED_PIN},
};
void printHex2(unsigned v) {
v &= 0xff;
if (v < 16)
Serial.print(‘0’);
Serial.print(v, HEX);
}
void onEvent (ev_t ev) {
switch(ev) {
case EV_JOINED:
Serial.println(F("EV_JOINED"));
{
u4_t netid = 0;
devaddr_t devaddr = 0;
u1_t nwkKey[16];
u1_t artKey[16];
LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey);
}
LMIC_setLinkCheckMode(0);
break;
case EV_TXCOMPLETE:
Serial.println("ev tx");
Serial.println(i);
do_send(&sendjob);
if(i == 1){
setupLoRa();
while (loraData) {
loraSend();
}
setup();
}
// os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
i++;
break;
default:
break;
}
}
void do_send(osjob_t* j){
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F(“OP_TXRXPEND, not sending”));
} else {
// Prepare upstream data transmission at the next possible time.
LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
Serial.println(F(“Packet queued”));
}
// Next TX is scheduled after TX_COMPLETE event.
}
// Configuración LoRa
void setupLoRa() {
Serial.println(“set lora”);
LoRa.setPins(SS, RST, DI0);
delay(3000);
if (!LoRa.begin(BAND)) {
Serial.println(“Iniciando LoRa fallido!”);
while (1);
}
}
void loraSend(){
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print(“Received packet '”);
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
// print RSSI of packet
Serial.print("’ with RSSI ");
Serial.println(LoRa.packetRssi());
loraData = false;
}
}
void setup() {
// while (! Serial);
Serial.begin(9600);
Serial.println(F(“Starting”));
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
LMIC_setClockError(MAX_CLOCK_ERROR * 7/ 100);
LMIC_setLinkCheckMode(1);
LMIC_setDrTxpow(DR_SF7,14);
LMIC_selectSubBand(1);
// Start job (sending automatically starts OTAA too)
do_send(&sendjob);
}
void loop() {
os_runloop_once();
}`