fang chen / Mbed 2 deprecated mDot_Connect_iotlab_demo2

Dependencies:   libmDot mbed mbed-rtos

Committer:
fangchen2
Date:
Tue Jan 29 01:20:20 2019 +0000
Revision:
14:3ce2765dd92f
Parent:
13:c966b64b227f
Child:
15:4131b64ca8f9
ok

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"
mfiore 4:36e214ebfa56 3 #include "MTSLog.h"
mfiore 0:09250cd371d2 4 #include <string>
mfiore 0:09250cd371d2 5 #include <vector>
mfiore 4:36e214ebfa56 6 #include <algorithm>
wang1tao 9:cf45820af9b9 7
wang1tao 13:c966b64b227f 8 const char TURNON[] = "turnon";
wang1tao 13:c966b64b227f 9 const char TURNOFF[] = "turnoff";
wang1tao 9:cf45820af9b9 10
fangchen2 14:3ce2765dd92f 11 const char TURN_ON_LIGHT[] = "TurnOnLight";
fangchen2 14:3ce2765dd92f 12 const char TURN_OFF_LIGHT[] = "TurnOffLight";
wang1tao 9:cf45820af9b9 13
wang1tao 13:c966b64b227f 14 const char _header[] = "PH Alarm";
fangchen2 14:3ce2765dd92f 15 //DigitalOut led1(LED1);
fangchen2 14:3ce2765dd92f 16 //DigitalOut pin(D7);
fangchen2 14:3ce2765dd92f 17 PwmOut pwm( LED1 );
fangchen2 14:3ce2765dd92f 18
fangchen2 14:3ce2765dd92f 19 //PwmOut pwmLED(GPIO4);
mfiore 0:09250cd371d2 20
mfiore 2:6e2c378339d9 21 // these options must match the settings on your Conduit
mfiore 2:6e2c378339d9 22 // uncomment the following lines and edit their values to match your configuration
fangchen2 14:3ce2765dd92f 23 static std::string config_network_name = "iotlab01";
fangchen2 14:3ce2765dd92f 24 static std::string config_network_pass = "1145141919810";
fangchen2 14:3ce2765dd92f 25 static uint8_t config_frequency_sub_band = 7;
mfiore 0:09250cd371d2 26
wang1tao 13:c966b64b227f 27 bool strCmp(char* str1, const char* str2, int len)
wang1tao 13:c966b64b227f 28 {
wang1tao 13:c966b64b227f 29 int i;
wang1tao 13:c966b64b227f 30 for (i=0; i<len; i++)
wang1tao 13:c966b64b227f 31 {
wang1tao 13:c966b64b227f 32 if(str1[i]!=str2[i])return false;
wang1tao 13:c966b64b227f 33 }
wang1tao 13:c966b64b227f 34 return true;
wang1tao 13:c966b64b227f 35 }
wang1tao 13:c966b64b227f 36
wang1tao 13:c966b64b227f 37
mfiore 0:09250cd371d2 38 int main() {
wang1tao 10:79124c0a5952 39 int32_t ret;
mfiore 0:09250cd371d2 40 mDot* dot;
wang1tao 13:c966b64b227f 41 std::vector<uint8_t> sendData, recvData;
wang1tao 13:c966b64b227f 42 std::vector<uint8_t> turnonData,turnoffData;
wang1tao 13:c966b64b227f 43 char sendBuf[11],recvBuf[30];
wang1tao 13:c966b64b227f 44 int i;
fangchen2 14:3ce2765dd92f 45 //pwm.period(0.001f);
fangchen2 14:3ce2765dd92f 46 pwm.period_ms(1);
fangchen2 14:3ce2765dd92f 47 pwm = 0.5f;
fangchen2 14:3ce2765dd92f 48 for (int val = 0; val < 10; val +=1){
fangchen2 14:3ce2765dd92f 49 pwm = !pwm;
fangchen2 14:3ce2765dd92f 50 wait(1);
fangchen2 14:3ce2765dd92f 51 }
fangchen2 14:3ce2765dd92f 52 //pwm.period_us(1000);
mfiore 0:09250cd371d2 53 // get a mDot handle
mfiore 0:09250cd371d2 54 dot = mDot::getInstance();
JCheng 5:6c7d2f4d7377 55
mfiore 2:6e2c378339d9 56 // print library version information
mfiore 2:6e2c378339d9 57 logInfo("version: %s", dot->getId().c_str());
mfiore 0:09250cd371d2 58
mfiore 2:6e2c378339d9 59 //*******************************************
mfiore 2:6e2c378339d9 60 // configuration
mfiore 2:6e2c378339d9 61 //*******************************************
mfiore 0:09250cd371d2 62 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 63 dot->resetConfig();
JCheng 5:6c7d2f4d7377 64
mfiore 4:36e214ebfa56 65 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
wang1tao 13:c966b64b227f 66
mfiore 2:6e2c378339d9 67 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 68 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
JCheng 5:6c7d2f4d7377 69
mfiore 4:36e214ebfa56 70 // frequency sub band is only applicable in the 915 (US) frequency band
mfiore 4:36e214ebfa56 71 // 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 72 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
mfiore 2:6e2c378339d9 73 logInfo("setting frequency sub band");
mfiore 0:09250cd371d2 74 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 75 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 76 }
JCheng 5:6c7d2f4d7377 77
mfiore 2:6e2c378339d9 78 logInfo("setting network name");
mfiore 2:6e2c378339d9 79 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 80 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 81 }
JCheng 5:6c7d2f4d7377 82
mfiore 2:6e2c378339d9 83 logInfo("setting network password");
mfiore 2:6e2c378339d9 84 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 85 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 86 }
JCheng 5:6c7d2f4d7377 87
mfiore 4:36e214ebfa56 88 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 89 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 90 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
mfiore 4:36e214ebfa56 91 logInfo("setting TX spreading factor");
mfiore 4:36e214ebfa56 92 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 93 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 94 }
JCheng 5:6c7d2f4d7377 95
mfiore 4:36e214ebfa56 96 // request receive confirmation of packets from the gateway
mfiore 4:36e214ebfa56 97 logInfo("enabling ACKs");
mfiore 4:36e214ebfa56 98 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 99 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 100 }
JCheng 5:6c7d2f4d7377 101
mfiore 4:36e214ebfa56 102 // save this configuration to the mDot's NVM
mfiore 2:6e2c378339d9 103 logInfo("saving config");
mfiore 2:6e2c378339d9 104 if (! dot->saveConfig()) {
mfiore 2:6e2c378339d9 105 logError("failed to save configuration");
mfiore 0:09250cd371d2 106 }
mfiore 2:6e2c378339d9 107 //*******************************************
mfiore 2:6e2c378339d9 108 // end of configuration
mfiore 2:6e2c378339d9 109 //*******************************************
mfiore 0:09250cd371d2 110
mfiore 0:09250cd371d2 111 // attempt to join the network
mfiore 2:6e2c378339d9 112 logInfo("joining network");
mfiore 0:09250cd371d2 113 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 114 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 115 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 116 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 117 }
mfiore 0:09250cd371d2 118 // format data for sending to the gateway
wang1tao 13:c966b64b227f 119 for(i=0; i< strlen(TURNON); i++ )
wang1tao 13:c966b64b227f 120 turnonData.push_back( TURNON[i] );
wang1tao 13:c966b64b227f 121
wang1tao 13:c966b64b227f 122 for(i=0; i< strlen(TURNOFF); i++ )
wang1tao 13:c966b64b227f 123 turnoffData.push_back( TURNOFF[i] );
wang1tao 13:c966b64b227f 124 // format data for sending to the gateway
wang1tao 13:c966b64b227f 125 for(i=0; i< strlen(_header); i++ )
wang1tao 13:c966b64b227f 126 sendData.push_back( _header[i] );
wang1tao 9:cf45820af9b9 127
wang1tao 9:cf45820af9b9 128 // send the data to the gateway
wang1tao 13:c966b64b227f 129 if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
wang1tao 9:cf45820af9b9 130 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 9:cf45820af9b9 131 } else {
wang1tao 9:cf45820af9b9 132 logInfo("successfully sent data to gateway");
wang1tao 9:cf45820af9b9 133 }
mfiore 0:09250cd371d2 134 while (true) {
wang1tao 13:c966b64b227f 135 for(i=0;i<30;i++)recvBuf[i]=0;
wang1tao 13:c966b64b227f 136 if ((ret = dot->send(turnonData)) != mDot::MDOT_OK) {
wang1tao 13:c966b64b227f 137 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 13:c966b64b227f 138 } else {
wang1tao 13:c966b64b227f 139 logInfo("successfully sent data to gateway");
wang1tao 13:c966b64b227f 140 recvData.clear();
wang1tao 13:c966b64b227f 141 if ((ret = dot->recv(recvData)) != mDot::MDOT_OK) {
wang1tao 13:c966b64b227f 142 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 13:c966b64b227f 143 } else {
wang1tao 13:c966b64b227f 144 logInfo("datasize = %d", recvData.size());
wang1tao 13:c966b64b227f 145 for( int i=0; i< recvData.size(); i++ )
wang1tao 13:c966b64b227f 146 recvBuf[i] = recvData[i];
wang1tao 13:c966b64b227f 147 logInfo("%s", recvBuf);
fangchen2 14:3ce2765dd92f 148 if(strCmp(recvBuf, TURN_ON_LIGHT, recvData.size())){
fangchen2 14:3ce2765dd92f 149 logInfo("Turn on the alarm");
fangchen2 14:3ce2765dd92f 150 std::vector<uint8_t> data;
fangchen2 14:3ce2765dd92f 151 data.push_back('1');
fangchen2 14:3ce2765dd92f 152 pwm = 0.0f;
fangchen2 14:3ce2765dd92f 153 //led1 = 1;
fangchen2 14:3ce2765dd92f 154 wait(0.10f);
fangchen2 14:3ce2765dd92f 155 dot->send(data);
fangchen2 14:3ce2765dd92f 156 }
fangchen2 14:3ce2765dd92f 157 if(strCmp(recvBuf, TURN_OFF_LIGHT, recvData.size())){
fangchen2 14:3ce2765dd92f 158 logInfo("Turn off the alarm");
fangchen2 14:3ce2765dd92f 159 std::vector<uint8_t> data;
fangchen2 14:3ce2765dd92f 160 data.push_back('0');
fangchen2 14:3ce2765dd92f 161 pwm = 1.0f;
fangchen2 14:3ce2765dd92f 162 //led1 = 0;
fangchen2 14:3ce2765dd92f 163 wait(0.10f);
fangchen2 14:3ce2765dd92f 164 dot->send(data);
fangchen2 14:3ce2765dd92f 165 }
wang1tao 13:c966b64b227f 166 }
wang1tao 13:c966b64b227f 167 }
fangchen2 14:3ce2765dd92f 168
mfiore 0:09250cd371d2 169 }
mfiore 0:09250cd371d2 170
mfiore 0:09250cd371d2 171 }