EasyButtonCode
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_TTN_DS18B20 by
main.cpp@1:45cec6aea002, 2015-09-03 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Thu Sep 03 23:36:45 2015 +0000
- Revision:
- 1:45cec6aea002
- Parent:
- 0:5a0b43f3b143
- Child:
- 2:9db840d12557
added deep sleep
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 0:5a0b43f3b143 | 1 | /** mDot_display - Uses 160x128 SPI LCD display to show results of message |
SomeRandomBloke | 0:5a0b43f3b143 | 2 | * sending and receiving. |
SomeRandomBloke | 0:5a0b43f3b143 | 3 | * Uses MultiTech mDot developer board http://www.multitech.com/models/94558010LF |
SomeRandomBloke | 0:5a0b43f3b143 | 4 | * Requires a MultiTech MultiConnect Conduit http://www.multitech.com/models/94557203LF |
SomeRandomBloke | 0:5a0b43f3b143 | 5 | * |
SomeRandomBloke | 0:5a0b43f3b143 | 6 | * Example JSON received on Conduit: |
SomeRandomBloke | 0:5a0b43f3b143 | 7 | { "chan": 5, "codr": "4/5", "datr": "SF9BW125", "freq": "869.5", |
SomeRandomBloke | 0:5a0b43f3b143 | 8 | "lsnr": "8.8", "modu": "LORA", "rfch": 1, "rssi": -41, "seqn": 13, |
SomeRandomBloke | 0:5a0b43f3b143 | 9 | "size": 12, "timestamp": "2015-07-22T21:19:11Z", "tmst": 517590990, |
SomeRandomBloke | 0:5a0b43f3b143 | 10 | "payload": "{\"temperature\":21.3}", "eui": "00:80:00:00:00:00:9a:63", "_msg |
SomeRandomBloke | 0:5a0b43f3b143 | 11 | id": "73bcd8dd.8c4328" } |
SomeRandomBloke | 0:5a0b43f3b143 | 12 | * |
SomeRandomBloke | 0:5a0b43f3b143 | 13 | */ |
SomeRandomBloke | 0:5a0b43f3b143 | 14 | |
SomeRandomBloke | 0:5a0b43f3b143 | 15 | #include "mbed.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 16 | #include "DS1820.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 17 | #include "mDot.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 18 | #include "MTSLog.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 19 | #include "MTSText.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 20 | #include <string> |
SomeRandomBloke | 0:5a0b43f3b143 | 21 | #include <vector> |
SomeRandomBloke | 0:5a0b43f3b143 | 22 | |
SomeRandomBloke | 0:5a0b43f3b143 | 23 | using namespace mts; |
SomeRandomBloke | 0:5a0b43f3b143 | 24 | |
SomeRandomBloke | 0:5a0b43f3b143 | 25 | // these options must match the settings on your Conduit in |
SomeRandomBloke | 0:5a0b43f3b143 | 26 | // /var/config/lora/lora-network-server.conf |
SomeRandomBloke | 0:5a0b43f3b143 | 27 | static std::string config_network_name = "ThingInnovations"; |
SomeRandomBloke | 0:5a0b43f3b143 | 28 | static std::string config_network_pass = "donkey123"; |
SomeRandomBloke | 1:45cec6aea002 | 29 | //static uint8_t config_frequency_sub_band = 1; |
SomeRandomBloke | 0:5a0b43f3b143 | 30 | |
SomeRandomBloke | 0:5a0b43f3b143 | 31 | // mDot/dev board activity LED |
SomeRandomBloke | 1:45cec6aea002 | 32 | //#define ACTIVITY_LED PA_0 |
SomeRandomBloke | 1:45cec6aea002 | 33 | // D13 |
SomeRandomBloke | 1:45cec6aea002 | 34 | #define DATA_PIN PA_5 |
SomeRandomBloke | 1:45cec6aea002 | 35 | // A0 |
SomeRandomBloke | 1:45cec6aea002 | 36 | //#define DATA_PIN PB_1 |
SomeRandomBloke | 0:5a0b43f3b143 | 37 | |
SomeRandomBloke | 0:5a0b43f3b143 | 38 | // must use the hardware SPI pins |
SomeRandomBloke | 0:5a0b43f3b143 | 39 | //SPI device(SPI_MOSI, SPI_MISO, SPI_SCK); |
SomeRandomBloke | 0:5a0b43f3b143 | 40 | |
SomeRandomBloke | 0:5a0b43f3b143 | 41 | // SPI device defines |
SomeRandomBloke | 0:5a0b43f3b143 | 42 | #define MDOT_MOSI PA_7 |
SomeRandomBloke | 0:5a0b43f3b143 | 43 | #define MDOT_MISO PA_6 |
SomeRandomBloke | 0:5a0b43f3b143 | 44 | #define MDOT_SCK PA_5 |
SomeRandomBloke | 0:5a0b43f3b143 | 45 | #define MDOT_TFT_CS PA_4 |
SomeRandomBloke | 0:5a0b43f3b143 | 46 | |
SomeRandomBloke | 0:5a0b43f3b143 | 47 | DS1820 probe(DATA_PIN); |
SomeRandomBloke | 0:5a0b43f3b143 | 48 | |
SomeRandomBloke | 0:5a0b43f3b143 | 49 | //void log_error(mDot* dot, const char* msg, int32_t retval); |
SomeRandomBloke | 0:5a0b43f3b143 | 50 | |
SomeRandomBloke | 0:5a0b43f3b143 | 51 | Serial pc(USBTX,USBRX); |
SomeRandomBloke | 0:5a0b43f3b143 | 52 | |
SomeRandomBloke | 0:5a0b43f3b143 | 53 | |
SomeRandomBloke | 0:5a0b43f3b143 | 54 | int main() |
SomeRandomBloke | 0:5a0b43f3b143 | 55 | { |
SomeRandomBloke | 0:5a0b43f3b143 | 56 | int32_t ret; |
SomeRandomBloke | 0:5a0b43f3b143 | 57 | mDot* dot; |
SomeRandomBloke | 0:5a0b43f3b143 | 58 | std::vector<uint8_t> send_data; |
SomeRandomBloke | 0:5a0b43f3b143 | 59 | std::vector<uint8_t> recv_data; |
SomeRandomBloke | 0:5a0b43f3b143 | 60 | |
SomeRandomBloke | 1:45cec6aea002 | 61 | // int32_t next_tx; |
SomeRandomBloke | 1:45cec6aea002 | 62 | // int32_t wait_time = 2; |
SomeRandomBloke | 0:5a0b43f3b143 | 63 | // uint8_t iterations = 99; |
SomeRandomBloke | 1:45cec6aea002 | 64 | // uint16_t txCount = 0; |
SomeRandomBloke | 1:45cec6aea002 | 65 | // uint16_t txFailCount = 0; |
SomeRandomBloke | 1:45cec6aea002 | 66 | // uint16_t rxCount = 0; |
SomeRandomBloke | 1:45cec6aea002 | 67 | // uint16_t rxOkCount = 0; |
SomeRandomBloke | 1:45cec6aea002 | 68 | // uint16_t noRxCount = 0; |
SomeRandomBloke | 1:45cec6aea002 | 69 | // uint16_t rxFailCount = 0; |
SomeRandomBloke | 1:45cec6aea002 | 70 | // bool initStatus = true; // All intialised OK until told otherwise |
SomeRandomBloke | 0:5a0b43f3b143 | 71 | float temperature = 0.0; |
SomeRandomBloke | 1:45cec6aea002 | 72 | |
SomeRandomBloke | 0:5a0b43f3b143 | 73 | pc.baud(115200); |
SomeRandomBloke | 0:5a0b43f3b143 | 74 | pc.printf("mDot LoRa Temperature sensor\n\r"); |
SomeRandomBloke | 1:45cec6aea002 | 75 | /* wait_ms(5000); |
SomeRandomBloke | 1:45cec6aea002 | 76 | for(int i=0; i<10; i++ ) |
SomeRandomBloke | 1:45cec6aea002 | 77 | pc.printf("."); |
SomeRandomBloke | 1:45cec6aea002 | 78 | pc.printf("\n\r"); |
SomeRandomBloke | 1:45cec6aea002 | 79 | */ |
SomeRandomBloke | 1:45cec6aea002 | 80 | /* |
SomeRandomBloke | 1:45cec6aea002 | 81 | while( 1 ) { |
SomeRandomBloke | 1:45cec6aea002 | 82 | // This takes upto 750mS, way too long. Change to 9 bit resolution if not already used. |
SomeRandomBloke | 1:45cec6aea002 | 83 | |
SomeRandomBloke | 1:45cec6aea002 | 84 | probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
SomeRandomBloke | 1:45cec6aea002 | 85 | // printf("It is %3.1fC\r\n", probe.temperature()); |
SomeRandomBloke | 1:45cec6aea002 | 86 | // Output data as JSON e.g. {"temperature":"21.3"} |
SomeRandomBloke | 1:45cec6aea002 | 87 | temperature = probe.temperature(); |
SomeRandomBloke | 1:45cec6aea002 | 88 | pc.printf("Temperature %3.1f\r\n", temperature ); |
SomeRandomBloke | 1:45cec6aea002 | 89 | wait_ms(2000); |
SomeRandomBloke | 1:45cec6aea002 | 90 | } |
SomeRandomBloke | 1:45cec6aea002 | 91 | |
SomeRandomBloke | 1:45cec6aea002 | 92 | #ifdef bollocks |
SomeRandomBloke | 1:45cec6aea002 | 93 | */ |
SomeRandomBloke | 0:5a0b43f3b143 | 94 | // get a mDot handle |
SomeRandomBloke | 0:5a0b43f3b143 | 95 | dot = mDot::getInstance(); |
SomeRandomBloke | 0:5a0b43f3b143 | 96 | |
SomeRandomBloke | 0:5a0b43f3b143 | 97 | dot->setLogLevel(MTSLog::TRACE_LEVEL); |
SomeRandomBloke | 0:5a0b43f3b143 | 98 | |
SomeRandomBloke | 1:45cec6aea002 | 99 | logInfo("Checking Config"); |
SomeRandomBloke | 1:45cec6aea002 | 100 | |
SomeRandomBloke | 1:45cec6aea002 | 101 | // Test if we've already saved the config |
SomeRandomBloke | 1:45cec6aea002 | 102 | std::string configNetworkName = dot->getNetworkName(); |
SomeRandomBloke | 1:45cec6aea002 | 103 | |
SomeRandomBloke | 1:45cec6aea002 | 104 | // Check pin, if low then reset config. |
SomeRandomBloke | 1:45cec6aea002 | 105 | |
SomeRandomBloke | 1:45cec6aea002 | 106 | |
SomeRandomBloke | 1:45cec6aea002 | 107 | if( config_network_name.compare(configNetworkName) != 0 ) { |
SomeRandomBloke | 1:45cec6aea002 | 108 | // Not saved config, reset |
SomeRandomBloke | 1:45cec6aea002 | 109 | logInfo("Setting Config"); |
SomeRandomBloke | 0:5a0b43f3b143 | 110 | |
SomeRandomBloke | 1:45cec6aea002 | 111 | // reset to default config so we know what state we're in |
SomeRandomBloke | 1:45cec6aea002 | 112 | dot->resetConfig(); |
SomeRandomBloke | 1:45cec6aea002 | 113 | |
SomeRandomBloke | 1:45cec6aea002 | 114 | // Set byte order - AEP less than 1.0.30 |
SomeRandomBloke | 1:45cec6aea002 | 115 | // dot->setJoinByteOrder(mDot::MSB); |
SomeRandomBloke | 1:45cec6aea002 | 116 | dot->setJoinByteOrder(mDot::LSB); |
SomeRandomBloke | 0:5a0b43f3b143 | 117 | |
SomeRandomBloke | 1:45cec6aea002 | 118 | logInfo("setting Join mode"); |
SomeRandomBloke | 1:45cec6aea002 | 119 | if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) { |
SomeRandomBloke | 1:45cec6aea002 | 120 | logError("failed to set Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 1:45cec6aea002 | 121 | } |
SomeRandomBloke | 1:45cec6aea002 | 122 | |
SomeRandomBloke | 1:45cec6aea002 | 123 | // If on developer board then you can enable activity LED |
SomeRandomBloke | 1:45cec6aea002 | 124 | // Currently no spare pins that LEDs are connected too. |
SomeRandomBloke | 0:5a0b43f3b143 | 125 | // dot->setActivityLedPin( ACTIVITY_LED ); |
SomeRandomBloke | 0:5a0b43f3b143 | 126 | // dot->setActivityLedEnable(false); |
SomeRandomBloke | 0:5a0b43f3b143 | 127 | |
SomeRandomBloke | 1:45cec6aea002 | 128 | dot->setJoinRetries( 5 ); |
SomeRandomBloke | 1:45cec6aea002 | 129 | dot->setTxDataRate( mDot::SF_9 ); |
SomeRandomBloke | 1:45cec6aea002 | 130 | dot->setTxPower( 14 ); |
SomeRandomBloke | 1:45cec6aea002 | 131 | dot->setAck( 0 ); // 1 retries on Ack, 0 to disable |
SomeRandomBloke | 0:5a0b43f3b143 | 132 | |
SomeRandomBloke | 1:45cec6aea002 | 133 | // Not applicable for 868MHz in EU |
SomeRandomBloke | 1:45cec6aea002 | 134 | // if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
SomeRandomBloke | 1:45cec6aea002 | 135 | // initStatus = false; |
SomeRandomBloke | 0:5a0b43f3b143 | 136 | // logError(dot, "failed to set frequency sub band", ret); |
SomeRandomBloke | 1:45cec6aea002 | 137 | // } |
SomeRandomBloke | 1:45cec6aea002 | 138 | |
SomeRandomBloke | 1:45cec6aea002 | 139 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
SomeRandomBloke | 1:45cec6aea002 | 140 | // initStatus = false; |
SomeRandomBloke | 1:45cec6aea002 | 141 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 1:45cec6aea002 | 142 | } |
SomeRandomBloke | 0:5a0b43f3b143 | 143 | |
SomeRandomBloke | 1:45cec6aea002 | 144 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
SomeRandomBloke | 1:45cec6aea002 | 145 | // initStatus = false; |
SomeRandomBloke | 1:45cec6aea002 | 146 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 1:45cec6aea002 | 147 | } |
SomeRandomBloke | 1:45cec6aea002 | 148 | |
SomeRandomBloke | 1:45cec6aea002 | 149 | logInfo("Saving Config"); |
SomeRandomBloke | 1:45cec6aea002 | 150 | |
SomeRandomBloke | 1:45cec6aea002 | 151 | // Save config |
SomeRandomBloke | 1:45cec6aea002 | 152 | if (! dot->saveConfig()) { |
SomeRandomBloke | 1:45cec6aea002 | 153 | logError("failed to save configuration"); |
SomeRandomBloke | 1:45cec6aea002 | 154 | } |
SomeRandomBloke | 1:45cec6aea002 | 155 | } else { |
SomeRandomBloke | 1:45cec6aea002 | 156 | logInfo("Using existing Config"); |
SomeRandomBloke | 0:5a0b43f3b143 | 157 | } |
SomeRandomBloke | 0:5a0b43f3b143 | 158 | // wait_ms(2000); |
SomeRandomBloke | 0:5a0b43f3b143 | 159 | // Display protocol/connection info |
SomeRandomBloke | 0:5a0b43f3b143 | 160 | |
SomeRandomBloke | 0:5a0b43f3b143 | 161 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
SomeRandomBloke | 0:5a0b43f3b143 | 162 | logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 0:5a0b43f3b143 | 163 | //wait(2); |
SomeRandomBloke | 0:5a0b43f3b143 | 164 | wait_ms(dot->getNextTxMs() + 1); |
SomeRandomBloke | 0:5a0b43f3b143 | 165 | } |
SomeRandomBloke | 1:45cec6aea002 | 166 | |
SomeRandomBloke | 0:5a0b43f3b143 | 167 | probe.setResolution(9); |
SomeRandomBloke | 0:5a0b43f3b143 | 168 | |
SomeRandomBloke | 0:5a0b43f3b143 | 169 | char dataBuf[50]; |
SomeRandomBloke | 0:5a0b43f3b143 | 170 | // for (uint8_t i = 0; i < iterations; i++) { |
SomeRandomBloke | 0:5a0b43f3b143 | 171 | while( 1 ) { |
SomeRandomBloke | 0:5a0b43f3b143 | 172 | // This takes upto 750mS, way too long. Change to 9 bit resolution if not already used. |
SomeRandomBloke | 1:45cec6aea002 | 173 | |
SomeRandomBloke | 0:5a0b43f3b143 | 174 | probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
SomeRandomBloke | 0:5a0b43f3b143 | 175 | // printf("It is %3.1fC\r\n", probe.temperature()); |
SomeRandomBloke | 0:5a0b43f3b143 | 176 | // Output data as JSON e.g. {"temperature":"21.3"} |
SomeRandomBloke | 0:5a0b43f3b143 | 177 | temperature = probe.temperature(); |
SomeRandomBloke | 1:45cec6aea002 | 178 | sprintf(dataBuf, "{\"tmp\":%3.1f}", temperature ); |
SomeRandomBloke | 0:5a0b43f3b143 | 179 | send_data.clear(); |
SomeRandomBloke | 0:5a0b43f3b143 | 180 | // probably not the most efficent way to do this |
SomeRandomBloke | 0:5a0b43f3b143 | 181 | for( int i=0; i< strlen(dataBuf); i++ ) |
SomeRandomBloke | 0:5a0b43f3b143 | 182 | send_data.push_back( dataBuf[i] ); |
SomeRandomBloke | 0:5a0b43f3b143 | 183 | |
SomeRandomBloke | 0:5a0b43f3b143 | 184 | if ((ret = dot->send(send_data)) != mDot::MDOT_OK) { |
SomeRandomBloke | 0:5a0b43f3b143 | 185 | logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 1:45cec6aea002 | 186 | |
SomeRandomBloke | 0:5a0b43f3b143 | 187 | } else { |
SomeRandomBloke | 1:45cec6aea002 | 188 | |
SomeRandomBloke | 0:5a0b43f3b143 | 189 | logInfo("send data: %s", Text::bin2hexString(send_data).c_str()); |
SomeRandomBloke | 1:45cec6aea002 | 190 | |
SomeRandomBloke | 0:5a0b43f3b143 | 191 | } |
SomeRandomBloke | 0:5a0b43f3b143 | 192 | |
SomeRandomBloke | 1:45cec6aea002 | 193 | // Should sleep here and wakeup after a set interval. |
SomeRandomBloke | 1:45cec6aea002 | 194 | |
SomeRandomBloke | 1:45cec6aea002 | 195 | uint32_t sleep_time = 60; //(dot->getNextTxMs() / 1000) + 10; |
SomeRandomBloke | 1:45cec6aea002 | 196 | logInfo("going to sleep..."); |
SomeRandomBloke | 0:5a0b43f3b143 | 197 | |
SomeRandomBloke | 1:45cec6aea002 | 198 | // go to sleep and wake up automatically sleep_time seconds later |
SomeRandomBloke | 1:45cec6aea002 | 199 | dot->sleep(sleep_time, mDot::RTC_ALARM, false); |
SomeRandomBloke | 1:45cec6aea002 | 200 | /* |
SomeRandomBloke | 1:45cec6aea002 | 201 | next_tx = dot->getNextTxMs() + 1; |
SomeRandomBloke | 1:45cec6aea002 | 202 | logInfo("waiting %ld ms to transmit again", next_tx); |
SomeRandomBloke | 1:45cec6aea002 | 203 | wait_ms(next_tx); |
SomeRandomBloke | 1:45cec6aea002 | 204 | logInfo("waiting another %d seconds", wait_time); |
SomeRandomBloke | 1:45cec6aea002 | 205 | wait(wait_time); |
SomeRandomBloke | 1:45cec6aea002 | 206 | */ |
SomeRandomBloke | 0:5a0b43f3b143 | 207 | } |
SomeRandomBloke | 0:5a0b43f3b143 | 208 | //#endif |
SomeRandomBloke | 0:5a0b43f3b143 | 209 | return 0; |
SomeRandomBloke | 0:5a0b43f3b143 | 210 | } |
SomeRandomBloke | 0:5a0b43f3b143 | 211 | /* |
SomeRandomBloke | 0:5a0b43f3b143 | 212 | void log_error(mDot* dot, const char* msg, int32_t retval) |
SomeRandomBloke | 0:5a0b43f3b143 | 213 | { |
SomeRandomBloke | 0:5a0b43f3b143 | 214 | printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); |
SomeRandomBloke | 0:5a0b43f3b143 | 215 | } |
SomeRandomBloke | 0:5a0b43f3b143 | 216 | */ |