Here it is ...

Dependencies:   libxDot-mbed5 TSL2561

Fork of Dot-Examples by MultiTech

Committer:
dgutsch
Date:
Sun Jul 22 17:25:10 2018 +0000
Revision:
34:fbdd22159585
Parent:
33:79e4c812d91d
Child:
35:b51d482e9731
small programatic change that could be reasoning behind why it only does the send once. (moving vector declaration)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:a151a6350d7f 1 #include "dot_util.h"
Mike Fiore 14:19fae4509473 2 #include "RadioEvent.h"
mfiore 0:a151a6350d7f 3
mfiore 0:a151a6350d7f 4 #if ACTIVE_EXAMPLE == OTA_EXAMPLE
mfiore 0:a151a6350d7f 5
mfiore 17:d4f82e16de5f 6 /////////////////////////////////////////////////////////////////////////////
mfiore 17:d4f82e16de5f 7 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
mfiore 17:d4f82e16de5f 8 // * Because these example programs can be used for both mDot and xDot //
mfiore 17:d4f82e16de5f 9 // devices, the LoRa stack is not included. The libmDot library should //
mfiore 17:d4f82e16de5f 10 // be imported if building for mDot devices. The libxDot library //
mfiore 17:d4f82e16de5f 11 // should be imported if building for xDot devices. //
mfiore 17:d4f82e16de5f 12 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ //
mfiore 17:d4f82e16de5f 13 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ //
mfiore 17:d4f82e16de5f 14 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ //
mfiore 17:d4f82e16de5f 15 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ //
mfiore 17:d4f82e16de5f 16 /////////////////////////////////////////////////////////////////////////////
mfiore 17:d4f82e16de5f 17
Mike Fiore 5:97ed5f2f099e 18 /////////////////////////////////////////////////////////////
Mike Fiore 5:97ed5f2f099e 19 // * these options must match the settings on your gateway //
Mike Fiore 5:97ed5f2f099e 20 // * edit their values to match your configuration //
Mike Fiore 5:97ed5f2f099e 21 // * frequency sub band is only relevant for the 915 bands //
Mike Fiore 5:97ed5f2f099e 22 // * either the network name and passphrase can be used or //
Mike Fiore 5:97ed5f2f099e 23 // the network ID (8 bytes) and KEY (16 bytes) //
Mike Fiore 5:97ed5f2f099e 24 /////////////////////////////////////////////////////////////
dgutsch 30:0059ae42ff02 25 static std::string network_name = "MTCDT-19400691";
dgutsch 30:0059ae42ff02 26 static std::string network_passphrase = "MTCDT-19400691";
Mike Fiore 5:97ed5f2f099e 27 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
Mike Fiore 5:97ed5f2f099e 28 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
dgutsch 30:0059ae42ff02 29 static uint8_t frequency_sub_band = 1;
dgutsch 30:0059ae42ff02 30 static lora::NetworkType public_network;// = lora::PUBLIC_LORAWAN;
David Gutsch dgutsch@umail.iu.edu 32:cc05a2e80969 31 static uint8_t join_delay = 1;
Mike Fiore 15:364df461110f 32 static uint8_t ack = 0;
Mike Fiore 21:09d05faf0e13 33 static bool adr = true;
mfiore 0:a151a6350d7f 34
mfiore 0:a151a6350d7f 35 // deepsleep consumes slightly less current than sleep
mfiore 0:a151a6350d7f 36 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
mfiore 0:a151a6350d7f 37 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
mfiore 0:a151a6350d7f 38 // if deep_sleep == true, device will enter deepsleep mode
mfiore 0:a151a6350d7f 39 static bool deep_sleep = false;
mfiore 0:a151a6350d7f 40
mfiore 0:a151a6350d7f 41 mDot* dot = NULL;
Mike Fiore 21:09d05faf0e13 42 lora::ChannelPlan* plan = NULL;
mfiore 0:a151a6350d7f 43
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 44
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 45 Serial pc(PA_9, PA_10);
mfiore 0:a151a6350d7f 46
mfiore 0:a151a6350d7f 47 int main() {
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 48 pc.baud(115200);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 49 pc.printf("main started \r\n");
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 50 // Custom event handler for automatically displaying RX data
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 51 RadioEvent events;
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 52
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 53
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 54
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 55
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 56 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 57
Mike Fiore 21:09d05faf0e13 58 #if CHANNEL_PLAN == CP_US915
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 59 plan = new lora::ChannelPlan_US915();
Mike Fiore 21:09d05faf0e13 60 #elif CHANNEL_PLAN == CP_AU915
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 61 plan = new lora::ChannelPlan_AU915();
Mike Fiore 21:09d05faf0e13 62 #elif CHANNEL_PLAN == CP_EU868
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 63 plan = new lora::ChannelPlan_EU868();
Mike Fiore 21:09d05faf0e13 64 #elif CHANNEL_PLAN == CP_KR920
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 65 plan = new lora::ChannelPlan_KR920();
Mike Fiore 21:09d05faf0e13 66 #elif CHANNEL_PLAN == CP_AS923
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 67 plan = new lora::ChannelPlan_AS923();
Mike Fiore 21:09d05faf0e13 68 #elif CHANNEL_PLAN == CP_AS923_JAPAN
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 69 plan = new lora::ChannelPlan_AS923_Japan();
mfiore 22:d9bc10bbc433 70 #elif CHANNEL_PLAN == CP_IN865
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 71 plan = new lora::ChannelPlan_IN865();
Mike Fiore 21:09d05faf0e13 72 #endif
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 73 assert(plan);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 74
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 75 dot = mDot::getInstance(plan);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 76 assert(dot);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 77
David Gutsch dgutsch@umail.iu.edu 32:cc05a2e80969 78
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 79 // attach the custom events handler
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 80 dot->setEvents(&events);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 81
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 82 if (!dot->getStandbyFlag()) {
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 83 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 84
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 85 // start from a well-known state
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 86 logInfo("defaulting Dot configuration");
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 87 dot->resetConfig();
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 88 dot->resetNetworkSession();
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 89
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 90 // set the data_rate
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 91 (*dot).setTxDataRate(lora::DR_1);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 92
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 93 // make sure library logging is turned on
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 94 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 95
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 96 // update configuration if necessary
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 97 if (dot->getJoinMode() != mDot::OTA) {
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 98 logInfo("changing network join mode to OTA");
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 99 if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 100 logError("failed to set network join mode to OTA");
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 101 }
mfiore 0:a151a6350d7f 102 }
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 103 // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 104 // only one method or the other should be used!
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 105 // network ID = crc64(network name)
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 106 // network KEY = cmac(network passphrase)
David Gutsch dgutsch@umail.iu.edu 32:cc05a2e80969 107 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
David Gutsch dgutsch@umail.iu.edu 32:cc05a2e80969 108 //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 109
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 110 // configure network link checks
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 111 // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 112 // check the link every count packets
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 113 // declare the Dot disconnected after threshold failed link checks
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 114 // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 115 update_network_link_check_config(3, 5);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 116
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 117 // enable or disable Adaptive Data Rate
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 118 dot->setAdr(adr);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 119
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 120 // Configure the join delay
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 121 dot->setJoinDelay(join_delay);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 122
David Gutsch dgutsch@umail.iu.edu 32:cc05a2e80969 123 pc.printf("network information set\r\n");
David Gutsch dgutsch@umail.iu.edu 32:cc05a2e80969 124
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 125 // save changes to configuration
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 126 logInfo("saving configuration");
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 127 if (!dot->saveConfig()) {
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 128 logError("failed to save configuration");
mfiore 0:a151a6350d7f 129 }
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 130
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 131 // display configuration
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 132 display_config();
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 133 } else {
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 134 // restore the saved session if the dot woke from deepsleep mode
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 135 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 136 logInfo("restoring network session from NVM");
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 137 dot->restoreNetworkSession();
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 138 }
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 139
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 140 uint8_t counter = 0;
dgutsch 34:fbdd22159585 141 std::vector<uint8_t> tx_data;
dgutsch 34:fbdd22159585 142
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 143 while (true) {
David Gutsch dgutsch@umail.iu.edu 32:cc05a2e80969 144
dgutsch 34:fbdd22159585 145 //******** perhaps I'm double decaling and the compiler isn't catching it *********
dgutsch 34:fbdd22159585 146 // try moving this up with the counter, y a ver como se comporta
dgutsch 34:fbdd22159585 147 // not dealocating just erasing/resizing buffer bc it's stack based???
dgutsch 34:fbdd22159585 148 //std::vector<uint8_t> tx_data;
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 149
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 150
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 151 // join network if not joined
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 152 if (!dot->getNetworkJoinStatus()) {
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 153 pc.printf("joining lora network.\r\n");
David Gutsch dgutsch@umail.iu.edu 32:cc05a2e80969 154 join_network(&pc);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 155 }
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 156
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 157 // hourly loop
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 158 while( counter < 12) {
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 159 uint8_t light_data = get_light_data();
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 160 tx_data.push_back(light_data);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 161 pc.printf("pseudo-light data: %d\r\n", light_data);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 162 pc.printf("hour: %d\r\n", ++counter);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 163
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 164 // if going into deepsleep mode, save the session so we don't need to join again after waking up
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 165 // not necessary if going into sleep mode since RAM is retained
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 166 if (deep_sleep) {
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 167 logInfo("saving network session to NVM");
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 168 dot->saveNetworkSession();
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 169 }
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 170
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 171 // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 172 //sleep_wake_rtc_only(deep_sleep);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 173 //sleep_wake_interrupt_only(deep_sleep);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 174 sleep_wake_rtc_or_interrupt(deep_sleep);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 175 }
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 176
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 177 //print vector data
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 178 pc.printf("vector data:");
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 179 for(std::vector<uint8_t>::const_iterator iter = tx_data.begin(); iter != tx_data.end(); ++iter) {
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 180 pc.printf(" %d",*iter);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 181 }
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 182 pc.printf("\r\n");
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 183 pc.printf("size of tx_data: %d\r\n", sizeof(tx_data));
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 184 //actually send the data
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 185 pc.printf("sending data\r\n");
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 186 send_data(tx_data, &pc);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 187
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 188 // erase vector data & reset counter
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 189 tx_data.erase(tx_data.begin(), tx_data.end());
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 190 counter = 0;
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 191 pc.printf("deleted tx_data and reset counter\r\n");
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 192
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 193 }
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 194
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 195 return 0;
mfiore 0:a151a6350d7f 196 }
mfiore 0:a151a6350d7f 197
mfiore 0:a151a6350d7f 198 #endif