LoRa sending TSL lux sensor

Dependencies:   TSL2561_I2C libmDot-mbed5

Committer:
kellybs1
Date:
Wed Nov 01 21:10:36 2017 +0000
Revision:
0:2abdab750246
initial;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kellybs1 0:2abdab750246 1 /*
kellybs1 0:2abdab750246 2 This program:
kellybs1 0:2abdab750246 3 - connects to a LoRaWAN by ABP/MANUAL
kellybs1 0:2abdab750246 4 - reads light data from an TSL2561 abient light sensor
kellybs1 0:2abdab750246 5 - sends the recorded data onto the LoRaWAN
kellybs1 0:2abdab750246 6 - sets the mDot to sleep
kellybs1 0:2abdab750246 7 - repeats these operations in a loop
kellybs1 0:2abdab750246 8 */
kellybs1 0:2abdab750246 9
kellybs1 0:2abdab750246 10 #include "mbed.h"
kellybs1 0:2abdab750246 11 #include "mDot.h"
kellybs1 0:2abdab750246 12 #include "ChannelPlans.h"
kellybs1 0:2abdab750246 13 #include "MTSLog.h"
kellybs1 0:2abdab750246 14 #include "dot_util.h"
kellybs1 0:2abdab750246 15 #include "mbed.h"
kellybs1 0:2abdab750246 16 #include "TSL2561_I2C.h"
kellybs1 0:2abdab750246 17 #include <string>
kellybs1 0:2abdab750246 18 #include <vector>
kellybs1 0:2abdab750246 19 #include <algorithm>
kellybs1 0:2abdab750246 20 #include <sstream>
kellybs1 0:2abdab750246 21
kellybs1 0:2abdab750246 22
kellybs1 0:2abdab750246 23 #define TSL_SDA_PIN PC_9
kellybs1 0:2abdab750246 24 #define TSL_SCL_PIN PA_8
kellybs1 0:2abdab750246 25 //TSL2561_I2C(inName sda, PinName scl)
kellybs1 0:2abdab750246 26 TSL2561_I2C tsl(TSL_SDA_PIN, TSL_SCL_PIN);
kellybs1 0:2abdab750246 27
kellybs1 0:2abdab750246 28 // these options must match the settings on your gateway/server
kellybs1 0:2abdab750246 29 /*
kellybs1 0:2abdab750246 30 Current test settings
kellybs1 0:2abdab750246 31 dev address: 072389f7
kellybs1 0:2abdab750246 32 net sess key: b35aca73d283996dc3cbc0803af04547
kellybs1 0:2abdab750246 33 app sess key: d6f28430da4035273b9e3c07eb30c0dd
kellybs1 0:2abdab750246 34 */
kellybs1 0:2abdab750246 35 //device address
kellybs1 0:2abdab750246 36 static uint8_t network_address[] = { 0x07, 0x23, 0x89, 0xf7 };
kellybs1 0:2abdab750246 37 //network session key
kellybs1 0:2abdab750246 38 static uint8_t network_session_key[] = { 0xb3, 0x5a, 0xca, 0x73, 0xd2, 0x83, 0x99, 0x6d, 0xc3, 0xcb, 0xc0, 0x80, 0x3a, 0xf0, 0x45, 0x47 };
kellybs1 0:2abdab750246 39 //application sesssion or data session key
kellybs1 0:2abdab750246 40 static uint8_t data_session_key[] = { 0xd6, 0xf2, 0x84, 0x30, 0xda, 0x40, 0x35, 0x27, 0x3b, 0x9e, 0x3c, 0x07, 0xeb, 0x30, 0xc0, 0xdd };
kellybs1 0:2abdab750246 41 static uint8_t frequency_sub_band = 2; //VFI
kellybs1 0:2abdab750246 42 static bool public_network = true;
kellybs1 0:2abdab750246 43 //enable receipt of ackknowledge packets 0 = No, 1 = Yes
kellybs1 0:2abdab750246 44 static uint8_t ack = 0;
kellybs1 0:2abdab750246 45 //adaptive data rate enabler
kellybs1 0:2abdab750246 46 static bool adr = false;
kellybs1 0:2abdab750246 47
kellybs1 0:2abdab750246 48 //USB serial
kellybs1 0:2abdab750246 49 Serial pc(USBTX, USBRX);
kellybs1 0:2abdab750246 50
kellybs1 0:2abdab750246 51 //get ourselves an mDot pointer - we will assign to it in main()
kellybs1 0:2abdab750246 52 mDot* dot = NULL;
kellybs1 0:2abdab750246 53
kellybs1 0:2abdab750246 54 //converts value to string
kellybs1 0:2abdab750246 55 template <typename T>
kellybs1 0:2abdab750246 56 string ToString(T val) {
kellybs1 0:2abdab750246 57 stringstream stream;
kellybs1 0:2abdab750246 58 stream << val;
kellybs1 0:2abdab750246 59 return stream.str();
kellybs1 0:2abdab750246 60 }
kellybs1 0:2abdab750246 61
kellybs1 0:2abdab750246 62 int main() {
kellybs1 0:2abdab750246 63 //setting serial rate
kellybs1 0:2abdab750246 64 pc.baud(9600);
kellybs1 0:2abdab750246 65
kellybs1 0:2abdab750246 66 // use AU915 plan
kellybs1 0:2abdab750246 67 lora::ChannelPlan* plan = new lora::ChannelPlan_AU915();
kellybs1 0:2abdab750246 68 assert(plan);
kellybs1 0:2abdab750246 69 // get a mDot handle with the plan we chose
kellybs1 0:2abdab750246 70 dot = mDot::getInstance(plan);
kellybs1 0:2abdab750246 71 assert(dot);
kellybs1 0:2abdab750246 72
kellybs1 0:2abdab750246 73 if (!dot->getStandbyFlag()) {
kellybs1 0:2abdab750246 74 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
kellybs1 0:2abdab750246 75
kellybs1 0:2abdab750246 76 // start from a well-known state
kellybs1 0:2abdab750246 77 logInfo("defaulting Dot configuration");
kellybs1 0:2abdab750246 78 dot->resetConfig();
kellybs1 0:2abdab750246 79 dot->resetNetworkSession();
kellybs1 0:2abdab750246 80
kellybs1 0:2abdab750246 81 // make sure library logging is turned on
kellybs1 0:2abdab750246 82 dot->setLogLevel(mts::MTSLog::DEBUG_LEVEL);
kellybs1 0:2abdab750246 83
kellybs1 0:2abdab750246 84 // update configuration if necessary
kellybs1 0:2abdab750246 85 if (dot->getJoinMode() != mDot::MANUAL) {
kellybs1 0:2abdab750246 86 logInfo("changing network join mode to MANUAL");
kellybs1 0:2abdab750246 87 if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) {
kellybs1 0:2abdab750246 88 logError("failed to set network join mode to MANUAL");
kellybs1 0:2abdab750246 89 }
kellybs1 0:2abdab750246 90 }
kellybs1 0:2abdab750246 91 // in MANUAL join mode there is no join request/response transaction
kellybs1 0:2abdab750246 92 // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate
kellybs1 0:2abdab750246 93 // network address - 4 bytes (00000001 - FFFFFFFE)
kellybs1 0:2abdab750246 94 // network session key - 16 bytes
kellybs1 0:2abdab750246 95 // data session key - 16 bytes
kellybs1 0:2abdab750246 96 // to provision your Dot with a Conduit gateway, follow the following steps
kellybs1 0:2abdab750246 97 // * ssh into the Conduit
kellybs1 0:2abdab750246 98 // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/
kellybs1 0:2abdab750246 99 // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304
kellybs1 0:2abdab750246 100 // * if you change the network address, network session key, or data session key, make sure you update them on the gateway
kellybs1 0:2abdab750246 101 // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation
kellybs1 0:2abdab750246 102 update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, public_network, ack);
kellybs1 0:2abdab750246 103
kellybs1 0:2abdab750246 104
kellybs1 0:2abdab750246 105 // enable or disable Adaptive Data Rate
kellybs1 0:2abdab750246 106 dot->setAdr(adr);
kellybs1 0:2abdab750246 107
kellybs1 0:2abdab750246 108 //* AU915 Datarates
kellybs1 0:2abdab750246 109 //* ---------------
kellybs1 0:2abdab750246 110 //* DR0 - SF10BW125 -- 11 bytes
kellybs1 0:2abdab750246 111 //* DR1 - SF9BW125 -- 53 bytes
kellybs1 0:2abdab750246 112 //* DR2 - SF8BW125 -- 129 byte
kellybs1 0:2abdab750246 113 //* DR3 - SF7BW125 -- 242 bytes
kellybs1 0:2abdab750246 114 //* DR4 - SF8BW500 -- 242 bytes
kellybs1 0:2abdab750246 115 dot->setTxDataRate(mDot::DR2);
kellybs1 0:2abdab750246 116
kellybs1 0:2abdab750246 117 // save changes to configuration
kellybs1 0:2abdab750246 118 logInfo("saving configuration");
kellybs1 0:2abdab750246 119 if (!dot->saveConfig()) {
kellybs1 0:2abdab750246 120 logError("failed to save configuration");
kellybs1 0:2abdab750246 121 }
kellybs1 0:2abdab750246 122
kellybs1 0:2abdab750246 123 // display configuration
kellybs1 0:2abdab750246 124 display_config();
kellybs1 0:2abdab750246 125 } else {
kellybs1 0:2abdab750246 126 // restore the saved session if the dot woke from deepsleep mode
kellybs1 0:2abdab750246 127 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
kellybs1 0:2abdab750246 128 logInfo("restoring network session from NVM");
kellybs1 0:2abdab750246 129 dot->restoreNetworkSession();
kellybs1 0:2abdab750246 130 }
kellybs1 0:2abdab750246 131
kellybs1 0:2abdab750246 132 //set gain level on lux sensor
kellybs1 0:2abdab750246 133 //setgain accepts 1 or 16 - 1 for normal, 16 for very low light
kellybs1 0:2abdab750246 134 logInfo("Gain return %d", tsl.setGain(1)); //16 seems to cause NaNs
kellybs1 0:2abdab750246 135
kellybs1 0:2abdab750246 136 //this is where the magic happens
kellybs1 0:2abdab750246 137 while (true) {
kellybs1 0:2abdab750246 138
kellybs1 0:2abdab750246 139 //init data variable
kellybs1 0:2abdab750246 140 std::vector<uint8_t> data;
kellybs1 0:2abdab750246 141
kellybs1 0:2abdab750246 142 //read Lux Level
kellybs1 0:2abdab750246 143 //thank you https://developer.mbed.org/users/merckeng/code/mDot_TTN_DHT11_Boston16_CAM/file/a2c9c4cc4863/main.cpp
kellybs1 0:2abdab750246 144 tsl.enablePower();
kellybs1 0:2abdab750246 145 //float lux = tsl.getLux();
kellybs1 0:2abdab750246 146 //logInfo("Lux level: %.4f", lux);
kellybs1 0:2abdab750246 147
kellybs1 0:2abdab750246 148
kellybs1 0:2abdab750246 149 int lux2 = tsl.getVisibleAndIR();
kellybs1 0:2abdab750246 150 logInfo("Lux level: %d", lux2);
kellybs1 0:2abdab750246 151 //put data in string
kellybs1 0:2abdab750246 152 string output = "D206.Lux: " + ToString(lux2);
kellybs1 0:2abdab750246 153
kellybs1 0:2abdab750246 154 //serial output for debugging
kellybs1 0:2abdab750246 155 logInfo("Sending %s", output.c_str());
kellybs1 0:2abdab750246 156
kellybs1 0:2abdab750246 157 // format data for sending to the gateway
kellybs1 0:2abdab750246 158 for (std::string::iterator it = output.begin(); it != output.end(); it++)
kellybs1 0:2abdab750246 159 data.push_back((uint8_t) *it);
kellybs1 0:2abdab750246 160
kellybs1 0:2abdab750246 161 //now send
kellybs1 0:2abdab750246 162 send_data(data);
kellybs1 0:2abdab750246 163
kellybs1 0:2abdab750246 164 // go to sleep and wake up automatically sleep_time seconds later
kellybs1 0:2abdab750246 165 uint32_t sleep_time = 60;
kellybs1 0:2abdab750246 166 //false is "don't deep sleep" - mDot doesn't do that
kellybs1 0:2abdab750246 167 dot->sleep(sleep_time, mDot::RTC_ALARM, false);
kellybs1 0:2abdab750246 168 }
kellybs1 0:2abdab750246 169
kellybs1 0:2abdab750246 170 return 0; //shouldn't happen
kellybs1 0:2abdab750246 171 }
kellybs1 0:2abdab750246 172