mDot source code for the "sensor" mDot in this demo: https://twitter.com/MultiTechSys/status/621708791009124352

Dependencies:   PinDetect libmDot mbed-rtos mbed

mDot source code for the "sensor" mDot in this demo: https://twitter.com/MultiTechSys/status/621708791009124352

Committer:
bbayer
Date:
Fri Oct 23 21:24:35 2015 +0000
Revision:
4:d823cd66d8f8
Parent:
3:d55b476ea5e6
Update libmDot to 0.0.9-1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bbayer 0:18a657d5431f 1 // ---------------------------------------------
bbayer 0:18a657d5431f 2 // MultiTech Thermostat & Fan Demo (Sensor unit)
bbayer 0:18a657d5431f 3 //
bbayer 0:18a657d5431f 4 // This program runs on a MultiTech mDot with:
bbayer 3:d55b476ea5e6 5 // - 1 MultiTech UDK
bbayer 2:d12fedc86ea5 6 // - 1 Arduino base shield
bbayer 0:18a657d5431f 7 // - 2 Grove COM08211P push buttons
bbayer 0:18a657d5431f 8 // - 1 Grove SEN23292P v1.2 temperature sensor
bbayer 0:18a657d5431f 9 // ---------------------------------------------
bbayer 0:18a657d5431f 10
bbayer 0:18a657d5431f 11 #include "mbed.h"
bbayer 0:18a657d5431f 12 #include "mDot.h"
bbayer 0:18a657d5431f 13 #include "PinDetect.h"
bbayer 0:18a657d5431f 14
bbayer 0:18a657d5431f 15 mDot* dot;
bbayer 0:18a657d5431f 16 Ticker ledTick;
bbayer 0:18a657d5431f 17 Ticker temperatureMonitorTick;
bbayer 0:18a657d5431f 18 Ticker periodicSendTick;
bbayer 0:18a657d5431f 19
bbayer 2:d12fedc86ea5 20 PinDetect up_button(PB_1); //A0 on Arduino base shield
bbayer 2:d12fedc86ea5 21 PinDetect down_button(PB_0); //A1 on Arduino base shield
bbayer 2:d12fedc86ea5 22 AnalogIn temperatureSensor(PC_1); //A2 on Arduino base shield
bbayer 2:d12fedc86ea5 23 DigitalOut transmitLED(LED1);
bbayer 2:d12fedc86ea5 24 DigitalOut buttonPressLED(PA_1); //D6 on Arduino base shield
bbayer 0:18a657d5431f 25
bbayer 0:18a657d5431f 26 // Configuration variables
bbayer 2:d12fedc86ea5 27 static std::string config_network_name = "your_name_here";
bbayer 2:d12fedc86ea5 28 static std::string config_network_pass = "your_key_here";
bbayer 1:bf2afd6890a2 29 static uint8_t config_frequency_sub_band = 1;
bbayer 0:18a657d5431f 30
bbayer 0:18a657d5431f 31 //Global Variables
bbayer 0:18a657d5431f 32 static uint16_t setPoint = 21; //21 C is standard room temp
bbayer 0:18a657d5431f 33 static volatile bool timeToReadTemperature = true;
bbayer 0:18a657d5431f 34 static volatile bool dataChanged = true;
bbayer 0:18a657d5431f 35 static volatile bool thermostatActivated = false;
bbayer 0:18a657d5431f 36 static uint16_t sentSetPoint;
bbayer 0:18a657d5431f 37
bbayer 0:18a657d5431f 38 //Function prototypes
bbayer 0:18a657d5431f 39 void ledTock();
bbayer 0:18a657d5431f 40 void periodicSendTock();
bbayer 0:18a657d5431f 41 void temperatureMonitorTock();
bbayer 0:18a657d5431f 42 void up_button_callback();
bbayer 0:18a657d5431f 43 void down_button_callback();
bbayer 0:18a657d5431f 44 void printError(mDot* dot, int32_t returnCode);
bbayer 0:18a657d5431f 45 void printVersion();
bbayer 0:18a657d5431f 46 bool setFrequencySubBand(uint8_t subBand);
bbayer 0:18a657d5431f 47 bool setNetworkName(const std::string name);
bbayer 0:18a657d5431f 48 bool setNetworkPassphrase(const std::string passphrase);
bbayer 0:18a657d5431f 49 bool setPower(uint8_t power);
bbayer 0:18a657d5431f 50 bool setAck(uint8_t retries);
bbayer 0:18a657d5431f 51 bool joinNetwork();
bbayer 0:18a657d5431f 52 bool send(const std::string text);
bbayer 0:18a657d5431f 53
bbayer 0:18a657d5431f 54
bbayer 0:18a657d5431f 55 int main()
bbayer 0:18a657d5431f 56 {
bbayer 0:18a657d5431f 57 //Start LED startup sequence
bbayer 0:18a657d5431f 58 ledTick.attach(&ledTock, 0.1);
bbayer 0:18a657d5431f 59
bbayer 0:18a657d5431f 60 printf("\r\n\r\n");
bbayer 0:18a657d5431f 61 printf("=====================================\r\n");
bbayer 0:18a657d5431f 62 printf("MTS Thermostat/Fan Demo (Sensor unit)\r\n");
bbayer 0:18a657d5431f 63 printf("=====================================\r\n");
bbayer 0:18a657d5431f 64 printVersion();
bbayer 0:18a657d5431f 65
bbayer 0:18a657d5431f 66 // get the mDot handle
bbayer 0:18a657d5431f 67 dot = mDot::getInstance();
bbayer 0:18a657d5431f 68
bbayer 0:18a657d5431f 69 // reset to default config so we know what state we're in
bbayer 0:18a657d5431f 70 dot->resetNetworkSession();
bbayer 0:18a657d5431f 71 dot->resetConfig();
bbayer 0:18a657d5431f 72
bbayer 0:18a657d5431f 73 // set up the mDot with our network information
bbayer 0:18a657d5431f 74 setNetworkName(config_network_name);
bbayer 0:18a657d5431f 75 setNetworkPassphrase(config_network_pass);
bbayer 0:18a657d5431f 76 setFrequencySubBand(config_frequency_sub_band);
bbayer 0:18a657d5431f 77 setPower(7); // Reduce latency for 868 units
bbayer 0:18a657d5431f 78 setAck(0); // Disable ack for less latency
bbayer 0:18a657d5431f 79
bbayer 0:18a657d5431f 80 while (!joinNetwork()) { wait(2); dot->resetNetworkSession(); }
bbayer 0:18a657d5431f 81
bbayer 0:18a657d5431f 82 // Stop LED startup sequence & configure them for operation
bbayer 0:18a657d5431f 83 ledTick.detach();
bbayer 0:18a657d5431f 84 transmitLED = 1;
bbayer 0:18a657d5431f 85 buttonPressLED = 1;
bbayer 0:18a657d5431f 86
bbayer 0:18a657d5431f 87 // Configure timers
bbayer 0:18a657d5431f 88 periodicSendTick.attach(&periodicSendTock, 5*60);
bbayer 0:18a657d5431f 89 temperatureMonitorTick.attach(&temperatureMonitorTock, 0.15);
bbayer 0:18a657d5431f 90
bbayer 0:18a657d5431f 91 // Setup Interrupt callback function
bbayer 0:18a657d5431f 92 up_button.attach_deasserted(&up_button_callback);
bbayer 0:18a657d5431f 93 down_button.attach_deasserted(&down_button_callback);
bbayer 0:18a657d5431f 94
bbayer 0:18a657d5431f 95 // Start sampling buttons using interrupts
bbayer 0:18a657d5431f 96 up_button.setSampleFrequency();
bbayer 0:18a657d5431f 97 down_button.setSampleFrequency();
bbayer 0:18a657d5431f 98
bbayer 0:18a657d5431f 99 uint16_t temperature = 0;
bbayer 0:18a657d5431f 100 while (1) {
bbayer 0:18a657d5431f 101 if (timeToReadTemperature) {
bbayer 0:18a657d5431f 102 uint16_t B = 4250; //B value of the thermistor
bbayer 0:18a657d5431f 103 uint16_t analogReading;
bbayer 0:18a657d5431f 104 float resistance;
bbayer 0:18a657d5431f 105
bbayer 0:18a657d5431f 106 analogReading = temperatureSensor.read_u16();
bbayer 0:18a657d5431f 107
bbayer 0:18a657d5431f 108 // get the resistance of the sensor
bbayer 0:18a657d5431f 109 resistance=(65534-analogReading)*10000/analogReading;
bbayer 0:18a657d5431f 110 // convert to temperature via datasheet
bbayer 0:18a657d5431f 111 temperature=1/(log(resistance/10000)/B+1/298.15)-273.15;
bbayer 0:18a657d5431f 112 //printf("read: %d, res: %d, temp: %d,set: %d\r\n", analogReading, (uint16_t)resistance, temperature, setPoint);
bbayer 0:18a657d5431f 113
bbayer 0:18a657d5431f 114 if (!thermostatActivated && temperature > setPoint) {
bbayer 0:18a657d5431f 115 thermostatActivated = true;
bbayer 0:18a657d5431f 116 dataChanged = true;
bbayer 0:18a657d5431f 117 }
bbayer 0:18a657d5431f 118 else if (thermostatActivated && temperature < setPoint) {
bbayer 0:18a657d5431f 119 thermostatActivated = false;
bbayer 0:18a657d5431f 120 dataChanged = true;
bbayer 0:18a657d5431f 121 }
bbayer 0:18a657d5431f 122
bbayer 0:18a657d5431f 123 timeToReadTemperature = false;
bbayer 0:18a657d5431f 124 }
bbayer 0:18a657d5431f 125
bbayer 0:18a657d5431f 126 if (dataChanged) {
bbayer 0:18a657d5431f 127 char latestData[100];
bbayer 0:18a657d5431f 128 transmitLED = 1;
bbayer 0:18a657d5431f 129 sentSetPoint = setPoint;
bbayer 0:18a657d5431f 130 sprintf(latestData, "temp: %d,set: %d", temperature, sentSetPoint);
bbayer 0:18a657d5431f 131 printf("%s\r\n", latestData);
bbayer 0:18a657d5431f 132
bbayer 0:18a657d5431f 133 if (send(latestData)) {
bbayer 0:18a657d5431f 134 dataChanged = false;
bbayer 0:18a657d5431f 135 }
bbayer 0:18a657d5431f 136 transmitLED = 0;
bbayer 0:18a657d5431f 137 }
bbayer 0:18a657d5431f 138 }
bbayer 0:18a657d5431f 139 }
bbayer 0:18a657d5431f 140
bbayer 0:18a657d5431f 141
bbayer 0:18a657d5431f 142 void ledTock() {
bbayer 0:18a657d5431f 143 transmitLED = !transmitLED;
bbayer 0:18a657d5431f 144 buttonPressLED = !buttonPressLED;
bbayer 0:18a657d5431f 145 }
bbayer 0:18a657d5431f 146
bbayer 0:18a657d5431f 147 void periodicSendTock() {
bbayer 0:18a657d5431f 148 dataChanged = true;
bbayer 0:18a657d5431f 149 }
bbayer 0:18a657d5431f 150
bbayer 0:18a657d5431f 151 void temperatureMonitorTock() {
bbayer 0:18a657d5431f 152 timeToReadTemperature = true;
bbayer 0:18a657d5431f 153
bbayer 0:18a657d5431f 154 if (sentSetPoint != setPoint) {
bbayer 0:18a657d5431f 155 dataChanged = true;
bbayer 0:18a657d5431f 156 }
bbayer 0:18a657d5431f 157 }
bbayer 0:18a657d5431f 158
bbayer 0:18a657d5431f 159 void up_button_callback() {
bbayer 0:18a657d5431f 160 if (setPoint < 51) {
bbayer 0:18a657d5431f 161 setPoint += 1;
bbayer 0:18a657d5431f 162 buttonPressLED = !buttonPressLED;
bbayer 0:18a657d5431f 163 dataChanged = true;
bbayer 0:18a657d5431f 164 }
bbayer 0:18a657d5431f 165 }
bbayer 0:18a657d5431f 166
bbayer 0:18a657d5431f 167 void down_button_callback() {
bbayer 0:18a657d5431f 168 if (setPoint > 0) {
bbayer 0:18a657d5431f 169 setPoint -= 1;
bbayer 0:18a657d5431f 170 buttonPressLED = !buttonPressLED;
bbayer 0:18a657d5431f 171 dataChanged = true;
bbayer 0:18a657d5431f 172 }
bbayer 0:18a657d5431f 173 }
bbayer 0:18a657d5431f 174
bbayer 0:18a657d5431f 175
bbayer 0:18a657d5431f 176 void printVersion()
bbayer 0:18a657d5431f 177 {
bbayer 0:18a657d5431f 178 printf("%s\r\n\r\n", dot->getId().c_str());
bbayer 0:18a657d5431f 179 }
bbayer 0:18a657d5431f 180
bbayer 0:18a657d5431f 181 bool setFrequencySubBand(uint8_t subBand)
bbayer 0:18a657d5431f 182 {
bbayer 0:18a657d5431f 183 int32_t returnCode;
bbayer 0:18a657d5431f 184 printf("Setting frequency sub band to '%d'...\r\n", subBand);
bbayer 0:18a657d5431f 185 if ((returnCode = dot->setFrequencySubBand(subBand)) != mDot::MDOT_OK) {
bbayer 0:18a657d5431f 186 printError(dot, returnCode);
bbayer 0:18a657d5431f 187 return false;
bbayer 0:18a657d5431f 188 }
bbayer 0:18a657d5431f 189 return true;
bbayer 0:18a657d5431f 190 }
bbayer 0:18a657d5431f 191
bbayer 0:18a657d5431f 192 bool setNetworkName(const std::string name)
bbayer 0:18a657d5431f 193 {
bbayer 0:18a657d5431f 194 int32_t returnCode;
bbayer 0:18a657d5431f 195 printf("Setting network name to '%s'...\r\n", name.c_str());
bbayer 0:18a657d5431f 196 if ((returnCode = dot->setNetworkName(name)) != mDot::MDOT_OK)
bbayer 0:18a657d5431f 197 {
bbayer 0:18a657d5431f 198 printError(dot, returnCode);
bbayer 0:18a657d5431f 199 return false;
bbayer 0:18a657d5431f 200 }
bbayer 0:18a657d5431f 201 return true;
bbayer 0:18a657d5431f 202 }
bbayer 0:18a657d5431f 203
bbayer 0:18a657d5431f 204 bool setNetworkPassphrase(const std::string passphrase)
bbayer 0:18a657d5431f 205 {
bbayer 0:18a657d5431f 206 int32_t returnCode;
bbayer 0:18a657d5431f 207 printf("Setting passphrase to '%s'...\r\n", passphrase.c_str());
bbayer 0:18a657d5431f 208 if ((returnCode = dot->setNetworkPassphrase(passphrase)) != mDot::MDOT_OK)
bbayer 0:18a657d5431f 209 {
bbayer 0:18a657d5431f 210 printError(dot, returnCode);
bbayer 0:18a657d5431f 211 return false;
bbayer 0:18a657d5431f 212 }
bbayer 0:18a657d5431f 213 return true;
bbayer 0:18a657d5431f 214 }
bbayer 0:18a657d5431f 215
bbayer 0:18a657d5431f 216 bool setPower(uint8_t power)
bbayer 0:18a657d5431f 217 {
bbayer 0:18a657d5431f 218 int32_t returnCode;
bbayer 0:18a657d5431f 219 printf("Setting tx power to '%d'...\r\n", power);
bbayer 0:18a657d5431f 220 if ((returnCode = dot->setTxPower(power)) != mDot::MDOT_OK) {
bbayer 0:18a657d5431f 221 printError(dot, returnCode);
bbayer 0:18a657d5431f 222 return false;
bbayer 0:18a657d5431f 223 }
bbayer 0:18a657d5431f 224 return true;
bbayer 0:18a657d5431f 225 }
bbayer 0:18a657d5431f 226
bbayer 0:18a657d5431f 227
bbayer 0:18a657d5431f 228 bool joinNetwork()
bbayer 0:18a657d5431f 229 {
bbayer 0:18a657d5431f 230 int32_t returnCode;
bbayer 0:18a657d5431f 231 printf("\r\nJoining network...\r\n");
bbayer 0:18a657d5431f 232 if ((returnCode = dot->joinNetworkOnce()) != mDot::MDOT_OK) {
bbayer 0:18a657d5431f 233 printError(dot, returnCode);
bbayer 0:18a657d5431f 234 return false;
bbayer 0:18a657d5431f 235 }
bbayer 0:18a657d5431f 236 printf("Network Joined!\r\n");
bbayer 0:18a657d5431f 237 return true;
bbayer 0:18a657d5431f 238 }
bbayer 0:18a657d5431f 239
bbayer 0:18a657d5431f 240 bool setAck(uint8_t retries)
bbayer 0:18a657d5431f 241 {
bbayer 0:18a657d5431f 242 int32_t returnCode;
bbayer 0:18a657d5431f 243 printf("Setting ack to '%d'...\r\n", retries);
bbayer 0:18a657d5431f 244 if ((returnCode = dot->setAck(retries)) != mDot::MDOT_OK)
bbayer 0:18a657d5431f 245 {
bbayer 0:18a657d5431f 246 printError(dot, returnCode);
bbayer 0:18a657d5431f 247 return false;
bbayer 0:18a657d5431f 248 }
bbayer 0:18a657d5431f 249 return true;
bbayer 0:18a657d5431f 250 }
bbayer 0:18a657d5431f 251
bbayer 0:18a657d5431f 252 bool send(const std::string text)
bbayer 0:18a657d5431f 253 {
bbayer 0:18a657d5431f 254 int32_t returnCode;
bbayer 0:18a657d5431f 255 uint32_t timeTillSend = dot->getNextTxMs();
bbayer 0:18a657d5431f 256 if (timeTillSend != 0) {
bbayer 0:18a657d5431f 257 printf("waiting %lu ms to send\r\n", timeTillSend);
bbayer 0:18a657d5431f 258 return false;
bbayer 0:18a657d5431f 259 }
bbayer 0:18a657d5431f 260
bbayer 0:18a657d5431f 261 printf("Sending data... ");
bbayer 0:18a657d5431f 262 std::vector<uint8_t> data(text.begin(), text.end());
bbayer 0:18a657d5431f 263 if ((returnCode = dot->send(data, 1)) != mDot::MDOT_OK)
bbayer 0:18a657d5431f 264 {
bbayer 0:18a657d5431f 265 printError(dot, returnCode);
bbayer 0:18a657d5431f 266 return false;
bbayer 0:18a657d5431f 267 }
bbayer 0:18a657d5431f 268 printf("Data sent!\r\n");
bbayer 0:18a657d5431f 269 return true;
bbayer 0:18a657d5431f 270 }
bbayer 0:18a657d5431f 271
bbayer 0:18a657d5431f 272 void printError(mDot* dot, int32_t returnCode)
bbayer 0:18a657d5431f 273 {
bbayer 0:18a657d5431f 274 std::string error = mDot::getReturnCodeString(returnCode) + " - " + dot->getLastError();
bbayer 0:18a657d5431f 275 printf("%s\r\n", error.c_str());
bbayer 0:18a657d5431f 276 }
bbayer 0:18a657d5431f 277