HM_IOT_Demo / Mbed 2 deprecated mDot_LoRa_Street_light

Dependencies:   MTS-Serial libmDot mbed-rtos mbed

Fork of mDot_LoRa_Connect_Example by ivan florido

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wrapper.cpp Source File

wrapper.cpp

00001 #include "mbed.h"
00002 #include "mDot.h"
00003 #include "MTSLog.h"
00004 #include <string>
00005 #include <vector>
00006 #include <algorithm>
00007 
00008 mDot* dot;
00009 void init_mdot(void)
00010 {
00011     // get a mDot handle
00012     dot = mDot::getInstance();
00013     
00014     // print library version information
00015     //logInfo("version: %s", dot->getId().c_str());
00016     
00017     //logInfo("Test msg \n\r", dot->getId().c_str());
00018     
00019     //logInfo("Test msg \n\r", dot->getId().c_str());
00020     
00021     //logInfo("Test msg \n\r", dot->getId().c_str());
00022     //logInfo("Test msg \n\r", dot->getId().c_str());
00023     //logInfo("Test msg \n\r", dot->getId().c_str());
00024     //logInfo("Test msg \n\r", dot->getId().c_str());   
00025 }
00026 
00027 void reset_config(void)
00028 {
00029     // reset to default config so we know what state we're in
00030     dot->resetConfig();    
00031 }
00032 
00033 void set_log_lavel(int logInfo)
00034 {
00035     switch(logInfo) {
00036         case 4:
00037         dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00038         break;
00039     }   
00040 }
00041 
00042 int set_public_network(int value)
00043 {
00044     return dot->setPublicNetwork(value);
00045 }
00046 
00047 int set_frequency_sub_band( int config_frequency_sub_band)
00048 {
00049      return dot->setFrequencySubBand(config_frequency_sub_band);   
00050 }
00051 
00052 int set_network_name(char *config_network_name)
00053 {
00054     return dot->setNetworkName(config_network_name);  
00055 }
00056 
00057 int set_network_passphrase(char *config_network_pass)
00058 {
00059     return dot->setNetworkPassphrase(config_network_pass);  
00060 }
00061 
00062 int set_txpower(int value)
00063 {
00064     int ret;
00065     logInfo("setting TX spreading factor");
00066     if ((ret = dot->setTxPower(value)) != mDot::MDOT_OK) {
00067         logError("failed to set TX datarate %d:%s", ret);
00068     }
00069     return ret;
00070 }
00071 
00072 int set_tx_datarate(void)
00073 {
00074     return dot->setTxDataRate(mDot::SF_10);
00075 }
00076 
00077 int set_ack(int value)
00078 {
00079     return dot->setAck(value);   
00080 }
00081 
00082 int save_config(void)
00083 {
00084     return dot->saveConfig();    
00085 }
00086 
00087 int join_network(void)
00088 {  
00089     int ret;
00090     while (!dot->getNetworkJoinStatus()) {
00091         osDelay(dot->getNextTxMs());
00092         if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
00093             printf("ERROR: failed to join network %d:%s\n\r", ret,  mDot::getReturnCodeString(ret).c_str());
00094         }
00095     }
00096     
00097     //return dot->joinNetwork();
00098     return 0;    
00099 }
00100 
00101 bool send(const std::string text)
00102 {
00103     int32_t returnCode;
00104     uint32_t timeTillSend = dot->getNextTxMs();
00105     if (timeTillSend != 0) {
00106         printf("waiting %lu ms to send\r\n", timeTillSend);
00107         return false;
00108     }
00109 
00110     //printf("Sending data...  ");
00111     std::vector<uint8_t> data(text.begin(), text.end());
00112     if ((returnCode = dot->send(data, 1)) != mDot::MDOT_OK)
00113     {
00114         //logInfo("error in sending data");
00115         return false;
00116     }
00117     //printf("Data sent!\r\n");
00118     return true;
00119 }
00120 
00121 /*
00122 int send_data(char *args)
00123 {
00124     int ret;
00125     std::vector<uint8_t> data;
00126     std::string data_str = "hello!";
00127         // format data for sending to the gateway
00128     for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
00129         data.push_back((uint8_t) *it);
00130 
00131     while (true) {
00132         // send the data to the gateway
00133         if ((ret = dot->send(data)) != mDot::MDOT_OK) {
00134             logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
00135         } else {
00136             logInfo("successfully sent data to gateway");
00137         }
00138 
00139         // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
00140         osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
00141     }
00142     return 0;
00143 }
00144 */
00145 
00146 bool receive_data(char *buffer)
00147 {
00148     std::vector<uint8_t>data;
00149 
00150     if (dot->recv(data) != mDot::MDOT_OK) {
00151         return false;
00152     }
00153     
00154     int i = 0;
00155     for (std::vector<uint8_t>::const_iterator it = data.begin(); it != data.end(); ++it)
00156         buffer[i++] = *it;
00157     buffer[i] = '\0';
00158 
00159     return true;
00160 }