MultiTech MultiConnect mDot and Ubidots

The MultiConnect® mDot™ is a LoRaWAN™ ready module, capable of 2-way communication over distances in excess of 10 miles / 16 km outdoors.

The mDot brings a low power ARM® Cortex®-M4 processor, running ARM’s mbed™ OS, along with developer friendly libraries. This example benefits from its onboard processor to read data from analog inputs and send it to Ubidots.

Requirements

To be able to see the data streamed by the mDot you will need a LoRa gateway. Check out our LoRa MultiTech MultiConnect Conduit tutorial here

Setup

  • Create an account in mbed Developer site.
  • Login to your mbed account and go to Platforms.
  • Select MultiTech under Platform vendor.
  • Click on MultiTech mDot and then click on Add to your mbed compiler.
  • Click on the Compiler button in the upper right corner.
  • Create a new project, select MultiTech mDot as your platform, then select the template Connecting to gateway and sending package with a MultiTech mDot and click OK.
  • Go to main.cpp, erase its content and paste the corresponding sample codes found below in this guide.

Send one value to Ubidots

To send a value from an Analog Pin copy this code and paste it into main.cpp. Don’t forget to change the LoRa network name, Frequency Sub-Band and Passphrase. Click on “compile” and then simply paste the resulting .bin file to the MultiConnect® mDot™ Micro Developer Kit, which you will see as a USB storage drive in our computer.

#include "mbed.h"
#include "mDot.h"
#include "MTSLog.h"
#include
#include
#include
#include
AnalogIn value(PB_1);
// 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 = "Network_Name_of_Conduit";
static std::string config_network_pass = "Network_Pass_of_Conduit";
static uint8_t config_frequency_sub_band = 1;
int main() {
    int32_t ret;
    mDot* dot;
    std::vector data;
    int inValue;
    char* str = new char[3];
    float sensorValue;    
    sensorValue = value.read();
    inValue = int(sensorValue*1000);
    sprintf(str, "%d", inValue );    // 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);
    // 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    
    if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
        logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
    }
    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->setTxPower(20)) != mDot::MDOT_OK) {
        logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
    }
    logInfo("setting TX power");
    if ((ret = dot->setTxDataRate(mDot::SF_9)) != 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()));
    }
    // format data for sending to the gateway
    for (int i = 0; i send(data)) != mDot::MDOT_OK) {
            logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
        } else {
            logInfo("successfully sent data to gateway");
        }
        // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
        osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
    }
    return 0;
}

Send multiple values to Ubidots

To send three values from the Analog Pins A0, A1 and A2, copy the followinf code and paste it into main.cpp. Don’t forget to change the LoRa network name, Frequency Sub-Band and Passphrase. Click on “compile” and then simply paste the resulting .bin file to the MultiConnect® mDot™ Micro Developer Kit, which you will see as a USB storage drive in our computer.

#include "mbed.h"
#include "mDot.h"
#include "MTSLog.h"
#include
#include
#include
#include
AnalogIn value(PB_1);
AnalogIn value2(PB_2);
AnalogIn value3(PB_3);
// 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 = "UbidotsLora";
static std::string config_network_pass = "clave123456789";
static uint8_t config_frequency_sub_band = 1;
int main() {
    int32_t ret;
    mDot* dot;
    std::vector data;
    std::vector data2;
    std::vector data3;
    int inValue, inValue2, inValue3;
    char* str = new char[3];
    char* str2 = new char[3];
    char* str3 = new char[3];
    float sensorValue, sensorValue2, sensorValue3;    
    sensorValue = value.read();
    sensorValue = value2.read();
    sensorValue = value3.read();
    inValue = int(sensorValue*1000);
    inValue2 = int(sensorValue*1000);
    inValue3 = int(sensorValue*1000);
    sprintf(str, "%d", inValue );
    sprintf(str2, "%d", inValue2 );
    sprintf(str3, "%d", inValue3 );    // 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);
    // 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    
    if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
        logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
    }
    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->setTxPower(20)) != mDot::MDOT_OK) {
        logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
    }
    logInfo("setting TX power");
    if ((ret = dot->setTxDataRate(mDot::SF_9)) != 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()));
    }
    // format data for sending to the gateway
    // data one
    for (int i = 0; i send(data)) != mDot::MDOT_OK) {
            logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
        } else {
            logInfo("successfully sent data to gateway");
        }
        if ((ret = dot->send(data2)) != mDot::MDOT_OK) {
            logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
        } else {
            logInfo("successfully sent data to gateway");
        }
        if ((ret = dot->send(data3)) != mDot::MDOT_OK) {
            logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
        } else {
            logInfo("successfully sent data to gateway");
        }
        // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
        osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
    }
    return 0;
}
Did this answer your question?