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
00001 #include "mbed.h" 00002 #include "mDot.h" 00003 #include "MTSLog.h" 00004 #include <string> 00005 #include <vector> 00006 #include <algorithm> 00007 00008 // Connect the Grove Shield to the UDK 00009 // Connect the Grove Temperature Sensor to A0 on Shield 00010 // http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor 00011 // Connect the Grove Light Sensor to the A1 00012 // http://www.seeedstudio.com/wiki/Grove_-_Light_Sensor 00013 00014 AnalogIn in(PB_1); // This corresponds to A0 Connector on the Grove Shield 00015 AnalogIn light(PB_0); // This corresponds to A1 Connector on the Grove Shield 00016 00017 float temperature; 00018 float llevel; 00019 00020 // these options must match the settings on your Conduit 00021 // uncomment the following lines and edit their values to match your configuration 00022 static std::string config_network_name = "ATLANTA_TTN"; 00023 static std::string config_network_pass = "ATLANTA_TTN"; 00024 static uint8_t config_frequency_sub_band = 7; 00025 00026 float readTemperature(){ 00027 float temp; 00028 int B=39751; 00029 float resistance; 00030 int a; 00031 // Read & Print Temperature 00032 a = in.read_u16(); 00033 //printf("analog value: %i \r\n", a); 00034 resistance = (float)(65534-a)*10000/a; //get the resistance of the sensor; 00035 temp=1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet 00036 //logInfo("Temperature: %f, temp); 00037 return temp; 00038 } 00039 00040 float readLightSensor() { 00041 float sensorValue; 00042 float rsensor; 00043 sensorValue = light.read(); 00044 rsensor = (float)(1023-sensorValue)*10/sensorValue; 00045 printf("Sensor reading: %2.2f - %2.2f\r\n", sensorValue, rsensor); 00046 00047 return rsensor; 00048 00049 } 00050 00051 00052 int main() { 00053 int32_t ret; 00054 mDot* dot; 00055 std::vector<uint8_t> data; 00056 std::string data_str = "hello!"; 00057 char string_buffer[64]; 00058 std::string separator_str = ","; 00059 std::string temp_cls = "TC"; 00060 00061 // get a mDot handle 00062 dot = mDot::getInstance(); 00063 00064 // print library version information 00065 logInfo("version: %s", dot->getId().c_str()); 00066 00067 //******************************************* 00068 // configuration 00069 //******************************************* 00070 // reset to default config so we know what state we're in 00071 //dot->resetConfig(); 00072 00073 //dot->setLogLevel(mts::MTSLog::INFO_LEVEL); 00074 dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); 00075 00076 // set up the mDot with our network information: frequency sub band, network name, and network password 00077 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() 00078 00079 // frequency sub band is only applicable in the 915 (US) frequency band 00080 // 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 00081 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels 00082 00083 logInfo("setting to public network"); 00084 if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { 00085 logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00086 } 00087 00088 logInfo("setting frequency sub band"); 00089 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { 00090 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00091 } 00092 00093 logInfo("setting network name"); 00094 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { 00095 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00096 } 00097 00098 logInfo("setting network password"); 00099 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { 00100 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00101 } 00102 00103 // a higher spreading factor allows for longer range but lower throughput 00104 // in the 915 (US) frequency band, spreading factors 7 - 10 are available 00105 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available 00106 logInfo("setting TX spreading factor"); 00107 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { 00108 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00109 } 00110 00111 // request receive confirmation of packets from the gateway 00112 logInfo("enabling ACKs"); 00113 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { 00114 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00115 } 00116 00117 // save this configuration to the mDot's NVM 00118 logInfo("saving config"); 00119 if (! dot->saveConfig()) { 00120 logError("failed to save configuration"); 00121 } 00122 //******************************************* 00123 // end of configuration 00124 //******************************************* 00125 00126 // attempt to join the network 00127 logInfo("joining network"); 00128 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { 00129 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00130 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again 00131 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); 00132 } 00133 00134 // format data for sending to the gateway 00135 //for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) 00136 // data.push_back((uint8_t) *it); 00137 00138 while (true) { 00139 data.clear(); 00140 temperature = readTemperature(); 00141 00142 // Temperature 00143 sprintf(string_buffer, "%s%3.2f", "TC:", temperature); 00144 //sprintf(string_buffer, "%3.1f", temperature); 00145 for (int i = 0; i<strlen(string_buffer); i++) 00146 { 00147 data.push_back(((char*)string_buffer)[i]); 00148 } 00149 00150 logDebug("Sending LoRa message, length: %d", data.size()); 00151 logDebug("sending data: "); 00152 for(int i = 0; i < data.size(); i++) 00153 { 00154 printf("%c", data[i]); 00155 } 00156 printf("\n"); 00157 00158 // send the data to the gateway 00159 if ((ret = dot->send(data)) != mDot::MDOT_OK) { 00160 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); 00161 } else { 00162 logInfo("successfully sent data to gateway"); 00163 } 00164 00165 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again 00166 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); 00167 00168 data.clear(); 00169 // Light Level 00170 llevel = readLightSensor(); 00171 sprintf(string_buffer, "%s%5.1f", "LL:", llevel); 00172 for (int i = 0; i<strlen(string_buffer); i++) 00173 { 00174 data.push_back(((char*)string_buffer)[i]); 00175 } 00176 logDebug("Sending LoRa message, length: %d", data.size()); 00177 logDebug("sending data: "); 00178 for(int i = 0; i < data.size(); i++) 00179 { 00180 printf("%c", data[i]); 00181 } 00182 printf("\n"); 00183 00184 // send the data to the gateway 00185 if ((ret = dot->send(data)) != mDot::MDOT_OK) { 00186 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); 00187 } else { 00188 logInfo("successfully sent data to gateway"); 00189 } 00190 00191 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again 00192 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); 00193 00194 } 00195 00196 return 0; 00197 }
Generated on Thu Jul 14 2022 21:23:36 by
1.7.2
