LoraWAN TTN node device Part2: with MCCI LoRaWAN LMIC library on TTGO ESP32 Lora

 For LMiC based lib, the most recommended is MCCI LoraWan

https://github.com/mcci-catena/arduino-lmic

since it's an active project, frequently maintained, well documented. even with a doc on how to add region, but I don't have enough knowledge to understand the code.

functions tested with loraWan 1.02/1.03, support OTAA.

Adafruit has an article on this lib with their feather board.

https://learn.adafruit.com/the-things-network-for-feather/overview

Before starting coding, you  have to go to your TTN console and register a device in your existing application,  the Device ID is some thing for your memo, nothing related to the network. 


For the device EUI, since we don't have one on chip, you can ckick on the symbol, this EUI will be generated by TTN. 

once every thing is ready, the register tap will become green, now register it.



then you will have your device EUI, applicaiton EUI and app key, you will need these in your code.

Some library will make these parameters configurable through AT command. there are good side and down side of it, but if you are going to deploy huge amount of device, then how to pass these parameters will be a serious topic.
You can change the device in to ABP by going to setting of the device. and use device address, network cession key, application cession key.

Config region

 under \LoraWAN_MCCI_LMiC\libraries\arduino_861190\project_config
there is a lmic_project_config.h where you can config your region. I will start with AS923


in the tutorial of adafruit it says:within the directory of your sketch, create a new folder named project_config

But I will first try direct modification on it. 

I take the example ttn-otaa, and ready to key in the APPEUI, DEEUI and APPKEY .The tricks are lsb or msb first, this may vary depends on the lib you use.

for MCCI it request little-endian format, so least-significant-byte first for both EUI and appkey in big endian format. Make right format when you copy from TTN cosole, normally it shows msb at beginning.

I also have to change the pinmap:
const lmic_pinmap lmic_pins = {
    .nss = 18,                       // chip select 
    .rxtx = LMIC_UNUSED_PIN,
    .rst = 14,                       // reset pin
    .dio = {26, 33, 32}, //

but it runs only once, and the serial output stopped at EV_TXSTART packet queued

same thing if I switch to abp...

similar issue on net:https://github.com/matthijskooijman/arduino-lmic/issues/75

it turns out that there is a pin wiring issue...

first, the board version from china is always confusing...

check this for "exact" version of TTGO lora32

https://github.com/manuelbl/ttn-esp32/wiki/Boards-and-Pins

extract wiring needed:https://github.com/LilyGO/TTGO-LORA32/issues/3

If you want to use your board for LoRaWAN communication (using LMIC), you need to bridge LoRa_DIO2 to GPIO 32 and LoRaDIO1 to GPIO 33 as indicated earlier in the forum thread I linked, or your program will hang after sending a packet (as I experienced), or not work.

Mine board looks like this:




Adding 2 external wire on DIO1 to pin33 and DIO2 to pin32






This time the code works...

in abp code, you can set the channel, the apb code come with example of 923.2 and 923.4, so the device send data on these 2.

in OTAA code, the join is good, I can getg EV_JOINED, and device start to send data. But I need to setup the frequency for AS2, since my router is set on AS2

The other question is that in OTAA, the data received in TTN is always 1 byte short.

if I send 04 05 06 07, I only get 04 05 06...

send 06 06 , only get 06...

therefore, I modified the setTxData

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);
        LMIC_setTxData2(1, mydata, sizeof(mydata), 0);
        Serial.println(F("Packet queued"));
    }
    // Next TX is scheduled after TX_COMPLETE event.
}


As to the frequency, I added these lines right after LMIC_reset();

//================define frequency for AS2 not working ============================

    // Set up the channels used in your country. Only two are defined by default,

    // and they cannot be changed.  Use BAND_CENTI to indicate 1% duty cycle.

     LMIC_setupChannel(0, 923200000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);

     LMIC_setupChannel(1, 923400000, DR_RANGE_MAP(DR_SF12, DR_SF7B),  BAND_CENTI);

     LMIC_setupChannel(2, 923600000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);

     LMIC_setupChannel(3, 923800000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);

     LMIC_setupChannel(4, 924000000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);

     LMIC_setupChannel(5, 924200000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);

     LMIC_setupChannel(6, 924400000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);

     LMIC_setupChannel(7, 924600000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);

//----------------end of define frequency


it seems to work, I can receive data from channels after 925.4, and the counter of payload is continuous.



留言

這個網誌中的熱門文章

Heltec ESP32+OLED+Lora, hardware testing

micro SD card for ESP32, on lolin32 with OLED and heltec 32 lora oled

Install Network Time Protocol(NTP) on BeagleBone with Angstrom linux and set local time zone