前回 CMWX1ZZABZ搭載ボードをArduino IDEで開発するための環境構築に関して説明しましたので
今回は実際にサンプルプログラムからLoRaWANにABPで接続してみます。
なお接続先は「福岡市LoRaWAN実証実験」ゲートウェイでテストしました。
ベースとしたプログラムは
LoRaWAN_TimerMillisとLoRaWAN_Join_ABP
です。
サンプルプログラム
LoRaWAN_Join_ABP.ino
/* Simple ABP join for a LoRaWAN network * * In setup() below please replace the argument to LoRaWAN.begin() * with your appropriate region specific band: * * AS923 * AU915 * EU868 * IN865 * KR920 * US915 * * AU915/US915 networks have 64+8 channels. Typical gateways support only * 8 (9) channels. Hence it's a good idea to pick the proper channel * subset via select via LoRaWAN.setSubBand(), * * EU868/IN865 have duty cycle restrictions. For debugging it makes sense * to disable those via setDutyCycle(false); * * For an external antenna one should set the proper antenna gain * (default is 2.0) via setAntennaGain(). * * Please edit the keys below as they are just debugging samples. * * * This example code is in the public domain. */ #include "LoRaWAN.h" #include "TimerMillis.h" #define Interval 360000 /* 送信間隔 単位はmS この場合6分おきに送信します */ const char *devAddr = "00000000" /* 書き換えてください。 */ const char *nwkSKey = "00000000000000000000000000000000"; /* 書き換えてください。 */ const char *appSKey = "00000000000000000000000000000000"; /* 書き換えてください。 */ TimerMillis transmitTimer; static char counter = 0; /* 送信カウンター Char型なので 255になったら0に戻ります */ void transmitCallback(void) { /* ここから */ if (!LoRaWAN.joined() || LoRaWAN.busy()) { Serial.println("LoRaWAN Non Active!"); LoRaWAN.rejoinABP(); } /* ここまで追加しました */ if (LoRaWAN.joined() && !LoRaWAN.busy()) { Serial.print("TRANSMIT( "); Serial.print("TimeOnAir: "); Serial.print(LoRaWAN.getTimeOnAir()); Serial.print(", NextTxTime: "); Serial.print("NextTxTime: "); Serial.print(LoRaWAN.getNextTxTime()); Serial.print(", MaxPayloadSize: "); Serial.print(LoRaWAN.getMaxPayloadSize()); Serial.print(", DR: "); Serial.print(LoRaWAN.getDataRate()); Serial.print(", TxPower: "); Serial.print(LoRaWAN.getTxPower(), 1); Serial.print("dbm, UpLinkCounter: "); Serial.print(LoRaWAN.getUpLinkCounter()); Serial.print(", DownLinkCounter: "); Serial.print(LoRaWAN.getDownLinkCounter()); Serial.println(" )"); LoRaWAN.beginPacket(); LoRaWAN.write(0xef); LoRaWAN.write(0xbe); LoRaWAN.write(0xad); LoRaWAN.write(counter); /* ここにカウンター値を入れています */ LoRaWAN.endPacket(); counter++; /* 送信カウンター インクリメント */ } } void setup( void ) { Serial.begin(9600); while (!Serial) { } LoRaWAN.begin(AS923); // LoRaWAN.setSubBand(2); /* 元々コメントアウト */ // LoRaWAN.setDutyCycle(false); /* 元々コメントアウト */ // LoRaWAN.setAntennaGain(2.0); /* 元々コメントアウト */ LoRaWAN.joinABP(devAddr, nwkSKey, appSKey); Serial.println("JOIN( )"); transmitTimer.start(transmitCallback, 0, Interval); } void loop( void ) { } |
公開されているサンプルプログラムに
rejoinABP()関数が無いので追加します。
追加内容
LoRaWAN.h 310行目
307 308 309 310 311 | int _joinOTAA(LoRaWANCommissioning *commissioning); int _joinABP(LoRaWANCommissioning *commissioning); int _rejoinOTAA(); int _rejoinABP(); bool _send(); |
LoRaWAN.cpp joinABP()の後にrejoinABP() を追加 694行目付近
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 | int LoRaWANClass::joinABP(const char *devAddr, const char *nwkSKey, const char *appSKey) { LoRaWANCommissioning commissioning; uint8_t DevAddr[4]; if (!_Band) { return 0; } if (!ConvertString((uint8_t*)&DevAddr, 4, devAddr)) { return 0; } commissioning.DevAddr = (DevAddr[0] << 24) | (DevAddr[1] << 16) | (DevAddr[2] << 8) | (DevAddr[3] << 0); if (!ConvertString(commissioning.NwkSKey, 16, nwkSKey)) { return 0; } if (!ConvertString(commissioning.AppSKey, 16, appSKey)) { return 0; } return _joinABP(&commissioning); } int LoRaWANClass::rejoinABP() { if (!_Band) { return 0; } if (_session.Activation != LORAWAN_ACTIVATION_ABP) { return 0; } return _rejoinABP(); } |
福岡市 LoRaWANのThingpark の画面、6分毎にデータが飛んで来ているのを確認出来ます。