Here it is ...

Dependencies:   libxDot-mbed5 TSL2561

Fork of Dot-Examples by MultiTech

Committer:
David Gutsch dgutsch@umail.iu.edu
Date:
Thu Jul 19 10:48:47 2018 -0400
Revision:
33:79e4c812d91d
Parent:
32:cc05a2e80969
Child:
35:b51d482e9731
app collects data 12 times and sends. Next step: 1) figure out why send is only successful once 2) set sleep time for correct interval

Who changed what in which revision?

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