Handle LoRa Basic Function

Dependents:   lora_example_miun

Files at this revision

API Documentation at this revision

Comitter:
biwa1400
Date:
Wed Mar 08 18:54:32 2017 +0000
Commit message:
20170308

Changed in this revision

ADC.cpp Show annotated file Show diff for this revision Revisions of this file
ADC.h Show annotated file Show diff for this revision Revisions of this file
Lora.cpp Show annotated file Show diff for this revision Revisions of this file
Lora.h Show annotated file Show diff for this revision Revisions of this file
Parking.cpp Show annotated file Show diff for this revision Revisions of this file
Parking.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADC.cpp	Wed Mar 08 18:54:32 2017 +0000
@@ -0,0 +1,42 @@
+#include "mbed.h"
+#include "ADC.h"
+#include "MTSLog.h"
+
+
+const int ADC_TOTAL_BITS = 16;
+const int ADC_bits = 8;
+const int ADC_period=50;    
+const int ADC_sample_times = 64;
+const int ADC_average_move = 6;
+
+AnalogIn Distance(PB_1);
+
+bool ADC_class::init()
+{
+    movBits = ADC_TOTAL_BITS-ADC_bits;  
+    return true;
+}
+
+unsigned short ADC_class::sample()
+{
+    DigitalOut SensorPower(PA_0);
+    SensorPower = 0;
+    adcDistance = 0;
+    for (int i=0;i<ADC_sample_times;i++)
+    {
+        wait_ms(ADC_period);
+        adcDistance += (Distance.read_u16()>>movBits);
+    }
+   // wait_ms(8000);
+    SensorPower = 1;
+    
+    return adcDistance>>ADC_average_move;
+}
+
+bool ADC_class::sample_and_judge_result(unsigned short threshold)
+{    
+    if(sample()>threshold)
+        return true;
+    else
+        return false; 
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADC.h	Wed Mar 08 18:54:32 2017 +0000
@@ -0,0 +1,24 @@
+#ifndef _ADC_H
+#define _ADC_H
+
+#include "mbed.h"
+#include "mDot.h"
+
+
+class ADC_class
+{
+private:
+    int movBits;
+    int ADCwaitTime;
+    unsigned short adcDistance;
+  //  AnalogIn Distance();
+    
+    bool init();
+public:
+    ADC_class(){init();}
+    unsigned short sample();
+    unsigned short read_ADC(){return adcDistance;}
+    bool sample_and_judge_result(unsigned short threshold);
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lora.cpp	Wed Mar 08 18:54:32 2017 +0000
@@ -0,0 +1,165 @@
+#include "Lora.h"
+#include "mbed.h"
+#include "mDot.h"
+#include "MTSLog.h"
+#include <string>
+#include <vector>
+
+Lora::Lora()
+{
+    // get a mDot handle
+    dot = mDot::getInstance();
+    logInfo("version: %s", dot->getId().c_str());
+    //joinNetwork();
+}
+
+
+
+bool Lora::setting(string config_network_name, string config_network_pass,uint8_t config_frequency_sub_band,uint8_t config_ACK)
+{
+    int32_t ret;
+    bool state = true;
+    //*******************************************
+    // 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
+   
+    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());
+        state = false;
+    }
+    
+    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());
+        state = false;
+    }
+    
+    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());
+        state = false;
+    }
+    
+    // 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());
+        state = false;
+    }
+    
+    // request receive confirmation of packets from the gateway
+    logInfo("enabling ACKs");
+    if ((ret = dot->setAck(config_ACK)) != mDot::MDOT_OK) 
+    {
+        logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+        state = false;
+    }
+    
+     // set join mode to AUTO_OTA so the mDot doesn't have to rejoin after sleeping   
+    logInfo("setting join mode to AUTO_OTA");
+    if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) {
+        logError("failed to set join mode %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");
+        state = false;
+    }
+    //*******************************************
+    // end of configuration
+    //*******************************************
+    return state;
+}
+
+bool Lora::joinNetwork()
+{
+     int32_t ret;
+     // attempt to join the network
+    if (!dot->getNetworkJoinStatus()) 
+    {    
+        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((uint32_t)dot->getNextTxMs());
+        } 
+    }
+    
+    return true;   
+}
+
+bool Lora::sendData_vector(const std::vector<uint8_t>& data)
+{
+    int32_t ret;
+    if ((ret = dot->send(data)) != mDot::MDOT_OK) 
+    {
+        logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
+        return false;
+    } 
+    else 
+    {
+        logInfo("successfully sent data to gateway");
+        return true;
+    }    
+}
+
+bool Lora::sendData_string(std::string& input_data)
+{
+     int32_t ret;
+    vector<uint8_t> sent_data;
+    for (std::string::iterator it = input_data.begin(); it != input_data.end(); it++)
+    {
+        sent_data.push_back((uint8_t) *it);   
+    }
+    
+    if ((ret = dot->send(sent_data)) != mDot::MDOT_OK) 
+    {
+            logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
+    } 
+    else 
+    {
+            logInfo("successfully sent data to gateway");
+    }
+    return true;
+    
+}
+
+ bool Lora::receiveData_string(std::string& output_data)
+{
+    int32_t ret;
+    vector<uint8_t> receive_data;
+     if ((ret = dot->recv(receive_data)) != mDot::MDOT_OK) 
+    {
+            logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
+            return false;
+    } 
+    else 
+    {
+            string str(receive_data.begin(),receive_data.end());
+            output_data=str;
+    }
+    return true;
+           
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lora.h	Wed Mar 08 18:54:32 2017 +0000
@@ -0,0 +1,31 @@
+#ifndef LORA_H_
+#define LORA_H_
+
+#include "mbed.h"
+#include "mDot.h"
+
+class Lora
+{
+private:
+    mDot* dot;
+
+public:
+    Lora();
+    /*
+    1.name
+    2.password
+    3.sub band
+    4.ACK 0=NULL
+    */
+    bool setting(string config_network_name, string config_network_pass, uint8_t config_frequency_sub_band,uint8_t config_ACK);    
+    bool joinNetwork();
+    bool sendData_vector(const std::vector<uint8_t>& data);
+    bool sendData_string(std::string& data);
+    bool receiveData_string(std::string& output_data);
+    mDot* useDot(){return dot;}
+        
+    
+};
+
+
+#endif 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Parking.cpp	Wed Mar 08 18:54:32 2017 +0000
@@ -0,0 +1,80 @@
+#include "Lora.h"
+#include "mbed.h"
+#include "mDot.h"
+#include "MTSLog.h"
+#include <string>
+#include <vector>
+#include <sstream>
+#include <iostream>
+#include "Parking.h"
+
+const string Parking::SATAE_TITLE = "DISTANCE=";
+//const string Parking::EXIST = "QWE";
+//const string Parking::DISAPPEAR = "ZXC";
+
+
+
+
+//ADC
+
+
+
+bool Parking::init()
+{
+     loraNode1.joinNetwork();
+     return true;
+}
+
+
+
+bool Parking::capture_sent()
+{
+     static int i=0;
+     stringstream  command;
+     command <<"num: "<<i;
+     string result;
+     command >> result;
+    // char buf[10];
+    // sprintf(buf,"%d;\0",sensor1.sample());
+     //result =SATAE_TITLE+(string)buf;
+     loraNode1.sendData_string(result);
+//     loraNode1.useDot()->openRxWindow(5000,2);
+     return true;
+}
+
+bool Parking::recevie_set_sleeptime()
+{
+    string sleepTimeString;
+    loraNode1.receiveData_string(sleepTimeString);
+    logInfo("receive: %s",sleepTimeString.c_str());
+    uint32_t sleeptime = (uint32_t)atoi(findKeyWord(sleepTimeString,"SLEEP").c_str());
+    logInfo("sleepTime: %d",sleeptime);
+    if( 0<sleeptime && sleeptime<1000)
+    {
+        uint32_t sleep_time = std::max(sleeptime*1000, (uint32_t)useDot()->getNextTxMs()) / 1000;
+        logInfo("SettingSleeping...");
+        useDot()->sleep(sleep_time, mDot::RTC_ALARM,false);
+    }
+    return true;
+}
+ 
+std::string
+Parking::findKeyWord(std::string in_string, std::string keyWord)
+{
+        int title;
+        int begin;
+        int end;
+
+        title = in_string.find(keyWord);
+        begin = in_string.find("=",title)+1;
+        if(in_string.find("{",begin)==begin)
+        {
+                begin++;
+                end = in_string.find("}",begin);
+        }
+        else end = in_string.find(";",begin);
+
+        return in_string.substr (begin, end-begin);
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Parking.h	Wed Mar 08 18:54:32 2017 +0000
@@ -0,0 +1,35 @@
+#ifndef PARKING_H_
+#define PARKING_H_
+
+#include "mbed.h"
+#include "mDot.h"
+#include "Lora.h"
+#include "ADC.h"
+
+class Parking
+{
+private:   
+    const static string SATAE_TITLE;
+    const static string EXIST;
+    const static string DISAPPEAR;
+    
+    Lora loraNode1;
+    ADC_class sensor1;
+    int sleepTime;
+    
+    bool init();
+    bool fill_data();
+    std::string findKeyWord(std::string in_string, std::string keyWord);
+    //capture();
+public:
+    Parking(){init(); sleepTime = 30000;}
+    int getSleepTime() {return sleepTime;}
+    void setSleepTime(int inSleepTime) {sleepTime = inSleepTime;}
+    mDot* useDot(){return loraNode1.useDot();}
+    Lora* useLora() {return &loraNode1;}
+    bool recevie_set_sleeptime();
+    bool capture_sent();
+    
+};
+
+#endif
\ No newline at end of file