Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: PinDetect libmDot mbed-rtos mbed
main.cpp
00001 /********************************************************************************** 00002 * This program monitors the power on state, kill state and battery voltage on 00003 * Woodstream mouse trap using the Multitech mDot and UDK2 development system. 00004 * The power on state is monitored on the PA_4(UDK2 Pin D10)and the kill state is 00005 * monitored on the PA_5 (UDK2 Pin D13) Digital Input Pins. The battery voltage 00006 * is monitored on the PB_1 (UDK2 Pin A0) Analog Input. The status of these pins 00007 * are transferred from the mDot LoRa to the Conduit LoRa gateway. The status is 00008 * also shown on the USB Debug Terminal Window. 00009 *********************************************************************************/ 00010 00011 #include "mbed.h" 00012 #include "mDot.h" 00013 #include "MTSLog.h" 00014 #include <string> 00015 #include <vector> 00016 #include <algorithm> 00017 #include "PinDetect.h" 00018 #include "common.h" 00019 00020 #define I2C_TIME 5 00021 #define LED_TIME 1 00022 #define BATTERY_TIME 2.0 00023 #define KILL_STATUS_TIME 3.0 00024 #define POWER_ON_TIME 4.0 00025 00026 //mDot* dot; 00027 Ticker ledTimer; 00028 Ticker batteryTimer; 00029 Ticker killStatusTimer; 00030 Ticker powerOnTimer; 00031 00032 AnalogIn batteryVoltage(PTB0); 00033 DigitalIn powerOn(PTC1); /* SW2 */ 00034 DigitalIn killStatus(PTB17); /* SW3 */ 00035 DigitalOut led_red(PTA1); 00036 DigitalOut led_green(PTA2); 00037 //DigitalOut led_blue(PTD5); 00038 DigitalOut spi_cs(PTD4); 00039 00040 //Serial pc(PTE0, PTE1); 00041 Serial pc(PTD3, PTD2); 00042 00043 // Configuration variables 00044 static std::string config_network_name = "LORA_NET"; 00045 static std::string config_network_pass = "BLUE_HEN1"; 00046 static uint8_t config_frequency_sub_band = 7; 00047 00048 //Global Variables 00049 bool readyToSendI2C; 00050 bool readyToSendBatteryVoltage; 00051 bool readyToSendKillStatus; 00052 bool readyToSendPowerOn; 00053 float lastBatteryVoltage; 00054 uint8_t lastKillStatus; 00055 uint8_t lastPowerOn; 00056 00057 //Function prototypes 00058 void timeOfDay(); 00059 void periodicSendTock(); 00060 void batteryRead(); 00061 void killStatusRead(); 00062 void powerOnRead(); 00063 void printError(mDot* dot, int32_t returnCode); 00064 void printVersion(); 00065 bool setFrequencySubBand(uint8_t subBand); 00066 bool setNetworkName(const std::string name); 00067 bool setNetworkPassphrase(const std::string passphrase); 00068 bool setTxDataRate(const int dataRate); 00069 bool setPower(uint8_t power); 00070 bool setAck(uint8_t retries); 00071 bool joinNetwork(); 00072 bool send(const std::string text); 00073 I2C i2c_mem(PTB3,PTB2); 00074 //SPI spi_rf(PTD6, PTD7, PTD5, PTD4); 00075 SPI spi_rf(PTD6, PTD7, PTD5); 00076 00077 int i2cAddr = 0xAA; 00078 char data[0x10]; 00079 char data1[0x10]; 00080 00081 int main() 00082 { 00083 bool configFail = false; 00084 int ack; 00085 00086 readyToSendI2C = false; 00087 readyToSendBatteryVoltage = false; 00088 readyToSendKillStatus = false; 00089 readyToSendPowerOn = false; 00090 lastBatteryVoltage = 0.0; 00091 lastKillStatus = killStatus; 00092 lastPowerOn = powerOn; 00093 00094 i2c_mem.frequency(100000); 00095 00096 // Setup the spi for 8 bit data, high steady state clock, 00097 // second edge capture, with a 1MHz clock rate 00098 // spi_rf.format(16,3); 00099 spi_rf.format(16,0); 00100 spi_rf.frequency(1000000); 00101 spi_cs = 1; 00102 00103 //Start LED startup sequence 00104 ledTimer.attach(&timeOfDay, LED_TIME); 00105 00106 pc.baud(115200); 00107 // pc.printf("\r\n\r\n"); 00108 // pc.printf("=====================================\r\n"); 00109 // pc.printf("JLC MT Demo \r\n"); 00110 // pc.printf("=====================================\r\n"); 00111 00112 // get the mDot handle 00113 // dot = mDot::getInstance(); 00114 /* dot->setLogLevel(mts::MTSLog::INFO_LEVEL); 00115 00116 //******************************************* 00117 // configuration 00118 //******************************************* 00119 // reset to default config so we know what state we're in 00120 dot->resetNetworkSession(); 00121 dot->resetConfig(); 00122 00123 // set up the mDot with our network information: frequency sub band, network name, and network password 00124 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() 00125 configFail = !setFrequencySubBand(config_frequency_sub_band); 00126 configFail |= !setNetworkName(config_network_name); 00127 configFail |= !setNetworkPassphrase(config_network_pass); 00128 configFail |= !setTxDataRate(mDot::SF_8); 00129 configFail |= !setPower(20); // Reduce latency for 868 units 00130 configFail |= !setAck(0); // Disable ack for less latency 00131 00132 printf("Configration %s\r\n",configFail?"Failed":"Passed"); 00133 00134 // save this configuration to the mDot's NVM 00135 printf("saving config\r\n"); 00136 if (! dot->saveConfig()) 00137 { 00138 logError("failed to save configuration"); 00139 } 00140 //******************************************* 00141 // end of configuration 00142 //******************************************* 00143 00144 // keep trying to join the network 00145 while (!joinNetwork()) 00146 { 00147 wait(200); 00148 dot->resetNetworkSession(); 00149 } 00150 */ 00151 // Stop LED startup sequence & configure them for operation 00152 led_red = 1; 00153 led_green = 1; 00154 // led_blue = 1; 00155 // start all paramter timers to sample analog and digital inputs 00156 // batteryTimer.attach(batteryRead, BATTERY_TIME); 00157 // killStatusTimer.attach(killStatusRead, KILL_STATUS_TIME); 00158 // powerOnTimer.attach(powerOnRead, POWER_ON_TIME); 00159 00160 /* initialize time and date */ 00161 epoch =0; 00162 00163 /* Send message to verify UART is connected properly */ 00164 printVersion(); 00165 sprintf(TransmitBuffer,"%s cmd>",time_string); 00166 pc.printf(TransmitBuffer); 00167 00168 while (1) 00169 { 00170 if (getLine() == 1) /* wait until command line complete */ 00171 { 00172 executeCmd(); 00173 sprintf(TransmitBuffer,"%s cmd> ",time_string); 00174 pc.printf("%s",TransmitBuffer); 00175 } 00176 #if 0 00177 // is there anything to send 00178 if(readyToSendI2C) 00179 { 00180 #if 0 00181 data[0] = 0x12; 00182 data[1] = 0x34; 00183 data[2] = 0x56; 00184 myI2CWrite(0,data,3); 00185 myI2CRead(0,data1,3); 00186 sprintf(latestData,"I2c %02X %02X %02X",data1[0], data1[1], data1[2]); 00187 pc.printf("%s\r\n",latestData); 00188 #endif 00189 00190 #if 1 00191 // Send 0x8f, the command to read the WHOAMI register 00192 data[0] = 0x12; 00193 mySPIWrite(0x0A,data,1); 00194 mySPIRead(0x0A,data1,1); 00195 mySPIRead(0x42,data1,2); 00196 // int spiData = spi_rf.write(0x0600 | 0x0000); 00197 sprintf(latestData,"SPI %02X %02X",data1[0], data1[1]); 00198 pc.printf("%s\r\n",latestData); 00199 00200 #endif 00201 send(latestData); 00202 readyToSendI2C = false; 00203 } 00204 if(readyToSendBatteryVoltage) 00205 { 00206 sprintf(latestData,"Voltage %2.2f",(double)lastBatteryVoltage); 00207 pc.printf("%s\r\n",latestData); 00208 send(latestData); 00209 readyToSendBatteryVoltage = false; 00210 } 00211 if(readyToSendKillStatus) 00212 { 00213 sprintf(latestData,"KillStatus %d",lastKillStatus); 00214 pc.printf("%s\r\n",latestData); 00215 send(latestData); 00216 readyToSendKillStatus = false; 00217 } 00218 if(readyToSendPowerOn) 00219 { 00220 sprintf(latestData,"PowerOn %d",lastPowerOn); 00221 pc.printf("%s\r\n",latestData); 00222 send(latestData); 00223 readyToSendPowerOn = false; 00224 } 00225 #endif 00226 } 00227 } 00228 00229 void I2CWrite(uint16 addr, uint8 *data, int length) 00230 { 00231 int i; 00232 char bytes[3]; 00233 00234 for(i=0; i< length; i++,addr++) 00235 { 00236 bytes[0] = addr >> 8; 00237 bytes[1] = addr & 0xFF; 00238 bytes[2] = data[i]; 00239 i2c_mem.write(i2cAddr, bytes, 3); /* write address and one byte */ 00240 while(i2c_mem.write(i2cAddr, bytes, 0) != 0); /* wait till write complete */ 00241 } 00242 return; 00243 } 00244 00245 void I2CRead(uint16 addr, uint8 *data, int length) 00246 { 00247 char bytes[2]; 00248 00249 bytes[0] = addr >> 8; 00250 bytes[1] = addr & 0xFF; 00251 // sprintf(TransmitBuffer,"read I2C %04X %02X %04X\r\n",regAddress, bytes[0], bytes[1]); 00252 // pc.printf(TransmitBuffer); 00253 i2c_mem.write(i2cAddr, bytes, 2,true); /* write address with repeated start */ 00254 i2c_mem.read(i2cAddr, (char *)data, length); /* read all bytes */ 00255 return; 00256 } 00257 00258 00259 void SPIWrite(uint16 addr, uint8 *data, int length) 00260 { 00261 int i; 00262 00263 for(i=0;i<length;i++,addr++) 00264 { 00265 spi_cs = 0; 00266 data[i] = spi_rf.write( (addr << 8) | data[i] | 0x8000); 00267 spi_cs = 1; 00268 } 00269 } 00270 00271 void SPIRead(uint16 addr, uint8 *data, int length) 00272 { 00273 int i; 00274 00275 for(i=0;i<length;i++,addr++) 00276 { 00277 spi_cs = 0; 00278 data[i] = spi_rf.write( (addr << 8) | data[i] | 0x0000); 00279 spi_cs = 1; 00280 } 00281 } 00282 00283 void timeOfDay() 00284 { 00285 epoch++; /* update time of day */ 00286 led_red = !led_red; 00287 // led_green = !led_green; 00288 // led_blue = !led_blue; 00289 readyToSendI2C = true; 00290 } 00291 00292 void batteryRead() 00293 { 00294 lastBatteryVoltage = batteryVoltage * (float)3.3; 00295 readyToSendBatteryVoltage = true; 00296 } 00297 00298 void killStatusRead() 00299 { 00300 lastKillStatus = killStatus; 00301 readyToSendKillStatus = true; 00302 } 00303 00304 void powerOnRead() 00305 { 00306 lastPowerOn = powerOn; 00307 readyToSendPowerOn = true; 00308 } 00309 00310 bool setFrequencySubBand(uint8_t subBand) 00311 { 00312 /* int32_t returnCode; 00313 00314 printf("Setting frequency sub band to '%d'...\r\n", subBand); 00315 if ((returnCode = dot->setFrequencySubBand(subBand)) != mDot::MDOT_OK) { 00316 printError(dot, returnCode); 00317 return false; 00318 } 00319 return true; 00320 */ 00321 } 00322 00323 bool setNetworkName(const std::string name) 00324 { 00325 /* int32_t returnCode; 00326 00327 printf("Setting network name to '%s'...\r\n", name.c_str()); 00328 if ((returnCode = dot->setNetworkName(name)) != mDot::MDOT_OK) 00329 { 00330 printError(dot, returnCode); 00331 return false; 00332 } 00333 return true; 00334 */} 00335 00336 bool setNetworkPassphrase(const std::string passphrase) 00337 { 00338 /* int32_t returnCode; 00339 00340 printf("Setting passphrase to '%s'...\r\n", passphrase.c_str()); 00341 if ((returnCode = dot->setNetworkPassphrase(passphrase)) != mDot::MDOT_OK) 00342 { 00343 printError(dot, returnCode); 00344 return false; 00345 } 00346 return true; 00347 */} 00348 00349 bool setTxDataRate(const int dataRate) 00350 { 00351 /* int32_t returnCode; 00352 00353 printf("Setting TX Spreading factor to %d...\r\n",dataRate); 00354 if ((returnCode = dot->setTxDataRate(dataRate)) != mDot::MDOT_OK) 00355 { 00356 logError("Failed To Set Tx Datarate %d:%s", returnCode, mDot::getReturnCodeString(returnCode).c_str()); 00357 printError(dot, returnCode); 00358 return false; 00359 } 00360 return true; 00361 */} 00362 00363 bool setPower(uint8_t power) 00364 { 00365 /* int32_t returnCode; 00366 printf("Setting tx power to '%d'...\r\n", power); 00367 if ((returnCode = dot->setTxPower(power)) != mDot::MDOT_OK) 00368 { 00369 printError(dot, returnCode); 00370 return false; 00371 } 00372 return true; 00373 */} 00374 00375 bool joinNetwork() 00376 { 00377 /* int32_t returnCode; 00378 printf("\r\nJoining network...\r\n"); 00379 if ((returnCode = dot->joinNetworkOnce()) != mDot::MDOT_OK) 00380 { 00381 printError(dot, returnCode); 00382 return false; 00383 } 00384 printf("Network Joined!\r\n"); 00385 return true; 00386 */} 00387 00388 bool setAck(uint8_t retries) 00389 { 00390 /* int32_t returnCode; 00391 printf("Setting ack to '%d'...\r\n", retries); 00392 if ((returnCode = dot->setAck(retries)) != mDot::MDOT_OK) 00393 { 00394 printError(dot, returnCode); 00395 return false; 00396 } 00397 return true; 00398 */} 00399 00400 bool send(const std::string text) 00401 { 00402 /* int32_t returnCode; 00403 uint32_t timeTillSend = dot->getNextTxMs(); 00404 if (timeTillSend != 0) { 00405 printf("waiting %lu ms to send\r\n", timeTillSend); 00406 return false; 00407 } 00408 00409 printf("Sending data... "); 00410 std::vector<uint8_t> data(text.begin(), text.end()); 00411 if ((returnCode = dot->send(data, 1)) != mDot::MDOT_OK) 00412 { 00413 printError(dot, returnCode); 00414 return false; 00415 } 00416 printf("Data sent!\r\n"); 00417 return true; 00418 */} 00419 00420 void printError(mDot* dot, int32_t returnCode) 00421 { 00422 // std::string error = mDot::getReturnCodeString(returnCode) + " - " + dot->getLastError(); 00423 // printf("%s\r\n", error.c_str()); 00424 } 00425
Generated on Sat Jul 16 2022 13:04:30 by
1.7.2