Demo of DHT11->mDot->TTN

Dependencies:   DHT22 DS18B20_1wire SHTx TSL2561_I2C libmDot mbed-rtos mbed

Fork of mDot_TTN_DHT11 by Chris Merck

main.cpp

Committer:
SomeRandomBloke
Date:
2015-09-21
Revision:
2:9db840d12557
Parent:
1:45cec6aea002
Child:
3:367aa95f9771

File content as of revision 2:9db840d12557:

/** mDot_DS18B20 - Simple mDot temperature sensor using Dallas Semiconductors DS18B20 OneWire temperature sensor.
 *
 * Uses MultiTech mDot developer board http://www.multitech.com/models/94558010LF
 * Requires a MultiTech MultiConnect Conduit http://www.multitech.com/models/94557203LF
 *
 * Example JSON received on Conduit:
 { "chan": 5, "codr": "4/5", "datr": "SF9BW125", "freq": "869.5",
 "lsnr": "8.8", "modu": "LORA", "rfch": 1, "rssi": -41, "seqn": 13,
 "size": 12, "timestamp": "2015-07-22T21:19:11Z", "tmst": 517590990,
 "payload": "{\"tmp\":21.3}", "eui": "00:80:00:00:00:00:9a:63", "_msg
 id": "73bcd8dd.8c4328" }
 *
 */

#include "mbed.h"
#include "DS1820.h"
#include "mDot.h"
#include "MTSLog.h"
#include "MTSText.h"
#include <string>
#include <vector>

using namespace mts;

// these options must match the settings on your Conduit in
// /var/config/lora/lora-network-server.conf
static std::string config_network_name = "ThingInnovations";
static std::string config_network_pass = "donkey123";
//static uint8_t config_frequency_sub_band = 1;

// mDot/dev board activity LED
//#define ACTIVITY_LED PA_0

// DS18B20 OneWire pin
// D13 on Dev Board, pin x on mDot
#define DATA_PIN     PA_5
// A0 on Dev Board, pin x on mDot
//#define DATA_PIN     PB_1

DS1820 probe(DATA_PIN);

//void log_error(mDot* dot, const char* msg, int32_t retval);

Serial pc(USBTX,USBRX);


int main()
{
    int32_t ret;
    mDot* dot;
    std::vector<uint8_t> send_data;
    std::vector<uint8_t> recv_data;

    float temperature = 0.0;

    pc.baud(115200);
    pc.printf("mDot LoRa Temperature sensor\n\r");

    // get a mDot handle
    dot = mDot::getInstance();

    dot->setLogLevel(MTSLog::TRACE_LEVEL);

    logInfo("Checking Config");

    // Test if we've already saved the config
    std::string configNetworkName = dot->getNetworkName();

    // Check pin, if low then reset config.
//       if ((ret = dot->setJoinMode( mDot::AUTO_OTA )) != mDot::MDOT_OK) {
//            logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
//        }


    if( config_network_name.compare(configNetworkName) != 0 ) {
        // Not saved config, reset
        logInfo("Setting Config");

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

        // Set byte order - AEP less than 1.0.30
//    dot->setJoinByteOrder(mDot::MSB);
        dot->setJoinByteOrder(mDot::LSB);

        logInfo("setting Join mode");
        if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) {
            logError("failed to set Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
        }

        // If on developer board then you can enable activity LED
        // Currently no spare pins that LEDs are connected too.
//    dot->setActivityLedPin( ACTIVITY_LED );
//    dot->setActivityLedEnable(false);

        // Have a decent nubmer of retries in connecting to LoRaWAN
        dot->setJoinRetries( 3 );

        // Set Spreading Factor, higher is lower data rate, smaller packets but longer range
        // Lower is higher data rate, larger packets and shorter range.
//        dot->setTxDataRate( mDot::SF_9 );
    dot->setTxDataRate( mDot::SF_12 );
        dot->setTxPower( 14 );
        dot->setAck( 0 );       // 1 retries on Ack, 0 to disable
 
//    Not applicable for 868MHz in EU
//    if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
//        initStatus = false;
//        logError(dot, "failed to set frequency sub band", ret);
//    }

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

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

        if ((ret = dot->setJoinMode( mDot::AUTO_OTA )) != mDot::MDOT_OK) {
            logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
        }

        logInfo("Saving Config");

        // Save config
        if (! dot->saveConfig()) {
            logError("failed to save configuration");
        }
    } else {
        logInfo("Using existing Config");
    }

    while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
        logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
        //wait(2);
        wait_ms(dot->getNextTxMs() + 1);
    }

    probe.setResolution(9);

    char dataBuf[50];
//    for (uint8_t i = 0; i < iterations; i++) {
    while( 1 ) {
        // This takes upto 750mS, way too long. Change to 9 bit resolution if not already used.

        probe.convertTemperature(true, DS1820::all_devices);         //Start temperature conversion, wait until ready
//        printf("It is %3.1fC\r\n", probe.temperature());
        // Output data as JSON e.g. {"temperature":"21.3"}
        temperature = probe.temperature();
        sprintf(dataBuf, "{\"tmp\":%3.1f}", temperature );
        send_data.clear();
        // probably not the most efficent way to do this
        for( int i=0; i< strlen(dataBuf); i++ )
            send_data.push_back( dataBuf[i] );

        if ((ret = dot->send(send_data)) != mDot::MDOT_OK) {
            logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
        } else {
            logInfo("send data: %s", Text::bin2hexString(send_data).c_str());
        }

        // Should  sleep here and wakeup after a set interval.
        uint32_t sleep_time = (dot->getNextTxMs() / 1000) + 60;
        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);

        /*
                next_tx = dot->getNextTxMs() + 1;
                logInfo("waiting %ld ms to transmit again", next_tx);
                wait_ms(next_tx);
                logInfo("waiting another %d seconds", wait_time);
                wait(wait_time);
        */
    }

    return 0;
}