Jonathan Lathem / Mbed 2 deprecated easyButton_ATL_IOT

Dependencies:   libmDot mbed-rtos mbed

Fork of easyButton_GP_IOT by Jonathan Lathem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mDot.h"
00003 #include "MTSLog.h"
00004 #include "MTSText.h"
00005 #include <string>
00006 #include <vector>
00007 
00008 using namespace mts;
00009 
00010 #define MIN(a,b) (((a)<(b))?(a):(b))
00011 #define MAX(a,b) (((a)>(b))?(a):(b))
00012 
00013 // Values as used by The Things Network
00014 // Application session key
00015 uint8_t AppSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
00016 // Network session key
00017 uint8_t NwkSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
00018 
00019 // Network Address - Get your own address range at http://thethingsnetwork.org/wiki/AddressSpace
00020 uint8_t NetworkAddr[4]= {0x02,0x01,0x6C,0x02};      // Our Network address or Node ID
00021 
00022 
00023 // Some defines for the LoRa configuration
00024 #define LORA_ACK 0
00025 #define LORA_TXPOWER 20
00026 
00027 //Ignoring sub band for EU modules.
00028 static uint8_t config_frequency_sub_band = 6;
00029 
00030 // Serial via USB for debugging only
00031 Serial pc(USBTX,USBRX);
00032 
00033 DigitalIn easyButton(PA_5);
00034 
00035 int main(){
00036     int32_t ret;
00037     mDot* dot;
00038     std::vector<uint8_t> send_data;
00039     std::vector<uint8_t> recv_data;
00040     std::vector<uint8_t> nwkSKey;
00041     std::vector<uint8_t> nodeAddr;
00042     std::vector<uint8_t> networkAddr;
00043 
00044     pc.baud(115200);
00045     pc.printf("TTN Easy Button\n\r");
00046 
00047     easyButton.mode(PullDown);
00048     
00049     // get a mDot handle
00050     dot = mDot::getInstance();
00051     
00052     dot->setLogLevel(MTSLog::INFO_LEVEL);
00053 
00054     logInfo("Checking Config");
00055 
00056     // Test if we've already saved the config
00057     std::string configNetworkName = dot->getNetworkName();
00058 
00059     uint8_t *it = NwkSKey;
00060     for (uint8_t i = 0; i<16; i++)
00061         nwkSKey.push_back((uint8_t) *it++);
00062 
00063     it = NetworkAddr;
00064     for (uint8_t i = 0; i<4; i++)
00065         networkAddr.push_back((uint8_t) *it++);
00066 
00067     logInfo("Resetting Config");
00068     // reset to default config so we know what state we're in
00069     dot->resetConfig();
00070 
00071     // Set byte order - AEP less than 1.0.30
00072     dot->setJoinByteOrder(mDot::MSB);       // This is default for > 1.0.30 Conduit
00073 
00074     // Set Spreading Factor, higher is lower data rate, smaller packets but longer range
00075     // Lower is higher data rate, larger packets and shorter range.
00076     logInfo("Set SF");
00077     if((ret = dot->setTxDataRate( mDot::SF_10 )) != mDot::MDOT_OK) {
00078         logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00079     }
00080 
00081     logInfo("Set TxPower");
00082     if((ret = dot->setTxPower( LORA_TXPOWER )) != mDot::MDOT_OK) {
00083         logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00084     }
00085 
00086     logInfo("Set Public mode");
00087     if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
00088         logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00089     }
00090 
00091     logInfo("Set MANUAL Join mode");
00092     if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) {
00093         logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00094     }
00095 
00096     logInfo("Set Ack");
00097     // 1 retries on Ack, 0 to disable
00098     if((ret = dot->setAck( LORA_ACK)) != mDot::MDOT_OK) {
00099         logError("Failed to set Ack %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00100     }
00101 
00102 //    Not applicable for 868MHz in EU
00103     if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
00104         logError("Failed to set frequency sub band %s", ret);
00105     }
00106 
00107     logInfo("Set Network Address");
00108     if ((ret = dot->setNetworkAddress(networkAddr)) != mDot::MDOT_OK) {
00109         logError("Failed to set Network Address %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00110     }
00111 
00112     logInfo("Set Data Session Key");
00113     if ((ret = dot->setDataSessionKey(nwkSKey)) != mDot::MDOT_OK) {
00114         logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00115     }
00116 
00117     logInfo("Set Network Session Key");
00118     if ((ret = dot->setNetworkSessionKey(nwkSKey)) != mDot::MDOT_OK) {
00119         logError("Failed to set Network Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00120     }
00121 
00122     logInfo("Saving Config");
00123     // Save config
00124     if (! dot->saveConfig()) {
00125         logError("failed to save configuration");
00126     }
00127 
00128     // Display what is set
00129     std::vector<uint8_t> tmp = dot->getNetworkSessionKey();
00130     pc.printf("Network Session Key: ");
00131     pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str());
00132 
00133     tmp = dot->getDataSessionKey();
00134     pc.printf("Data Session Key: ");
00135     pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str());
00136 
00137     pc.printf("Device ID ");
00138     std::vector<uint8_t> deviceId;
00139     deviceId = dot->getDeviceId();
00140     for (std::vector<uint8_t>::iterator it = deviceId.begin() ; it != deviceId.end(); ++it)
00141         pc.printf("%2.2x",*it );
00142     pc.printf("\r\n");
00143 
00144     std::vector<uint8_t> netAddress;
00145 
00146     pc.printf("Network Address ");
00147     netAddress = dot->getNetworkAddress();
00148     for (std::vector<uint8_t>::iterator it = netAddress.begin() ; it != netAddress.end(); ++it)
00149         pc.printf("%2.2x",*it );
00150 
00151     pc.printf("\r\n");
00152 
00153     // Display LoRa parameters
00154     // Display label and values in different colours, show pretty values not numeric values where applicable
00155     pc.printf("Public Network: %s\r\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") );
00156     pc.printf("Frequency: %s\r\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() );
00157     pc.printf("Sub Band: %s\r\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() );
00158     pc.printf("Join Mode: %s\r\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() );
00159     pc.printf("Join Retries: %d\r\n", dot->getJoinRetries() );
00160     pc.printf("Join Byte Order: %s\r\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") );
00161     pc.printf("Link Check Count: %d\r\n", dot->getLinkCheckCount() );
00162     pc.printf("Link Check Thold: %d\r\n", dot->getLinkCheckThreshold() );
00163     pc.printf("Tx Data Rate: %s\r\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() );
00164     pc.printf("Tx Power: %d\r\n", dot->getTxPower() );
00165     pc.printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" ));
00166     pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") );
00167     pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N")  );
00168 
00169     char dataBuf[50];
00170 
00171     logInfo("Joining Network");
00172 
00173     while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
00174         logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00175         wait_ms(dot->getNextTxMs() + 1);
00176     }
00177     
00178     logInfo("Joined Network");
00179 
00180     
00181     while( 1 ) {
00182         pc.printf("Waiting for Button Push....");
00183         while (easyButton == 0){wait(0.1);}
00184         pc.printf("Button Pushed!\n");
00185         while (easyButton == 1) {wait(0.1);}
00186 
00187         sprintf(dataBuf, "easy");
00188         
00189         send_data.clear();
00190         // probably not the most efficent way to do this
00191         for( int i=0; i< strlen(dataBuf); i++ )
00192             send_data.push_back( dataBuf[i] );
00193 
00194         if ((ret = dot->send(send_data)) != mDot::MDOT_OK) {
00195             logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00196         } else {
00197             logInfo("send data: %s", Text::bin2hexString(send_data).c_str());
00198         }
00199         
00200         dot->resetCpu();
00201     }
00202     return 0;
00203 }