mDot + SparkFun Moisture Sensor

Dependencies:   libmDot mbed-rtos mbed

Fork of libmDot_sample by MultiTech

Committer:
jepickett
Date:
Fri Oct 16 17:12:47 2015 +0000
Revision:
4:dd8dc04d561a
Parent:
2:6e2c378339d9
initial commit of SparkFun moisture sensor on mDot

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:09250cd371d2 1 #include "mbed.h"
mfiore 0:09250cd371d2 2 #include "mDot.h"
mfiore 0:09250cd371d2 3 #include <string>
mfiore 0:09250cd371d2 4 #include <vector>
mfiore 2:6e2c378339d9 5 #include "MTSLog.h"
mfiore 0:09250cd371d2 6
jepickett 4:dd8dc04d561a 7 // TODO: use config file for these
mfiore 2:6e2c378339d9 8 // these options must match the settings on your Conduit
mfiore 2:6e2c378339d9 9 // uncomment the following lines and edit their values to match your configuration
jepickett 4:dd8dc04d561a 10 static std::string config_network_name = "Schakra_LoRa";
jepickett 4:dd8dc04d561a 11 static std::string config_network_pass = "Schakra_password";
jepickett 4:dd8dc04d561a 12 static uint8_t config_frequency_sub_band = 7;
jepickett 4:dd8dc04d561a 13 const uint32_t cSleepPeriodSeconds = 15;
jepickett 4:dd8dc04d561a 14 const uint32_t cLoRaMaxPacketSize = 246;
jepickett 4:dd8dc04d561a 15 const float cSensorStartupSeconds = 0.1f;
jepickett 4:dd8dc04d561a 16 const float cAwakeLedCyclePeriod = 0.3f;
jepickett 4:dd8dc04d561a 17 const uint32_t cMaxJoinRetries = 5;
jepickett 4:dd8dc04d561a 18 const uint32_t cJoinRetryWait = 5;
jepickett 4:dd8dc04d561a 19
jepickett 4:dd8dc04d561a 20 //#define DEVELOPER_BOARD
jepickett 4:dd8dc04d561a 21
jepickett 4:dd8dc04d561a 22 #ifdef DEVELOPER_BOARD
jepickett 4:dd8dc04d561a 23 // spark fun moisture sensor
jepickett 4:dd8dc04d561a 24 const PinName sensorPowerPin = PA_11; // D7
jepickett 4:dd8dc04d561a 25 const PinName analogInputPin = PB_1; // A0
jepickett 4:dd8dc04d561a 26
jepickett 4:dd8dc04d561a 27 // Optional LED to demonstrate power to device/out of sleep.
jepickett 4:dd8dc04d561a 28 const PinName awakeLedPin = PA_2; // D1
jepickett 4:dd8dc04d561a 29
jepickett 4:dd8dc04d561a 30 const int32_t onState = 0;
jepickett 4:dd8dc04d561a 31 const int32_t offState = 1;
jepickett 4:dd8dc04d561a 32 #else
jepickett 4:dd8dc04d561a 33 // spark fun moisture sensor
jepickett 4:dd8dc04d561a 34 const PinName sensorPowerPin = PB_0; // J3 pin 7 : active high
jepickett 4:dd8dc04d561a 35 const PinName analogInputPin = PB_1; // J3 pin 8 : 0V-1V
jepickett 4:dd8dc04d561a 36 // J3 pin 10 : ground
jepickett 4:dd8dc04d561a 37
jepickett 4:dd8dc04d561a 38 // Optional LED to demonstrate power to device/out of sleep.
jepickett 4:dd8dc04d561a 39 const PinName awakeLedPin = PA_5; // J1 pin 15 : active low
jepickett 4:dd8dc04d561a 40 // J1 pin 19 : GND
jepickett 4:dd8dc04d561a 41
jepickett 4:dd8dc04d561a 42 const int32_t onState = 1;
jepickett 4:dd8dc04d561a 43 const int32_t offState = 0;
jepickett 4:dd8dc04d561a 44 #endif
mfiore 0:09250cd371d2 45
jepickett 4:dd8dc04d561a 46 DigitalOut awakeLed (awakeLedPin,offState);
jepickett 4:dd8dc04d561a 47 DigitalOut sensorPower (sensorPowerPin, offState);
jepickett 4:dd8dc04d561a 48
jepickett 4:dd8dc04d561a 49
jepickett 4:dd8dc04d561a 50 void TurnOnSensor()
jepickett 4:dd8dc04d561a 51 {
jepickett 4:dd8dc04d561a 52 sensorPower = onState;
jepickett 4:dd8dc04d561a 53 wait(cSensorStartupSeconds);
jepickett 4:dd8dc04d561a 54 }
jepickett 4:dd8dc04d561a 55
jepickett 4:dd8dc04d561a 56 void TurnOffSensor()
jepickett 4:dd8dc04d561a 57 {
jepickett 4:dd8dc04d561a 58 sensorPower = offState;
jepickett 4:dd8dc04d561a 59 #ifdef DEVELOPER_BOARD
jepickett 4:dd8dc04d561a 60 wait(cSensorStartupSeconds);
jepickett 4:dd8dc04d561a 61 #endif
jepickett 4:dd8dc04d561a 62 }
jepickett 4:dd8dc04d561a 63
jepickett 4:dd8dc04d561a 64 void awakeLedToggle(uint32_t cycles)
jepickett 4:dd8dc04d561a 65 {
jepickett 4:dd8dc04d561a 66 for(uint32_t n = 0; n < cycles; n++)
jepickett 4:dd8dc04d561a 67 {
jepickett 4:dd8dc04d561a 68 awakeLed = onState;
jepickett 4:dd8dc04d561a 69 wait(cAwakeLedCyclePeriod/10);
jepickett 4:dd8dc04d561a 70 awakeLed = offState;
jepickett 4:dd8dc04d561a 71 wait(cAwakeLedCyclePeriod/10);
jepickett 4:dd8dc04d561a 72 }
jepickett 4:dd8dc04d561a 73 }
jepickett 4:dd8dc04d561a 74
jepickett 4:dd8dc04d561a 75 int main()
jepickett 4:dd8dc04d561a 76 {
mfiore 0:09250cd371d2 77 int32_t ret;
jepickett 4:dd8dc04d561a 78
jepickett 4:dd8dc04d561a 79 AnalogIn analogValue (analogInputPin);
jepickett 4:dd8dc04d561a 80
jepickett 4:dd8dc04d561a 81
mfiore 0:09250cd371d2 82 mDot* dot;
mfiore 0:09250cd371d2 83 std::vector<uint8_t> data;
jepickett 4:dd8dc04d561a 84 char buf [cLoRaMaxPacketSize];
jepickett 4:dd8dc04d561a 85
jepickett 4:dd8dc04d561a 86 // indicate device out of sleep
jepickett 4:dd8dc04d561a 87 awakeLedToggle(1);
mfiore 2:6e2c378339d9 88
mfiore 0:09250cd371d2 89 // get a mDot handle
mfiore 0:09250cd371d2 90 dot = mDot::getInstance();
mfiore 2:6e2c378339d9 91
mfiore 2:6e2c378339d9 92 // print library version information
mfiore 2:6e2c378339d9 93 logInfo("version: %s", dot->getId().c_str());
mfiore 0:09250cd371d2 94
mfiore 2:6e2c378339d9 95 //*******************************************
mfiore 2:6e2c378339d9 96 // configuration
mfiore 2:6e2c378339d9 97 //*******************************************
mfiore 0:09250cd371d2 98 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 99 dot->resetConfig();
mfiore 0:09250cd371d2 100
mfiore 2:6e2c378339d9 101 dot->setLogLevel(mts::MTSLog::TRACE_LEVEL);
mfiore 0:09250cd371d2 102
jepickett 4:dd8dc04d561a 103 // max output power
jepickett 4:dd8dc04d561a 104 dot->setTxPower (20);
jepickett 4:dd8dc04d561a 105
jepickett 4:dd8dc04d561a 106 // disable Ack return
jepickett 4:dd8dc04d561a 107 dot->setAck(0);
jepickett 4:dd8dc04d561a 108
mfiore 2:6e2c378339d9 109 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 110 logInfo("setting frequency sub band");
jepickett 4:dd8dc04d561a 111 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK)
jepickett 4:dd8dc04d561a 112 {
mfiore 2:6e2c378339d9 113 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 114 }
mfiore 2:6e2c378339d9 115 logInfo("setting network name");
jepickett 4:dd8dc04d561a 116 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK)
jepickett 4:dd8dc04d561a 117 {
mfiore 2:6e2c378339d9 118 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 119 }
mfiore 2:6e2c378339d9 120 logInfo("setting network password");
jepickett 4:dd8dc04d561a 121 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK)
jepickett 4:dd8dc04d561a 122 {
mfiore 2:6e2c378339d9 123 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 124 }
mfiore 2:6e2c378339d9 125 logInfo("saving config");
jepickett 4:dd8dc04d561a 126 if (! dot->saveConfig())
jepickett 4:dd8dc04d561a 127 {
mfiore 2:6e2c378339d9 128 logError("failed to save configuration");
jepickett 4:dd8dc04d561a 129 }
jepickett 4:dd8dc04d561a 130
mfiore 2:6e2c378339d9 131 //*******************************************
mfiore 2:6e2c378339d9 132 // end of configuration
mfiore 2:6e2c378339d9 133 //*******************************************
mfiore 0:09250cd371d2 134
jepickett 4:dd8dc04d561a 135 // join the network
mfiore 2:6e2c378339d9 136 logInfo("joining network");
jepickett 4:dd8dc04d561a 137 for(int32_t retry = 0; retry <= cMaxJoinRetries; retry++)
jepickett 4:dd8dc04d561a 138 {
jepickett 4:dd8dc04d561a 139 if((ret = dot->joinNetwork()) != mDot::MDOT_OK)
jepickett 4:dd8dc04d561a 140 {
jepickett 4:dd8dc04d561a 141 if(retry >= cMaxJoinRetries)
jepickett 4:dd8dc04d561a 142 {
jepickett 4:dd8dc04d561a 143 dot->sleep(cSleepPeriodSeconds);
jepickett 4:dd8dc04d561a 144 }
jepickett 4:dd8dc04d561a 145 else
jepickett 4:dd8dc04d561a 146 {
jepickett 4:dd8dc04d561a 147 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
jepickett 4:dd8dc04d561a 148 wait(cJoinRetryWait);
jepickett 4:dd8dc04d561a 149 }
jepickett 4:dd8dc04d561a 150 }
jepickett 4:dd8dc04d561a 151 else
jepickett 4:dd8dc04d561a 152 {
jepickett 4:dd8dc04d561a 153 logInfo("joined network!!!");
jepickett 4:dd8dc04d561a 154 break;
jepickett 4:dd8dc04d561a 155 }
mfiore 0:09250cd371d2 156 }
mfiore 0:09250cd371d2 157
jepickett 4:dd8dc04d561a 158 awakeLedToggle(2);
jepickett 4:dd8dc04d561a 159
jepickett 4:dd8dc04d561a 160 TurnOnSensor();
jepickett 4:dd8dc04d561a 161 wait(cSensorStartupSeconds);
jepickett 4:dd8dc04d561a 162 float reading = analogValue.read();
jepickett 4:dd8dc04d561a 163 TurnOffSensor();
jepickett 4:dd8dc04d561a 164
mfiore 0:09250cd371d2 165 // format data for sending to the gateway
jepickett 4:dd8dc04d561a 166 vector<uint8_t> buffer;
jepickett 4:dd8dc04d561a 167 char* p = buf;
jepickett 4:dd8dc04d561a 168 sprintf( buf, "Moisture=%f volts", reading);
jepickett 4:dd8dc04d561a 169 while( *p != 0 )
jepickett 4:dd8dc04d561a 170 {
jepickett 4:dd8dc04d561a 171 buffer.push_back( *p);
jepickett 4:dd8dc04d561a 172 p++;
jepickett 4:dd8dc04d561a 173 }
mfiore 0:09250cd371d2 174
jepickett 4:dd8dc04d561a 175 // send the data
jepickett 4:dd8dc04d561a 176 // ACKs are enabled by default, so we're expecting to get one back
jepickett 4:dd8dc04d561a 177 if ((ret = dot->send(buffer)) != mDot::MDOT_OK)
jepickett 4:dd8dc04d561a 178 {
jepickett 4:dd8dc04d561a 179 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
jepickett 4:dd8dc04d561a 180 }
jepickett 4:dd8dc04d561a 181 else
jepickett 4:dd8dc04d561a 182 {
jepickett 4:dd8dc04d561a 183 logInfo("successfully sent data to gateway");
jepickett 4:dd8dc04d561a 184 }
mfiore 0:09250cd371d2 185
jepickett 4:dd8dc04d561a 186 awakeLedToggle(3);
jepickett 4:dd8dc04d561a 187
jepickett 4:dd8dc04d561a 188 // Deep sleep and awake to RTC. Wake is essentially reboot.
jepickett 4:dd8dc04d561a 189 dot->sleep(cSleepPeriodSeconds);
mfiore 0:09250cd371d2 190
mfiore 0:09250cd371d2 191 return 0;
mfiore 0:09250cd371d2 192 }