EasyButtonCode
Dependencies: libmDot mbed-rtos mbed
Fork of easyButton_GP_IOT by
main.cpp
- Committer:
- jlathem
- Date:
- 2016-04-26
- Revision:
- 14:9358bae86990
- Parent:
- 13:aebe2bf2aa8b
File content as of revision 14:9358bae86990:
#include "mbed.h"
#include "mDot.h"
#include "MTSLog.h"
#include "MTSText.h"
#include <string>
#include <vector>
using namespace mts;
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
// Values as used by The Things Network
// Application session key
uint8_t AppSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
// Network session key
uint8_t NwkSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
// Network Address - Get your own address range at http://thethingsnetwork.org/wiki/AddressSpace
uint8_t NetworkAddr[4]= {0x02,0x01,0x6C,0x02}; // Our Network address or Node ID
// Some defines for the LoRa configuration
#define LORA_ACK 0
#define LORA_TXPOWER 20
//Ignoring sub band for EU modules.
static uint8_t config_frequency_sub_band = 6;
// Serial via USB for debugging only
Serial pc(USBTX,USBRX);
DigitalIn easyButton(PA_5);
int main(){
int32_t ret;
mDot* dot;
std::vector<uint8_t> send_data;
std::vector<uint8_t> recv_data;
std::vector<uint8_t> nwkSKey;
std::vector<uint8_t> nodeAddr;
std::vector<uint8_t> networkAddr;
pc.baud(115200);
pc.printf("TTN Easy Button\n\r");
easyButton.mode(PullDown);
// get a mDot handle
dot = mDot::getInstance();
dot->setLogLevel(MTSLog::INFO_LEVEL);
logInfo("Checking Config");
// Test if we've already saved the config
std::string configNetworkName = dot->getNetworkName();
uint8_t *it = NwkSKey;
for (uint8_t i = 0; i<16; i++)
nwkSKey.push_back((uint8_t) *it++);
it = NetworkAddr;
for (uint8_t i = 0; i<4; i++)
networkAddr.push_back((uint8_t) *it++);
logInfo("Resetting Config");
// reset to default config so we know what state we're in
dot->resetConfig();
// Set byte order - AEP less than 1.0.30
dot->setJoinByteOrder(mDot::MSB); // This is default for > 1.0.30 Conduit
// Set Spreading Factor, higher is lower data rate, smaller packets but longer range
// Lower is higher data rate, larger packets and shorter range.
logInfo("Set SF");
if((ret = dot->setTxDataRate( mDot::SF_10 )) != mDot::MDOT_OK) {
logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
logInfo("Set TxPower");
if((ret = dot->setTxPower( LORA_TXPOWER )) != mDot::MDOT_OK) {
logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
logInfo("Set Public mode");
if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
logInfo("Set MANUAL Join mode");
if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) {
logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
logInfo("Set Ack");
// 1 retries on Ack, 0 to disable
if((ret = dot->setAck( LORA_ACK)) != mDot::MDOT_OK) {
logError("Failed to set Ack %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
// Not applicable for 868MHz in EU
if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
logError("Failed to set frequency sub band %s", ret);
}
logInfo("Set Network Address");
if ((ret = dot->setNetworkAddress(networkAddr)) != mDot::MDOT_OK) {
logError("Failed to set Network Address %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
logInfo("Set Data Session Key");
if ((ret = dot->setDataSessionKey(nwkSKey)) != mDot::MDOT_OK) {
logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
logInfo("Set Network Session Key");
if ((ret = dot->setNetworkSessionKey(nwkSKey)) != mDot::MDOT_OK) {
logError("Failed to set Network Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
logInfo("Saving Config");
// Save config
if (! dot->saveConfig()) {
logError("failed to save configuration");
}
// Display what is set
std::vector<uint8_t> tmp = dot->getNetworkSessionKey();
pc.printf("Network Session Key: ");
pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str());
tmp = dot->getDataSessionKey();
pc.printf("Data Session Key: ");
pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str());
pc.printf("Device ID ");
std::vector<uint8_t> deviceId;
deviceId = dot->getDeviceId();
for (std::vector<uint8_t>::iterator it = deviceId.begin() ; it != deviceId.end(); ++it)
pc.printf("%2.2x",*it );
pc.printf("\r\n");
std::vector<uint8_t> netAddress;
pc.printf("Network Address ");
netAddress = dot->getNetworkAddress();
for (std::vector<uint8_t>::iterator it = netAddress.begin() ; it != netAddress.end(); ++it)
pc.printf("%2.2x",*it );
pc.printf("\r\n");
// Display LoRa parameters
// Display label and values in different colours, show pretty values not numeric values where applicable
pc.printf("Public Network: %s\r\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") );
pc.printf("Frequency: %s\r\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() );
pc.printf("Sub Band: %s\r\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() );
pc.printf("Join Mode: %s\r\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() );
pc.printf("Join Retries: %d\r\n", dot->getJoinRetries() );
pc.printf("Join Byte Order: %s\r\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") );
pc.printf("Link Check Count: %d\r\n", dot->getLinkCheckCount() );
pc.printf("Link Check Thold: %d\r\n", dot->getLinkCheckThreshold() );
pc.printf("Tx Data Rate: %s\r\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() );
pc.printf("Tx Power: %d\r\n", dot->getTxPower() );
pc.printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" ));
pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") );
pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N") );
char dataBuf[50];
logInfo("Joining Network");
while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
wait_ms(dot->getNextTxMs() + 1);
}
logInfo("Joined Network");
while( 1 ) {
pc.printf("Waiting for Button Push....");
while (easyButton == 0){wait(0.1);}
pc.printf("Button Pushed!\n");
while (easyButton == 1) {wait(0.1);}
sprintf(dataBuf, "easy");
send_data.clear();
// probably not the most efficent way to do this
for( int i=0; i< strlen(dataBuf); i++ )
send_data.push_back( dataBuf[i] );
if ((ret = dot->send(send_data)) != mDot::MDOT_OK) {
logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
} else {
logInfo("send data: %s", Text::bin2hexString(send_data).c_str());
}
dot->resetCpu();
}
return 0;
}
