Nathanaël Semhoun / Mbed OS mdot_commonsense

Dependencies:   libmDot-mbed5

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #define REVISION "1.4"
00002  
00003 #include <string>
00004 #include <vector>
00005 #include <MTSText.h>
00006  
00007 #include <mbed.h>
00008  
00009 #include "DHT.h"
00010 #include "LoRa.h"
00011 #include "Sensors.h"
00012  
00013 #include "Config.h"
00014  
00015 #include "Logging.h"
00016 
00017 LoRa* loRa;
00018 Sensors* sensors;
00019 int forceUpdate;
00020  
00021 struct SensorsVal_t {
00022     float temp;
00023     float humidity; 
00024     bool motion;
00025     float light;
00026     bool switch1;
00027     bool switch2;
00028 } sensorsVal;
00029 
00030 void SendLoraError(char p_idx) {
00031     std::string res;
00032     res += p_idx;
00033     res += "0\n";
00034     loRa->send(res);
00035 }
00036 
00037 void CmdGet(std::string p_line)
00038 {
00039     if (p_line.size() < 3) {
00040         SendLoraError(p_line[0]);
00041         return; // <==
00042     }
00043 
00044     char val[255];
00045 
00046     if (p_line[2] == 'L') {
00047         sprintf(val, "%4.2f", sensors->getLight());
00048     } else if (p_line[2] == 'T') {
00049         sprintf(val, "%4.2f", sensors->getTemp());
00050     } else if (p_line[2] == 'H') {
00051         sprintf(val, "%4.2f", sensors->getHumidity());
00052     } else if (p_line[2] == 'M') {
00053         sprintf(val, "%d", sensors->getMotion() ? 1 : 0);
00054     } else if (p_line[2] == '1') {
00055         sprintf(val, "%d", (sensors->getSwitch1() ? 1 : 0));
00056     } else if (p_line[2] == '2') {
00057         sprintf(val, "%d", (sensors->getSwitch2() ? 1 : 0));
00058     } else if (p_line[2] == 'N') {
00059         mDot::link_check lc = mDot::getInstance()->networkLinkCheck();
00060         sprintf(val, "%d", lc.dBm);
00061     } else  {
00062         SendLoraError(p_line[0]);
00063         return; // <==
00064     }
00065 
00066     string res;
00067     res = p_line[0];
00068     res = "A";
00069     res += p_line[2];
00070     res += val;
00071     res += "\n";
00072     loRa->send(res);
00073 }
00074 
00075 void CmdSet(std::string p_line)
00076 {
00077     if (p_line.size() < 4) {
00078         SendLoraError(p_line[0]);
00079         return; // <==
00080     }
00081 
00082     if (p_line[2] == '1') {
00083         sensors->setSwitch1(p_line[3] != '0');
00084     } else if (p_line[2] == '2') {
00085         sensors->setSwitch2(p_line[3] != '0');
00086     } else {
00087         SendLoraError(p_line[0]);
00088         return; // <==
00089     }
00090 
00091     std::string res;
00092     res += p_line[0];
00093     res += "D\n";
00094     loRa->send(res);
00095 }
00096 
00097 void CmdShow(std::string p_line)
00098 {
00099     if (p_line.size() < 3) {
00100         SendLoraError(p_line[0]);
00101         return; // <==
00102     }
00103 
00104     if (p_line[2] == 'V') {
00105         loRa->send(p_line[0] + std::string(REVISION) + "\n");
00106         return; // <==
00107     }
00108 
00109     SendLoraError(p_line[0]);
00110 }
00111 
00112 void SensorsTimerCb()
00113 {
00114     // Check sensors for change
00115     //
00116     double minDelta = 0.15; // 15%
00117     char buff[LORA_PACKET_MAX_SIZE + 1];
00118     if (forceUpdate <= 0 || sensors->getTemp() > (sensorsVal.temp * (1.0 + minDelta))
00119             || sensors->getTemp() < (sensorsVal.temp * (1.0 - minDelta))) {
00120         sensorsVal.temp = sensors->getTemp();
00121         snprintf(buff, LORA_PACKET_MAX_SIZE, "0PT%4.2f\n", sensorsVal.temp);
00122         loRa->send(buff);
00123     }
00124     if (forceUpdate <= 0 || sensors->getHumidity() > (sensorsVal.humidity * (1.0 + minDelta))
00125             || sensors->getHumidity() < (sensorsVal.humidity * (1.0 - minDelta))) {
00126         sensorsVal.humidity = sensors->getHumidity();
00127         snprintf(buff, LORA_PACKET_MAX_SIZE, "0PH%4.2f\n", sensorsVal.humidity);
00128         loRa->send(buff);
00129     }
00130     if (forceUpdate <= 0 || sensors->getLight() > (sensorsVal.light * (1.0 + minDelta))
00131             || sensors->getLight() < (sensorsVal.light * (1.0 - minDelta))) {
00132         sensorsVal.light = sensors->getLight();
00133         snprintf(buff, LORA_PACKET_MAX_SIZE, "0PL%4.2f\n", sensorsVal.light);
00134         loRa->send(buff);
00135     }
00136     if (forceUpdate <= 0 || sensorsVal.motion != sensors->getMotion()) {
00137         sensorsVal.motion = sensors->getMotion();
00138         snprintf(buff, LORA_PACKET_MAX_SIZE, "0PM%d\n", sensorsVal.motion);
00139         loRa->send(buff);
00140     }
00141     if (forceUpdate <= 0 || sensorsVal.switch1 != sensors->getSwitch1()) {
00142         sensorsVal.switch1 = sensors->getSwitch1();
00143         snprintf(buff, LORA_PACKET_MAX_SIZE, "0P1%d\n", sensorsVal.switch1);
00144         loRa->send(buff);
00145     }
00146     if (forceUpdate <= 0 || sensorsVal.switch2 != sensors->getSwitch2()) {
00147         sensorsVal.switch2 = sensors->getSwitch2();
00148         snprintf(buff, LORA_PACKET_MAX_SIZE, "0P2%d\n", sensorsVal.switch2);
00149        loRa->send(buff);
00150     }
00151     
00152     if (forceUpdate <= 0) {
00153         forceUpdate = ForceUpdatePeriod;
00154     } else {
00155         forceUpdate--;
00156     }
00157 }
00158 
00159 void ReadTimerCb()
00160 {
00161     std::string data = loRa->receive();
00162     if (data == "") {
00163         return; // <==
00164     }
00165 
00166     size_t cursor = 0;
00167     while (true) {
00168         if (cursor == (size_t)-1) return;
00169         std::string line = mts::Text::getLine(data, cursor, cursor);
00170         if (line.size() == 0) return;
00171         if (line.size() < 2) {
00172             return;
00173         } else if (line[1] == 'G') {
00174             CmdGet(line);
00175         } else if (line[1] == 'S') {
00176             CmdSet(line);
00177         } else if (line[1] == 'H') {
00178             CmdShow(line);
00179         } else if (line[1] == 'I') {
00180             std::string res;
00181             res += line[0];
00182             res += "O\n";
00183             loRa->send(res);
00184         } else {
00185             std::string res;
00186             res += line[0];
00187             res += "E\n";
00188             loRa->send(res);
00189         }
00190     }
00191 }
00192 
00193 int main()
00194 {
00195     printf("--STARTING-- %s\n", mDot::getInstance()->getId().c_str());
00196      
00197     sensors = new Sensors(sensorsConfig);
00198     
00199     forceUpdate = 1;
00200 
00201     // Get initial Sensors value
00202     sensorsVal.temp = sensors->getTemp();
00203     sensorsVal.humidity = sensors->getHumidity();
00204     sensorsVal.motion = sensors->getMotion();
00205     sensorsVal.light = sensors->getLight();
00206     sensorsVal.switch1 = sensors->getSwitch1();
00207     sensorsVal.switch2 = sensors->getSwitch2();
00208  
00209     loRa = new LoRa();
00210     loRa->init(LoRaConfig);
00211     loRa->connect();
00212     loRa->send("0S1\n");
00213 
00214     while (true) {
00215         loRa->PeriodicTimer();
00216         sensors->updateValues();
00217         ReadTimerCb();
00218         SensorsTimerCb();
00219         osDelay(1000);
00220     }
00221 }