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:
Mon Jun 29 21:34:20 2015 +0000
Revision:
0:18a657d5431f
Child:
1:bf2afd6890a2
use formula for temperature reading, only update every 5 mins or when changed.; ; (moved from offline git)

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