edge / Mbed 2 deprecated testReed

Dependencies:   libmDot mbed-rtos mbed

Committer:
waltertakens
Date:
Thu Jun 02 12:27:37 2016 +0000
Revision:
5:5654666925e1
Parent:
4:0fb159501a04
met lora

Who changed what in which revision?

UserRevisionLine numberNew contents of line
boddeke 0:208bd045dd31 1 #include "mbed.h"
boddeke 0:208bd045dd31 2 #include "mDot.h"
boddeke 0:208bd045dd31 3 #include "MTSLog.h"
waltertakens 5:5654666925e1 4 #include <string>
waltertakens 5:5654666925e1 5 #include <vector>
waltertakens 5:5654666925e1 6 #include <algorithm>
boddeke 0:208bd045dd31 7
waltertakens 5:5654666925e1 8 #include "payload.h"
boddeke 3:23fae3efa1c0 9
boddeke 3:23fae3efa1c0 10 #define REED_PORT PA_0
boddeke 0:208bd045dd31 11
waltertakens 5:5654666925e1 12 // PA_0 = WKUP
waltertakens 5:5654666925e1 13 // wakeup on rising edge
waltertakens 5:5654666925e1 14 // reeed on PA_0 to Vss (3.3V)
waltertakens 5:5654666925e1 15 // no internal pull in sleep mode
waltertakens 5:5654666925e1 16 DigitalIn reed_sensor(PA_0, PullNone);
boddeke 3:23fae3efa1c0 17 volatile int reed_has_changed = 0;
boddeke 0:208bd045dd31 18
waltertakens 5:5654666925e1 19 void isr_reed_sensor_change(void) {
boddeke 3:23fae3efa1c0 20 reed_has_changed++;
boddeke 0:208bd045dd31 21 }
boddeke 0:208bd045dd31 22
waltertakens 5:5654666925e1 23 #define NODETYPE_OPENCLOSE 1
waltertakens 5:5654666925e1 24
waltertakens 5:5654666925e1 25 static std::string config_network_name = "edgelorareed2";
waltertakens 5:5654666925e1 26 static std::string config_network_pass = "edgeIsGaaf";
waltertakens 5:5654666925e1 27
boddeke 1:3348d15fefdb 28
waltertakens 5:5654666925e1 29 static struct {
waltertakens 5:5654666925e1 30 uint8_t openorclosedid;
waltertakens 5:5654666925e1 31 uint8_t closedvalue; //open=1 close-0
waltertakens 5:5654666925e1 32
waltertakens 5:5654666925e1 33 } payloadOpenClose;
boddeke 1:3348d15fefdb 34
waltertakens 5:5654666925e1 35
waltertakens 5:5654666925e1 36
waltertakens 5:5654666925e1 37 int DoNode1 (uint8_t*b,uint8_t v){
waltertakens 5:5654666925e1 38 *b++=NODETYPE_OPENCLOSE;
waltertakens 5:5654666925e1 39 *b++=v;
waltertakens 5:5654666925e1 40 return(sizeof(payloadOpenClose));
boddeke 0:208bd045dd31 41 }
boddeke 0:208bd045dd31 42
waltertakens 5:5654666925e1 43
waltertakens 5:5654666925e1 44 // these options must match the settings on your Conduit
waltertakens 5:5654666925e1 45 // uncomment the following lines and edit their values to match your configuration
waltertakens 5:5654666925e1 46
waltertakens 5:5654666925e1 47 //static uint8_t config_network_addr[] = { 0x02, 0x02, 0x02, 0x02 };
waltertakens 5:5654666925e1 48 static uint8_t config_network_addr[] = { 0x02, 0x02, 0x02, 0x02 };
waltertakens 5:5654666925e1 49
waltertakens 5:5654666925e1 50 //static uint8_t config_network_nskey[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
waltertakens 5:5654666925e1 51 static uint8_t config_network_nskey[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
waltertakens 5:5654666925e1 52
waltertakens 5:5654666925e1 53 // static uint8_t config_network_dskey[] = { 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03 };
waltertakens 5:5654666925e1 54 static uint8_t config_network_dskey[] = { 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03 };
waltertakens 5:5654666925e1 55
waltertakens 5:5654666925e1 56
waltertakens 5:5654666925e1 57 //WBT commentout, only for 915 Mhz static uint8_t config_frequency_sub_band = 1;
boddeke 0:208bd045dd31 58
boddeke 0:208bd045dd31 59 int main() {
waltertakens 5:5654666925e1 60 int32_t ret;
boddeke 0:208bd045dd31 61 mDot* dot;
waltertakens 5:5654666925e1 62 std::vector<uint8_t> data;
waltertakens 5:5654666925e1 63
waltertakens 5:5654666925e1 64 //std::string data_str = "hello this is Edge calling!";
waltertakens 5:5654666925e1 65 std::string data_str = "hello!";
waltertakens 5:5654666925e1 66
waltertakens 5:5654666925e1 67 DigitalIn enable(PA_3);
waltertakens 5:5654666925e1 68
waltertakens 5:5654666925e1 69 InterruptIn reed_sensor_change(REED_PORT);
waltertakens 5:5654666925e1 70 reed_sensor_change.fall(&isr_reed_sensor_change);
waltertakens 5:5654666925e1 71 reed_sensor_change.rise(&isr_reed_sensor_change);
waltertakens 5:5654666925e1 72 reed_sensor_change.mode(PullNone);
boddeke 0:208bd045dd31 73
waltertakens 5:5654666925e1 74 logInfo("\n\n\n\n\n");
waltertakens 5:5654666925e1 75 wait(5);
waltertakens 5:5654666925e1 76 logInfo("\n\n\n***************************************\nStarting\n");
waltertakens 5:5654666925e1 77 wait(5);
waltertakens 5:5654666925e1 78 logInfo("Started version 1.1 2 june 14.25 \n");
waltertakens 5:5654666925e1 79 logInfo( "* Build: " __DATE__ ", " __TIME__" *\r\n");
waltertakens 5:5654666925e1 80
waltertakens 5:5654666925e1 81 time_t seconds = time(NULL);
waltertakens 5:5654666925e1 82 uint32_t boottime=(uint32_t) seconds;
waltertakens 5:5654666925e1 83
waltertakens 5:5654666925e1 84
waltertakens 5:5654666925e1 85 // get a mDot handle
waltertakens 5:5654666925e1 86 dot = mDot::getInstance();
boddeke 0:208bd045dd31 87
waltertakens 5:5654666925e1 88 // Test if we've already saved the config
waltertakens 5:5654666925e1 89 std::string configNetworkName = dot->getNetworkName();
waltertakens 5:5654666925e1 90 // Reset config if network name is different or pin is low then reset config.
waltertakens 5:5654666925e1 91 if( config_network_name.compare(configNetworkName) != 0 ) {
waltertakens 5:5654666925e1 92 // Not saved config, reset
waltertakens 5:5654666925e1 93 logInfo("Setting Config");
waltertakens 5:5654666925e1 94 // reset to default config so we know what state we're in
waltertakens 5:5654666925e1 95 dot->resetConfig();
waltertakens 5:5654666925e1 96 }
waltertakens 5:5654666925e1 97 else {
waltertakens 5:5654666925e1 98 logInfo("Resume with old Config");
waltertakens 5:5654666925e1 99 }
waltertakens 5:5654666925e1 100
waltertakens 5:5654666925e1 101 // clear the EWUP state
waltertakens 5:5654666925e1 102 if(dot->getStandbyFlag()){
waltertakens 5:5654666925e1 103 logInfo("clearing standby flag\r\n");
waltertakens 5:5654666925e1 104 PWR->CSR &= ~PWR_CSR_EWUP;
waltertakens 5:5654666925e1 105 }
waltertakens 5:5654666925e1 106
waltertakens 5:5654666925e1 107 dot->setLogLevel(mts::MTSLog::TRACE_LEVEL);
waltertakens 5:5654666925e1 108
waltertakens 5:5654666925e1 109 logError("JoinMode= %d\n", dot->getJoinMode());
waltertakens 5:5654666925e1 110
waltertakens 5:5654666925e1 111 // print library version information
waltertakens 5:5654666925e1 112 logInfo("version: %s", dot->getId().c_str());
waltertakens 5:5654666925e1 113
waltertakens 5:5654666925e1 114 if ((ret = dot->setNetworkName("edgeedge")) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 115 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 116 }
waltertakens 5:5654666925e1 117
waltertakens 5:5654666925e1 118 if ((ret = dot->setNetworkPassphrase("svensven")) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 119 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 120 }
waltertakens 5:5654666925e1 121
waltertakens 5:5654666925e1 122 logInfo("Name: %s\r\n", dot->getNetworkName());
waltertakens 5:5654666925e1 123 dot->setPublicNetwork(true);
waltertakens 5:5654666925e1 124
boddeke 0:208bd045dd31 125
waltertakens 5:5654666925e1 126 //*******************************************
waltertakens 5:5654666925e1 127 // configuration
waltertakens 5:5654666925e1 128 //*******************************************
boddeke 3:23fae3efa1c0 129
waltertakens 5:5654666925e1 130 // set up the mDot with our network information: frequency sub band, network name, and network password
waltertakens 5:5654666925e1 131 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
waltertakens 5:5654666925e1 132
waltertakens 5:5654666925e1 133 // frequency sub band is only applicable in the 915 (US) frequency band
waltertakens 5:5654666925e1 134 // 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
waltertakens 5:5654666925e1 135 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
waltertakens 5:5654666925e1 136
waltertakens 5:5654666925e1 137 //WBT logInfo("setting frequency sub band");
waltertakens 5:5654666925e1 138 // if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 139 // logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 140 //}
waltertakens 5:5654666925e1 141
waltertakens 5:5654666925e1 142
waltertakens 5:5654666925e1 143
waltertakens 5:5654666925e1 144 std::vector<uint8_t> temp;
waltertakens 5:5654666925e1 145
waltertakens 5:5654666925e1 146 for (int i = 0; i < 4; i++) {
waltertakens 5:5654666925e1 147 temp.push_back(config_network_addr[i]);
waltertakens 5:5654666925e1 148 }
waltertakens 5:5654666925e1 149
waltertakens 5:5654666925e1 150 logInfo("setting network addr");
waltertakens 5:5654666925e1 151 if ((ret = dot->setNetworkAddress(temp)) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 152 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 153 }
waltertakens 5:5654666925e1 154
waltertakens 5:5654666925e1 155 temp.clear();
waltertakens 5:5654666925e1 156 for (int i = 0; i < 16; i++) {
waltertakens 5:5654666925e1 157 temp.push_back(config_network_nskey[i]);
waltertakens 5:5654666925e1 158 }
waltertakens 5:5654666925e1 159
waltertakens 5:5654666925e1 160 logInfo("setting network password");
waltertakens 5:5654666925e1 161 if ((ret = dot->setNetworkSessionKey(temp)) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 162 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 163 }
waltertakens 5:5654666925e1 164
waltertakens 5:5654666925e1 165 temp.clear();
waltertakens 5:5654666925e1 166 for (int i = 0; i < 16; i++) {
waltertakens 5:5654666925e1 167 temp.push_back(config_network_dskey[i]);
boddeke 3:23fae3efa1c0 168 }
waltertakens 5:5654666925e1 169
waltertakens 5:5654666925e1 170 logInfo("setting network password");
waltertakens 5:5654666925e1 171 if ((ret = dot->setDataSessionKey(temp)) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 172 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 173 }
waltertakens 5:5654666925e1 174
waltertakens 5:5654666925e1 175 // a higher spreading factor allows for longer range but lower throughput
waltertakens 5:5654666925e1 176 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
waltertakens 5:5654666925e1 177 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
waltertakens 5:5654666925e1 178 logInfo("setting TX spreading factor");
waltertakens 5:5654666925e1 179 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 180 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 181 }
waltertakens 5:5654666925e1 182
waltertakens 5:5654666925e1 183 // WBT DONT request receive confirmation of packets from the gateway
waltertakens 5:5654666925e1 184 //logInfo("WBT DONT enabling ACKs");
waltertakens 5:5654666925e1 185 //if ((ret = dot->setAck(0)) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 186 //logError("failed to NOT enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 187 //}
waltertakens 5:5654666925e1 188
waltertakens 5:5654666925e1 189 /*
waltertakens 5:5654666925e1 190 logInfo("WBT DO enabling ACKs");
waltertakens 5:5654666925e1 191 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 192 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 193 }
waltertakens 5:5654666925e1 194 */
waltertakens 5:5654666925e1 195 logInfo("WBT UNABLE ACKs");
waltertakens 5:5654666925e1 196 if ((ret = dot->setAck(0)) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 197 logError("failed to unnable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 198 }
waltertakens 5:5654666925e1 199
waltertakens 5:5654666925e1 200 // TODO Command(dot, "Rx Output", "AT+RXO", "Set the Rx output type (0:hexadecimal, 1:raw)"), _serial(serial)
waltertakens 5:5654666925e1 201
waltertakens 5:5654666925e1 202
waltertakens 5:5654666925e1 203
waltertakens 5:5654666925e1 204 /*
waltertakens 5:5654666925e1 205 logInfo("WBT SET JOIN");
waltertakens 5:5654666925e1 206 if ((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 207 logError("failed to set to manual %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 208 }
waltertakens 5:5654666925e1 209
waltertakens 5:5654666925e1 210 logInfo("WBT GET JOIN");
waltertakens 5:5654666925e1 211 ret = dot->getJoinMode();
waltertakens 5:5654666925e1 212 logError("WBTGET JOIN %d\n", ret);
boddeke 0:208bd045dd31 213
waltertakens 5:5654666925e1 214 */
boddeke 0:208bd045dd31 215
waltertakens 5:5654666925e1 216
waltertakens 5:5654666925e1 217 // save this configuration to the mDot's NVM
waltertakens 5:5654666925e1 218 logInfo("saving config");
waltertakens 5:5654666925e1 219 if (! dot->saveConfig()) {
waltertakens 5:5654666925e1 220 logError("failed to save configuration");
boddeke 3:23fae3efa1c0 221 }
waltertakens 5:5654666925e1 222 //*******************************************
waltertakens 5:5654666925e1 223 // end of configuration
waltertakens 5:5654666925e1 224 //*******************************************
waltertakens 5:5654666925e1 225 /*
waltertakens 5:5654666925e1 226 // attempt to join the network
waltertakens 5:5654666925e1 227 logInfo("joining network");
waltertakens 5:5654666925e1 228 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 229 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 230 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
waltertakens 5:5654666925e1 231 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
boddeke 3:23fae3efa1c0 232
waltertakens 5:5654666925e1 233 //WBT
waltertakens 5:5654666925e1 234 wait(60);
waltertakens 5:5654666925e1 235 }
waltertakens 5:5654666925e1 236 */
waltertakens 5:5654666925e1 237
waltertakens 5:5654666925e1 238 uint32_t now = (uint32_t)seconds - boottime;
waltertakens 5:5654666925e1 239
waltertakens 5:5654666925e1 240
waltertakens 5:5654666925e1 241 //debugformat data for sending to the gateway
waltertakens 5:5654666925e1 242 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
waltertakens 5:5654666925e1 243 data.push_back((uint8_t) *it);
waltertakens 5:5654666925e1 244
boddeke 0:208bd045dd31 245 while (true) {
boddeke 0:208bd045dd31 246
waltertakens 5:5654666925e1 247 //for now fake the absolute value of reed_has_changed into battery level
waltertakens 5:5654666925e1 248 //(to avoid that only 0/1 are valid values
waltertakens 5:5654666925e1 249
waltertakens 5:5654666925e1 250 //setBatteryLevel(reed_has_changed);
waltertakens 5:5654666925e1 251 //getDataAndUpdate(&data);
boddeke 0:208bd045dd31 252
waltertakens 5:5654666925e1 253 // meet
waltertakens 5:5654666925e1 254 logInfo("Reed = %d", reed_has_changed);
waltertakens 5:5654666925e1 255
waltertakens 5:5654666925e1 256
waltertakens 5:5654666925e1 257
waltertakens 5:5654666925e1 258 // send the data to the gateway
waltertakens 5:5654666925e1 259 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
waltertakens 5:5654666925e1 260 logError("failed to send err=%d errs=%s", ret, mDot::getReturnCodeString(ret).c_str());
waltertakens 5:5654666925e1 261 } else {
waltertakens 5:5654666925e1 262 logInfo("successfully sent data to gateway");
boddeke 0:208bd045dd31 263 }
boddeke 0:208bd045dd31 264
waltertakens 5:5654666925e1 265 // inpired by https://developer.mbed.org/teams/MultiTech/code/mDot_AT_firmware/file/6a12bf1f6723/CommandTerminal/CmdReceiveOnce.cpp
waltertakens 5:5654666925e1 266 //if we asked for an ack, then the send will have returned with the ack recption and data is ready ???
waltertakens 5:5654666925e1 267 if (dot->recv(data) == mDot::MDOT_OK) {
waltertakens 5:5654666925e1 268 printf("data received=\n");
waltertakens 5:5654666925e1 269 //printf("data received= %s\n", dot->getRxOutput().c_str(););
waltertakens 5:5654666925e1 270 }
waltertakens 5:5654666925e1 271
waltertakens 5:5654666925e1 272 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
waltertakens 5:5654666925e1 273 //osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
boddeke 1:3348d15fefdb 274
waltertakens 5:5654666925e1 275 //deepsleep
waltertakens 5:5654666925e1 276 dot->sleep((int)60, mDot::RTC_ALARM_OR_INTERRUPT, true);
boddeke 0:208bd045dd31 277 }
waltertakens 5:5654666925e1 278 return 0;
waltertakens 5:5654666925e1 279 }