Da ka xi tong.

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_Connect_IoTClub_one_wire_4ch_8_24 by wireless sensor

main.cpp

Committer:
Dengjj
Date:
2016-08-24
Revision:
14:0616e4361f0c
Parent:
13:5d050b414f16
Child:
15:04b37f329c84

File content as of revision 14:0616e4361f0c:

#include "mbed.h"
#include "mDot.h"
#include "MTSLog.h"
#include <string>
#include <vector>
#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
 
AnalogPHSensor CH1_PH(PB_1, 0.0, 0.0);

#if (NUM_OF_CH >1)
  AnalogPHSensor CH2_PH(PB_0, 0.0, 0.0);
#endif

#if (NUM_OF_CH >2)
  AnalogPHSensor CH3_PH(PA_5, 0.0, 0.0);
#endif
#if (NUM_OF_CH >3)
  AnalogPHSensor CH4_PH(PA_4, 0.0, 0.0);
#endif

// these options must match the settings on your Conduit
// uncomment the following lines and edit their values to match your configuration
static std::string config_network_name = "chinaiot";
static std::string config_network_pass = "password";
static uint8_t config_frequency_sub_band = 2; 

int main() {
    int32_t ret; 
    mDot* dot;
    std::vector<uint8_t> sendData, recvData;
    float phValue;
    char _header[] = "PH Sensors";
    char sendBuf[11], recvBuf[11];
    int  i, k;
    int send_failed;
    bool alarm_on;
    const uint32_t baud = 1200;
    
    int32_t setBaud(const uint32_t& baud);
    uint32_t getBaud();
 
    // get a mDot handle
    dot = mDot::getInstance();

    // print library version information
    logInfo("version: %s", dot->getId().c_str());

    //*******************************************
    // configuration
    //*******************************************
    // reset to default config so we know what state we're in
    dot->resetConfig();

    dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
#ifndef NO_MULTITECH_GATEWAY
    // set up the mDot with our network information: frequency sub band, network name, and network password
    // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()

    // frequency sub band is only applicable in the 915 (US) frequency band
    // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band
    // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
    logInfo("setting frequency sub band");
    if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
        logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
    }

    logInfo("setting network name");
    if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
        logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
    }

    logInfo("setting network password");
    if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
        logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
    }

    // a higher spreading factor allows for longer range but lower throughput
    // in the 915 (US) frequency band, spreading factors 7 - 10 are available
    // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
    logInfo("setting TX spreading factor");
    if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
        logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
    }

    // request receive confirmation of packets from the gateway
    logInfo("enabling ACKs");
    if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
        logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
    }

    // save this configuration to the mDot's NVM
    logInfo("saving config");
    if (! dot->saveConfig()) {
        logError("failed to save configuration");
    }
    //*******************************************
    // end of configuration
    //*******************************************

    // attempt to join the network
    logInfo("joining network");
    while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
        logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
        // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
        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++ )
               sendData.push_back( _header[i] );
               
    // send the data to the gateway
    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");
    } 
#else
  logInfo("%s", _header);
#endif 
    while (true) {
        send_failed = 0;
        alarm_on = false;
        // Read the PH values
        for(k=1; k<(NUM_OF_CH+1); k++){
           switch(k){
              case 1:  phValue = CH1_PH; break;
            #if (NUM_OF_CH >1)
              case 2:  phValue = CH2_PH; break;
            #endif
            #if (NUM_OF_CH >2)
              case 3:  phValue = CH3_PH; break;
            #endif  
            #if (NUM_OF_CH >3)
              case 4:  phValue = CH4_PH; break;
            #endif
              default: 
                   break;
            }           
            sprintf(sendBuf, "CH%d:%5.4f", k, phValue);
            logInfo("%s", sendBuf);
            
         #ifndef NO_MULTITECH_GATEWAY
            //Send the data to Gateway
            sendData.clear();   
            // probably not the most efficent way to do this
            for( int i=0; i< strlen(sendBuf); i++ )
               sendData.push_back( sendBuf[i] );
        
            // send the data to the gateway
            if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
               logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
               send_failed++;
               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
        } 
        
        //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....");
           if ((ret = dot->joinNetworkOnce()) != mDot::MDOT_OK) {
                logError("Failed to rejoin network!"); // %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
           }else{
                logInfo("Rejoin network successfully!");    
           } 
           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
        osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
     #else
        osDelay(1000);
     #endif
    }

}