Closed johnp2605 closed 3 months ago
First comments:
uint64_t
as in uint64_t appEui = 0x0000000000000000
? That way, you could skip the reverseArray
thing and just give appEui
to persist.provision
instead of *(uint64_t*)appEui
.nwkSKey
looks wrong.Below is a tested example for using this library with statically compiled provisioning data. You have to get your own endpoint parameters, I deleted this endpoint after testing.
Note that while it can do so just fine, the true power of this library is of course not to use statically compiled values, but to use values entered by the user at runtime, either serially using the built-in dialog, from a web interface you make, or from somewhere else.
// Pause between sends in seconds, so this is every 15 minutes. (Delay will be
// longer if regulatory or TTN Fair Use Policy requires it.)
#define MINIMUM_DELAY 900
#include <heltec_unofficial.h>
#include <LoRaWAN_ESP32.h>
LoRaWANNode* node;
RTC_DATA_ATTR uint8_t count = 0;
const char* band = "EU868";
const uint8_t subband = 0;
const uint64_t joinEUI = 0x0000000000000000;
const uint64_t devEUI = 0x70B3D57ED00670C8;
const uint8_t appKey[] = { 0x4A, 0x2F, 0x86, 0x95, 0x40, 0x25, 0x7C, 0x96, 0xDE, 0xCE, 0x3E, 0x15, 0xBA, 0x4A, 0xF9, 0xD2 };
const uint8_t nwkKey[] = { 0xF4, 0xA8, 0x58, 0xAD, 0x9B, 0x80, 0xA6, 0x97, 0x15, 0x51, 0x75, 0x06, 0x4F, 0xD1, 0x9E, 0xA5 };
void setup() {
heltec_setup();
// Obtain directly after deep sleep
// May or may not reflect room temperature, sort of.
float temp = heltec_temperature();
Serial.printf("Temperature: %.2f °C\n", temp);
// Put the provisioning data in flash. Can do this every time: flash values
// are only ever written if they differ from what's already there.
persist.provision(band, subband, joinEUI, devEUI, appKey, nwkKey);
// initialize radio
Serial.println("Radio init");
int16_t state = radio.begin();
if (state != RADIOLIB_ERR_NONE) {
Serial.println("Radio did not initialize. We'll try again later.");
goToSleep(); // Does not return, program starts over after sleep
}
node = persist.manage(&radio);
if (!node->isJoined()) {
Serial.println("Could not join network. We'll try again later.");
goToSleep(); // Does not return, program starts over after sleep
}
// If we're still here, it means we joined, and we can send something
// Manages uplink intervals to the TTN Fair Use Policy
node->setDutyCycle(true, 1250);
uint8_t uplinkData[2];
uplinkData[0] = count++;
uplinkData[1] = temp + 100;
uint8_t downlinkData[256];
size_t lenDown = sizeof(downlinkData);
state = node->sendReceive(uplinkData, sizeof(uplinkData), 1, downlinkData, &lenDown);
if(state == RADIOLIB_ERR_NONE || state == RADIOLIB_ERR_RX_TIMEOUT) {
Serial.println("Message sent");
} else {
Serial.printf("sendReceive returned error %d, we'll try again later.\n", state);
}
goToSleep(); // Does not return, program starts over after sleep
}
void loop() {
// This is never called. There is no repetition: we always go back to
// deep sleep one way or the other at the end of setup()
}
void goToSleep() {
Serial.println("Going to deep sleep now");
// allows recall of the session after deepsleep
persist.saveSession(node);
// Calculate minimum duty cycle delay (per FUP & law!)
uint32_t interval = node->timeUntilUplink();
// And then pick it or our MINIMUM_DELAY, whichever is greater
uint32_t delayMs = max(interval, (uint32_t)MINIMUM_DELAY * 1000);
Serial.printf("Next TX in %i s\n", delayMs/1000);
delay(100); // So message prints
// And off to bed we go
heltec_deep_sleep(delayMs / 1000);
}
Please let me know if this works for you, if you can. That way I know I can close the issue.
Please let me know if this works for you, if you can. That way I know I can close the issue.
I could not get it to work and have switched to the library from Heltec. It works right now. Thanks for the help.
Alright, then I’ll close it because the thing I posted works for me here. Strange though.
I have an Heltec Lora board and want to sent a message to the KPN Things network. What is wrong I can't get it to work. I have tried lots of code, but there is no connection possible. When I take the example LoraWan from Heltec it can connect and sent, but the signal is not strong enough. I only want to sent one small message every hour.
I'am getting the following message:
The code I'am using is (the devEui, appEui and appKey is filled in with other values):