mcci-catena / arduino-lorawan

User-friendly library for using arduino-lmic with The Things Network and other LoRaWAN® networks
MIT License
289 stars 55 forks source link

Not starting the connection process #69

Closed chris-gunawardena closed 5 years ago

chris-gunawardena commented 5 years ago

Hi Thanks for the library, this is much easier to maintain than lmic code.

I have a TTGO ESP32 lora connecting to TTN with the limc code but the same settings don't work with this lib. It compile but only prints RXMODE_RSSI and nothing more. The code in question below:

Have I implemented all the needed methods for OTAA?

#include <Arduino.h>
#include <SPI.h>
#include <Arduino_LoRaWAN_ttn.h>

class cMyLoRaWAN : public Arduino_LoRaWAN_ttn
{
  public:
    cMyLoRaWAN(){};

  protected:
    // you'll need to provide implementations for each of the following.
    virtual bool GetOtaaProvisioningInfo(Arduino_LoRaWAN::OtaaProvisioningInfo *) override;
    virtual void NetSaveFCntUp(uint32_t uFCntUp) override;
    virtual void NetSaveFCntDown(uint32_t uFCntDown) override;
    virtual void NetSaveSessionInfo(const SessionInfo &Info, const uint8_t *pExtraInfo, size_t nExtraInfo) override;
    // virtual bool NetBegin(void) override;
};

cMyLoRaWAN myLoRaWAN{};

const cMyLoRaWAN::lmic_pinmap myPinMap = {
    .nss = 18,
    .rxtx = cMyLoRaWAN::lmic_pinmap::LMIC_UNUSED_PIN,
    .rst = 14,
    .dio = {26, 33, 32},
};

void setup()
{ myLoRaWAN.begin(myPinMap);
}

void loop()
{   myLoRaWAN.loop();
}

bool cMyLoRaWAN::GetOtaaProvisioningInfo(
    OtaaProvisioningInfo *pInfo)
{
    static const uint8_t PROGMEM APPKEY[16] = { 0xD4, 0x51, .... };
    static const uint8_t PROGMEM DEVEUI[8] = { 0x55,  ....};
    static const uint8_t PROGMEM APPEUI[8] = { 0xD2, .... };

    memcpy_P(pInfo->AppKey, APPKEY, sizeof(APPKEY));
    memcpy_P(pInfo->DevEUI, DEVEUI, sizeof(DEVEUI));
    memcpy_P(pInfo->AppEUI, APPEUI, sizeof(APPEUI));
    return true;
}

void cMyLoRaWAN::NetSaveFCntDown(uint32_t uFCntDown) {}
void cMyLoRaWAN::NetSaveFCntUp(uint32_t uFCntUp) {}

void cMyLoRaWAN::NetSaveSessionInfo(
    const SessionInfo &Info,
    const uint8_t *pExtraInfo,
    size_t nExtraInfo)
{}
chris-gunawardena commented 5 years ago

Found a better working example here: https://github.com/mcci-catena/Catena-Arduino-Platform/blob/master/examples/catena_hello_lora/catena_hello_lora.ino

terrillmoore commented 5 years ago

Hi @chris-gunawardena -- sorry that things were not clear. If you can suggest how to improve docs, I'm open to suggestions.

chris-gunawardena commented 5 years ago

Maybe add an another example, this is what I got so far but it's still not joining. Also would be nice to add the Rx event handler here.


#include <Arduino.h>
#include <SPI.h>
#include <Arduino_LoRaWAN_ttn.h>

Arduino_LoRaWAN::SendBufferCbFn uplinkDone;

uint8_t uplinkBuffer[] = {/* port */ 0x10, 0xCA, 0xFE, 0xBA, 0xBE}; //?? not sure how to get this

class cMyLoRaWAN : public Arduino_LoRaWAN_ttn
{
  public:
    cMyLoRaWAN(){};

  protected:
    virtual bool GetOtaaProvisioningInfo(Arduino_LoRaWAN::OtaaProvisioningInfo *) override;
};

cMyLoRaWAN myLoRaWAN{};

const cMyLoRaWAN::lmic_pinmap myPinMap = {
    .nss = 18,
    .rxtx = cMyLoRaWAN::lmic_pinmap::LMIC_UNUSED_PIN,
    .rst = 14,
    .dio = {26, 33, 32},
    //  .rxtx_rx_active = 0,
    //  .rssi_cal = 0,
    //  .spi_freq = 8000000,
};

void setup()
{
    Serial.begin(9600);
    delay(2000);
    myLoRaWAN.begin(myPinMap);

    if (!myLoRaWAN.IsProvisioned())
        Serial.println("LoRaWAN not provisioned yet. Use the commands to set it up.\n");
    else
    {
        // send a confirmed uplink
        if (myLoRaWAN.SendBuffer(uplinkBuffer, sizeof(uplinkBuffer), uplinkDone, nullptr, true))
            Serial.println("gfTxStarted!\n");
        else
            Serial.println("SendBuffer failed!\n");
    }
}

void loop()
{
    myLoRaWAN.loop();
}

// this method is called when the LMIC needs OTAA info.
// return false to indicate "no provisioning", otherwise
// fill in the data and return true.
bool cMyLoRaWAN::GetOtaaProvisioningInfo(
    OtaaProvisioningInfo *pInfo)
{

     if(!pInfo){
        Serial.println("null pointer, create data");

        OtaaProvisioningInfo info;
        pInfo = &info;

        static const uint8_t  APPKEY[16] = {0xXX, 0xXX, 0xA5, 0x68, 0xE2, 0x9B, 0x53, 0x1A, 0x0F, 0x7B, 0x51, 0x12, 0xDC, 0x12, 0xXX, 0xXX};
        static const uint8_t  DEVEUI[8] = {0xXX, 0xXX, 0x6E, 0x35, 0xAF, 0xD3, 0xXX, 0xXX};
        static const uint8_t  APPEUI[8] = {0xXX, 0xXX, 0x00, 0xD0, 0x7E, 0xD5, 0xXX, 0xXX};

        memcpy(pInfo->AppKey, APPKEY, sizeof(APPKEY));
        memcpy(pInfo->DevEUI, DEVEUI, sizeof(DEVEUI));
        memcpy(pInfo->AppEUI, APPEUI, sizeof(APPEUI));

     }else
        Serial.println("not null");

    return true;
}

void uplinkDone(void *pCtx, bool fSuccess)
{
        Serial.println("uplinkDone.
}