updated

Dependencies:   MTS-Serial libmDot mbed-rtos mbed

Fork of mDot_LoRa_Street_light by HM_IOT_Demo

Committer:
ivaneco
Date:
Fri Jul 15 17:36:22 2016 +0000
Revision:
0:451bc0b1d5ee
Child:
1:c999b29cd8ad
set network;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ivaneco 0:451bc0b1d5ee 1 #include "mbed.h"
ivaneco 0:451bc0b1d5ee 2 #include "mDot.h"
ivaneco 0:451bc0b1d5ee 3 #include "MTSLog.h"
ivaneco 0:451bc0b1d5ee 4 #include <string>
ivaneco 0:451bc0b1d5ee 5 #include <vector>
ivaneco 0:451bc0b1d5ee 6 #include <algorithm>
ivaneco 0:451bc0b1d5ee 7
ivaneco 0:451bc0b1d5ee 8 // these options must match the settings on your Conduit
ivaneco 0:451bc0b1d5ee 9 // uncomment the following lines and edit their values to match your configuration
ivaneco 0:451bc0b1d5ee 10 static std::string config_network_name = "networklora";
ivaneco 0:451bc0b1d5ee 11 static std::string config_network_pass = "senhalora";
ivaneco 0:451bc0b1d5ee 12 static uint8_t config_frequency_sub_band = 7;
ivaneco 0:451bc0b1d5ee 13
ivaneco 0:451bc0b1d5ee 14 int main() {
ivaneco 0:451bc0b1d5ee 15 int32_t ret;
ivaneco 0:451bc0b1d5ee 16 mDot* dot;
ivaneco 0:451bc0b1d5ee 17 std::vector<uint8_t> data;
ivaneco 0:451bc0b1d5ee 18 std::string data_str = "hello!";
ivaneco 0:451bc0b1d5ee 19
ivaneco 0:451bc0b1d5ee 20 // get a mDot handle
ivaneco 0:451bc0b1d5ee 21 dot = mDot::getInstance();
ivaneco 0:451bc0b1d5ee 22
ivaneco 0:451bc0b1d5ee 23 // print library version information
ivaneco 0:451bc0b1d5ee 24 logInfo("version: %s", dot->getId().c_str());
ivaneco 0:451bc0b1d5ee 25
ivaneco 0:451bc0b1d5ee 26 //*******************************************
ivaneco 0:451bc0b1d5ee 27 // configuration
ivaneco 0:451bc0b1d5ee 28 //*******************************************
ivaneco 0:451bc0b1d5ee 29 // reset to default config so we know what state we're in
ivaneco 0:451bc0b1d5ee 30 dot->resetConfig();
ivaneco 0:451bc0b1d5ee 31
ivaneco 0:451bc0b1d5ee 32 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
ivaneco 0:451bc0b1d5ee 33
ivaneco 0:451bc0b1d5ee 34 // set up the mDot with our network information: frequency sub band, network name, and network password
ivaneco 0:451bc0b1d5ee 35 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
ivaneco 0:451bc0b1d5ee 36
ivaneco 0:451bc0b1d5ee 37 // frequency sub band is only applicable in the 915 (US) frequency band
ivaneco 0:451bc0b1d5ee 38 // 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
ivaneco 0:451bc0b1d5ee 39 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
ivaneco 0:451bc0b1d5ee 40
ivaneco 0:451bc0b1d5ee 41 // logInfo("setting to public network");
ivaneco 0:451bc0b1d5ee 42 // if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
ivaneco 0:451bc0b1d5ee 43 // logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ivaneco 0:451bc0b1d5ee 44 // }
ivaneco 0:451bc0b1d5ee 45
ivaneco 0:451bc0b1d5ee 46 logInfo("setting frequency sub band");
ivaneco 0:451bc0b1d5ee 47 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
ivaneco 0:451bc0b1d5ee 48 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ivaneco 0:451bc0b1d5ee 49 }
ivaneco 0:451bc0b1d5ee 50
ivaneco 0:451bc0b1d5ee 51 logInfo("setting network name");
ivaneco 0:451bc0b1d5ee 52 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
ivaneco 0:451bc0b1d5ee 53 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ivaneco 0:451bc0b1d5ee 54 }
ivaneco 0:451bc0b1d5ee 55
ivaneco 0:451bc0b1d5ee 56 logInfo("setting network password");
ivaneco 0:451bc0b1d5ee 57 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
ivaneco 0:451bc0b1d5ee 58 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ivaneco 0:451bc0b1d5ee 59 }
ivaneco 0:451bc0b1d5ee 60
ivaneco 0:451bc0b1d5ee 61 // a higher spreading factor allows for longer range but lower throughput
ivaneco 0:451bc0b1d5ee 62 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
ivaneco 0:451bc0b1d5ee 63 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
ivaneco 0:451bc0b1d5ee 64 logInfo("setting TX spreading factor");
ivaneco 0:451bc0b1d5ee 65 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
ivaneco 0:451bc0b1d5ee 66 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ivaneco 0:451bc0b1d5ee 67 }
ivaneco 0:451bc0b1d5ee 68
ivaneco 0:451bc0b1d5ee 69 // request receive confirmation of packets from the gateway
ivaneco 0:451bc0b1d5ee 70 logInfo("enabling ACKs");
ivaneco 0:451bc0b1d5ee 71 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
ivaneco 0:451bc0b1d5ee 72 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ivaneco 0:451bc0b1d5ee 73 }
ivaneco 0:451bc0b1d5ee 74
ivaneco 0:451bc0b1d5ee 75 // save this configuration to the mDot's NVM
ivaneco 0:451bc0b1d5ee 76 logInfo("saving config");
ivaneco 0:451bc0b1d5ee 77 if (! dot->saveConfig()) {
ivaneco 0:451bc0b1d5ee 78 logError("failed to save configuration");
ivaneco 0:451bc0b1d5ee 79 }
ivaneco 0:451bc0b1d5ee 80 //*******************************************
ivaneco 0:451bc0b1d5ee 81 // end of configuration
ivaneco 0:451bc0b1d5ee 82 //*******************************************
ivaneco 0:451bc0b1d5ee 83
ivaneco 0:451bc0b1d5ee 84 // attempt to join the network
ivaneco 0:451bc0b1d5ee 85 logInfo("joining network");
ivaneco 0:451bc0b1d5ee 86 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
ivaneco 0:451bc0b1d5ee 87 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ivaneco 0:451bc0b1d5ee 88 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
ivaneco 0:451bc0b1d5ee 89 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
ivaneco 0:451bc0b1d5ee 90 }
ivaneco 0:451bc0b1d5ee 91
ivaneco 0:451bc0b1d5ee 92 // format data for sending to the gateway
ivaneco 0:451bc0b1d5ee 93 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
ivaneco 0:451bc0b1d5ee 94 data.push_back((uint8_t) *it);
ivaneco 0:451bc0b1d5ee 95
ivaneco 0:451bc0b1d5ee 96 while (true) {
ivaneco 0:451bc0b1d5ee 97 // send the data to the gateway
ivaneco 0:451bc0b1d5ee 98 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
ivaneco 0:451bc0b1d5ee 99 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
ivaneco 0:451bc0b1d5ee 100 } else {
ivaneco 0:451bc0b1d5ee 101 logInfo("successfully sent data to gateway");
ivaneco 0:451bc0b1d5ee 102 }
ivaneco 0:451bc0b1d5ee 103
ivaneco 0:451bc0b1d5ee 104 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
ivaneco 0:451bc0b1d5ee 105 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
ivaneco 0:451bc0b1d5ee 106 }
ivaneco 0:451bc0b1d5ee 107
ivaneco 0:451bc0b1d5ee 108 return 0;
ivaneco 0:451bc0b1d5ee 109 }