Here it is ...

Dependencies:   libxDot-mbed5 TSL2561

Fork of Dot-Examples by MultiTech

Committer:
dgutsch
Date:
Wed Jul 25 18:30:01 2018 +0000
Revision:
38:401bd606d2dd
Parent:
37:4818522075f8
Child:
39:0d4362063352
added tsl2561 library

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"
dgutsch 38:401bd606d2dd 3 #include "TSL2561.h"
mfiore 0:a151a6350d7f 4
mfiore 0:a151a6350d7f 5 #if ACTIVE_EXAMPLE == OTA_EXAMPLE
mfiore 0:a151a6350d7f 6
mfiore 17:d4f82e16de5f 7 /////////////////////////////////////////////////////////////////////////////
mfiore 17:d4f82e16de5f 8 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
mfiore 17:d4f82e16de5f 9 // * Because these example programs can be used for both mDot and xDot //
mfiore 17:d4f82e16de5f 10 // devices, the LoRa stack is not included. The libmDot library should //
mfiore 17:d4f82e16de5f 11 // be imported if building for mDot devices. The libxDot library //
mfiore 17:d4f82e16de5f 12 // should be imported if building for xDot devices. //
mfiore 17:d4f82e16de5f 13 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ //
mfiore 17:d4f82e16de5f 14 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ //
mfiore 17:d4f82e16de5f 15 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ //
mfiore 17:d4f82e16de5f 16 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ //
mfiore 17:d4f82e16de5f 17 /////////////////////////////////////////////////////////////////////////////
mfiore 17:d4f82e16de5f 18
Mike Fiore 5:97ed5f2f099e 19 /////////////////////////////////////////////////////////////
Mike Fiore 5:97ed5f2f099e 20 // * these options must match the settings on your gateway //
Mike Fiore 5:97ed5f2f099e 21 // * edit their values to match your configuration //
Mike Fiore 5:97ed5f2f099e 22 // * frequency sub band is only relevant for the 915 bands //
Mike Fiore 5:97ed5f2f099e 23 // * either the network name and passphrase can be used or //
Mike Fiore 5:97ed5f2f099e 24 // the network ID (8 bytes) and KEY (16 bytes) //
Mike Fiore 5:97ed5f2f099e 25 /////////////////////////////////////////////////////////////
dgutsch 30:0059ae42ff02 26 static std::string network_name = "MTCDT-19400691";
dgutsch 30:0059ae42ff02 27 static std::string network_passphrase = "MTCDT-19400691";
Mike Fiore 5:97ed5f2f099e 28 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
Mike Fiore 5:97ed5f2f099e 29 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
dgutsch 30:0059ae42ff02 30 static uint8_t frequency_sub_band = 1;
dgutsch 30:0059ae42ff02 31 static lora::NetworkType public_network;// = lora::PUBLIC_LORAWAN;
David Gutsch dgutsch@umail.iu.edu 32:cc05a2e80969 32 static uint8_t join_delay = 1;
Mike Fiore 15:364df461110f 33 static uint8_t ack = 0;
Mike Fiore 21:09d05faf0e13 34 static bool adr = true;
mfiore 0:a151a6350d7f 35
mfiore 0:a151a6350d7f 36 // deepsleep consumes slightly less current than sleep
mfiore 0:a151a6350d7f 37 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
mfiore 0:a151a6350d7f 38 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
mfiore 0:a151a6350d7f 39 // if deep_sleep == true, device will enter deepsleep mode
David Gutsch dgutsch@umail.iu.edu 37:4818522075f8 40 static bool deep_sleep = true;
mfiore 0:a151a6350d7f 41
mfiore 0:a151a6350d7f 42 mDot* dot = NULL;
Mike Fiore 21:09d05faf0e13 43 lora::ChannelPlan* plan = NULL;
mfiore 0:a151a6350d7f 44
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 45
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 46 Serial pc(PA_9, PA_10);
mfiore 0:a151a6350d7f 47
mfiore 0:a151a6350d7f 48 int main() {
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 49 pc.baud(115200);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 50 pc.printf("main started \r\n");
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 51 // Custom event handler for automatically displaying RX data
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 52 RadioEvent events;
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 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 35:b51d482e9731 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;
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 142 char buffer[5];
David Gutsch dgutsch@umail.iu.edu 32:cc05a2e80969 143
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 144 while (true) {
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 145
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 146 // join network if not joined
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 147 if (!dot->getNetworkJoinStatus()) {
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 148 pc.printf("joining lora network.\r\n");
David Gutsch dgutsch@umail.iu.edu 32:cc05a2e80969 149 join_network(&pc);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 150 }
David Gutsch dgutsch@umail.iu.edu 36:5529d26e97e6 151
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 152
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 153 // hourly loop
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 154 while( counter < 12) {
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 155
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 156 uint8_t light_data = get_light_data();
David Gutsch dgutsch@umail.iu.edu 36:5529d26e97e6 157 //pc.printf("pseudo-light data: %d\r\n", light_data);
David Gutsch dgutsch@umail.iu.edu 37:4818522075f8 158 pc.printf("\r\nhour: %d\r\n", ++counter);
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 159
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 160
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 161 // clean out char buffer
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 162 strcpy(buffer, "XXXX");
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 163
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 164 // convert to ascii char && push to vector
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 165 ascii_converter(buffer, light_data, &tx_data, &pc);
David Gutsch dgutsch@umail.iu.edu 36:5529d26e97e6 166 pc.printf("char buffer: %s\r\n", buffer);
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 167
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 168 if( counter < 12)
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 169 tx_data.push_back(' ');
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 170
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 171
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 172 // 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 173 // not necessary if going into sleep mode since RAM is retained
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 174 if (deep_sleep) {
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 175 logInfo("saving network session to NVM");
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 176 dot->saveNetworkSession();
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 177 }
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 178
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 179 // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
David Gutsch dgutsch@umail.iu.edu 37:4818522075f8 180 sleep_wake_rtc_only(deep_sleep, 30, &pc);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 181 //sleep_wake_interrupt_only(deep_sleep);
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 182 //sleep_wake_rtc_or_interrupt(deep_sleep);
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 183 }
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 184
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 185 //print vector data
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 186 pc.printf("vector data:");
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 187
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 188 for(std::vector<uint8_t>::const_iterator iter = tx_data.begin(); iter != tx_data.end(); ++iter) {
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 189 pc.printf(" %c",*iter);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 190 }
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 191 pc.printf("\r\n");
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 192
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 193 pc.printf("capacity tx_data: %d\r\n", tx_data.capacity());
David Gutsch dgutsch@umail.iu.edu 37:4818522075f8 194 //tx_data.reserve(tx_data.size());
David Gutsch dgutsch@umail.iu.edu 37:4818522075f8 195 //pc.printf("cap resized: %d\r\n", tx_data.capacity());
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 196 pc.printf("sized tx_data: %d\r\n", tx_data.size());
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 197 //actually send the data
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 198 pc.printf("sending data\r\n");
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 199
David Gutsch dgutsch@umail.iu.edu 36:5529d26e97e6 200
David Gutsch dgutsch@umail.iu.edu 36:5529d26e97e6 201 // set the data_rate (from forum)
David Gutsch dgutsch@umail.iu.edu 37:4818522075f8 202 dot->setTxDataRate(lora::DR_1);
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 203 int retVal = send_data(tx_data, &pc);
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 204 //while(retVal == 1)
David Gutsch dgutsch@umail.iu.edu 35:b51d482e9731 205 //retVal = send_data(tx_data, &pc);
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 206
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 207 // erase vector data & reset counter
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 208 tx_data.erase(tx_data.begin(), tx_data.end());
David Gutsch dgutsch@umail.iu.edu 33:79e4c812d91d 209 counter = 0;
David Gutsch dgutsch@umail.iu.edu 36:5529d26e97e6 210 pc.printf("deleted tx_data and reset counter\r\ncap: %d size: %d\r\n", tx_data.capacity(), tx_data.size());
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 211
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 212 }
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 213
David Gutsch dgutsch@umail.iu.edu 31:b990f61b9ee0 214 return 0;
mfiore 0:a151a6350d7f 215 }
mfiore 0:a151a6350d7f 216
mfiore 0:a151a6350d7f 217 #endif