This is a fork of the Multitech mDot LoRa Connect Example modified to log data or send from Temperature and Light Sensors connected to the mDot UDK
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example by
main.cpp@7:928e848b92c8, 2016-06-16 (annotated)
- Committer:
- AshuJoshi
- Date:
- Thu Jun 16 00:37:16 2016 +0000
- Revision:
- 7:928e848b92c8
- Parent:
- 6:f0b6fea36e28
Modified the LoRa Connect Example published by MultiTech for reading Grove Temperature and Light Sensors and sending the sensor reading to the MultiTech mLinux LoRa Gateway
Who changed what in which revision?
User | Revision | Line number | New 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> |
mfiore | 0:09250cd371d2 | 7 | |
AshuJoshi | 7:928e848b92c8 | 8 | // Connect the Grove Shield to the UDK |
AshuJoshi | 7:928e848b92c8 | 9 | // Connect the Grove Temperature Sensor to A0 on Shield |
AshuJoshi | 7:928e848b92c8 | 10 | // http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor |
AshuJoshi | 7:928e848b92c8 | 11 | // Connect the Grove Light Sensor to the A1 |
AshuJoshi | 7:928e848b92c8 | 12 | // http://www.seeedstudio.com/wiki/Grove_-_Light_Sensor |
AshuJoshi | 7:928e848b92c8 | 13 | |
AshuJoshi | 7:928e848b92c8 | 14 | AnalogIn in(PB_1); // This corresponds to A0 Connector on the Grove Shield |
AshuJoshi | 7:928e848b92c8 | 15 | AnalogIn light(PB_0); // This corresponds to A1 Connector on the Grove Shield |
AshuJoshi | 7:928e848b92c8 | 16 | |
AshuJoshi | 7:928e848b92c8 | 17 | float temperature; |
AshuJoshi | 7:928e848b92c8 | 18 | float llevel; |
AshuJoshi | 7:928e848b92c8 | 19 | |
mfiore | 2:6e2c378339d9 | 20 | // these options must match the settings on your Conduit |
mfiore | 2:6e2c378339d9 | 21 | // uncomment the following lines and edit their values to match your configuration |
AshuJoshi | 7:928e848b92c8 | 22 | static std::string config_network_name = "ATLANTA_TTN"; |
AshuJoshi | 7:928e848b92c8 | 23 | static std::string config_network_pass = "ATLANTA_TTN"; |
AshuJoshi | 7:928e848b92c8 | 24 | static uint8_t config_frequency_sub_band = 7; |
AshuJoshi | 7:928e848b92c8 | 25 | |
AshuJoshi | 7:928e848b92c8 | 26 | float readTemperature(){ |
AshuJoshi | 7:928e848b92c8 | 27 | float temp; |
AshuJoshi | 7:928e848b92c8 | 28 | int B=39751; |
AshuJoshi | 7:928e848b92c8 | 29 | float resistance; |
AshuJoshi | 7:928e848b92c8 | 30 | int a; |
AshuJoshi | 7:928e848b92c8 | 31 | // Read & Print Temperature |
AshuJoshi | 7:928e848b92c8 | 32 | a = in.read_u16(); |
AshuJoshi | 7:928e848b92c8 | 33 | //printf("analog value: %i \r\n", a); |
AshuJoshi | 7:928e848b92c8 | 34 | resistance = (float)(65534-a)*10000/a; //get the resistance of the sensor; |
AshuJoshi | 7:928e848b92c8 | 35 | temp=1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet |
AshuJoshi | 7:928e848b92c8 | 36 | //logInfo("Temperature: %f, temp); |
AshuJoshi | 7:928e848b92c8 | 37 | return temp; |
AshuJoshi | 7:928e848b92c8 | 38 | } |
AshuJoshi | 7:928e848b92c8 | 39 | |
AshuJoshi | 7:928e848b92c8 | 40 | float readLightSensor() { |
AshuJoshi | 7:928e848b92c8 | 41 | float sensorValue; |
AshuJoshi | 7:928e848b92c8 | 42 | float rsensor; |
AshuJoshi | 7:928e848b92c8 | 43 | sensorValue = light.read(); |
AshuJoshi | 7:928e848b92c8 | 44 | rsensor = (float)(1023-sensorValue)*10/sensorValue; |
AshuJoshi | 7:928e848b92c8 | 45 | printf("Sensor reading: %2.2f - %2.2f\r\n", sensorValue, rsensor); |
AshuJoshi | 7:928e848b92c8 | 46 | |
AshuJoshi | 7:928e848b92c8 | 47 | return rsensor; |
AshuJoshi | 7:928e848b92c8 | 48 | |
AshuJoshi | 7:928e848b92c8 | 49 | } |
AshuJoshi | 7:928e848b92c8 | 50 | |
mfiore | 0:09250cd371d2 | 51 | |
mfiore | 0:09250cd371d2 | 52 | int main() { |
mfiore | 0:09250cd371d2 | 53 | int32_t ret; |
mfiore | 0:09250cd371d2 | 54 | mDot* dot; |
mfiore | 0:09250cd371d2 | 55 | std::vector<uint8_t> data; |
mfiore | 4:36e214ebfa56 | 56 | std::string data_str = "hello!"; |
AshuJoshi | 7:928e848b92c8 | 57 | char string_buffer[64]; |
AshuJoshi | 7:928e848b92c8 | 58 | std::string separator_str = ","; |
AshuJoshi | 7:928e848b92c8 | 59 | std::string temp_cls = "TC"; |
mfiore | 2:6e2c378339d9 | 60 | |
mfiore | 0:09250cd371d2 | 61 | // get a mDot handle |
mfiore | 0:09250cd371d2 | 62 | dot = mDot::getInstance(); |
mfiore | 2:6e2c378339d9 | 63 | |
mfiore | 2:6e2c378339d9 | 64 | // print library version information |
mfiore | 2:6e2c378339d9 | 65 | logInfo("version: %s", dot->getId().c_str()); |
mfiore | 0:09250cd371d2 | 66 | |
mfiore | 2:6e2c378339d9 | 67 | //******************************************* |
mfiore | 2:6e2c378339d9 | 68 | // configuration |
mfiore | 2:6e2c378339d9 | 69 | //******************************************* |
mfiore | 0:09250cd371d2 | 70 | // reset to default config so we know what state we're in |
AshuJoshi | 7:928e848b92c8 | 71 | //dot->resetConfig(); |
mfiore | 0:09250cd371d2 | 72 | |
AshuJoshi | 7:928e848b92c8 | 73 | //dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
AshuJoshi | 7:928e848b92c8 | 74 | dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); |
AshuJoshi | 7:928e848b92c8 | 75 | |
mfiore | 2:6e2c378339d9 | 76 | // set up the mDot with our network information: frequency sub band, network name, and network password |
mfiore | 2:6e2c378339d9 | 77 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
mfiore | 4:36e214ebfa56 | 78 | |
mfiore | 4:36e214ebfa56 | 79 | // frequency sub band is only applicable in the 915 (US) frequency band |
mfiore | 4:36e214ebfa56 | 80 | // 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 | 81 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
jreiss | 5:e9d78a9bafc5 | 82 | |
AshuJoshi | 7:928e848b92c8 | 83 | logInfo("setting to public network"); |
AshuJoshi | 7:928e848b92c8 | 84 | if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
AshuJoshi | 7:928e848b92c8 | 85 | logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
AshuJoshi | 7:928e848b92c8 | 86 | } |
jreiss | 5:e9d78a9bafc5 | 87 | |
jreiss | 5:e9d78a9bafc5 | 88 | logInfo("setting frequency sub band"); |
mfiore | 0:09250cd371d2 | 89 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 90 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 2:6e2c378339d9 | 91 | } |
mfiore | 4:36e214ebfa56 | 92 | |
mfiore | 2:6e2c378339d9 | 93 | logInfo("setting network name"); |
mfiore | 2:6e2c378339d9 | 94 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 95 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 96 | } |
mfiore | 4:36e214ebfa56 | 97 | |
mfiore | 2:6e2c378339d9 | 98 | logInfo("setting network password"); |
mfiore | 2:6e2c378339d9 | 99 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 100 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 101 | } |
mfiore | 4:36e214ebfa56 | 102 | |
mfiore | 4:36e214ebfa56 | 103 | // a higher spreading factor allows for longer range but lower throughput |
mfiore | 4:36e214ebfa56 | 104 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
mfiore | 4:36e214ebfa56 | 105 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
mfiore | 4:36e214ebfa56 | 106 | logInfo("setting TX spreading factor"); |
mfiore | 4:36e214ebfa56 | 107 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 108 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 109 | } |
mfiore | 4:36e214ebfa56 | 110 | |
mfiore | 4:36e214ebfa56 | 111 | // request receive confirmation of packets from the gateway |
mfiore | 4:36e214ebfa56 | 112 | logInfo("enabling ACKs"); |
mfiore | 4:36e214ebfa56 | 113 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 114 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 115 | } |
mfiore | 4:36e214ebfa56 | 116 | |
mfiore | 4:36e214ebfa56 | 117 | // save this configuration to the mDot's NVM |
mfiore | 2:6e2c378339d9 | 118 | logInfo("saving config"); |
mfiore | 2:6e2c378339d9 | 119 | if (! dot->saveConfig()) { |
mfiore | 2:6e2c378339d9 | 120 | logError("failed to save configuration"); |
mfiore | 0:09250cd371d2 | 121 | } |
mfiore | 2:6e2c378339d9 | 122 | //******************************************* |
mfiore | 2:6e2c378339d9 | 123 | // end of configuration |
mfiore | 2:6e2c378339d9 | 124 | //******************************************* |
mfiore | 0:09250cd371d2 | 125 | |
mfiore | 0:09250cd371d2 | 126 | // attempt to join the network |
mfiore | 2:6e2c378339d9 | 127 | logInfo("joining network"); |
mfiore | 0:09250cd371d2 | 128 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 129 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 130 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:36e214ebfa56 | 131 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
mfiore | 0:09250cd371d2 | 132 | } |
mfiore | 0:09250cd371d2 | 133 | |
mfiore | 0:09250cd371d2 | 134 | // format data for sending to the gateway |
AshuJoshi | 7:928e848b92c8 | 135 | //for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
AshuJoshi | 7:928e848b92c8 | 136 | // data.push_back((uint8_t) *it); |
mfiore | 0:09250cd371d2 | 137 | |
mfiore | 0:09250cd371d2 | 138 | while (true) { |
AshuJoshi | 7:928e848b92c8 | 139 | data.clear(); |
AshuJoshi | 7:928e848b92c8 | 140 | temperature = readTemperature(); |
AshuJoshi | 7:928e848b92c8 | 141 | |
AshuJoshi | 7:928e848b92c8 | 142 | // Temperature |
AshuJoshi | 7:928e848b92c8 | 143 | sprintf(string_buffer, "%s%3.2f", "TC:", temperature); |
AshuJoshi | 7:928e848b92c8 | 144 | //sprintf(string_buffer, "%3.1f", temperature); |
AshuJoshi | 7:928e848b92c8 | 145 | for (int i = 0; i<strlen(string_buffer); i++) |
AshuJoshi | 7:928e848b92c8 | 146 | { |
AshuJoshi | 7:928e848b92c8 | 147 | data.push_back(((char*)string_buffer)[i]); |
AshuJoshi | 7:928e848b92c8 | 148 | } |
AshuJoshi | 7:928e848b92c8 | 149 | |
AshuJoshi | 7:928e848b92c8 | 150 | logDebug("Sending LoRa message, length: %d", data.size()); |
AshuJoshi | 7:928e848b92c8 | 151 | logDebug("sending data: "); |
AshuJoshi | 7:928e848b92c8 | 152 | for(int i = 0; i < data.size(); i++) |
AshuJoshi | 7:928e848b92c8 | 153 | { |
AshuJoshi | 7:928e848b92c8 | 154 | printf("%c", data[i]); |
AshuJoshi | 7:928e848b92c8 | 155 | } |
AshuJoshi | 7:928e848b92c8 | 156 | printf("\n"); |
AshuJoshi | 7:928e848b92c8 | 157 | |
mfiore | 4:36e214ebfa56 | 158 | // send the data to the gateway |
mfiore | 0:09250cd371d2 | 159 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 160 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 161 | } else { |
mfiore | 2:6e2c378339d9 | 162 | logInfo("successfully sent data to gateway"); |
mfiore | 0:09250cd371d2 | 163 | } |
mfiore | 0:09250cd371d2 | 164 | |
mfiore | 4:36e214ebfa56 | 165 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:36e214ebfa56 | 166 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
AshuJoshi | 7:928e848b92c8 | 167 | |
AshuJoshi | 7:928e848b92c8 | 168 | data.clear(); |
AshuJoshi | 7:928e848b92c8 | 169 | // Light Level |
AshuJoshi | 7:928e848b92c8 | 170 | llevel = readLightSensor(); |
AshuJoshi | 7:928e848b92c8 | 171 | sprintf(string_buffer, "%s%5.1f", "LL:", llevel); |
AshuJoshi | 7:928e848b92c8 | 172 | for (int i = 0; i<strlen(string_buffer); i++) |
AshuJoshi | 7:928e848b92c8 | 173 | { |
AshuJoshi | 7:928e848b92c8 | 174 | data.push_back(((char*)string_buffer)[i]); |
AshuJoshi | 7:928e848b92c8 | 175 | } |
AshuJoshi | 7:928e848b92c8 | 176 | logDebug("Sending LoRa message, length: %d", data.size()); |
AshuJoshi | 7:928e848b92c8 | 177 | logDebug("sending data: "); |
AshuJoshi | 7:928e848b92c8 | 178 | for(int i = 0; i < data.size(); i++) |
AshuJoshi | 7:928e848b92c8 | 179 | { |
AshuJoshi | 7:928e848b92c8 | 180 | printf("%c", data[i]); |
AshuJoshi | 7:928e848b92c8 | 181 | } |
AshuJoshi | 7:928e848b92c8 | 182 | printf("\n"); |
AshuJoshi | 7:928e848b92c8 | 183 | |
AshuJoshi | 7:928e848b92c8 | 184 | // send the data to the gateway |
AshuJoshi | 7:928e848b92c8 | 185 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
AshuJoshi | 7:928e848b92c8 | 186 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
AshuJoshi | 7:928e848b92c8 | 187 | } else { |
AshuJoshi | 7:928e848b92c8 | 188 | logInfo("successfully sent data to gateway"); |
AshuJoshi | 7:928e848b92c8 | 189 | } |
AshuJoshi | 7:928e848b92c8 | 190 | |
AshuJoshi | 7:928e848b92c8 | 191 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
AshuJoshi | 7:928e848b92c8 | 192 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
AshuJoshi | 7:928e848b92c8 | 193 | |
mfiore | 0:09250cd371d2 | 194 | } |
mfiore | 0:09250cd371d2 | 195 | |
mfiore | 0:09250cd371d2 | 196 | return 0; |
mfiore | 0:09250cd371d2 | 197 | } |