Forest fire prediction using sensors and LoRa communications
Dependencies: X_NUCLEO_IKS01A2
main.cpp
- Committer:
- spadala
- Date:
- 2019-06-03
- Revision:
- 51:925c07d0d7cf
- Parent:
- 50:6e615eea1e6f
File content as of revision 51:925c07d0d7cf:
/** * Copyright (c) 2017, Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include <stdio.h> #include "lorawan/LoRaWANInterface.h" #include "lorawan/system/lorawan_data_structures.h" #include "events/EventQueue.h" // Sensor headers #include "mbed.h" #include "XNucleoIKS01A2.h" // Application helpers #include "trace_helper.h" #include "lora_radio_helper.h" using namespace events; //#define WITH_SENSORS #ifdef WITH_SENSORS // Instantiate the expansion board static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); // Retrieve the composing elements of the expansion board static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; #endif // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. // This example only communicates with much shorter messages (<30 bytes). // If longer messages are used, these buffers must be changed accordingly. uint8_t tx_buffer[100]; uint8_t rx_buffer[100]; // Buffers needed to read values from the sensors float value_temp, value_hum; #define SENSOR_BUFFER_SIZE 32 char buffer_temp[SENSOR_BUFFER_SIZE], buffer_hum[SENSOR_BUFFER_SIZE]; /* * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing */ #define TX_TIMER 11000 /** * Maximum number of events for the event queue. * 10 is the safe number for the stack events, however, if application * also uses the queue for whatever purposes, this number should be increased. */ #define MAX_NUMBER_OF_EVENTS 10 /** * Maximum number of retries for CONFIRMED messages before giving up */ #define CONFIRMED_MSG_RETRY_COUNTER 3 /** * This event queue is the global event queue for both the * application and stack. To conserve memory, the stack is designed to run * in the same thread as the application and the application is responsible for * providing an event queue to the stack that will be used for ISR deferment as * well as application information event queuing. */ static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE); /** * Event handler. * * This will be passed to the LoRaWAN stack to queue events for the * application which in turn drive the application. */ static void lora_event_handler(lorawan_event_t event); /** * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. */ static LoRaWANInterface lorawan(radio); /** * Application specific callbacks */ static lorawan_app_callbacks_t callbacks; static char *print_double(char* str, double v); /** * Entry point for application */ int main(void) { printf("\r\n Hello, starting..."); #ifdef WITH_SENSORS // setup sensor stack hum_temp->enable(); press_temp->enable(); // testing sensors hum_temp->get_temperature(&value_temp); hum_temp->get_humidity(&value_hum); printf("\r\n Sensors test:\r\n HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer_temp, value_temp), print_double(buffer_hum, value_hum)); #endif // setup tracing setup_trace(); // stores the status of a call to LoRaWAN protocol lorawan_status_t retcode; // Initialize LoRaWAN stack if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { printf("\r\n LoRa initialization failed! \r\n"); return -1; } printf("\r\n Mbed LoRaWANStack initialized \r\n"); // prepare application callbacks callbacks.events = mbed::callback(lora_event_handler); lorawan.add_app_callbacks(&callbacks); // Set number of retries in case of CONFIRMED messages if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) != LORAWAN_STATUS_OK) { printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); return -1; } printf("\r\n CONFIRMED message retries : %d \r\n", CONFIRMED_MSG_RETRY_COUNTER); // Enable adaptive data rate if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { printf("\r\n enable_adaptive_datarate failed! \r\n"); return -1; } printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); retcode = lorawan.connect(); if (retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { } else { printf("\r\n Connection error, code = %d \r\n", retcode); return -1; } printf("\r\n Connection - In Progress ...\r\n"); // make your event queue dispatching events forever ev_queue.dispatch_forever(); return 0; } #ifndef WITH_SENSORS /* * Sends a message to the Network Server */ static void send_message() { uint16_t packet_len; int16_t retcode; printf("\n\rSending message"); packet_len = sprintf((char *) tx_buffer, "This is a test. LoRa Works! <3"); retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG); if (retcode < 0) { printf("retcode < 0 in send\r\n"); retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") : printf("\r\n send() - Error code %d \r\n", retcode); if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { //retry in 3 seconds printf("LORAWAN_STATUS_WOULD_BLOCK verified, retrying"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { ev_queue.call_in(3000, send_message); } } return; } printf("\r\n %d bytes scheduled for transmission \r\n", retcode); memset(tx_buffer, 0, sizeof(tx_buffer)); } #else static void send_message() { printf("\n\rSending message"); uint16_t packet_len; int16_t retcode; // reading from sensors memset(buffer_temp, 0, sizeof(buffer_temp)); memset(buffer_hum, 0, sizeof(buffer_hum)); hum_temp->get_temperature(&value_temp); hum_temp->get_humidity(&value_hum); printf("\r\nSensors reading: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer_temp, value_temp), print_double(buffer_hum, value_hum)); // format data uint16_t temp_len = sprintf((char *) tx_buffer, buffer_temp); tx_buffer[temp_len] = ':'; uint16_t hum_len = sprintf((char *) &tx_buffer[temp_len+1], buffer_hum); // set final payload and send packet_len = temp_len + hum_len + 1; printf("\r\n Sending to server %s", tx_buffer); retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG); if (retcode < 0) { printf("retcode < 0 in send\r\n"); retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") : printf("\r\n send() - Error code %d \r\n", retcode); if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { //retry in 3 seconds printf("LORAWAN_STATUS_WOULD_BLOCK verified, retrying"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { ev_queue.call_in(3000, send_message); } } return; } printf("\r\n %d bytes scheduled for transmission \r\n", retcode); memset(tx_buffer, 0, sizeof(tx_buffer)); } #endif /* Helper function for printing floats & doubles */ static char *print_double(char* str, double v) { int decimalDigits=2; int i = 1; int intPart, fractPart; int len; char *ptr; /* prepare decimal digits multiplicator */ for (;decimalDigits!=0; i*=10, decimalDigits--); /* calculate integer & fractinal parts */ intPart = (int)v; fractPart = (int)((v-(double)(int)v)*i); /* fill in integer part */ sprintf(str, "%i.", intPart); /* prepare fill in of fractional part */ len = strlen(str); ptr = &str[len]; /* fill in leading fractional zeros */ for (i/=10;i>1; i/=10, ptr++) { if (fractPart >= i) { break; } *ptr = '0'; } /* fill in (rest of) fractional part */ sprintf(ptr, "%i", fractPart); return str; } /** * Event handler */ static void lora_event_handler(lorawan_event_t event) { switch (event) { case CONNECTED: printf("\r\n Connection - Successful \r\n"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); } else { ev_queue.call_every(TX_TIMER, send_message); } break; case DISCONNECTED: ev_queue.break_dispatch(); printf("\r\n Disconnected Successfully \r\n"); break; case TX_DONE: printf("\r\n Message Sent to Network Server \r\n"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); } break; case TX_TIMEOUT: case TX_ERROR: case TX_CRYPTO_ERROR: case TX_SCHEDULING_ERROR: printf("\r\n Transmission Error - EventCode = %d \r\n", event); // try again if (MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); } break; case RX_DONE: printf("\r\n Received message from Network Server \r\n"); break; case RX_TIMEOUT: case RX_ERROR: printf("\r\n Error in reception - Code = %d \r\n", event); break; case JOIN_FAILURE: printf("\r\n OTAA Failed - Check Keys \r\n"); break; case UPLINK_REQUIRED: printf("\r\n Uplink required by NS \r\n"); if (MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); } break; default: MBED_ASSERT("Unknown Event"); } } // EOF