![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
To read a light sensor
Dependencies: DHT GPS MTS-Serial PulseCounter libmDot mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example by
Revision 5:951cfd165a58, committed 2016-06-10
- Comitter:
- Mehrad
- Date:
- Fri Jun 10 00:04:08 2016 +0000
- Parent:
- 4:36e214ebfa56
- Commit message:
- Initial commit
Changed in this revision
diff -r 36e214ebfa56 -r 951cfd165a58 Command.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Command.h Fri Jun 10 00:04:08 2016 +0000 @@ -0,0 +1,98 @@ +/** + ****************************************************************************** + * File Name : Command.h + * Date : 18/04/2014 10:57:12 + * Description : This file provides code for command line prompt + ****************************************************************************** + * + * COPYRIGHT(c) 2014 MultiTech Systems, Inc. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#include "mbed.h" +#include "mDot.h" +#include "MTSSerial.h" +#include "MTSText.h" +#include <cstdlib> +#include <string> +#include <vector> +#include "limits.h" +#include "debug.h" + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __command_H +#define __command_H + +#define KEY_LENGTH 16 +#define EUI_LENGTH 8 +#define PASSPHRASE_LENGTH 128 + +class Command { + + public: + + Command(mDot* dot); + Command(mDot* dot, const char* name, const char* text, const char* desc); + virtual ~Command() {}; + + const char* name() const { return _name; }; + const char* text() const { return _text; }; + const char* desc() const { return _desc; }; + const char* help() const { return _help.c_str(); }; + + virtual uint32_t action(std::vector<std::string> args) = 0; + virtual bool verify(std::vector<std::string> args); + const std::string usage() const; + std::string& errorMessage(); + const bool queryable(); + + static const char newline[]; + static void readByteArray(const std::string& input, std::vector<uint8_t>& out, size_t len); + + static bool isHexString(const std::string& str, size_t bytes); + static bool isBaudRate(uint32_t baud); + + protected: + + void setErrorMessage(const char* message); + void setErrorMessage(const std::string& message); + std::string _help; + std::string _usage; + bool _queryable; + mDot* _dot; + + private: + + const char* _name; + const char* _text; + const char* _desc; + std::string _errorMessage; + +}; + +#endif /*__ command_H */ + +/************************ (C) COPYRIGHT MultiTech Systems, Inc *****END OF FILE****/
diff -r 36e214ebfa56 -r 951cfd165a58 DHT.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHT.lib Fri Jun 10 00:04:08 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/components/code/DHT/#df22ddf10d75
diff -r 36e214ebfa56 -r 951cfd165a58 GPS.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GPS.lib Fri Jun 10 00:04:08 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/tylerjw/code/GPS/#39d75e44b214
diff -r 36e214ebfa56 -r 951cfd165a58 MTS-Serial.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MTS-Serial.lib Fri Jun 10 00:04:08 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/MultiTech/code/MTS-Serial/#4afbbafcd6b3
diff -r 36e214ebfa56 -r 951cfd165a58 PulseCounter.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PulseCounter.lib Fri Jun 10 00:04:08 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/Mehrad/code/PulseCounter/#f068f31da101
diff -r 36e214ebfa56 -r 951cfd165a58 debug.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/debug.h Fri Jun 10 00:04:08 2016 +0000 @@ -0,0 +1,8 @@ +#include "mbed.h" +#include <stdio.h> + +#if 1 + #define DEBUG_PRINTF(fmt, args...) printf("%s:%d: "fmt, __FUNCTION__, __LINE__, ## args) +#else + #define DEBUG_PRINTF(fmt, args...) +#endif
diff -r 36e214ebfa56 -r 951cfd165a58 libmDot.lib --- a/libmDot.lib Wed Sep 30 19:49:02 2015 +0000 +++ b/libmDot.lib Fri Jun 10 00:04:08 2016 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/MultiTech/code/libmDot/#0b4eb17d07ae +https://developer.mbed.org/users/Mehrad/code/libmDot/#9e811e5a03be
diff -r 36e214ebfa56 -r 951cfd165a58 main.cpp --- a/main.cpp Wed Sep 30 19:49:02 2015 +0000 +++ b/main.cpp Fri Jun 10 00:04:08 2016 +0000 @@ -1,102 +1,308 @@ #include "mbed.h" #include "mDot.h" #include "MTSLog.h" +#include "MTSSerial.h" #include <string> #include <vector> #include <algorithm> +#include <sstream> +#include <iomanip> +//#include "PulseCounter.h" // 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 = "<lora network id>"; -//static std::string config_network_pass = "<lora network key>"; -//static uint8_t config_frequency_sub_band = 1; +static std::string config_network_name = "campbellsci"; +static std::string config_network_pass = "campbellsci"; +static uint8_t config_frequency_sub_band = 1; + +DigitalOut greenLed(PA_11); // To turn the join LED on/off + +std::string VectorInt8_To_StrHex (const vector<uint8_t>& v) +{ + stringstream ss; + ss << std::hex << std::setfill('0'); + vector<uint8_t>::const_iterator it; + + for (it = v.begin(); it != v.end(); it++) { + ss << ":" << std::setw(2) << static_cast<unsigned>(*it); + } + return ss.str(); +} + +std::string VectorInt32_To_StrDec (const vector<uint32_t>& v) +{ + // Serial output for debugging + //mts::MTSSerial serial(XBEE_DOUT, XBEE_DIN, 512, 512); + //serial.baud(115200); + //mts::MTSSerial& _serial(serial); + stringstream ss; + // Body + ss << std::dec << std::setfill('0'); + vector<uint32_t>::const_iterator it; -int main() { + for (it = v.begin(); it != v.end(); it++) { + ss << ":" << std::setw(3) << static_cast<unsigned>(*it); + //_serial.writef(static_cast<unsigned>(*it)); + } + return ss.str(); +} + +std::string VectorMdotFile_To_List (const vector<mDot::mdot_file>& v) +{ + // Serial output for debugging + mts::MTSSerial serial(XBEE_DOUT, XBEE_DIN, 512, 512); + serial.baud(115200); + mts::MTSSerial& _serial(serial); + + stringstream ss; + vector<mDot::mdot_file>::const_iterator it; + + _serial.writef(" DEBUG: Vector size for user files list %d\r\n" , v.size()); + + for (it = v.begin(); it != v.end(); it++) { + ss << (*it).name << " " ; + } + return ss.str(); +} + +int main() +{ + greenLed = 0; + AnalogIn in(PB_1); // Read Analog + int32_t ret; mDot* dot; std::vector<uint8_t> data; - std::string data_str = "hello!"; + //std::string data_str = "hello!"; + std::ostringstream ss; + std::string data_str = "Hello"; + //ss << in.read_u16(); + //data_str = ss.str(); + // get a mDot handle dot = mDot::getInstance(); - + + // This section is to enable the debuging info over serial (rs232) + mts::MTSSerial serial(XBEE_DOUT, XBEE_DIN, 512, 512); + serial.baud(115200); + mts::MTSSerial& _serial(serial); + // Hello world + _serial.writef("\r\n\r\n\r\n\r\n"); + _serial.writef("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); + _serial.writef("+ Hello mDot + \r\n"); + _serial.writef("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \r\n"); + + + // print library version information - logInfo("version: %s", dot->getId().c_str()); + _serial.writef("INFO: version: %s\r\n", 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 - logInfo("setting frequency sub band"); + _serial.writef("INFO: setting frequency sub band: %d\r\n", config_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()); + _serial.writef("ERROR: failed to set frequency sub band %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); + } + + /* Dont' need network name when setting it network EUI + _serial.writef("INFO: setting network name: %s\r\n", config_network_name.c_str()); + if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { + _serial.writef("ERROR: failed to set network name %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); + } + */ + // Initializing a vecotr in C++ + // This compiler doesn't accept -> static std::vector<uint8_t> config_network_key {00,00,00,00,00,00,00,01}; + static std::uint8_t netId[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; + static std::vector<uint8_t> netIdV(&netId[0], &netId[0] + 8); // It is +8 since 8 members. You need start and end of the array. + _serial.writef("INFO: setting network ID (EUI): %s\r\n", VectorInt8_To_StrHex(netIdV)); + if ((ret = dot->setNetworkId(netIdV)) != mDot::MDOT_OK) { + _serial.writef("ERROR: failed to set network ID %d:%s\r\n", 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()); + /* + _serial.writef("INFO: setting network password: %s\r\n", config_network_pass.c_str()); + if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { + _serial.writef("ERROR: failed to set network password %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); + } + */ + // Initializing a vecotr in C++ + static std::uint8_t netKey[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; + static std::vector<uint8_t> netKeyV(&netKey[0], &netKey[0] + 16); // It is +16 since 16 members. You need start and end of the array. + _serial.writef("INFO: setting network key: %s\r\n", VectorInt8_To_StrHex(netKeyV)); + if ((ret = dot->setNetworkKey(netKeyV)) != mDot::MDOT_OK) { + _serial.writef("ERROR: failed to set network key %d:%s\r\n", 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()); + + bool adrStatus = false; + _serial.writef("ADR Status is %d\r\n", (*dot).getAdr()); + _serial.writef("INFO: setting Adr %d: \r\n", adrStatus); + if ((ret = dot->setAdr(adrStatus)) != mDot::MDOT_OK) { + _serial.writef("ERROR: failed to set ADR %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); + } else { + _serial.writef("Data Rate: %d\r\n", (*dot).getTxDataRate()); + _serial.writef("ADR Status is %d\r\n", (*dot).getAdr()); + } - + + /* // 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()); - } - + _serial.writef("INFO: setting TX data rate: "); + if ((ret = dot->setTxDataRate(mDot::DR2)) != mDot::MDOT_OK) { + _serial.writef("ERROR: failed to set TX datarate %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); + } else + _serial.writef("%d\r\n", (*dot).getTxDataRate()); + + */ + + // request receive confirmation of packets from the gateway - logInfo("enabling ACKs"); + _serial.writef("INFO: enabling ACKs\r\n"); if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { - logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); + _serial.writef("ERROR: failed to enable ACKs %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); + } + + // Public Network Mode + _serial.writef("INFO: Public network state: %d\r\n", (*dot).getPublicNetwork()); + _serial.writef("INFO: enabling Public network mode\r\n"); + if ((ret = dot->setPublicNetwork(1)) != mDot::MDOT_OK) { + _serial.writef("ERROR: failed to enable Public Mode %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); } - + _serial.writef("INFO: Public network state: %d\r\n", (*dot).getPublicNetwork()); + + + _serial.writef("INFO: frequency band: %s\r\n", mDot::FrequencyBandStr((*dot).getFrequencyBand()).c_str()); + _serial.writef("INFO: frequency band: %d\r\n", (*dot).getFrequencyBand()); + + + const uint8_t _freqBand = 2; + _serial.writef("INFO: Setting frequency band to: %s\r\n", mDot::FrequencyBandStr(_freqBand).c_str()); + if ((ret = dot->setFrequencyBand(_freqBand)) != mDot::MDOT_OK) { + _serial.writef("ERROR: failed to enable Public Mode %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); + } + + // Workaround to lock the TX frequencies to a single Aussie one while working with the incorrect library. + // The latest library has the fixed 8 Aussie libraries and it makes the following lines irrelevent. + //_serial.writef("INFO: Setting TX frequency to: %s\r\n", mDot::FrequencyBandStr(_freqBand).c_str()); + //if ((ret = dot->setTxFrequency(915200000)) != mDot::MDOT_OK) { + // _serial.writef("ERROR: failed to Set TX frequency %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); + //} + //_serial.writef("INFO: TX frequency: %d\r\n", (*dot).getTxFrequency()); + + // save this configuration to the mDot's NVM - logInfo("saving config"); + _serial.writef("INFO: saving config\r\n"); if (! dot->saveConfig()) { - logError("failed to save configuration"); + _serial.writef("ERROR: failed to save configuration\r\n"); } + //******************************************* // 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()); + _serial.writef("INFO: frequency band: %s\r\n", mDot::FrequencyBandStr((*dot).getFrequencyBand()).c_str()); + + _serial.writef("INFO: Device ID%s\r\n", VectorInt8_To_StrHex((*dot).getDeviceId()).c_str()); + + _serial.writef("INFO: Network ID%s\r\n", VectorInt8_To_StrHex((*dot).getNetworkId()).c_str()); + _serial.writef("INFO: Network Key%s\r\n", VectorInt8_To_StrHex((*dot).getNetworkKey()).c_str()); + + _serial.writef("INFO: Join mode: %s\r\n", mDot::JoinModeStr((*dot).getJoinMode()).c_str()); + + _serial.writef("INFO: List of channel frequencies currently in use%s\r\n", VectorInt32_To_StrDec((*dot).getChannels()).c_str()); + // TODO: These data rate lists don't make sense. + _serial.writef("INFO: List of channel datarate ranges currently in use%s\r\n", VectorInt8_To_StrHex((*dot).getChannelRanges()).c_str()); + //_serial.writef("INFO: List of channel frequencies in config file to be used as session defaults%s\r\n", VectorInt32_To_StrDec((*dot).getConfigChannels())); + //_serial.writef("INFO: List of channel frequencies currently in use%s\r\n", VectorInt32_To_StrDec((*dot).getChannels())); + + + +//_serial.writef("INFO: List user files stored in flash%s\r\n", VectorMdotFile_To_List((*dot).listUserFiles())); + + +//******************************************* +// end of debuging info +//******************************************* + + +// attempt to join the network + _serial.writef("INFO: joining network...\r\n"); + while ((ret = dot->joinNetworkOnce()) != mDot::MDOT_OK) { + _serial.writef("ERROR: failed to join network %d:%s\r\n", 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())); } + greenLed = 1; // Joined Successfuly - // format data for sending to the gateway - for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) - data.push_back((uint8_t) *it); + mDot::rssi_stats _rssiStats; + + int err; + while (true) { - while (true) { + //format data for sending to the gateway + for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) { + data.push_back((uint8_t) *it); + } + // send the data to the gateway if ((ret = dot->send(data)) != mDot::MDOT_OK) { - logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); + _serial.writef("ERROR: failed to send\r\n", ret, mDot::getReturnCodeString(ret).c_str()); } else { - logInfo("successfully sent data to gateway"); + _serial.writef("INFO: successfully sent data to gateway "); + + _rssiStats = dot->getRssiStats(); + _serial.writef("+ RSSI values( last:%d min:%d max:%d avg:%d ) \r\n", _rssiStats.last, _rssiStats.min, _rssiStats.max, _rssiStats.avg); } // 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())); + //osDelay(std::max((uint32_t)50, (uint32_t)dot->getNextTxMs())); + //osDelay(std::max((uint32_t)2000, (uint32_t)dot->getNextTxMs())); + wait(1); + + ss.str(""); + +/* + //int temp = dht11->readTemperature(); + if (err == 0) { + float temp = dht.ReadTemperature(CELCIUS); + float rh = dht.ReadHumidity(); + _serial.writef(" Temp: %f Rh: %f \r\n", temp, rh); + ss << (temp); + ss << ','; + ss << rh; + } else { + ss.str(" :err "); + ss << err; + } + */ + + //double temp = ((in.read_u16() * 3.3) - 0.424) / 0.00625; + double adc = in.read_u16(); + /* + double a = 1E-12; + double b = 9E-8; + double temp; + temp = (a*adc*adc*adc)-(b*adc*adc)+0.0041*adc-43.313; + */ + double light = 9.15541*adc; + ss << (light); + + data.clear(); + // Converting int to string in C++ using <sstream> + data_str = ss.str(); } return 0;