updated
Dependencies: MTS-Serial libmDot mbed-rtos mbed
Fork of mDot_LoRa_Street_light by
wrapper.cpp@9:531bddbe36a9, 2017-10-17 (annotated)
- Committer:
- venubk
- Date:
- Tue Oct 17 11:52:13 2017 +0000
- Revision:
- 9:531bddbe36a9
- Parent:
- 2:20a5d4681abf
energy meter integrated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hariom | 2:20a5d4681abf | 1 | #include "mbed.h" |
hariom | 2:20a5d4681abf | 2 | #include "mDot.h" |
hariom | 2:20a5d4681abf | 3 | #include "MTSLog.h" |
hariom | 2:20a5d4681abf | 4 | #include <string> |
hariom | 2:20a5d4681abf | 5 | #include <vector> |
hariom | 2:20a5d4681abf | 6 | #include <algorithm> |
hariom | 2:20a5d4681abf | 7 | |
hariom | 2:20a5d4681abf | 8 | mDot* dot; |
hariom | 2:20a5d4681abf | 9 | void init_mdot(void) |
hariom | 2:20a5d4681abf | 10 | { |
hariom | 2:20a5d4681abf | 11 | // get a mDot handle |
hariom | 2:20a5d4681abf | 12 | dot = mDot::getInstance(); |
hariom | 2:20a5d4681abf | 13 | |
hariom | 2:20a5d4681abf | 14 | // print library version information |
hariom | 2:20a5d4681abf | 15 | //logInfo("version: %s", dot->getId().c_str()); |
hariom | 2:20a5d4681abf | 16 | |
hariom | 2:20a5d4681abf | 17 | //logInfo("Test msg \n\r", dot->getId().c_str()); |
hariom | 2:20a5d4681abf | 18 | |
hariom | 2:20a5d4681abf | 19 | //logInfo("Test msg \n\r", dot->getId().c_str()); |
hariom | 2:20a5d4681abf | 20 | |
hariom | 2:20a5d4681abf | 21 | //logInfo("Test msg \n\r", dot->getId().c_str()); |
hariom | 2:20a5d4681abf | 22 | //logInfo("Test msg \n\r", dot->getId().c_str()); |
hariom | 2:20a5d4681abf | 23 | //logInfo("Test msg \n\r", dot->getId().c_str()); |
hariom | 2:20a5d4681abf | 24 | //logInfo("Test msg \n\r", dot->getId().c_str()); |
hariom | 2:20a5d4681abf | 25 | } |
hariom | 2:20a5d4681abf | 26 | |
hariom | 2:20a5d4681abf | 27 | void reset_config(void) |
hariom | 2:20a5d4681abf | 28 | { |
hariom | 2:20a5d4681abf | 29 | // reset to default config so we know what state we're in |
hariom | 2:20a5d4681abf | 30 | dot->resetConfig(); |
hariom | 2:20a5d4681abf | 31 | } |
hariom | 2:20a5d4681abf | 32 | |
hariom | 2:20a5d4681abf | 33 | void set_log_lavel(int logInfo) |
hariom | 2:20a5d4681abf | 34 | { |
hariom | 2:20a5d4681abf | 35 | switch(logInfo) { |
hariom | 2:20a5d4681abf | 36 | case 4: |
hariom | 2:20a5d4681abf | 37 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
hariom | 2:20a5d4681abf | 38 | break; |
hariom | 2:20a5d4681abf | 39 | } |
hariom | 2:20a5d4681abf | 40 | } |
hariom | 2:20a5d4681abf | 41 | |
hariom | 2:20a5d4681abf | 42 | int set_public_network(int value) |
hariom | 2:20a5d4681abf | 43 | { |
hariom | 2:20a5d4681abf | 44 | return dot->setPublicNetwork(value); |
hariom | 2:20a5d4681abf | 45 | } |
hariom | 2:20a5d4681abf | 46 | |
hariom | 2:20a5d4681abf | 47 | int set_frequency_sub_band( int config_frequency_sub_band) |
hariom | 2:20a5d4681abf | 48 | { |
hariom | 2:20a5d4681abf | 49 | return dot->setFrequencySubBand(config_frequency_sub_band); |
hariom | 2:20a5d4681abf | 50 | } |
hariom | 2:20a5d4681abf | 51 | |
hariom | 2:20a5d4681abf | 52 | int set_network_name(char *config_network_name) |
hariom | 2:20a5d4681abf | 53 | { |
hariom | 2:20a5d4681abf | 54 | return dot->setNetworkName(config_network_name); |
hariom | 2:20a5d4681abf | 55 | } |
hariom | 2:20a5d4681abf | 56 | |
hariom | 2:20a5d4681abf | 57 | int set_network_passphrase(char *config_network_pass) |
hariom | 2:20a5d4681abf | 58 | { |
hariom | 2:20a5d4681abf | 59 | return dot->setNetworkPassphrase(config_network_pass); |
hariom | 2:20a5d4681abf | 60 | } |
hariom | 2:20a5d4681abf | 61 | |
hariom | 2:20a5d4681abf | 62 | int set_txpower(int value) |
hariom | 2:20a5d4681abf | 63 | { |
hariom | 2:20a5d4681abf | 64 | int ret; |
hariom | 2:20a5d4681abf | 65 | logInfo("setting TX spreading factor"); |
hariom | 2:20a5d4681abf | 66 | if ((ret = dot->setTxPower(value)) != mDot::MDOT_OK) { |
hariom | 2:20a5d4681abf | 67 | logError("failed to set TX datarate %d:%s", ret); |
hariom | 2:20a5d4681abf | 68 | } |
hariom | 2:20a5d4681abf | 69 | return ret; |
hariom | 2:20a5d4681abf | 70 | } |
hariom | 2:20a5d4681abf | 71 | |
hariom | 2:20a5d4681abf | 72 | int set_tx_datarate(void) |
hariom | 2:20a5d4681abf | 73 | { |
hariom | 2:20a5d4681abf | 74 | return dot->setTxDataRate(mDot::SF_10); |
hariom | 2:20a5d4681abf | 75 | } |
hariom | 2:20a5d4681abf | 76 | |
hariom | 2:20a5d4681abf | 77 | int set_ack(int value) |
hariom | 2:20a5d4681abf | 78 | { |
hariom | 2:20a5d4681abf | 79 | return dot->setAck(value); |
hariom | 2:20a5d4681abf | 80 | } |
hariom | 2:20a5d4681abf | 81 | |
hariom | 2:20a5d4681abf | 82 | int save_config(void) |
hariom | 2:20a5d4681abf | 83 | { |
hariom | 2:20a5d4681abf | 84 | return dot->saveConfig(); |
hariom | 2:20a5d4681abf | 85 | } |
hariom | 2:20a5d4681abf | 86 | |
hariom | 2:20a5d4681abf | 87 | int join_network(void) |
hariom | 2:20a5d4681abf | 88 | { |
hariom | 2:20a5d4681abf | 89 | int ret; |
hariom | 2:20a5d4681abf | 90 | while (!dot->getNetworkJoinStatus()) { |
hariom | 2:20a5d4681abf | 91 | osDelay(dot->getNextTxMs()); |
hariom | 2:20a5d4681abf | 92 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
hariom | 2:20a5d4681abf | 93 | printf("ERROR: failed to join network %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); |
hariom | 2:20a5d4681abf | 94 | } |
hariom | 2:20a5d4681abf | 95 | } |
hariom | 2:20a5d4681abf | 96 | |
hariom | 2:20a5d4681abf | 97 | //return dot->joinNetwork(); |
hariom | 2:20a5d4681abf | 98 | return 0; |
hariom | 2:20a5d4681abf | 99 | } |
hariom | 2:20a5d4681abf | 100 | |
hariom | 2:20a5d4681abf | 101 | bool send(const std::string text) |
hariom | 2:20a5d4681abf | 102 | { |
hariom | 2:20a5d4681abf | 103 | int32_t returnCode; |
hariom | 2:20a5d4681abf | 104 | uint32_t timeTillSend = dot->getNextTxMs(); |
hariom | 2:20a5d4681abf | 105 | if (timeTillSend != 0) { |
hariom | 2:20a5d4681abf | 106 | printf("waiting %lu ms to send\r\n", timeTillSend); |
hariom | 2:20a5d4681abf | 107 | return false; |
hariom | 2:20a5d4681abf | 108 | } |
hariom | 2:20a5d4681abf | 109 | |
hariom | 2:20a5d4681abf | 110 | //printf("Sending data... "); |
hariom | 2:20a5d4681abf | 111 | std::vector<uint8_t> data(text.begin(), text.end()); |
hariom | 2:20a5d4681abf | 112 | if ((returnCode = dot->send(data, 1)) != mDot::MDOT_OK) |
hariom | 2:20a5d4681abf | 113 | { |
hariom | 2:20a5d4681abf | 114 | //logInfo("error in sending data"); |
hariom | 2:20a5d4681abf | 115 | return false; |
hariom | 2:20a5d4681abf | 116 | } |
hariom | 2:20a5d4681abf | 117 | //printf("Data sent!\r\n"); |
hariom | 2:20a5d4681abf | 118 | return true; |
hariom | 2:20a5d4681abf | 119 | } |
hariom | 2:20a5d4681abf | 120 | |
hariom | 2:20a5d4681abf | 121 | /* |
hariom | 2:20a5d4681abf | 122 | int send_data(char *args) |
hariom | 2:20a5d4681abf | 123 | { |
hariom | 2:20a5d4681abf | 124 | int ret; |
hariom | 2:20a5d4681abf | 125 | std::vector<uint8_t> data; |
hariom | 2:20a5d4681abf | 126 | std::string data_str = "hello!"; |
hariom | 2:20a5d4681abf | 127 | // format data for sending to the gateway |
hariom | 2:20a5d4681abf | 128 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
hariom | 2:20a5d4681abf | 129 | data.push_back((uint8_t) *it); |
hariom | 2:20a5d4681abf | 130 | |
hariom | 2:20a5d4681abf | 131 | while (true) { |
hariom | 2:20a5d4681abf | 132 | // send the data to the gateway |
hariom | 2:20a5d4681abf | 133 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
hariom | 2:20a5d4681abf | 134 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
hariom | 2:20a5d4681abf | 135 | } else { |
hariom | 2:20a5d4681abf | 136 | logInfo("successfully sent data to gateway"); |
hariom | 2:20a5d4681abf | 137 | } |
hariom | 2:20a5d4681abf | 138 | |
hariom | 2:20a5d4681abf | 139 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
hariom | 2:20a5d4681abf | 140 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
hariom | 2:20a5d4681abf | 141 | } |
hariom | 2:20a5d4681abf | 142 | return 0; |
hariom | 2:20a5d4681abf | 143 | } |
hariom | 2:20a5d4681abf | 144 | */ |
hariom | 2:20a5d4681abf | 145 | |
hariom | 2:20a5d4681abf | 146 | bool receive_data(char *buffer) |
hariom | 2:20a5d4681abf | 147 | { |
hariom | 2:20a5d4681abf | 148 | std::vector<uint8_t>data; |
hariom | 2:20a5d4681abf | 149 | |
hariom | 2:20a5d4681abf | 150 | if (dot->recv(data) != mDot::MDOT_OK) { |
hariom | 2:20a5d4681abf | 151 | return false; |
hariom | 2:20a5d4681abf | 152 | } |
hariom | 2:20a5d4681abf | 153 | |
hariom | 2:20a5d4681abf | 154 | int i = 0; |
hariom | 2:20a5d4681abf | 155 | for (std::vector<uint8_t>::const_iterator it = data.begin(); it != data.end(); ++it) |
hariom | 2:20a5d4681abf | 156 | buffer[i++] = *it; |
hariom | 2:20a5d4681abf | 157 | buffer[i] = '\0'; |
hariom | 2:20a5d4681abf | 158 | |
hariom | 2:20a5d4681abf | 159 | return true; |
hariom | 2:20a5d4681abf | 160 | } |