Hi,
I’m trying to send some data with my heltec Lora board.
But I have the problem that with the code I “wrote” the gateway is not getting a join request.
Im using an Heltec Lora v2 board and im having my own gateway with LoraWan at EU868
That’s the code:
#include <lmic.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <hal/hal.h>
#include <SPI.h>
float temp;
float pressure;
float humidity;
float wind;
int wind_cnt;
int anopin = 36; //PIN for anemometer input
Adafruit_BME280 bme;
static const u1_t PROGMEM APPEUI[8] = {DELETED};
void os_getArtEui (u1_t* buf) {
memcpy_P(buf, APPEUI, 8);
}
static const u1_t PROGMEM DEVEUI[8] = {DELETED};
void os_getDevEui (u1_t* buf) {
memcpy_P(buf, DEVEUI, 8);
}
static const u1_t PROGMEM APPKEY[16] = {DELETED};
void os_getDevKey (u1_t* buf) {
memcpy_P(buf, APPKEY, 16);
}
boolean join_success = false;
uint8_t tx_payload[7];
static osjob_t sendjob;
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 180; //for testing
//LoRa pin mapping ESP32 (HELTEC LORA Board V2)
const lmic_pinmap lmic_pins = {
.nss = 18,
.rxtx = LMIC_UNUSED_PIN,
.rst = 14,
.dio = {26, 34, 35},
};
void printHex2(unsigned v) {
v &= 0xff;
if (v < 16)
Serial.print('0');
Serial.print(v, HEX);
}
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch (ev) {
case EV_SCAN_TIMEOUT:
Serial.println(F("EV_SCAN_TIMEOUT"));
break;
case EV_BEACON_FOUND:
Serial.println(F("EV_BEACON_FOUND"));
break;
case EV_BEACON_MISSED:
Serial.println(F("EV_BEACON_MISSED"));
break;
case EV_BEACON_TRACKED:
Serial.println(F("EV_BEACON_TRACKED"));
break;
case EV_JOINING:
Serial.println(F("EV_JOINING"));
break;
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);
Serial.print("netid: ");
Serial.println(netid, DEC);
Serial.print("devaddr: ");
Serial.println(devaddr, HEX);
Serial.print("AppSKey: ");
for (size_t i = 0; i < sizeof(artKey); ++i) {
if (i != 0)
Serial.print("-");
printHex2(artKey[i]);
}
Serial.println("");
Serial.print("NwkSKey: ");
for (size_t i = 0; i < sizeof(nwkKey); ++i) {
if (i != 0)
Serial.print("-");
printHex2(nwkKey[i]);
}
Serial.println();
join_success = true;
}
// Disable link check validation (automatically enabled
// during join, but because slow data rates change max TX
// size, we don't use it in this example.
LMIC_setLinkCheckMode(0);
break;
case EV_JOIN_FAILED:
Serial.println(F("EV_JOIN_FAILED"));
break;
case EV_REJOIN_FAILED:
Serial.println(F("EV_REJOIN_FAILED"));
break;
case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F("Received ack"));
if (LMIC.dataLen) {
Serial.print(F("Received "));
Serial.print(LMIC.dataLen);
Serial.println(F(" bytes of payload"));
}
// Schedule next transmission
os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
break;
case EV_LOST_TSYNC:
Serial.println(F("EV_LOST_TSYNC"));
break;
case EV_RESET:
Serial.println(F("EV_RESET"));
break;
case EV_RXCOMPLETE:
// data received in ping slot
Serial.println(F("EV_RXCOMPLETE"));
break;
case EV_LINK_DEAD:
Serial.println(F("EV_LINK_DEAD"));
break;
case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE"));
break;
case EV_TXSTART:
Serial.println(F("EV_TXSTART"));
break;
case EV_TXCANCELED:
Serial.println(F("EV_TXCANCELED"));
break;
case EV_RXSTART:
/* do not print anything -- it wrecks timing */
break;
case EV_JOIN_TXCOMPLETE:
Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept"));
break;
default:
Serial.print(F("Unknown event: "));
Serial.println((unsigned) ev);
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.
getSensorData();
generate_payload(temp, pressure, humidity, wind);
Serial.print("Payload: ");
int x = 0;
while (x < sizeof(tx_payload)) {
printHex2(tx_payload[x]);
Serial.print(" ");
x++;
}
Serial.println();
LMIC_setTxData2(1, tx_payload, sizeof(tx_payload), 0);
Serial.println(F("Packet queued"));
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup() {
delay(5000);
Serial.begin(115200);
pinMode(anopin, INPUT_PULLDOWN);
bme.begin(0x76); //define I2C-Address
//SPI pin mapping ESP32 (HELTEC LORA Board V2)
SPI.begin(5, 19, 27, 18);
getSensorData();
#ifdef VCC_ENABLE
// For Pinoccio Scout boards
pinMode(VCC_ENABLE, OUTPUT);
digitalWrite(VCC_ENABLE, HIGH);
delay(1000);
#endif
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
// Start job (sending automatically starts OTAA too)
do_send(&sendjob);
//Set FS for RX2 (SF9 = TTN,TTS EU)
LMIC.dn2Dr = DR_SF9;
}
void loop() {
os_runloop_once();
}
void getSensorData() {
temp = bme.readTemperature();
pressure = bme.readPressure() / 100;
humidity = bme.readHumidity();
meassure_wind();
}
void meassure_wind() {
wind_cnt = 0;
attachInterrupt(anopin, intrrupt_cnt, RISING);
delay(3000); //Time for measure counts
detachInterrupt(anopin);
wind = ((float)wind_cnt / (float)3 * 2.4) / 2; //Convert counts & time to km/h
}
void intrrupt_cnt() {//On count detected
wind_cnt++;//Add one count
}
void generate_payload(float temperature, float airpressure, float humidity, float windspeed) {
uint8_t payload[7];
int tmp = ((int)(temperature * 100)) + 5000;
int pre = (int)(airpressure * 10);
byte hum = (int)(humidity * 2);
int wnd = (int)(windspeed * 10);
payload[0] = tmp >> 8;
payload[1] = tmp;
payload[2] = pre >> 8;
payload[3] = pre;
payload[4] = hum;
payload[5] = wnd >> 8;
payload[6] = wnd;
int i = 0;
while (i < sizeof(payload)) {
tx_payload[i] = payload[i];
i++;
}
}
And im getting no join accept or an request.
13:10:01.018 -> 31814096: EV_JOIN_TXCOMPLETE: no JoinAccept
13:10:01.018 -> 31814103: EV_JOIN_FAILED
I could not figure out why it is not working.
Maybe you guys will see an error…
thank you very much!