d

Dependencies:   BME280 DOGS102 DS1820 MMA845x_timmeh MTS-Serial libmDot_Australia915Mhz mbed-rtos mbed

Fork of mDot_TTN_OTAA_Node by Thing Innovations

Committer:
SomeRandomBloke
Date:
Mon Sep 21 18:45:15 2015 +0000
Revision:
2:9db840d12557
Parent:
1:45cec6aea002
Child:
3:367aa95f9771
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 2:9db840d12557 1 /** mDot_DS18B20 - Simple mDot temperature sensor using Dallas Semiconductors DS18B20 OneWire temperature sensor.
SomeRandomBloke 2:9db840d12557 2 *
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 2:9db840d12557 10 "payload": "{\"tmp\":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 2:9db840d12557 33
SomeRandomBloke 2:9db840d12557 34 // DS18B20 OneWire pin
SomeRandomBloke 2:9db840d12557 35 // D13 on Dev Board, pin x on mDot
SomeRandomBloke 1:45cec6aea002 36 #define DATA_PIN PA_5
SomeRandomBloke 2:9db840d12557 37 // A0 on Dev Board, pin x on mDot
SomeRandomBloke 1:45cec6aea002 38 //#define DATA_PIN PB_1
SomeRandomBloke 0:5a0b43f3b143 39
SomeRandomBloke 0:5a0b43f3b143 40 DS1820 probe(DATA_PIN);
SomeRandomBloke 0:5a0b43f3b143 41
SomeRandomBloke 0:5a0b43f3b143 42 //void log_error(mDot* dot, const char* msg, int32_t retval);
SomeRandomBloke 0:5a0b43f3b143 43
SomeRandomBloke 0:5a0b43f3b143 44 Serial pc(USBTX,USBRX);
SomeRandomBloke 0:5a0b43f3b143 45
SomeRandomBloke 0:5a0b43f3b143 46
SomeRandomBloke 0:5a0b43f3b143 47 int main()
SomeRandomBloke 0:5a0b43f3b143 48 {
SomeRandomBloke 0:5a0b43f3b143 49 int32_t ret;
SomeRandomBloke 0:5a0b43f3b143 50 mDot* dot;
SomeRandomBloke 0:5a0b43f3b143 51 std::vector<uint8_t> send_data;
SomeRandomBloke 0:5a0b43f3b143 52 std::vector<uint8_t> recv_data;
SomeRandomBloke 0:5a0b43f3b143 53
SomeRandomBloke 0:5a0b43f3b143 54 float temperature = 0.0;
SomeRandomBloke 1:45cec6aea002 55
SomeRandomBloke 0:5a0b43f3b143 56 pc.baud(115200);
SomeRandomBloke 0:5a0b43f3b143 57 pc.printf("mDot LoRa Temperature sensor\n\r");
SomeRandomBloke 1:45cec6aea002 58
SomeRandomBloke 0:5a0b43f3b143 59 // get a mDot handle
SomeRandomBloke 0:5a0b43f3b143 60 dot = mDot::getInstance();
SomeRandomBloke 0:5a0b43f3b143 61
SomeRandomBloke 0:5a0b43f3b143 62 dot->setLogLevel(MTSLog::TRACE_LEVEL);
SomeRandomBloke 0:5a0b43f3b143 63
SomeRandomBloke 1:45cec6aea002 64 logInfo("Checking Config");
SomeRandomBloke 1:45cec6aea002 65
SomeRandomBloke 1:45cec6aea002 66 // Test if we've already saved the config
SomeRandomBloke 1:45cec6aea002 67 std::string configNetworkName = dot->getNetworkName();
SomeRandomBloke 1:45cec6aea002 68
SomeRandomBloke 1:45cec6aea002 69 // Check pin, if low then reset config.
SomeRandomBloke 2:9db840d12557 70 // if ((ret = dot->setJoinMode( mDot::AUTO_OTA )) != mDot::MDOT_OK) {
SomeRandomBloke 2:9db840d12557 71 // logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 2:9db840d12557 72 // }
SomeRandomBloke 2:9db840d12557 73
SomeRandomBloke 2:9db840d12557 74
SomeRandomBloke 1:45cec6aea002 75 if( config_network_name.compare(configNetworkName) != 0 ) {
SomeRandomBloke 1:45cec6aea002 76 // Not saved config, reset
SomeRandomBloke 1:45cec6aea002 77 logInfo("Setting Config");
SomeRandomBloke 0:5a0b43f3b143 78
SomeRandomBloke 1:45cec6aea002 79 // reset to default config so we know what state we're in
SomeRandomBloke 1:45cec6aea002 80 dot->resetConfig();
SomeRandomBloke 1:45cec6aea002 81
SomeRandomBloke 1:45cec6aea002 82 // Set byte order - AEP less than 1.0.30
SomeRandomBloke 1:45cec6aea002 83 // dot->setJoinByteOrder(mDot::MSB);
SomeRandomBloke 1:45cec6aea002 84 dot->setJoinByteOrder(mDot::LSB);
SomeRandomBloke 0:5a0b43f3b143 85
SomeRandomBloke 1:45cec6aea002 86 logInfo("setting Join mode");
SomeRandomBloke 1:45cec6aea002 87 if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) {
SomeRandomBloke 1:45cec6aea002 88 logError("failed to set Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 1:45cec6aea002 89 }
SomeRandomBloke 1:45cec6aea002 90
SomeRandomBloke 1:45cec6aea002 91 // If on developer board then you can enable activity LED
SomeRandomBloke 1:45cec6aea002 92 // Currently no spare pins that LEDs are connected too.
SomeRandomBloke 0:5a0b43f3b143 93 // dot->setActivityLedPin( ACTIVITY_LED );
SomeRandomBloke 0:5a0b43f3b143 94 // dot->setActivityLedEnable(false);
SomeRandomBloke 0:5a0b43f3b143 95
SomeRandomBloke 2:9db840d12557 96 // Have a decent nubmer of retries in connecting to LoRaWAN
SomeRandomBloke 2:9db840d12557 97 dot->setJoinRetries( 3 );
SomeRandomBloke 2:9db840d12557 98
SomeRandomBloke 2:9db840d12557 99 // Set Spreading Factor, higher is lower data rate, smaller packets but longer range
SomeRandomBloke 2:9db840d12557 100 // Lower is higher data rate, larger packets and shorter range.
SomeRandomBloke 2:9db840d12557 101 // dot->setTxDataRate( mDot::SF_9 );
SomeRandomBloke 2:9db840d12557 102 dot->setTxDataRate( mDot::SF_12 );
SomeRandomBloke 1:45cec6aea002 103 dot->setTxPower( 14 );
SomeRandomBloke 1:45cec6aea002 104 dot->setAck( 0 ); // 1 retries on Ack, 0 to disable
SomeRandomBloke 2:9db840d12557 105
SomeRandomBloke 1:45cec6aea002 106 // Not applicable for 868MHz in EU
SomeRandomBloke 1:45cec6aea002 107 // if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
SomeRandomBloke 1:45cec6aea002 108 // initStatus = false;
SomeRandomBloke 0:5a0b43f3b143 109 // logError(dot, "failed to set frequency sub band", ret);
SomeRandomBloke 1:45cec6aea002 110 // }
SomeRandomBloke 1:45cec6aea002 111
SomeRandomBloke 1:45cec6aea002 112 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
SomeRandomBloke 1:45cec6aea002 113 // initStatus = false;
SomeRandomBloke 1:45cec6aea002 114 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 1:45cec6aea002 115 }
SomeRandomBloke 0:5a0b43f3b143 116
SomeRandomBloke 1:45cec6aea002 117 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
SomeRandomBloke 1:45cec6aea002 118 // initStatus = false;
SomeRandomBloke 1:45cec6aea002 119 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 1:45cec6aea002 120 }
SomeRandomBloke 1:45cec6aea002 121
SomeRandomBloke 2:9db840d12557 122 if ((ret = dot->setJoinMode( mDot::AUTO_OTA )) != mDot::MDOT_OK) {
SomeRandomBloke 2:9db840d12557 123 logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 2:9db840d12557 124 }
SomeRandomBloke 2:9db840d12557 125
SomeRandomBloke 1:45cec6aea002 126 logInfo("Saving Config");
SomeRandomBloke 1:45cec6aea002 127
SomeRandomBloke 1:45cec6aea002 128 // Save config
SomeRandomBloke 1:45cec6aea002 129 if (! dot->saveConfig()) {
SomeRandomBloke 1:45cec6aea002 130 logError("failed to save configuration");
SomeRandomBloke 1:45cec6aea002 131 }
SomeRandomBloke 1:45cec6aea002 132 } else {
SomeRandomBloke 1:45cec6aea002 133 logInfo("Using existing Config");
SomeRandomBloke 0:5a0b43f3b143 134 }
SomeRandomBloke 0:5a0b43f3b143 135
SomeRandomBloke 0:5a0b43f3b143 136 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
SomeRandomBloke 0:5a0b43f3b143 137 logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 0:5a0b43f3b143 138 //wait(2);
SomeRandomBloke 0:5a0b43f3b143 139 wait_ms(dot->getNextTxMs() + 1);
SomeRandomBloke 0:5a0b43f3b143 140 }
SomeRandomBloke 1:45cec6aea002 141
SomeRandomBloke 0:5a0b43f3b143 142 probe.setResolution(9);
SomeRandomBloke 0:5a0b43f3b143 143
SomeRandomBloke 0:5a0b43f3b143 144 char dataBuf[50];
SomeRandomBloke 0:5a0b43f3b143 145 // for (uint8_t i = 0; i < iterations; i++) {
SomeRandomBloke 0:5a0b43f3b143 146 while( 1 ) {
SomeRandomBloke 0:5a0b43f3b143 147 // This takes upto 750mS, way too long. Change to 9 bit resolution if not already used.
SomeRandomBloke 1:45cec6aea002 148
SomeRandomBloke 0:5a0b43f3b143 149 probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready
SomeRandomBloke 0:5a0b43f3b143 150 // printf("It is %3.1fC\r\n", probe.temperature());
SomeRandomBloke 0:5a0b43f3b143 151 // Output data as JSON e.g. {"temperature":"21.3"}
SomeRandomBloke 0:5a0b43f3b143 152 temperature = probe.temperature();
SomeRandomBloke 1:45cec6aea002 153 sprintf(dataBuf, "{\"tmp\":%3.1f}", temperature );
SomeRandomBloke 0:5a0b43f3b143 154 send_data.clear();
SomeRandomBloke 0:5a0b43f3b143 155 // probably not the most efficent way to do this
SomeRandomBloke 0:5a0b43f3b143 156 for( int i=0; i< strlen(dataBuf); i++ )
SomeRandomBloke 0:5a0b43f3b143 157 send_data.push_back( dataBuf[i] );
SomeRandomBloke 0:5a0b43f3b143 158
SomeRandomBloke 0:5a0b43f3b143 159 if ((ret = dot->send(send_data)) != mDot::MDOT_OK) {
SomeRandomBloke 0:5a0b43f3b143 160 logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 0:5a0b43f3b143 161 } else {
SomeRandomBloke 0:5a0b43f3b143 162 logInfo("send data: %s", Text::bin2hexString(send_data).c_str());
SomeRandomBloke 0:5a0b43f3b143 163 }
SomeRandomBloke 0:5a0b43f3b143 164
SomeRandomBloke 1:45cec6aea002 165 // Should sleep here and wakeup after a set interval.
SomeRandomBloke 2:9db840d12557 166 uint32_t sleep_time = (dot->getNextTxMs() / 1000) + 60;
SomeRandomBloke 2:9db840d12557 167 logInfo("going to sleep for %d seconds", sleep_time);
SomeRandomBloke 0:5a0b43f3b143 168
SomeRandomBloke 1:45cec6aea002 169 // go to sleep and wake up automatically sleep_time seconds later
SomeRandomBloke 2:9db840d12557 170 dot->sleep(sleep_time, mDot::RTC_ALARM);
SomeRandomBloke 2:9db840d12557 171
SomeRandomBloke 1:45cec6aea002 172 /*
SomeRandomBloke 1:45cec6aea002 173 next_tx = dot->getNextTxMs() + 1;
SomeRandomBloke 1:45cec6aea002 174 logInfo("waiting %ld ms to transmit again", next_tx);
SomeRandomBloke 1:45cec6aea002 175 wait_ms(next_tx);
SomeRandomBloke 1:45cec6aea002 176 logInfo("waiting another %d seconds", wait_time);
SomeRandomBloke 1:45cec6aea002 177 wait(wait_time);
SomeRandomBloke 1:45cec6aea002 178 */
SomeRandomBloke 0:5a0b43f3b143 179 }
SomeRandomBloke 2:9db840d12557 180
SomeRandomBloke 0:5a0b43f3b143 181 return 0;
SomeRandomBloke 0:5a0b43f3b143 182 }