A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.

Dependencies:   mbed

Committer:
fahadmirza
Date:
Mon Jul 16 23:54:41 2018 +0000
Revision:
6:1d09b7960d57
Parent:
5:53302861bfea
Child:
7:3c5d342068dd
Bug fix: Added NULL at the end of Recvd data buffer; ASCII downlink data parser added; New keys;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fahadmirza 4:511677d804ac 1 /*
fahadmirza 4:511677d804ac 2 _ _ _____ _______
fahadmirza 4:511677d804ac 3 | | | | |_ _| |__ __|
fahadmirza 4:511677d804ac 4 | |__| | __ ___ __ | | ___ | |
fahadmirza 4:511677d804ac 5 | __ |/ _` \ \/ / | | / _ \| |
fahadmirza 4:511677d804ac 6 | | | | (_| |> < _| || (_) | |
fahadmirza 4:511677d804ac 7 |_| |_|\__,_/_/\_\_____\___/|_|
fahadmirza 4:511677d804ac 8 (C)2017 HaxIoT
fahadmirza 4:511677d804ac 9 */
fahadmirza 4:511677d804ac 10 /*******************************************************************************
fahadmirza 4:511677d804ac 11 * File : lora_driver.h
fahadmirza 4:511677d804ac 12 * Author : Fahad Mirza (Haxiot)
fahadmirza 4:511677d804ac 13 * Version : V1.0.0
fahadmirza 4:511677d804ac 14 * Modified: 13-April-2018
fahadmirza 4:511677d804ac 15 * Brief : Header for driver lora_driver.h module
fahadmirza 4:511677d804ac 16 ******************************************************************************
fahadmirza 4:511677d804ac 17 * @attention
fahadmirza 4:511677d804ac 18 *
fahadmirza 4:511677d804ac 19 * <h2><center>&copy; COPYRIGHT(c) 2017 Haxiot</center></h2>
fahadmirza 4:511677d804ac 20 *
fahadmirza 4:511677d804ac 21 * Redistribution and use in source and binary forms, with or without modification,
fahadmirza 4:511677d804ac 22 * are permitted provided that the following conditions are met:
fahadmirza 4:511677d804ac 23 * 1. Redistributions of source code must retain the above copyright notice,
fahadmirza 4:511677d804ac 24 * this list of conditions and the following disclaimer.
fahadmirza 4:511677d804ac 25 * 2. Redistributions in binary form must reproduce the above copyright notice,
fahadmirza 4:511677d804ac 26 * this list of conditions and the following disclaimer in the documentation
fahadmirza 4:511677d804ac 27 * and/or other materials provided with the distribution.
fahadmirza 4:511677d804ac 28 * 3. Neither the name of Haxiot nor the names of its contributors
fahadmirza 4:511677d804ac 29 * may be used to endorse or promote products derived from this software
fahadmirza 4:511677d804ac 30 * without specific prior written permission.
fahadmirza 4:511677d804ac 31 *
fahadmirza 4:511677d804ac 32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
fahadmirza 4:511677d804ac 33 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
fahadmirza 4:511677d804ac 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
fahadmirza 4:511677d804ac 35 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
fahadmirza 4:511677d804ac 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
fahadmirza 4:511677d804ac 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
fahadmirza 4:511677d804ac 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
fahadmirza 4:511677d804ac 39 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
fahadmirza 4:511677d804ac 40 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
fahadmirza 4:511677d804ac 41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
fahadmirza 4:511677d804ac 42 *
fahadmirza 4:511677d804ac 43 ******************************************************************************
fahadmirza 4:511677d804ac 44 */
fahadmirza 4:511677d804ac 45
fahadmirza 4:511677d804ac 46 #ifndef __LORA_CONF_H
fahadmirza 4:511677d804ac 47 #define __LORA_CONF_H
fahadmirza 4:511677d804ac 48
fahadmirza 4:511677d804ac 49 #ifdef __cplusplus
fahadmirza 4:511677d804ac 50 extern "C" {
fahadmirza 4:511677d804ac 51 #endif
fahadmirza 4:511677d804ac 52
fahadmirza 6:1d09b7960d57 53 #include <ctype.h>
fahadmirza 6:1d09b7960d57 54 #include <string.h>
fahadmirza 4:511677d804ac 55
fahadmirza 4:511677d804ac 56 /* Private Macros ------------------------------------------------------------*/
fahadmirza 4:511677d804ac 57 #define SENSORS_MEASURE_CYCLE (15000U) // Send packet every 15s
fahadmirza 4:511677d804ac 58 #define SENSOR_ON true
fahadmirza 4:511677d804ac 59 #define STRING_PACKET "YourName"
fahadmirza 4:511677d804ac 60
fahadmirza 5:53302861bfea 61 // Uplink packet acknowledgment configuration
fahadmirza 5:53302861bfea 62 #define UNCONFIRMED 0
fahadmirza 5:53302861bfea 63 #define CONFIRMED 1
fahadmirza 4:511677d804ac 64
fahadmirza 4:511677d804ac 65
fahadmirza 4:511677d804ac 66 // Mbed specific declaration
fahadmirza 4:511677d804ac 67 AnalogIn temperatureSensor(A0);
fahadmirza 4:511677d804ac 68 DigitalOut nucleoLED(LED1);
fahadmirza 6:1d09b7960d57 69 DigitalOut blueLED(D11);
fahadmirza 6:1d09b7960d57 70 DigitalOut redLED(D10);
fahadmirza 6:1d09b7960d57 71 DigitalOut greenLED(D9);
fahadmirza 4:511677d804ac 72
fahadmirza 6:1d09b7960d57 73 static void tolower_str(char *strBuffer, uint8_t bufferSize);
fahadmirza 4:511677d804ac 74 /* Private function definitions ----------------------------------------------*/
fahadmirza 4:511677d804ac 75 /******************************************************************************
fahadmirza 4:511677d804ac 76 * @Brief : Uplink packet handler for lora_driver
fahadmirza 4:511677d804ac 77 * @Param : Pointer for payload buffer, data size, ack configuration and port
fahadmirza 4:511677d804ac 78 * @return: none
fahadmirza 4:511677d804ac 79 ******************************************************************************/
fahadmirza 4:511677d804ac 80 static void GetSensorData(uint8_t *buffer, uint8_t *dataSize, uint8_t *ack, uint8_t *port)
fahadmirza 4:511677d804ac 81 {
fahadmirza 4:511677d804ac 82 // Prepare an unconfirmed uplink packet for port 2
fahadmirza 4:511677d804ac 83 uint8_t size = 0;
fahadmirza 4:511677d804ac 84
fahadmirza 4:511677d804ac 85 if(SENSOR_ON == true)
fahadmirza 4:511677d804ac 86 {
fahadmirza 4:511677d804ac 87 uint16_t temperatureValue = temperatureSensor.read_u16();
fahadmirza 4:511677d804ac 88 buffer[size++] = (temperatureValue >> 8) & 0xFF;
fahadmirza 4:511677d804ac 89 buffer[size++] = temperatureValue & 0xFF;
fahadmirza 4:511677d804ac 90 }
fahadmirza 4:511677d804ac 91 else
fahadmirza 4:511677d804ac 92 {
fahadmirza 4:511677d804ac 93 char yourName[] = STRING_PACKET;
fahadmirza 4:511677d804ac 94 for(uint8_t i = 0; (i < 11) && (yourName[i] != NULL); i++)
fahadmirza 4:511677d804ac 95 {
fahadmirza 4:511677d804ac 96 buffer[size++] = yourName[i];
fahadmirza 4:511677d804ac 97 }
fahadmirza 4:511677d804ac 98 }
fahadmirza 4:511677d804ac 99
fahadmirza 4:511677d804ac 100 *dataSize = size;
fahadmirza 4:511677d804ac 101 *ack = UNCONFIRMED;
fahadmirza 4:511677d804ac 102 *port = 2;
fahadmirza 4:511677d804ac 103 }
fahadmirza 4:511677d804ac 104
fahadmirza 4:511677d804ac 105 /******************************************************************************
fahadmirza 4:511677d804ac 106 * @Brief : Downlink packet handler for lora_driver
fahadmirza 4:511677d804ac 107 * LSBytes of the payload controls the Nucleo LED
fahadmirza 4:511677d804ac 108 * @Param : Payload buffer, data size, ack configuration and port
fahadmirza 4:511677d804ac 109 * @return: none
fahadmirza 4:511677d804ac 110 ******************************************************************************/
fahadmirza 4:511677d804ac 111 static void LedControl(uint8_t *buffer, uint8_t dataSize, uint8_t ack, uint8_t port)
fahadmirza 4:511677d804ac 112 {
fahadmirza 6:1d09b7960d57 113 tolower_str((char *)buffer, dataSize);
fahadmirza 6:1d09b7960d57 114
fahadmirza 6:1d09b7960d57 115 if(strncmp("red", (const char *)buffer, 3) == 0)
fahadmirza 6:1d09b7960d57 116 {
fahadmirza 6:1d09b7960d57 117 redLED = 1;
fahadmirza 6:1d09b7960d57 118 greenLED = 0;
fahadmirza 6:1d09b7960d57 119 blueLED = 0;
fahadmirza 6:1d09b7960d57 120 }
fahadmirza 6:1d09b7960d57 121 else if(strncmp("green", (const char *)buffer, 5) == 0)
fahadmirza 6:1d09b7960d57 122 {
fahadmirza 6:1d09b7960d57 123 redLED = 0;
fahadmirza 6:1d09b7960d57 124 greenLED = 1;
fahadmirza 6:1d09b7960d57 125 blueLED = 0;
fahadmirza 6:1d09b7960d57 126 }
fahadmirza 6:1d09b7960d57 127 else if(strncmp("blue", (const char *)buffer, 4) == 0)
fahadmirza 4:511677d804ac 128 {
fahadmirza 6:1d09b7960d57 129 redLED = 0;
fahadmirza 6:1d09b7960d57 130 greenLED = 0;
fahadmirza 6:1d09b7960d57 131 blueLED = 1;
fahadmirza 6:1d09b7960d57 132 }
fahadmirza 6:1d09b7960d57 133 else if(strncmp("off", (const char *)buffer, 4) == 0)
fahadmirza 6:1d09b7960d57 134 {
fahadmirza 6:1d09b7960d57 135 redLED = 0;
fahadmirza 6:1d09b7960d57 136 greenLED = 0;
fahadmirza 6:1d09b7960d57 137 blueLED = 0;
fahadmirza 6:1d09b7960d57 138 }
fahadmirza 6:1d09b7960d57 139 }
fahadmirza 6:1d09b7960d57 140
fahadmirza 6:1d09b7960d57 141 static void tolower_str(char *strBuffer, uint8_t bufferSize)
fahadmirza 6:1d09b7960d57 142 {
fahadmirza 6:1d09b7960d57 143 for(uint8_t i = 0; i < bufferSize; i++)
fahadmirza 6:1d09b7960d57 144 {
fahadmirza 6:1d09b7960d57 145 strBuffer[i] = tolower(strBuffer[i]);
fahadmirza 4:511677d804ac 146 }
fahadmirza 4:511677d804ac 147 }
fahadmirza 4:511677d804ac 148
fahadmirza 4:511677d804ac 149 #ifdef __cplusplus
fahadmirza 4:511677d804ac 150 }
fahadmirza 4:511677d804ac 151 #endif /* __cplusplus */
fahadmirza 4:511677d804ac 152
fahadmirza 4:511677d804ac 153 #endif