patricio alba / Mbed OS EXPO_ANDA_Copilotoco

Dependencies:   DHT HX711 libmDot-mbed5 ISL29011

Fork of EXPO_ANDA by Santiago García Eleisequi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ota_example.cpp Source File

ota_example.cpp

00001 #include "dot_util.h"
00002 #include "RadioEvent.h"
00003 #include "HX711.h"
00004 #include "DHT.h"
00005 #include <iostream>
00006 #include <string>
00007 #include <sstream>
00008  
00009 #if ACTIVE_EXAMPLE == OTA_EXAMPLE
00010 
00011 /////////////////////////////////////////////////////////////////////////////
00012 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
00013 // * Because these example programs can be used for both mDot and xDot     //
00014 //     devices, the LoRa stack is not included. The libmDot library should //
00015 //     be imported if building for mDot devices. The libxDot library       //
00016 //     should be imported if building for xDot devices.                    //
00017 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/    //
00018 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/        //
00019 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/    //
00020 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/        //
00021 /////////////////////////////////////////////////////////////////////////////
00022 
00023 /////////////////////////////////////////////////////////////
00024 // * these options must match the settings on your gateway //
00025 // * edit their values to match your configuration         //
00026 // * frequency sub band is only relevant for the 915 bands //
00027 // * either the network name and passphrase can be used or //
00028 //     the network ID (8 bytes) and KEY (16 bytes)         //
00029 /////////////////////////////////////////////////////////////
00030 static std::string network_name = "xaviaiot";
00031 static std::string network_passphrase = "xaviaiot";
00032 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
00033 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
00034 static uint8_t frequency_sub_band = 1;
00035 static bool public_network = true;
00036 static uint8_t ack = 0;
00037 static bool adr = true;
00038 
00039 // deepsleep consumes slightly less current than sleep
00040 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
00041 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
00042 // if deep_sleep == true, device will enter deepsleep mode
00043 static bool deep_sleep = false;
00044 
00045 mDot* dot = NULL;
00046 lora::ChannelPlan* plan = NULL;
00047 
00048 Serial pc(USBTX, USBRX);
00049 
00050 int main() {
00051     // Custom event handler for automatically displaying RX data
00052     RadioEvent events;
00053 
00054     pc.baud(115200);
00055 
00056     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00057     
00058 #if CHANNEL_PLAN == CP_US915
00059     plan = new lora::ChannelPlan_US915();
00060 #elif CHANNEL_PLAN == CP_AU915
00061     plan = new lora::ChannelPlan_AU915();
00062 #elif CHANNEL_PLAN == CP_EU868
00063     plan = new lora::ChannelPlan_EU868();
00064 #elif CHANNEL_PLAN == CP_KR920
00065     plan = new lora::ChannelPlan_KR920();
00066 #elif CHANNEL_PLAN == CP_AS923
00067     plan = new lora::ChannelPlan_AS923();
00068 #elif CHANNEL_PLAN == CP_AS923_JAPAN
00069     plan = new lora::ChannelPlan_AS923_Japan();
00070 #elif CHANNEL_PLAN == CP_IN865
00071     plan = new lora::ChannelPlan_IN865();
00072 #endif
00073     assert(plan);
00074 
00075     dot = mDot::getInstance(plan);
00076     assert(dot);
00077 
00078     // attach the custom events handler
00079     dot->setEvents(&events);
00080 
00081     if (!dot->getStandbyFlag()) {
00082         logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
00083 
00084         // start from a well-known state
00085         logInfo("defaulting Dot configuration");
00086         dot->resetConfig();
00087         dot->resetNetworkSession();
00088 
00089         // make sure library logging is turned on
00090         dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00091 
00092         // update configuration if necessary
00093         if (dot->getJoinMode() != mDot::OTA) {
00094             logInfo("changing network join mode to OTA");
00095             if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
00096                 logError("failed to set network join mode to OTA");
00097             }
00098         }
00099         // 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
00100         // only one method or the other should be used!
00101         // network ID = crc64(network name)
00102         // network KEY = cmac(network passphrase)
00103         update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
00104         //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
00105 
00106         // configure network link checks
00107         // 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
00108         // check the link every count packets
00109         // declare the Dot disconnected after threshold failed link checks
00110         // 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
00111         update_network_link_check_config(3, 5);
00112 
00113         // enable or disable Adaptive Data Rate
00114         dot->setAdr(adr);
00115     
00116         // save changes to configuration
00117         logInfo("saving configuration");
00118         if (!dot->saveConfig()) {
00119             logError("failed to save configuration");
00120         }
00121 
00122         // display configuration
00123         display_config();
00124     } else {
00125         // restore the saved session if the dot woke from deepsleep mode
00126         // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
00127         logInfo("restoring network session from NVM");
00128         dot->restoreNetworkSession();
00129     }
00130                 
00131         
00132     while (true) { 
00133         float light;
00134         float hum;
00135         float weight;
00136         uint8_t comma = 'T';
00137 
00138         std::vector<uint8_t> tx_data;
00139 
00140         // join network if not joined
00141         if (!dot->getNetworkJoinStatus()) {
00142             join_network();
00143         }
00144         
00145 
00146         light = 20.20;//dht.ReadTemperature(CELCIUS);
00147         hum = 30.10; //dht.ReadHumidity();
00148         weight = 10.52;
00149      
00150         uint8_t* s_light;
00151         int size_s_light = sprintf((char*)s_light,"%f",light);
00152         //pc.printf("%s",s_light);
00153         
00154         //pc.printf("Temperatura: %d/t Humedad: %d/t Peso: %d\n",temp,hum,weight);
00155         
00156         //tx_data.push_back((light >> 8) & 0xFF);
00157         //tx_data.push_back(light & 0xFF);
00158         //logInfo("light: %lu [0x%04X]", light, light);
00159         
00160         //tx_data.push_back((comma >> 8) & 0xFF);
00161         //tx_data.push_back(comma & 0xFF);
00162         //logInfo("comma: %lu [0x%04X]", comma, comma);
00163         
00164         //tx_data.push_back((hum >> 8) & 0xFF);
00165         //tx_data.push_back(hum & 0xFF);
00166         //logInfo("hum: %lu [0x%04X]", hum, hum);
00167         
00168         //tx_data.push_back(comma & 0xFF);
00169         
00170         //tx_data.push_back((weight >> 8) & 0xFF);
00171         //tx_data.push_back(weight & 0xFF);
00172         //logInfo("weight: %lu [0x%04X]", weight, weight);
00173         
00174         tx_data.begin();
00175         tx_data.insert(s_light, size_s_light);
00176         
00177         send_data(tx_data);
00178         
00179         tx_data.end();
00180 
00181         //length = sprintf(pepe, "%d;%d;%d", weight, temp, hum);
00182         //std::vector<uint8_t> vec(pepe, pepe+ length / chat);
00183       
00184         //datos.clear();
00185 
00186 
00187 
00188  
00189         
00190 
00191 
00192         // if going into deepsleep mode, save the session so we don't need to join again after waking up
00193         // not necessary if going into sleep mode since RAM is retained
00194        
00195        if (deep_sleep) {
00196             logInfo("saving network session to NVM");
00197             dot->saveNetworkSession();
00198         }
00199 
00200         // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
00201         //sleep_wake_rtc_only(deep_sleep);
00202         //sleep_wake_interrupt_only(deep_sleep);
00203         sleep_wake_rtc_or_interrupt(deep_sleep);
00204     }
00205  
00206     return 0;
00207 }
00208 
00209 
00210 #endif