EasyButtonCode
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_TTN_DS18B20 by
Diff: main.cpp
- Revision:
- 13:aebe2bf2aa8b
- Parent:
- 12:e82913ee9c61
--- a/main.cpp Sat Nov 14 20:58:36 2015 +0000 +++ b/main.cpp Tue Apr 19 13:01:23 2016 +0000 @@ -1,16 +1,4 @@ -/** mDot_TTN_DS18B20 - Simple mDot temperature sensor using Dallas Semiconductors DS18B20 OneWire temperature sensor. - * It used the MANUAL join mode with parameters for The Things Network. - * - * - * Uses MultiTech mDot developer board http://www.multitech.com/models/94558010LF - * Requires a MultiTech MultiConnect Conduit http://www.multitech.com/models/94557203LF - * http://www.multitech.net/developer/software/lora/conduit-mlinux-convert-to-basic-packet-forwarder/ - * http://forum.thethingsnetwork.org/t/setting-up-multitech-conduit-gateway-for-ttn/216/35 - * - */ - #include "mbed.h" -#include "DS1820.h" #include "mDot.h" #include "MTSLog.h" #include "MTSText.h" @@ -23,38 +11,28 @@ #define MAX(a,b) (((a)>(b))?(a):(b)) // Values as used by The Things Network -//const uint8_t AppKey[16]={0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}; // 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,0x00,0x00}; // Our Network address or Node ID +uint8_t NetworkAddr[4]= {0x02,0x01,0x6C,0x02}; // Our Network address or Node ID // Some defines for the LoRa configuration -#define LORA_SF mDot::SF_12 #define LORA_ACK 0 -#define LORA_TXPOWER 14 - -// Ignoring sub band for EU modules. -//static uint8_t config_frequency_sub_band = 1; +#define LORA_TXPOWER 20 -// DS18B20 OneWire pin -// D13 on Dev Board, pin 18 on mDot, Compatible with Oxford Flood Network PCB temperature sensor. -#define DATA_PIN PA_5 -// A0 on Dev Board, pin 20 on mDot -//#define DATA_PIN PB_1 - -// Temperature sensor object -DS1820 probe(DATA_PIN); +//Ignoring sub band for EU modules. +static uint8_t config_frequency_sub_band = 6; // Serial via USB for debugging only Serial pc(USBTX,USBRX); -int main() -{ +DigitalIn easyButton(PA_5); + +int main(){ int32_t ret; mDot* dot; std::vector<uint8_t> send_data; @@ -63,16 +41,14 @@ std::vector<uint8_t> nodeAddr; std::vector<uint8_t> networkAddr; - float temperature = 0.0; - pc.baud(115200); - pc.printf("TTN mDot LoRa Temperature sensor\n\r"); + pc.printf("TTN Easy Button\n\r"); // get a mDot handle dot = mDot::getInstance(); - dot->setLogLevel(MTSLog::WARNING_LEVEL); -// dot->setLogLevel(MTSLog::TRACE_LEVEL); +// dot->setLogLevel(MTSLog::WARNING_LEVEL); + dot->setLogLevel(MTSLog::INFO_LEVEL); logInfo("Checking Config"); @@ -92,13 +68,12 @@ dot->resetConfig(); // Set byte order - AEP less than 1.0.30 -// dot->setJoinByteOrder(mDot::LSB); 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( LORA_SF )) != mDot::MDOT_OK) { + if((ret = dot->setTxDataRate( mDot::SF_10 )) != mDot::MDOT_OK) { logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); } @@ -124,10 +99,9 @@ } // Not applicable for 868MHz in EU -// if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { -// initStatus = false; -// logError(dot, "failed to set frequency sub band", ret); -// } + 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) { @@ -191,25 +165,26 @@ 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"); - // Set the Temperature sesnor resolution, 9 bits is enough and makes it faster to provide a reading. - probe.setResolution(9); - - char dataBuf[50]; + while( 1 ) { - //Start temperature conversion, wait until ready - probe.convertTemperature(true, DS1820::all_devices); - // Output data as JSON e.g. {"t":21.3} - temperature = probe.temperature(); - sprintf(dataBuf, "{\"t\":%3.1f}", temperature ); + 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++ ) @@ -220,14 +195,8 @@ } else { logInfo("send data: %s", Text::bin2hexString(send_data).c_str()); } - - // Should sleep here and wakeup after a set 10 minute interval. - uint32_t sleep_time = MAX((dot->getNextTxMs() / 1000), 600); - logInfo("going to sleep for %d seconds", sleep_time); - - // go to sleep and wake up automatically sleep_time seconds later - dot->sleep(sleep_time, mDot::RTC_ALARM); + + dot->resetCpu(); } - return 0; }