This program connects to the The Things Network backend in OTAA Mode. It logs sensor values from a BME 280 to the backend. Tried adding support for Grove GPS using SerialGPS library but it is not working - conflicting with mbed-rtos, so it commented. Deep Sleep for mDot implemented BUT avoiding reprogramming of the mDot config is NOT working.

Dependencies:   BME280 SerialGPS libmDot mbed-rtos mbed

main.cpp

Committer:
AshuJoshi
Date:
2016-07-07
Revision:
4:97f9ad3f2566
Parent:
3:5c2bcba214b5
Child:
5:4bc6ba66f28e

File content as of revision 4:97f9ad3f2566:

/******************************************************
 * A Program to interface the Grove Base Shielf V2
 * to the mDot UDK.
 * Additionally sample code to compress the data
 * for use with LPWANs such as LoRa
*****************************************************/
 
 #include "mbed.h"
 #include <math.h>
 #include <string>
 #include "mDot.h"
 #include "MTSLog.h"
 #include "MTSText.h"
 #include "BME280.h"

 
using namespace mts;
 
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
 
// mDot UDK Specific
// MDot Pinout: https://developer.mbed.org/platforms/MTS-mDot-F411/#pinout-diagram
// Uncomment this line if using a full sized UDK2.0 instead of a Micro UDK

#define UDK2 1
#ifdef UDK2
DigitalOut led(LED1);
#else
DigitalOut led(XBEE_RSSI);
#endif

//BME280 sensor(I2C_SDA, I2C_SCL)
// MDot UDK - I2C_SDA and I2C_SCL connected to PC_9/PA_*
BME280 b280(PC_9, PA_8);


// Globals
Ticker tick;
mDot* dot;


// Function Declarations
void endLessTestLoop();
void setUpLEDBlink();
void blink();
void readandprintBME280();
void mDotConfig();
void mDotGotoDeepSleep(int seconds);
void mDotConfigPrint();
void initSerialGPS();



/*****************************************************
*                MAIN
*****************************************************/
int main(){

    // Simple Test Functions, "Hello World on UDK
    setUpLEDBlink();
    mDotConfig();
    mDotConfigPrint();

    endLessTestLoop();
    
    return 0;
}

/*****************************************************
 *         mDot Functions
 ****************************************************/


void mDotConfig() {
    // get a mDot handle
    dot = mDot::getInstance();
    //dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
    dot->setLogLevel(mts::MTSLog::TRACE_LEVEL);
    // print library version information
    logInfo("version: %s", dot->getId().c_str());
}

void mDotGotoDeepSleep(int seconds) {
    // logInfo("input to sleep routine %d", seconds);
    // Should  sleep here and wakeup after a set interval.
    uint32_t sleep_time = MAX((dot->getNextTxMs() / 1000), seconds);
    logInfo("going to sleep for %d seconds", sleep_time);
    // go to sleep and wake up automatically sleep_time seconds later
    //dot->sleep(sleep_time, mDot::RTC_ALARM, false);
    dot->sleep(sleep_time, mDot::RTC_ALARM);

}

void mDotConfigPrint() {

    // Display what is set
    printf("\r\n");
    printf(" **********  mDot Configuration ************ \n");
    std::vector<uint8_t> tmp = dot->getNetworkSessionKey();
    printf("Network Session Key: ");
    printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
 
    tmp = dot->getDataSessionKey();
    printf("Data Session Key: ");
    printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
    
    // App EUI
    tmp = dot->getNetworkId();
    printf("App EUI: ");
    printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
        
    // App Key
    tmp = dot->getNetworkKey();
    printf("App Key: ");
    printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());    
    
 
    printf("Device ID ");
    std::vector<uint8_t> deviceId;
    deviceId = dot->getDeviceId();
    for (std::vector<uint8_t>::iterator it = deviceId.begin() ; it != deviceId.end(); ++it)
        printf("%2.2x",*it );
    printf("\n");
 
    std::vector<uint8_t> netAddress;
 
    printf("Network Address ");
    netAddress = dot->getNetworkAddress();
    for (std::vector<uint8_t>::iterator it = netAddress.begin() ; it != netAddress.end(); ++it)
        printf("%2.2x",*it );
 
    printf("\n");
 
    // Display LoRa parameters
    // Display label and values in different colours, show pretty values not numeric values where applicable
    printf("Public Network: %s\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") );
    printf("Frequency: %s\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() );
    printf("Sub Band: %s\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() );
    printf("Join Mode: %s\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() );
    printf("Join Retries: %d\n", dot->getJoinRetries() );
    printf("Join Byte Order: %s\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") );
    printf("Link Check Count: %d\n", dot->getLinkCheckCount() );
    printf("Link Check Thold: %d\n", dot->getLinkCheckThreshold() );
    printf("Tx Data Rate: %s\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() );
    printf("Tx Power: %d\n", dot->getTxPower() );
    printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" ));
    printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") );
    printf("Ack: %s\n", (dot->getAck() ? "Y" : "N")  );



}



/*****************************************************
 *         Sensor Functions
 ****************************************************/

void readandprintBME280() {
    float temperature;
    float pressure;
    float humidity;
    char string_buffer[64];
    time_t secs;
    
    secs = time(NULL);
    printf("Seconds since January 1, 1970: %d\n", secs);
    printf("Time as a basic string = %s", ctime(&secs));
    
    // Temperature
    temperature = b280.getTemperature();
    sprintf(string_buffer, "%s%3.2f", "TC:", temperature);
    logInfo("The temperature is %s", string_buffer);
    // Pressure
    pressure = b280.getPressure();
    sprintf(string_buffer, "%s%04.2f", "hPa:", pressure);
    logInfo("The pressure is %s", string_buffer);
    // Humidity
    humidity = b280.getHumidity();
    sprintf(string_buffer, "%s%03.2f", "H%:", humidity);
    logInfo("The humidty is %s", string_buffer);
    
    // printf("%2.2f degC, %04.2f hPa, %2.2f %%\n", temperature, pressure, humidity);
}



/*****************************************************
 *         FUNCTIONS for Simple Testing
 ****************************************************/

void setUpLEDBlink(){
    // configure the Ticker to blink the LED on 500ms interval
    tick.attach(&blink, 0.5);
}

void endLessTestLoop() {
    while(true) {
        // printf("Hello world!\r\n");
        printf("BME280 Sensor: \n");
        readandprintBME280();
        
        mDotGotoDeepSleep(60);
        //wait(5);
       
    }
}

// Callback function to change LED state
void blink() {
    led = !led;
}