mDot + SparkFun Moisture Sensor

Dependencies:   libmDot mbed-rtos mbed

Fork of libmDot_sample by MultiTech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mDot.h"
00003 #include <string>
00004 #include <vector>
00005 #include "MTSLog.h"
00006 
00007 // TODO: use config file for these
00008 // these options must match the settings on your Conduit
00009 // uncomment the following lines and edit their values to match your configuration
00010 static std::string config_network_name      = "Schakra_LoRa";
00011 static std::string config_network_pass      = "Schakra_password";
00012 static uint8_t config_frequency_sub_band    = 7;
00013 const uint32_t cSleepPeriodSeconds          = 15;
00014 const uint32_t cLoRaMaxPacketSize           = 246;
00015 const float cSensorStartupSeconds           = 0.1f;
00016 const float cAwakeLedCyclePeriod            = 0.3f;
00017 const uint32_t cMaxJoinRetries              = 5;
00018 const uint32_t cJoinRetryWait               = 5;
00019 
00020 //#define DEVELOPER_BOARD
00021 
00022 #ifdef DEVELOPER_BOARD
00023 // spark fun moisture sensor
00024 const PinName sensorPowerPin    = PA_11;    // D7           
00025 const PinName analogInputPin    = PB_1;     // A0           
00026 
00027 // Optional LED to demonstrate power to device/out of sleep. 
00028 const PinName awakeLedPin       = PA_2;     // D1           
00029 
00030 const int32_t onState = 0;
00031 const int32_t offState = 1;
00032 #else
00033 // spark fun moisture sensor
00034 const PinName sensorPowerPin    = PB_0;     // J3 pin 7     : active high
00035 const PinName analogInputPin    = PB_1;     // J3 pin 8     : 0V-1V
00036                                             // J3 pin 10    : ground
00037 
00038 // Optional LED to demonstrate power to device/out of sleep. 
00039 const PinName awakeLedPin       = PA_5;     // J1 pin 15    : active low
00040                                             // J1 pin 19    : GND
00041 
00042 const int32_t onState = 1;
00043 const int32_t offState = 0;
00044 #endif
00045 
00046 DigitalOut  awakeLed    (awakeLedPin,offState);
00047 DigitalOut  sensorPower (sensorPowerPin, offState);
00048 
00049 
00050 void TurnOnSensor()
00051 {
00052     sensorPower = onState;
00053     wait(cSensorStartupSeconds);
00054 }
00055 
00056 void TurnOffSensor()
00057 {
00058     sensorPower = offState;
00059 #ifdef DEVELOPER_BOARD
00060     wait(cSensorStartupSeconds);
00061 #endif    
00062 }
00063                                           
00064 void awakeLedToggle(uint32_t cycles)
00065 {
00066     for(uint32_t n = 0; n < cycles; n++)
00067     {
00068         awakeLed = onState; 
00069         wait(cAwakeLedCyclePeriod/10);
00070         awakeLed = offState; 
00071         wait(cAwakeLedCyclePeriod/10);
00072     }
00073 }
00074 
00075 int main()
00076 {
00077     int32_t ret;
00078     
00079     AnalogIn    analogValue (analogInputPin);
00080 
00081     
00082     mDot* dot;
00083     std::vector<uint8_t> data;
00084     char buf [cLoRaMaxPacketSize];
00085     
00086     // indicate device out of sleep
00087     awakeLedToggle(1);
00088     
00089     // get a mDot handle
00090     dot = mDot::getInstance();
00091     
00092     // print library version information
00093     logInfo("version: %s", dot->getId().c_str());
00094 
00095     //*******************************************
00096     // configuration
00097     //*******************************************
00098     // reset to default config so we know what state we're in
00099     dot->resetConfig();
00100     
00101     dot->setLogLevel(mts::MTSLog::TRACE_LEVEL);
00102 
00103     // max output power    
00104     dot->setTxPower (20);
00105     
00106     // disable Ack return
00107     dot->setAck(0);
00108 
00109     // set up the mDot with our network information: frequency sub band, network name, and network password
00110     logInfo("setting frequency sub band");
00111     if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK)
00112     {
00113         logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00114     }
00115     logInfo("setting network name");
00116     if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK)
00117     {
00118         logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00119     }
00120     logInfo("setting network password");
00121     if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK)
00122     {
00123         logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00124     }
00125     logInfo("saving config");
00126     if (! dot->saveConfig())
00127     {
00128         logError("failed to save configuration");
00129     }    
00130 
00131     //*******************************************
00132     // end of configuration
00133     //*******************************************
00134 
00135     // join the network
00136     logInfo("joining network");
00137     for(int32_t retry = 0; retry <= cMaxJoinRetries; retry++)
00138     {
00139         if((ret = dot->joinNetwork()) != mDot::MDOT_OK)
00140         {
00141             if(retry >= cMaxJoinRetries)
00142             {
00143                 dot->sleep(cSleepPeriodSeconds);
00144             }
00145             else
00146             {
00147                 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00148                 wait(cJoinRetryWait);
00149             }
00150         }
00151         else
00152         {
00153             logInfo("joined network!!!");
00154             break;
00155         }
00156     }
00157 
00158     awakeLedToggle(2);
00159 
00160     TurnOnSensor();
00161     wait(cSensorStartupSeconds);
00162     float reading = analogValue.read();
00163     TurnOffSensor();
00164 
00165     // format data for sending to the gateway
00166     vector<uint8_t> buffer;
00167     char* p = buf;
00168     sprintf( buf, "Moisture=%f volts", reading);
00169     while( *p != 0 )
00170     {
00171         buffer.push_back( *p);
00172         p++;
00173     }
00174 
00175     // send the data
00176     // ACKs are enabled by default, so we're expecting to get one back
00177     if ((ret = dot->send(buffer)) != mDot::MDOT_OK)
00178     {
00179         logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
00180     }
00181     else
00182     {
00183         logInfo("successfully sent data to gateway");
00184     }
00185 
00186     awakeLedToggle(3);
00187 
00188     // Deep sleep and awake to RTC. Wake is essentially reboot.
00189     dot->sleep(cSleepPeriodSeconds);
00190 
00191     return 0;
00192 }