lora's drivers taken out of examples repo to make a smaller binary
Dependencies: libxDot-mbed5
Diff: src/main.cpp
- Revision:
- 1:80bc4501abc5
- Parent:
- 0:973a5bbb2a17
diff -r 973a5bbb2a17 -r 80bc4501abc5 src/main.cpp --- a/src/main.cpp Thu Nov 21 08:46:20 2019 +0000 +++ b/src/main.cpp Sun Dec 08 01:56:17 2019 +0000 @@ -1,69 +1,120 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + */ +#include <stdio.h> +#include <LoRaRadio.h> +#include "dot_util.h" +#include "RadioEvent.h" +// Application helper #include "mbed.h" -#define u16_max 65526 -AnalogIn mic(PB_0); -DigitalOut vcc(GPIO0); -Serial pc(UART_TX, UART_RX, 115200); -const int sampleTime = 50; -int micOut; -// Find the Peak-to-Peak Amplitude Function -int findPTPAmp(float* sampleArr){ -// Time variables to find the peak-to-peak amplitude - - unsigned int PTPAmp = 0; +#include <AnalogIn.h> +#include <DigitalOut.h> + +static std::string network_name = "LAleakers"; +static std::string network_passphrase = "stephisbroke"; +static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 }; +static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B }; +static uint8_t frequency_sub_band = 7; +static lora::NetworkType network_type = lora::PUBLIC_LORAWAN; +static uint8_t join_delay = 5; +static uint8_t ack = 0; +static bool adr = true; + +mDot* dot = NULL; +lora::ChannelPlan* plan = new lora::ChannelPlan_US915(); + +I2C i2c(I2C_SDA, I2C_SCL); -// Signal variables to find the peak-to-peak amplitude - unsigned int maxAmp = 0; - unsigned int minAmp = 1023; +// PIN DEFINITIONS +DigitalOut vcc(GPIO0); +AnalogIn mic(PB_0); + +// To sleep, 'wait' should be replaced by 'ThisThread::sleep_for' (C++) or 'thread_sleep_for' (C). If you wish to wait (without sleeping), call 'wait_us'. 'wait_us' is safe to call from ISR context. [since mbed-os-5.14] [-Wdeprecated-declarations] in "main.cpp", Line: 59, Col: 9 + +int main() { + i2c.frequency(400000); + assert(plan); + RadioEvent events; + dot = mDot::getInstance(plan); + assert(dot); + + // attach the custom events handler + dot->setEvents(&events); -// Find the max and min of the mic output within the 50 ms timeframe - for(int i=0; i<8000; i++) - { - if( sampleArr[i] < 1023) //prevent erroneous readings - { - if (sampleArr[i] > maxAmp) - { - maxAmp = sampleArr[i]; //save only the max reading - } - else if (sampleArr[i] < minAmp) - { - minAmp = sampleArr[i]; //save only the min reading + if (!dot->getStandbyFlag()) { + logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); + + // start from a well-known state + logInfo("defaulting Dot configuration"); + dot->resetConfig(); + dot->resetNetworkSession(); + + // make sure library logging is turned on + dot->setLogLevel(mts::MTSLog::INFO_LEVEL); + + // update configuration if necessary + if (dot->getJoinMode() != mDot::OTA) { + logInfo("changing network join mode to OTA"); + if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) { + logError("failed to set network join mode to OTA"); + } } - } - } - - PTPAmp = maxAmp - minAmp; // (max amp) - (min amp) = peak-to-peak amplitude - double micOut_Volts = (PTPAmp * 3.3) / 1024; // Convert ADC into voltage + // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY + // only one method or the other should be used! + // network ID = crc64(network name) + // network KEY = cmac(network passphrase) + update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, network_type, ack); + //update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack); - //Uncomment this line for help debugging (be sure to also comment out the VUMeter function) - //Serial.println(PTPAmp); + // configure network link checks + // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots + // check the link every count packets + // declare the Dot disconnected after threshold failed link checks + // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row + update_network_link_check_config(3, 5); - //Return the PTP amplitude to use in the soundLevel function. - // You can also return the micOut_Volts if you prefer to use the voltage level. - return PTPAmp; -} + // enable or disable Adaptive Data Rate + dot->setAdr(adr); + + // Configure the join delay + dot->setJoinDelay(join_delay); + + // save changes to configuration + logInfo("saving configuration"); + if (!dot->saveConfig()) { + logError("failed to save configuration"); + } -void record(float* sampleArr) { - //record sound for 1 second - for(int i=0; i<8000; i++) { - sampleArr[i] = mic.read(); //put samples in array - wait(0.000125f); //sample rate of 8000 Hz - } -} - -int main(){ - float sampleArr[8000]; //used to store sound - pc.printf("\r\n Sparkfun MEM Microphone Test\n"); - pc.printf("******************\n"); - vcc = 1; - unsigned int val; - int ptpAmp; - while(1){ - wait(0.2); - record(sampleArr); - ptpAmp=findPTPAmp(sampleArr); + // display configuration + display_config(); + } else { + // restore the saved session if the dot woke from deepsleep mode + // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep + logInfo("restoring network session from NVM"); + dot->restoreNetworkSession(); + } + // join network if not joined + if (!dot->getNetworkJoinStatus()) { + join_network(); + } + + /* + tests: + 1. Check received transmissions/second from this loop + 2. make the packet size a lot bigger + 3. + */ + // get some data in send vector + std::vector<uint8_t> tx_data; + tx_data.clear(); + snprintf(arr, 5, "%u", mic.read_u16()); + for(int i = 0; i < 5; i++) { + tx_data.push_back(arr[i]); + } + + while(1) { + send_data(tx_data); } } - - -