Da ka xi tong.
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_Connect_IoTClub_one_wire_4ch_8_24 by
Diff: main.cpp
- Revision:
- 13:5d050b414f16
- Parent:
- 12:b31e43c9fb15
- Child:
- 14:0616e4361f0c
diff -r b31e43c9fb15 -r 5d050b414f16 main.cpp --- a/main.cpp Mon Aug 15 14:48:41 2016 +0000 +++ b/main.cpp Sat Aug 20 11:43:09 2016 +0000 @@ -6,11 +6,16 @@ #include <algorithm> #include "AnalogPH.h" +const char TURNON[] = "ON"; +const char TURNOFF[] = "OFF"; + +DigitalOut Alarm(PA_2); + //defined for mDot SVB debug, comment it if applying for whole system //#define NO_MULTITECH_GATEWAY //Define the number of channels -#define NUM_OF_CH 4 +#define NUM_OF_CH 1 AnalogPHSensor CH0_PH(PB_0, 0.0, 0.0); @@ -34,12 +39,13 @@ int main() { int32_t ret; mDot* dot; - std::vector<uint8_t> data; + std::vector<uint8_t> sendData, recvData; float phValue; char _header[] = "PH Sensors"; - char dataBuf[11]; + char sendBuf[11], recvBuf[11]; int i, k; int send_failed; + bool alarm_on; // get a mDot handle dot = mDot::getInstance(); @@ -107,12 +113,13 @@ osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); } + sendData.clear(); // format data for sending to the gateway for( int i=0; i< strlen(_header); i++ ) - data.push_back( _header[i] ); + sendData.push_back( _header[i] ); // send the data to the gateway - if ((ret = dot->send(data)) != mDot::MDOT_OK) { + if ((ret = dot->send(sendData)) != mDot::MDOT_OK) { logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); } else { logInfo("successfully sent data to gateway"); @@ -122,6 +129,7 @@ #endif while (true) { send_failed = 0; + alarm_on = false; // Read the PH values for(k=0; k<NUM_OF_CH; k++){ switch(k){ @@ -138,27 +146,48 @@ default: break; } - sprintf(dataBuf, "CH%d:%5.4f", k, phValue); - logInfo("%s", dataBuf); + sprintf(sendBuf, "CH%d:%5.4f", k, phValue); + logInfo("%s", sendBuf); #ifndef NO_MULTITECH_GATEWAY //Send the data to Gateway - data.clear(); + sendData.clear(); // probably not the most efficent way to do this - for( int i=0; i< strlen(dataBuf); i++ ) - data.push_back( dataBuf[i] ); + for( int i=0; i< strlen(sendBuf); i++ ) + sendData.push_back( sendBuf[i] ); // send the data to the gateway - if ((ret = dot->send(data)) != mDot::MDOT_OK) { + if ((ret = dot->send(sendData)) != mDot::MDOT_OK) { logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); send_failed++; - osDelay(5000); + osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); } else { logInfo("successfully sent data to gateway"); + for(i=0;i<11;i++)recvBuf[i]=0; //clear recv buffer + recvData.clear(); //clear recv data + if ((ret = dot->recv(recvData)) != mDot::MDOT_OK) { + logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); + } else { + //logInfo("datasize = %d", recvData.size()); + for(i=0; i< recvData.size(); i++ ) + recvBuf[i] = recvData[i]; + logInfo("RECV:%s", recvBuf); + if(0==strcmp(recvBuf, TURNON)) alarm_on = true; + } } + osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); #endif } - //for + + //handle the alarm + if(alarm_on){ + logInfo("Turn on the alarm"); + Alarm = 1; + }else{ + logInfo("Turn off the alarm"); + Alarm = 0; + } + if(send_failed>=3){ // attempt to rejoin the network logInfo("Attemp to rejoin network...."); @@ -167,8 +196,9 @@ }else{ logInfo("Rejoin network successfully!"); } - osDelay(5000); + osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); } + //printf("++++++++++++++++++++++\t"); // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again #ifndef NO_MULTITECH_GATEWAY