Multi-Hackers / Mbed 2 deprecated thermostat_fan_demo--fan

Dependencies:   DigitDisplay MTS-Utils libmDot mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // ---------------------------------------------
00002 // MultiTech Thermostat & Fan Demo (fan unit)
00003 //
00004 // This program runs on a MultiTech mDot with:
00005 //   - 1 MultiTech UDK
00006 //   - 1 Arduino base shield
00007 //   - 1 Grove LED05291P 4-digit display
00008 //   - 1 relay for fan control
00009 // ---------------------------------------------
00010 
00011 #include "mbed.h"
00012 #include "mDot.h"
00013 #include "MTSText.h"
00014 #include "DigitDisplay.h"
00015 
00016 mDot* dot;
00017 Ticker ledTick;
00018 Ticker periodicPollTick;
00019 
00020 DigitalOut led(LED1);
00021 DigitalOut fan(PA_1);             //D6 on Arduino base shield
00022 DigitDisplay display(PB_1, PB_0); //A0 & A1 on Arduino base shield
00023 
00024 // Configuration variables
00025 static std::string config_network_name = "your_name_here";
00026 static std::string config_network_pass = "your_key_here";
00027 static uint8_t config_frequency_sub_band = 1;
00028 
00029 static volatile bool timeToPollConduit = true;
00030 
00031 //Function prototypes
00032 void printError(mDot* dot, int32_t returnCode);
00033 void printVersion();
00034 bool setFrequencySubBand(uint8_t subBand);
00035 bool setNetworkName(const std::string name);
00036 bool setNetworkPassphrase(const std::string passphrase);
00037 bool setPower(uint8_t power);
00038 bool setAck(uint8_t retries);
00039 bool joinNetwork();
00040 bool send(const std::string text);
00041 std::string receive();
00042 void ledTock();
00043 void periodicPollTock();
00044 
00045 
00046 int main()
00047 {
00048     //Start LED startup sequence
00049     ledTick.attach(&ledTock, 0.1);
00050 
00051     printf("\r\n\r\n");
00052     printf("=====================================\r\n");
00053     printf("MTS Thermostat/Fan Demo (Fan unit)\r\n");
00054     printf("=====================================\r\n");
00055     printVersion();
00056 
00057     // get the mDot handle
00058     dot = mDot::getInstance();
00059 
00060     // reset to default config so we know what state we're in
00061     dot->resetNetworkSession();
00062     dot->resetConfig();
00063 
00064     // set up the mDot with our network information
00065     setNetworkName(config_network_name);
00066     setNetworkPassphrase(config_network_pass);
00067     setFrequencySubBand(config_frequency_sub_band);
00068     setPower(7);    // Reduce latency for 868 units
00069     setAck(0);      // Disable ack for less latency
00070 
00071     while (!joinNetwork()) { wait(2); dot->resetNetworkSession();  }
00072 
00073     // Stop LED startup sequence & configure them for operation
00074     ledTick.detach();
00075     led = 1;
00076 
00077     // Configure timers
00078     periodicPollTick.attach(&periodicPollTock, 2);
00079 
00080     static bool fanState = false;
00081     uint16_t setPoint = 0;
00082     uint16_t temperature = 0;
00083 
00084     while (1) {
00085         if (timeToPollConduit) {
00086             std::string rxData;
00087 
00088             led = 1;
00089             display.setColon(false);
00090 
00091             // Send packet (Only send payload if no data pending because Node-RED queues new packet if payload not empty)
00092             send(dot->getDataPending() ? "" : " ");
00093             // Receive a packet
00094             rxData = receive();
00095 
00096             if (rxData.size() > 0) {
00097                 printf("Rx packet data: %s\r\n", rxData.c_str());
00098                 
00099                 // Parse receive data
00100                 std::vector<std::string> bytes;
00101                 bytes = mts::Text::split(rxData, " ");
00102                 temperature = atoi(bytes[0].c_str());
00103                 setPoint = atoi(bytes[1].c_str());
00104                 fanState = atoi(bytes[2].c_str());
00105                 
00106                 // Update 4 digit display
00107                 display.write(0, temperature / 10);
00108                 display.write(1, temperature % 10);
00109                 display.write(2, setPoint / 10);
00110                 display.write(3, setPoint % 10);
00111 
00112                 fan = fanState;
00113             }
00114             else {
00115                 printf("Nothing!\r\n");
00116             }
00117 
00118             timeToPollConduit = false;
00119             display.setColon(true);
00120             led = 0;
00121         }
00122     }
00123 }
00124 
00125 void ledTock() {
00126     led = !led;
00127 }
00128 
00129 void periodicPollTock() {
00130     timeToPollConduit = true;
00131 }
00132 
00133 
00134 void printVersion()
00135 {
00136     printf("%s\r\n\r\n", dot->getId().c_str());
00137 }
00138 
00139 bool setFrequencySubBand(uint8_t subBand)
00140 {
00141     int32_t returnCode;
00142     printf("Setting frequency sub band to '%d'...\r\n", subBand);
00143     if ((returnCode = dot->setFrequencySubBand(subBand)) != mDot::MDOT_OK) {
00144         printError(dot, returnCode);
00145         return false;
00146     }
00147     return true;
00148 }
00149 
00150 bool setNetworkName(const std::string name)
00151 {
00152     int32_t returnCode;
00153     printf("Setting network name to '%s'...\r\n", name.c_str());
00154     if ((returnCode = dot->setNetworkName(name)) != mDot::MDOT_OK)
00155     {
00156         printError(dot, returnCode);
00157         return false;
00158     }
00159     return true;
00160 }
00161 
00162 bool setNetworkPassphrase(const std::string passphrase)
00163 {
00164     int32_t returnCode;
00165     printf("Setting passphrase to '%s'...\r\n", passphrase.c_str());
00166     if ((returnCode = dot->setNetworkPassphrase(passphrase)) != mDot::MDOT_OK)
00167     {
00168         printError(dot, returnCode);
00169         return false;
00170     }
00171     return true;
00172 }
00173 
00174 bool setPower(uint8_t power)
00175 {
00176     int32_t returnCode;
00177     printf("Setting tx power to '%d'...\r\n", power);
00178     if ((returnCode = dot->setTxPower(power)) != mDot::MDOT_OK) {
00179         printError(dot, returnCode);
00180         return false;
00181     }
00182     return true;
00183 }
00184 
00185 
00186 bool joinNetwork()
00187 {
00188     int32_t returnCode;
00189     printf("\r\nJoining network...\r\n");
00190     if ((returnCode = dot->joinNetworkOnce()) != mDot::MDOT_OK) {
00191         printError(dot, returnCode);
00192         return false;
00193     }
00194     printf("Network Joined!\r\n");
00195     return true;
00196 }
00197 
00198 bool setAck(uint8_t retries)
00199 {
00200     int32_t returnCode;
00201     printf("Setting ack to '%d'...\r\n", retries);
00202     if ((returnCode = dot->setAck(retries)) != mDot::MDOT_OK)
00203     {
00204         printError(dot, returnCode);
00205         return false;
00206     }
00207     return true;
00208 }
00209 
00210 bool send(const std::string text)
00211 {
00212     int32_t returnCode;
00213     uint32_t timeTillSend = dot->getNextTxMs();
00214     if (timeTillSend != 0) {
00215         printf("waiting %lu ms to send\r\n", timeTillSend);
00216         return false;
00217     }
00218 
00219     printf("Sending data...  ");
00220     std::vector<uint8_t> data(text.begin(), text.end());
00221     if ((returnCode = dot->send(data, 1)) != mDot::MDOT_OK)
00222     {
00223         printError(dot, returnCode);
00224         return false;
00225     }
00226     printf("Data sent!\r\n");
00227     return true;
00228 }
00229 
00230 std::string receive()
00231 {
00232     printf("Receiving packet...  ");
00233     std::string text;
00234     std::vector<uint8_t> data(text.begin(), text.end());
00235     data.clear();
00236 
00237     if (dot->recv(data) == mDot::MDOT_OK) {
00238         if (data.size() > 0) {
00239             return std::string((const char*) &data[0], data.size());
00240         }
00241     }
00242     return "";
00243 }
00244 
00245 void printError(mDot* dot, int32_t returnCode)
00246 {
00247     std::string error = mDot::getReturnCodeString(returnCode) + " - " + dot->getLastError();
00248     printf("%s\r\n", error.c_str());
00249 }