Gate Sensor code

Dependencies:   libmDot-mbed5 DHT22

Committer:
kellybs1
Date:
Mon Aug 07 07:33:20 2017 +0000
Revision:
9:7f7194b5b4e3
Parent:
8:206e0563e1a1
Child:
10:02615da7a9fe
touchups

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:09250cd371d2 1 #include "mbed.h"
mfiore 0:09250cd371d2 2 #include "mDot.h"
kellybs1 8:206e0563e1a1 3 #include "ChannelPlans.h"
mfiore 4:36e214ebfa56 4 #include "MTSLog.h"
mfiore 0:09250cd371d2 5 #include <string>
mfiore 0:09250cd371d2 6 #include <vector>
mfiore 4:36e214ebfa56 7 #include <algorithm>
mfiore 0:09250cd371d2 8
mfiore 2:6e2c378339d9 9 // these options must match the settings on your Conduit
mfiore 2:6e2c378339d9 10 // uncomment the following lines and edit their values to match your configuration
jreiss 5:6b988a804fcb 11 static uint8_t config_network_addr[] = { 0x01, 0x02, 0x03, 0x04 };
jreiss 5:6b988a804fcb 12 static uint8_t config_network_nskey[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
jreiss 5:6b988a804fcb 13 static uint8_t config_network_dskey[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
jreiss 5:6b988a804fcb 14 static uint8_t config_frequency_sub_band = 1;
mfiore 0:09250cd371d2 15
kellybs1 9:7f7194b5b4e3 16 Serial pc(USBTX, USBRX);
kellybs1 9:7f7194b5b4e3 17
kellybs1 9:7f7194b5b4e3 18
kellybs1 9:7f7194b5b4e3 19
mfiore 0:09250cd371d2 20 int main() {
kellybs1 9:7f7194b5b4e3 21 pc.baud(9600);
mfiore 0:09250cd371d2 22 int32_t ret;
mfiore 0:09250cd371d2 23 std::vector<uint8_t> data;
mfiore 4:36e214ebfa56 24 std::string data_str = "hello!";
mfiore 2:6e2c378339d9 25
kellybs1 9:7f7194b5b4e3 26 // use AU915 plan
kellybs1 9:7f7194b5b4e3 27 lora::ChannelPlan* plan = new lora::ChannelPlan_AU915();
kellybs1 7:e29228e39982 28 assert(plan);
mfiore 0:09250cd371d2 29 // get a mDot handle
kellybs1 7:e29228e39982 30 mDot* dot = mDot::getInstance(plan);
kellybs1 7:e29228e39982 31 assert(dot);
kellybs1 7:e29228e39982 32
mfiore 2:6e2c378339d9 33
mfiore 2:6e2c378339d9 34 // print library version information
mfiore 2:6e2c378339d9 35 logInfo("version: %s", dot->getId().c_str());
mfiore 0:09250cd371d2 36
mfiore 2:6e2c378339d9 37 //*******************************************
mfiore 2:6e2c378339d9 38 // configuration
mfiore 2:6e2c378339d9 39 //*******************************************
mfiore 0:09250cd371d2 40 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 41 dot->resetConfig();
mfiore 0:09250cd371d2 42
mfiore 4:36e214ebfa56 43 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
mfiore 0:09250cd371d2 44
mfiore 2:6e2c378339d9 45 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 46 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
mfiore 4:36e214ebfa56 47
mfiore 4:36e214ebfa56 48 // frequency sub band is only applicable in the 915 (US) frequency band
mfiore 4:36e214ebfa56 49 // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band
mfiore 4:36e214ebfa56 50 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
mfiore 2:6e2c378339d9 51 logInfo("setting frequency sub band");
mfiore 0:09250cd371d2 52 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 53 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 54 }
mfiore 4:36e214ebfa56 55
jreiss 5:6b988a804fcb 56 std::vector<uint8_t> temp;
jreiss 5:6b988a804fcb 57
jreiss 5:6b988a804fcb 58 for (int i = 0; i < 4; i++) {
jreiss 5:6b988a804fcb 59 temp.push_back(config_network_addr[i]);
jreiss 5:6b988a804fcb 60 }
jreiss 5:6b988a804fcb 61
jreiss 5:6b988a804fcb 62 logInfo("setting network addr");
jreiss 5:6b988a804fcb 63 if ((ret = dot->setNetworkAddress(temp)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 64 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 65 }
mfiore 4:36e214ebfa56 66
jreiss 5:6b988a804fcb 67 temp.clear();
jreiss 5:6b988a804fcb 68 for (int i = 0; i < 16; i++) {
jreiss 5:6b988a804fcb 69 temp.push_back(config_network_nskey[i]);
jreiss 5:6b988a804fcb 70 }
jreiss 5:6b988a804fcb 71
mfiore 2:6e2c378339d9 72 logInfo("setting network password");
jreiss 5:6b988a804fcb 73 if ((ret = dot->setNetworkSessionKey(temp)) != mDot::MDOT_OK) {
jreiss 5:6b988a804fcb 74 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
jreiss 5:6b988a804fcb 75 }
jreiss 5:6b988a804fcb 76
jreiss 5:6b988a804fcb 77 temp.clear();
jreiss 5:6b988a804fcb 78 for (int i = 0; i < 16; i++) {
jreiss 5:6b988a804fcb 79 temp.push_back(config_network_dskey[i]);
jreiss 5:6b988a804fcb 80 }
jreiss 5:6b988a804fcb 81
jreiss 5:6b988a804fcb 82 logInfo("setting network password");
jreiss 5:6b988a804fcb 83 if ((ret = dot->setDataSessionKey(temp)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 84 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 85 }
mfiore 4:36e214ebfa56 86
mfiore 4:36e214ebfa56 87 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 88 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 89 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
mfiore 4:36e214ebfa56 90 logInfo("setting TX spreading factor");
mfiore 4:36e214ebfa56 91 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 92 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 93 }
mfiore 4:36e214ebfa56 94
mfiore 4:36e214ebfa56 95 // request receive confirmation of packets from the gateway
mfiore 4:36e214ebfa56 96 logInfo("enabling ACKs");
mfiore 4:36e214ebfa56 97 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 98 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 99 }
mfiore 4:36e214ebfa56 100
mfiore 4:36e214ebfa56 101 // save this configuration to the mDot's NVM
mfiore 2:6e2c378339d9 102 logInfo("saving config");
mfiore 2:6e2c378339d9 103 if (! dot->saveConfig()) {
mfiore 2:6e2c378339d9 104 logError("failed to save configuration");
mfiore 0:09250cd371d2 105 }
mfiore 2:6e2c378339d9 106 //*******************************************
mfiore 2:6e2c378339d9 107 // end of configuration
mfiore 2:6e2c378339d9 108 //*******************************************
mfiore 0:09250cd371d2 109
mfiore 0:09250cd371d2 110 // attempt to join the network
mfiore 2:6e2c378339d9 111 logInfo("joining network");
mfiore 0:09250cd371d2 112 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 113 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 114 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 115 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 116 }
mfiore 0:09250cd371d2 117
mfiore 0:09250cd371d2 118 // format data for sending to the gateway
mfiore 0:09250cd371d2 119 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
mfiore 0:09250cd371d2 120 data.push_back((uint8_t) *it);
mfiore 0:09250cd371d2 121
mfiore 0:09250cd371d2 122 while (true) {
mfiore 4:36e214ebfa56 123 // send the data to the gateway
mfiore 0:09250cd371d2 124 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 125 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 126 } else {
mfiore 2:6e2c378339d9 127 logInfo("successfully sent data to gateway");
mfiore 0:09250cd371d2 128 }
mfiore 0:09250cd371d2 129
mfiore 4:36e214ebfa56 130 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 131 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 132 }
mfiore 0:09250cd371d2 133
kellybs1 9:7f7194b5b4e3 134 return 0; //unreachable
mfiore 0:09250cd371d2 135 }