Gate Sensor code

Dependencies:   libmDot-mbed5 DHT22

Committer:
kellybs1
Date:
Mon Aug 07 07:21:45 2017 +0000
Revision:
8:206e0563e1a1
Parent:
7:e29228e39982
Child:
9:7f7194b5b4e3
this one might work

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