![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
mDot source code for the "fan" mDot in this demo: https://twitter.com/MultiTechSys/status/621708791009124352
Dependencies: DigitDisplay MTS-Utils libmDot mbed-rtos mbed
mDot source code for the "fan" mDot in this demo: https://twitter.com/MultiTechSys/status/621708791009124352
main.cpp@3:68094a3aa36f, 2015-07-17 (annotated)
- Committer:
- bbayer
- Date:
- Fri Jul 17 17:04:24 2015 +0000
- Revision:
- 3:68094a3aa36f
- Parent:
- 2:756d0baab3fb
doc: add UDK
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bbayer | 0:eaddea2e2d72 | 1 | // --------------------------------------------- |
bbayer | 0:eaddea2e2d72 | 2 | // MultiTech Thermostat & Fan Demo (fan unit) |
bbayer | 0:eaddea2e2d72 | 3 | // |
bbayer | 0:eaddea2e2d72 | 4 | // This program runs on a MultiTech mDot with: |
bbayer | 3:68094a3aa36f | 5 | // - 1 MultiTech UDK |
bbayer | 2:756d0baab3fb | 6 | // - 1 Arduino base shield |
bbayer | 0:eaddea2e2d72 | 7 | // - 1 Grove LED05291P 4-digit display |
bbayer | 0:eaddea2e2d72 | 8 | // - 1 relay for fan control |
bbayer | 0:eaddea2e2d72 | 9 | // --------------------------------------------- |
bbayer | 0:eaddea2e2d72 | 10 | |
bbayer | 0:eaddea2e2d72 | 11 | #include "mbed.h" |
bbayer | 0:eaddea2e2d72 | 12 | #include "mDot.h" |
bbayer | 0:eaddea2e2d72 | 13 | #include "MTSText.h" |
bbayer | 0:eaddea2e2d72 | 14 | #include "DigitDisplay.h" |
bbayer | 0:eaddea2e2d72 | 15 | |
bbayer | 0:eaddea2e2d72 | 16 | mDot* dot; |
bbayer | 0:eaddea2e2d72 | 17 | Ticker ledTick; |
bbayer | 0:eaddea2e2d72 | 18 | Ticker periodicPollTick; |
bbayer | 0:eaddea2e2d72 | 19 | |
bbayer | 0:eaddea2e2d72 | 20 | DigitalOut led(LED1); |
bbayer | 2:756d0baab3fb | 21 | DigitalOut fan(PA_1); //D6 on Arduino base shield |
bbayer | 2:756d0baab3fb | 22 | DigitDisplay display(PB_1, PB_0); //A0 & A1 on Arduino base shield |
bbayer | 0:eaddea2e2d72 | 23 | |
bbayer | 0:eaddea2e2d72 | 24 | // Configuration variables |
bbayer | 2:756d0baab3fb | 25 | static std::string config_network_name = "your_name_here"; |
bbayer | 2:756d0baab3fb | 26 | static std::string config_network_pass = "your_key_here"; |
bbayer | 1:cf83fcba7d02 | 27 | static uint8_t config_frequency_sub_band = 1; |
bbayer | 0:eaddea2e2d72 | 28 | |
bbayer | 0:eaddea2e2d72 | 29 | static volatile bool timeToPollConduit = true; |
bbayer | 0:eaddea2e2d72 | 30 | |
bbayer | 0:eaddea2e2d72 | 31 | //Function prototypes |
bbayer | 0:eaddea2e2d72 | 32 | void printError(mDot* dot, int32_t returnCode); |
bbayer | 0:eaddea2e2d72 | 33 | void printVersion(); |
bbayer | 0:eaddea2e2d72 | 34 | bool setFrequencySubBand(uint8_t subBand); |
bbayer | 0:eaddea2e2d72 | 35 | bool setNetworkName(const std::string name); |
bbayer | 0:eaddea2e2d72 | 36 | bool setNetworkPassphrase(const std::string passphrase); |
bbayer | 0:eaddea2e2d72 | 37 | bool setPower(uint8_t power); |
bbayer | 0:eaddea2e2d72 | 38 | bool setAck(uint8_t retries); |
bbayer | 0:eaddea2e2d72 | 39 | bool joinNetwork(); |
bbayer | 0:eaddea2e2d72 | 40 | bool send(const std::string text); |
bbayer | 0:eaddea2e2d72 | 41 | std::string receive(); |
bbayer | 0:eaddea2e2d72 | 42 | void ledTock(); |
bbayer | 0:eaddea2e2d72 | 43 | void periodicPollTock(); |
bbayer | 0:eaddea2e2d72 | 44 | |
bbayer | 0:eaddea2e2d72 | 45 | |
bbayer | 0:eaddea2e2d72 | 46 | int main() |
bbayer | 0:eaddea2e2d72 | 47 | { |
bbayer | 0:eaddea2e2d72 | 48 | //Start LED startup sequence |
bbayer | 0:eaddea2e2d72 | 49 | ledTick.attach(&ledTock, 0.1); |
bbayer | 0:eaddea2e2d72 | 50 | |
bbayer | 0:eaddea2e2d72 | 51 | printf("\r\n\r\n"); |
bbayer | 0:eaddea2e2d72 | 52 | printf("=====================================\r\n"); |
bbayer | 0:eaddea2e2d72 | 53 | printf("MTS Thermostat/Fan Demo (Fan unit)\r\n"); |
bbayer | 0:eaddea2e2d72 | 54 | printf("=====================================\r\n"); |
bbayer | 0:eaddea2e2d72 | 55 | printVersion(); |
bbayer | 0:eaddea2e2d72 | 56 | |
bbayer | 0:eaddea2e2d72 | 57 | // get the mDot handle |
bbayer | 0:eaddea2e2d72 | 58 | dot = mDot::getInstance(); |
bbayer | 0:eaddea2e2d72 | 59 | |
bbayer | 0:eaddea2e2d72 | 60 | // reset to default config so we know what state we're in |
bbayer | 0:eaddea2e2d72 | 61 | dot->resetNetworkSession(); |
bbayer | 0:eaddea2e2d72 | 62 | dot->resetConfig(); |
bbayer | 0:eaddea2e2d72 | 63 | |
bbayer | 0:eaddea2e2d72 | 64 | // set up the mDot with our network information |
bbayer | 0:eaddea2e2d72 | 65 | setNetworkName(config_network_name); |
bbayer | 0:eaddea2e2d72 | 66 | setNetworkPassphrase(config_network_pass); |
bbayer | 0:eaddea2e2d72 | 67 | setFrequencySubBand(config_frequency_sub_band); |
bbayer | 0:eaddea2e2d72 | 68 | setPower(7); // Reduce latency for 868 units |
bbayer | 0:eaddea2e2d72 | 69 | setAck(0); // Disable ack for less latency |
bbayer | 0:eaddea2e2d72 | 70 | |
bbayer | 0:eaddea2e2d72 | 71 | while (!joinNetwork()) { wait(2); dot->resetNetworkSession(); } |
bbayer | 0:eaddea2e2d72 | 72 | |
bbayer | 0:eaddea2e2d72 | 73 | // Stop LED startup sequence & configure them for operation |
bbayer | 0:eaddea2e2d72 | 74 | ledTick.detach(); |
bbayer | 0:eaddea2e2d72 | 75 | led = 1; |
bbayer | 0:eaddea2e2d72 | 76 | |
bbayer | 0:eaddea2e2d72 | 77 | // Configure timers |
bbayer | 0:eaddea2e2d72 | 78 | periodicPollTick.attach(&periodicPollTock, 2); |
bbayer | 0:eaddea2e2d72 | 79 | |
bbayer | 0:eaddea2e2d72 | 80 | static bool fanState = false; |
bbayer | 0:eaddea2e2d72 | 81 | uint16_t setPoint = 0; |
bbayer | 0:eaddea2e2d72 | 82 | uint16_t temperature = 0; |
bbayer | 0:eaddea2e2d72 | 83 | |
bbayer | 0:eaddea2e2d72 | 84 | while (1) { |
bbayer | 0:eaddea2e2d72 | 85 | if (timeToPollConduit) { |
bbayer | 0:eaddea2e2d72 | 86 | std::string rxData; |
bbayer | 0:eaddea2e2d72 | 87 | |
bbayer | 0:eaddea2e2d72 | 88 | led = 1; |
bbayer | 0:eaddea2e2d72 | 89 | display.setColon(false); |
bbayer | 0:eaddea2e2d72 | 90 | |
bbayer | 0:eaddea2e2d72 | 91 | // Send packet (Only send payload if no data pending because Node-RED queues new packet if payload not empty) |
bbayer | 0:eaddea2e2d72 | 92 | send(dot->getDataPending() ? "" : " "); |
bbayer | 0:eaddea2e2d72 | 93 | // Receive a packet |
bbayer | 0:eaddea2e2d72 | 94 | rxData = receive(); |
bbayer | 0:eaddea2e2d72 | 95 | |
bbayer | 0:eaddea2e2d72 | 96 | if (rxData.size() > 0) { |
bbayer | 0:eaddea2e2d72 | 97 | printf("Rx packet data: %s\r\n", rxData.c_str()); |
bbayer | 0:eaddea2e2d72 | 98 | |
bbayer | 0:eaddea2e2d72 | 99 | // Parse receive data |
bbayer | 0:eaddea2e2d72 | 100 | std::vector<std::string> bytes; |
bbayer | 0:eaddea2e2d72 | 101 | bytes = mts::Text::split(rxData, " "); |
bbayer | 0:eaddea2e2d72 | 102 | temperature = atoi(bytes[0].c_str()); |
bbayer | 0:eaddea2e2d72 | 103 | setPoint = atoi(bytes[1].c_str()); |
bbayer | 0:eaddea2e2d72 | 104 | fanState = atoi(bytes[2].c_str()); |
bbayer | 0:eaddea2e2d72 | 105 | |
bbayer | 0:eaddea2e2d72 | 106 | // Update 4 digit display |
bbayer | 0:eaddea2e2d72 | 107 | display.write(0, temperature / 10); |
bbayer | 0:eaddea2e2d72 | 108 | display.write(1, temperature % 10); |
bbayer | 0:eaddea2e2d72 | 109 | display.write(2, setPoint / 10); |
bbayer | 0:eaddea2e2d72 | 110 | display.write(3, setPoint % 10); |
bbayer | 0:eaddea2e2d72 | 111 | |
bbayer | 0:eaddea2e2d72 | 112 | fan = fanState; |
bbayer | 0:eaddea2e2d72 | 113 | } |
bbayer | 0:eaddea2e2d72 | 114 | else { |
bbayer | 0:eaddea2e2d72 | 115 | printf("Nothing!\r\n"); |
bbayer | 0:eaddea2e2d72 | 116 | } |
bbayer | 0:eaddea2e2d72 | 117 | |
bbayer | 0:eaddea2e2d72 | 118 | timeToPollConduit = false; |
bbayer | 0:eaddea2e2d72 | 119 | display.setColon(true); |
bbayer | 0:eaddea2e2d72 | 120 | led = 0; |
bbayer | 0:eaddea2e2d72 | 121 | } |
bbayer | 0:eaddea2e2d72 | 122 | } |
bbayer | 0:eaddea2e2d72 | 123 | } |
bbayer | 0:eaddea2e2d72 | 124 | |
bbayer | 0:eaddea2e2d72 | 125 | void ledTock() { |
bbayer | 0:eaddea2e2d72 | 126 | led = !led; |
bbayer | 0:eaddea2e2d72 | 127 | } |
bbayer | 0:eaddea2e2d72 | 128 | |
bbayer | 0:eaddea2e2d72 | 129 | void periodicPollTock() { |
bbayer | 0:eaddea2e2d72 | 130 | timeToPollConduit = true; |
bbayer | 0:eaddea2e2d72 | 131 | } |
bbayer | 0:eaddea2e2d72 | 132 | |
bbayer | 0:eaddea2e2d72 | 133 | |
bbayer | 0:eaddea2e2d72 | 134 | void printVersion() |
bbayer | 0:eaddea2e2d72 | 135 | { |
bbayer | 0:eaddea2e2d72 | 136 | printf("%s\r\n\r\n", dot->getId().c_str()); |
bbayer | 0:eaddea2e2d72 | 137 | } |
bbayer | 0:eaddea2e2d72 | 138 | |
bbayer | 0:eaddea2e2d72 | 139 | bool setFrequencySubBand(uint8_t subBand) |
bbayer | 0:eaddea2e2d72 | 140 | { |
bbayer | 0:eaddea2e2d72 | 141 | int32_t returnCode; |
bbayer | 0:eaddea2e2d72 | 142 | printf("Setting frequency sub band to '%d'...\r\n", subBand); |
bbayer | 0:eaddea2e2d72 | 143 | if ((returnCode = dot->setFrequencySubBand(subBand)) != mDot::MDOT_OK) { |
bbayer | 0:eaddea2e2d72 | 144 | printError(dot, returnCode); |
bbayer | 0:eaddea2e2d72 | 145 | return false; |
bbayer | 0:eaddea2e2d72 | 146 | } |
bbayer | 0:eaddea2e2d72 | 147 | return true; |
bbayer | 0:eaddea2e2d72 | 148 | } |
bbayer | 0:eaddea2e2d72 | 149 | |
bbayer | 0:eaddea2e2d72 | 150 | bool setNetworkName(const std::string name) |
bbayer | 0:eaddea2e2d72 | 151 | { |
bbayer | 0:eaddea2e2d72 | 152 | int32_t returnCode; |
bbayer | 0:eaddea2e2d72 | 153 | printf("Setting network name to '%s'...\r\n", name.c_str()); |
bbayer | 0:eaddea2e2d72 | 154 | if ((returnCode = dot->setNetworkName(name)) != mDot::MDOT_OK) |
bbayer | 0:eaddea2e2d72 | 155 | { |
bbayer | 0:eaddea2e2d72 | 156 | printError(dot, returnCode); |
bbayer | 0:eaddea2e2d72 | 157 | return false; |
bbayer | 0:eaddea2e2d72 | 158 | } |
bbayer | 0:eaddea2e2d72 | 159 | return true; |
bbayer | 0:eaddea2e2d72 | 160 | } |
bbayer | 0:eaddea2e2d72 | 161 | |
bbayer | 0:eaddea2e2d72 | 162 | bool setNetworkPassphrase(const std::string passphrase) |
bbayer | 0:eaddea2e2d72 | 163 | { |
bbayer | 0:eaddea2e2d72 | 164 | int32_t returnCode; |
bbayer | 0:eaddea2e2d72 | 165 | printf("Setting passphrase to '%s'...\r\n", passphrase.c_str()); |
bbayer | 0:eaddea2e2d72 | 166 | if ((returnCode = dot->setNetworkPassphrase(passphrase)) != mDot::MDOT_OK) |
bbayer | 0:eaddea2e2d72 | 167 | { |
bbayer | 0:eaddea2e2d72 | 168 | printError(dot, returnCode); |
bbayer | 0:eaddea2e2d72 | 169 | return false; |
bbayer | 0:eaddea2e2d72 | 170 | } |
bbayer | 0:eaddea2e2d72 | 171 | return true; |
bbayer | 0:eaddea2e2d72 | 172 | } |
bbayer | 0:eaddea2e2d72 | 173 | |
bbayer | 0:eaddea2e2d72 | 174 | bool setPower(uint8_t power) |
bbayer | 0:eaddea2e2d72 | 175 | { |
bbayer | 0:eaddea2e2d72 | 176 | int32_t returnCode; |
bbayer | 0:eaddea2e2d72 | 177 | printf("Setting tx power to '%d'...\r\n", power); |
bbayer | 0:eaddea2e2d72 | 178 | if ((returnCode = dot->setTxPower(power)) != mDot::MDOT_OK) { |
bbayer | 0:eaddea2e2d72 | 179 | printError(dot, returnCode); |
bbayer | 0:eaddea2e2d72 | 180 | return false; |
bbayer | 0:eaddea2e2d72 | 181 | } |
bbayer | 0:eaddea2e2d72 | 182 | return true; |
bbayer | 0:eaddea2e2d72 | 183 | } |
bbayer | 0:eaddea2e2d72 | 184 | |
bbayer | 0:eaddea2e2d72 | 185 | |
bbayer | 0:eaddea2e2d72 | 186 | bool joinNetwork() |
bbayer | 0:eaddea2e2d72 | 187 | { |
bbayer | 0:eaddea2e2d72 | 188 | int32_t returnCode; |
bbayer | 0:eaddea2e2d72 | 189 | printf("\r\nJoining network...\r\n"); |
bbayer | 0:eaddea2e2d72 | 190 | if ((returnCode = dot->joinNetworkOnce()) != mDot::MDOT_OK) { |
bbayer | 0:eaddea2e2d72 | 191 | printError(dot, returnCode); |
bbayer | 0:eaddea2e2d72 | 192 | return false; |
bbayer | 0:eaddea2e2d72 | 193 | } |
bbayer | 0:eaddea2e2d72 | 194 | printf("Network Joined!\r\n"); |
bbayer | 0:eaddea2e2d72 | 195 | return true; |
bbayer | 0:eaddea2e2d72 | 196 | } |
bbayer | 0:eaddea2e2d72 | 197 | |
bbayer | 0:eaddea2e2d72 | 198 | bool setAck(uint8_t retries) |
bbayer | 0:eaddea2e2d72 | 199 | { |
bbayer | 0:eaddea2e2d72 | 200 | int32_t returnCode; |
bbayer | 0:eaddea2e2d72 | 201 | printf("Setting ack to '%d'...\r\n", retries); |
bbayer | 0:eaddea2e2d72 | 202 | if ((returnCode = dot->setAck(retries)) != mDot::MDOT_OK) |
bbayer | 0:eaddea2e2d72 | 203 | { |
bbayer | 0:eaddea2e2d72 | 204 | printError(dot, returnCode); |
bbayer | 0:eaddea2e2d72 | 205 | return false; |
bbayer | 0:eaddea2e2d72 | 206 | } |
bbayer | 0:eaddea2e2d72 | 207 | return true; |
bbayer | 0:eaddea2e2d72 | 208 | } |
bbayer | 0:eaddea2e2d72 | 209 | |
bbayer | 0:eaddea2e2d72 | 210 | bool send(const std::string text) |
bbayer | 0:eaddea2e2d72 | 211 | { |
bbayer | 0:eaddea2e2d72 | 212 | int32_t returnCode; |
bbayer | 0:eaddea2e2d72 | 213 | uint32_t timeTillSend = dot->getNextTxMs(); |
bbayer | 0:eaddea2e2d72 | 214 | if (timeTillSend != 0) { |
bbayer | 0:eaddea2e2d72 | 215 | printf("waiting %lu ms to send\r\n", timeTillSend); |
bbayer | 0:eaddea2e2d72 | 216 | return false; |
bbayer | 0:eaddea2e2d72 | 217 | } |
bbayer | 0:eaddea2e2d72 | 218 | |
bbayer | 0:eaddea2e2d72 | 219 | printf("Sending data... "); |
bbayer | 0:eaddea2e2d72 | 220 | std::vector<uint8_t> data(text.begin(), text.end()); |
bbayer | 0:eaddea2e2d72 | 221 | if ((returnCode = dot->send(data, 1)) != mDot::MDOT_OK) |
bbayer | 0:eaddea2e2d72 | 222 | { |
bbayer | 0:eaddea2e2d72 | 223 | printError(dot, returnCode); |
bbayer | 0:eaddea2e2d72 | 224 | return false; |
bbayer | 0:eaddea2e2d72 | 225 | } |
bbayer | 0:eaddea2e2d72 | 226 | printf("Data sent!\r\n"); |
bbayer | 0:eaddea2e2d72 | 227 | return true; |
bbayer | 0:eaddea2e2d72 | 228 | } |
bbayer | 0:eaddea2e2d72 | 229 | |
bbayer | 0:eaddea2e2d72 | 230 | std::string receive() |
bbayer | 0:eaddea2e2d72 | 231 | { |
bbayer | 0:eaddea2e2d72 | 232 | printf("Receiving packet... "); |
bbayer | 0:eaddea2e2d72 | 233 | std::string text; |
bbayer | 0:eaddea2e2d72 | 234 | std::vector<uint8_t> data(text.begin(), text.end()); |
bbayer | 0:eaddea2e2d72 | 235 | data.clear(); |
bbayer | 0:eaddea2e2d72 | 236 | |
bbayer | 0:eaddea2e2d72 | 237 | if (dot->recv(data) == mDot::MDOT_OK) { |
bbayer | 0:eaddea2e2d72 | 238 | if (data.size() > 0) { |
bbayer | 0:eaddea2e2d72 | 239 | return std::string((const char*) &data[0], data.size()); |
bbayer | 0:eaddea2e2d72 | 240 | } |
bbayer | 0:eaddea2e2d72 | 241 | } |
bbayer | 0:eaddea2e2d72 | 242 | return ""; |
bbayer | 0:eaddea2e2d72 | 243 | } |
bbayer | 0:eaddea2e2d72 | 244 | |
bbayer | 0:eaddea2e2d72 | 245 | void printError(mDot* dot, int32_t returnCode) |
bbayer | 0:eaddea2e2d72 | 246 | { |
bbayer | 0:eaddea2e2d72 | 247 | std::string error = mDot::getReturnCodeString(returnCode) + " - " + dot->getLastError(); |
bbayer | 0:eaddea2e2d72 | 248 | printf("%s\r\n", error.c_str()); |
bbayer | 0:eaddea2e2d72 | 249 | } |