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

Dependencies:   mbed

Committer:
fahadmirza
Date:
Fri Nov 02 23:27:16 2018 +0000
Revision:
28:c777d977b252
Parent:
27:517ca3a30ad7
Child:
29:b48f6176c2cd
- Implemented firmware version get function.; - Firmware version is added to the payload.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fahadmirza 0:a0c5877bd360 1 /*
fahadmirza 0:a0c5877bd360 2 _ _ _____ _______
fahadmirza 0:a0c5877bd360 3 | | | | |_ _| |__ __|
fahadmirza 0:a0c5877bd360 4 | |__| | __ ___ __ | | ___ | |
fahadmirza 0:a0c5877bd360 5 | __ |/ _` \ \/ / | | / _ \| |
fahadmirza 0:a0c5877bd360 6 | | | | (_| |> < _| || (_) | |
fahadmirza 0:a0c5877bd360 7 |_| |_|\__,_/_/\_\_____\___/|_|
fahadmirza 0:a0c5877bd360 8 (C)2017 HaxIoT
fahadmirza 0:a0c5877bd360 9 */
fahadmirza 0:a0c5877bd360 10 /*******************************************************************************
fahadmirza 0:a0c5877bd360 11 * @File : lora_driver.c
fahadmirza 0:a0c5877bd360 12 * @Author : Fahad Mirza (Haxiot)
fahadmirza 0:a0c5877bd360 13 * @Version : V1.0.0
fahadmirza 24:f3b987589609 14 * @Modified: 18 October, 2018
fahadmirza 0:a0c5877bd360 15 * @Brief : LoRa Driver
fahadmirza 0:a0c5877bd360 16 ******************************************************************************
fahadmirza 0:a0c5877bd360 17 * @attention
fahadmirza 0:a0c5877bd360 18 *
fahadmirza 0:a0c5877bd360 19 * <h2><center>&copy; COPYRIGHT(c) 2017 Haxiot</center></h2>
fahadmirza 0:a0c5877bd360 20 *
fahadmirza 0:a0c5877bd360 21 * Redistribution and use in source and binary forms, with or without modification,
fahadmirza 0:a0c5877bd360 22 * are permitted provided that the following conditions are met:
fahadmirza 0:a0c5877bd360 23 * 1. Redistributions of source code must retain the above copyright notice,
fahadmirza 0:a0c5877bd360 24 * this list of conditions and the following disclaimer.
fahadmirza 0:a0c5877bd360 25 * 2. Redistributions in binary form must reproduce the above copyright notice,
fahadmirza 0:a0c5877bd360 26 * this list of conditions and the following disclaimer in the documentation
fahadmirza 0:a0c5877bd360 27 * and/or other materials provided with the distribution.
fahadmirza 0:a0c5877bd360 28 * 3. Neither the name of Haxiot nor the names of its contributors
fahadmirza 0:a0c5877bd360 29 * may be used to endorse or promote products derived from this software
fahadmirza 0:a0c5877bd360 30 * without specific prior written permission.
fahadmirza 0:a0c5877bd360 31 *
fahadmirza 0:a0c5877bd360 32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
fahadmirza 0:a0c5877bd360 33 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
fahadmirza 0:a0c5877bd360 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
fahadmirza 0:a0c5877bd360 35 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
fahadmirza 0:a0c5877bd360 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
fahadmirza 0:a0c5877bd360 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
fahadmirza 0:a0c5877bd360 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
fahadmirza 0:a0c5877bd360 39 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
fahadmirza 0:a0c5877bd360 40 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
fahadmirza 0:a0c5877bd360 41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
fahadmirza 0:a0c5877bd360 42 *
fahadmirza 0:a0c5877bd360 43 ******************************************************************************
fahadmirza 0:a0c5877bd360 44 */
fahadmirza 0:a0c5877bd360 45 /* Includes ------------------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 46 #include <stdio.h>
fahadmirza 0:a0c5877bd360 47 #include "hxc_client.h"
fahadmirza 0:a0c5877bd360 48 #include "debug.h"
fahadmirza 0:a0c5877bd360 49 #include "stm32l0xx_nucleo.h"
fahadmirza 0:a0c5877bd360 50 #include "time_server.h"
fahadmirza 0:a0c5877bd360 51 #include "lora_driver.h"
fahadmirza 0:a0c5877bd360 52 #include "tiny_sscanf.h"
fahadmirza 0:a0c5877bd360 53 #include "utilities.h"
fahadmirza 0:a0c5877bd360 54
fahadmirza 0:a0c5877bd360 55 /* Private Macros ------------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 56 #define JOIN_SEND_DELAY_MAX (10000U) // Randomization range - 10s
fahadmirza 0:a0c5877bd360 57 #define JOIN_STATUS_REQ_DELAY (7000U) // milliseconds. Join req takes 6s.
fahadmirza 0:a0c5877bd360 58
fahadmirza 17:e682a18c3914 59 // Re Authenticate every REJOIN_TIME
fahadmirza 25:50414f44a431 60 #define REJOIN_TIME (1 * 60 * 60 * 1000U) // 1 hour
fahadmirza 17:e682a18c3914 61
fahadmirza 0:a0c5877bd360 62 /* Private global variables --------------------------------------------------*/
fahadmirza 0:a0c5877bd360 63 static sLoraConfig_t *LoraConfigParam;
fahadmirza 0:a0c5877bd360 64 static sLoraDriverParam_t *LoraDriverParam;
fahadmirza 26:176e648c03f6 65 static volatile eDeviceState_t DeviceState = CLIENT_INIT;
fahadmirza 28:c777d977b252 66 static uint8_t FwVersion;
fahadmirza 0:a0c5877bd360 67
fahadmirza 0:a0c5877bd360 68 static TimerEvent_t JoinRequestTimer;
fahadmirza 17:e682a18c3914 69 static TimerEvent_t RejoinTimer;
fahadmirza 0:a0c5877bd360 70 static TimerEvent_t SensorMeasureTimer;
fahadmirza 0:a0c5877bd360 71 static TimerEvent_t NucleoLedTimer;
fahadmirza 0:a0c5877bd360 72 static TimerEvent_t JoinStatusDelayTimer;
fahadmirza 0:a0c5877bd360 73
fahadmirza 0:a0c5877bd360 74 // Object definition for data to be sent to loRa application server
fahadmirza 27:517ca3a30ad7 75 static uint8_t DataBinaryBuff[MAX_PAYLOAD_SIZE];
fahadmirza 0:a0c5877bd360 76 static sSendDataBinary_t SendDataBinary = {DataBinaryBuff, 0 , 0, 0};
fahadmirza 0:a0c5877bd360 77
fahadmirza 0:a0c5877bd360 78 // RNG handler declaration
fahadmirza 0:a0c5877bd360 79 RNG_HandleTypeDef RngHandle = {.Instance = RNG};
fahadmirza 0:a0c5877bd360 80
fahadmirza 0:a0c5877bd360 81 /* Private functions ---------------------------------------------------------*/
fahadmirza 17:e682a18c3914 82 static void OnRejoinTimerEvent(void);
fahadmirza 10:19da323c2bc0 83 static void OnLedTimerEvent(void);
fahadmirza 10:19da323c2bc0 84 static void OnJoinRequestTimerEvt(void);
fahadmirza 10:19da323c2bc0 85 static void OnJoinStatusDelayTimerEvt(void);
fahadmirza 10:19da323c2bc0 86 static void OnSensorMeasureTimerEvt(void);
fahadmirza 0:a0c5877bd360 87 static void setJoinRequestTimer(void);
fahadmirza 0:a0c5877bd360 88
fahadmirza 0:a0c5877bd360 89
fahadmirza 0:a0c5877bd360 90 /* Function definitions ------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 91 /******************************************************************************
fahadmirza 0:a0c5877bd360 92 * @Brief : Initialize LoRa Modem
fahadmirza 0:a0c5877bd360 93 * @Param : sLoraConfig_t
fahadmirza 16:2179ec24dff8 94 * @Return : None
fahadmirza 0:a0c5877bd360 95 ******************************************************************************/
fahadmirza 0:a0c5877bd360 96 void Lora_init(sLoraConfig_t *loraConfig, sLoraDriverParam_t *loraDriverParam)
fahadmirza 0:a0c5877bd360 97 {
fahadmirza 0:a0c5877bd360 98
fahadmirza 0:a0c5877bd360 99 LoraConfigParam = loraConfig;
fahadmirza 0:a0c5877bd360 100 LoraDriverParam = loraDriverParam;
fahadmirza 0:a0c5877bd360 101
fahadmirza 0:a0c5877bd360 102 if(Modem_Init() != AT_OK)
fahadmirza 0:a0c5877bd360 103 {
fahadmirza 0:a0c5877bd360 104 DBG_PRINTF("Modem_Init failed\r\n");
fahadmirza 0:a0c5877bd360 105 }
fahadmirza 0:a0c5877bd360 106
fahadmirza 0:a0c5877bd360 107 // Initialize RNG for join request send randomization
fahadmirza 0:a0c5877bd360 108 HAL_RNG_Init(&RngHandle);
fahadmirza 0:a0c5877bd360 109
fahadmirza 0:a0c5877bd360 110
fahadmirza 0:a0c5877bd360 111 // Timer for join request send
fahadmirza 10:19da323c2bc0 112 TimerInit(&JoinRequestTimer, OnJoinRequestTimerEvt);
fahadmirza 0:a0c5877bd360 113 // Timer for join status check
fahadmirza 10:19da323c2bc0 114 TimerInit(&JoinStatusDelayTimer, OnJoinStatusDelayTimerEvt);
fahadmirza 0:a0c5877bd360 115 // Timer for sensor occurrence measure
fahadmirza 10:19da323c2bc0 116 TimerInit(&SensorMeasureTimer, OnSensorMeasureTimerEvt);
fahadmirza 0:a0c5877bd360 117 // Timer for Nucleo LED
fahadmirza 10:19da323c2bc0 118 TimerInit(&NucleoLedTimer, OnLedTimerEvent);
fahadmirza 17:e682a18c3914 119 // Timer for Re Authenticate / Join
fahadmirza 17:e682a18c3914 120 TimerInit(&RejoinTimer, OnRejoinTimerEvent);
fahadmirza 0:a0c5877bd360 121 }
fahadmirza 0:a0c5877bd360 122
fahadmirza 0:a0c5877bd360 123 /******************************************************************************
fahadmirza 0:a0c5877bd360 124 * @Brief : Check if the modem responds
fahadmirza 0:a0c5877bd360 125 * @Param : void
fahadmirza 0:a0c5877bd360 126 * @Return : AT_OK or other eAtStatus_t
fahadmirza 0:a0c5877bd360 127 ******************************************************************************/
fahadmirza 0:a0c5877bd360 128 static eAtStatus_t is_modem_working(void)
fahadmirza 0:a0c5877bd360 129 {
fahadmirza 0:a0c5877bd360 130 return Modem_AT_Cmd(AT_CTRL, AT, NULL);
fahadmirza 0:a0c5877bd360 131 }
fahadmirza 0:a0c5877bd360 132
fahadmirza 0:a0c5877bd360 133 /******************************************************************************
fahadmirza 0:a0c5877bd360 134 * @Brief : Set Device EUI
fahadmirza 0:a0c5877bd360 135 * @Param : Pointer to Device EUI
fahadmirza 0:a0c5877bd360 136 * @Return : AT_OK or other eAtStatus_t
fahadmirza 0:a0c5877bd360 137 ******************************************************************************/
fahadmirza 10:19da323c2bc0 138 static eAtStatus_t Lora_setDevEui(char *devEui)
fahadmirza 0:a0c5877bd360 139 {
fahadmirza 0:a0c5877bd360 140 return Modem_AT_Cmd(AT_SET, AT_DEVEUI, devEui);
fahadmirza 0:a0c5877bd360 141 }
fahadmirza 0:a0c5877bd360 142
fahadmirza 0:a0c5877bd360 143 /******************************************************************************
fahadmirza 27:517ca3a30ad7 144 * @Brief : Print Device EUI
fahadmirza 27:517ca3a30ad7 145 * @Param : None
fahadmirza 27:517ca3a30ad7 146 * @Retval : AT_OK or other eAtStatus_t
fahadmirza 27:517ca3a30ad7 147 ******************************************************************************/
fahadmirza 27:517ca3a30ad7 148 static void Lora_printDevEui(void)
fahadmirza 27:517ca3a30ad7 149 {
fahadmirza 27:517ca3a30ad7 150 char deveui_str[25];
fahadmirza 27:517ca3a30ad7 151
fahadmirza 27:517ca3a30ad7 152 Modem_AT_Cmd(AT_GET, AT_DEVEUI, deveui_str);
fahadmirza 27:517ca3a30ad7 153
fahadmirza 27:517ca3a30ad7 154 DBG_PRINTF("DevEui: %s\n", deveui_str);
fahadmirza 27:517ca3a30ad7 155 }
fahadmirza 27:517ca3a30ad7 156
fahadmirza 27:517ca3a30ad7 157 /******************************************************************************
fahadmirza 2:1ef859bc5cd2 158 * @Brief : Turn on or off ADR
fahadmirza 2:1ef859bc5cd2 159 * @Param : ADR_ON or ADR_OFF
fahadmirza 2:1ef859bc5cd2 160 * @Return : AT_OK or other eAtStatus_t
fahadmirza 2:1ef859bc5cd2 161 ******************************************************************************/
fahadmirza 10:19da323c2bc0 162 static eAtStatus_t Lora_setAdr(eAdrStatus_t adrStatus)
fahadmirza 2:1ef859bc5cd2 163 {
fahadmirza 2:1ef859bc5cd2 164 //char adr = (adrStatus == ADR_ON ? '1' : '0');
fahadmirza 2:1ef859bc5cd2 165 return Modem_AT_Cmd(AT_SET, AT_ADR, (uint8_t *)(&adrStatus));
fahadmirza 2:1ef859bc5cd2 166 }
fahadmirza 2:1ef859bc5cd2 167
fahadmirza 2:1ef859bc5cd2 168 /******************************************************************************
fahadmirza 0:a0c5877bd360 169 * @Brief : Set Application EUI
fahadmirza 0:a0c5877bd360 170 * @Param : Pointer to Application EUI
fahadmirza 0:a0c5877bd360 171 * @Return : AT_OK or other eAtStatus_t
fahadmirza 0:a0c5877bd360 172 ******************************************************************************/
fahadmirza 10:19da323c2bc0 173 static eAtStatus_t Lora_setAppEui(char *appEui)
fahadmirza 0:a0c5877bd360 174 {
fahadmirza 0:a0c5877bd360 175 return Modem_AT_Cmd(AT_SET, AT_APPEUI, appEui);
fahadmirza 0:a0c5877bd360 176 }
fahadmirza 0:a0c5877bd360 177
fahadmirza 0:a0c5877bd360 178 /******************************************************************************
fahadmirza 0:a0c5877bd360 179 * @Brief : Set Application Key
fahadmirza 0:a0c5877bd360 180 * @Param : Pointer to Application Key
fahadmirza 0:a0c5877bd360 181 * @Return : AT_OK or other eAtStatus_t
fahadmirza 0:a0c5877bd360 182 ******************************************************************************/
fahadmirza 10:19da323c2bc0 183 static eAtStatus_t Lora_setAppKey(char *appKey)
fahadmirza 0:a0c5877bd360 184 {
fahadmirza 0:a0c5877bd360 185 return Modem_AT_Cmd(AT_SET, AT_APPKEY, appKey);
fahadmirza 0:a0c5877bd360 186 }
fahadmirza 0:a0c5877bd360 187
fahadmirza 0:a0c5877bd360 188 /******************************************************************************
fahadmirza 0:a0c5877bd360 189 * @Brief : Set join mode
fahadmirza 0:a0c5877bd360 190 * @Param : OTAA or ABP
fahadmirza 0:a0c5877bd360 191 * @Retval : AT_OK if successful, otherwise other eAtStatus_t
fahadmirza 0:a0c5877bd360 192 ******************************************************************************/
fahadmirza 10:19da323c2bc0 193 static eAtStatus_t Lora_setJoinMode(eJoinMode_t joinMode)
fahadmirza 0:a0c5877bd360 194 {
fahadmirza 0:a0c5877bd360 195 if(joinMode == OTAA)
fahadmirza 0:a0c5877bd360 196 {
fahadmirza 0:a0c5877bd360 197 return Modem_AT_Cmd(AT_SET, AT_NJM, "OTAA");
fahadmirza 0:a0c5877bd360 198 }
fahadmirza 0:a0c5877bd360 199
fahadmirza 0:a0c5877bd360 200 return Modem_AT_Cmd(AT_SET, AT_NJM, "ABP");
fahadmirza 0:a0c5877bd360 201 }
fahadmirza 0:a0c5877bd360 202
fahadmirza 0:a0c5877bd360 203 /******************************************************************************
fahadmirza 0:a0c5877bd360 204 * @Brief : Set Class
fahadmirza 0:a0c5877bd360 205 * @Param : CLASS_A or CLASS_C
fahadmirza 0:a0c5877bd360 206 * @Retval : AT_OK if successful, otherwise other eAtStatus_t
fahadmirza 0:a0c5877bd360 207 ******************************************************************************/
fahadmirza 10:19da323c2bc0 208 static eAtStatus_t Lora_setClass(char class)
fahadmirza 0:a0c5877bd360 209 {
fahadmirza 0:a0c5877bd360 210 return Modem_AT_Cmd(AT_SET, AT_CLASS, &class);
fahadmirza 0:a0c5877bd360 211 }
fahadmirza 0:a0c5877bd360 212
fahadmirza 0:a0c5877bd360 213 /******************************************************************************
fahadmirza 0:a0c5877bd360 214 * @Brief : Join network and initiate join sleep transition timer
fahadmirza 0:a0c5877bd360 215 * @Param : None
fahadmirza 0:a0c5877bd360 216 * @Retval : AT_OK or other eAtStatus_t
fahadmirza 0:a0c5877bd360 217 ******************************************************************************/
fahadmirza 0:a0c5877bd360 218 static eAtStatus_t Lora_Join(void)
fahadmirza 0:a0c5877bd360 219 {
fahadmirza 0:a0c5877bd360 220 return Modem_AT_Cmd(AT_CTRL, AT_JOIN, NULL);
fahadmirza 0:a0c5877bd360 221 }
fahadmirza 0:a0c5877bd360 222
fahadmirza 0:a0c5877bd360 223 /******************************************************************************
fahadmirza 0:a0c5877bd360 224 * @Brief : Check JOIN status
fahadmirza 0:a0c5877bd360 225 * @Param : None
fahadmirza 0:a0c5877bd360 226 * @Retval : JOINED or NOT_JOINED
fahadmirza 0:a0c5877bd360 227 ******************************************************************************/
fahadmirza 0:a0c5877bd360 228 static eJoinStatus_t Lora_getJoinStatus(void)
fahadmirza 0:a0c5877bd360 229 {
fahadmirza 0:a0c5877bd360 230 char joinStatus[12]; // "NOT JOINED" is 10 characters + 1 Null char
fahadmirza 0:a0c5877bd360 231
fahadmirza 0:a0c5877bd360 232 Modem_AT_Cmd(AT_GET, AT_NJS, joinStatus);
fahadmirza 0:a0c5877bd360 233
fahadmirza 0:a0c5877bd360 234 if(strncmp("JOINED", joinStatus, 6) == 0)
fahadmirza 0:a0c5877bd360 235 {
fahadmirza 0:a0c5877bd360 236 return JOINED;
fahadmirza 0:a0c5877bd360 237 }
fahadmirza 0:a0c5877bd360 238
fahadmirza 0:a0c5877bd360 239 return NOT_JOINED;
fahadmirza 0:a0c5877bd360 240 }
fahadmirza 0:a0c5877bd360 241
fahadmirza 0:a0c5877bd360 242 /******************************************************************************
fahadmirza 28:c777d977b252 243 * @Brief : Get firmware version
fahadmirza 28:c777d977b252 244 * @Param : None
fahadmirza 28:c777d977b252 245 * @Retval : Integer firmware version
fahadmirza 28:c777d977b252 246 ******************************************************************************/
fahadmirza 28:c777d977b252 247 static uint8_t Lora_getFwVersion(void)
fahadmirza 28:c777d977b252 248 {
fahadmirza 28:c777d977b252 249 char fwVersion_str[5];
fahadmirza 28:c777d977b252 250 float fwVer_float;
fahadmirza 28:c777d977b252 251
fahadmirza 28:c777d977b252 252 eAtStatus_t status = Modem_AT_Cmd(AT_GET, AT_VER, fwVersion_str);
fahadmirza 28:c777d977b252 253
fahadmirza 28:c777d977b252 254 if(status != AT_OK)
fahadmirza 28:c777d977b252 255 {
fahadmirza 28:c777d977b252 256 return 0;
fahadmirza 28:c777d977b252 257 }
fahadmirza 28:c777d977b252 258
fahadmirza 28:c777d977b252 259 sscanf(fwVersion_str, "%f", &fwVer_float);
fahadmirza 28:c777d977b252 260 // This weird division was needed. Otherwise, because of
fahadmirza 28:c777d977b252 261 // the floating point calculation the result was rounded.
fahadmirza 28:c777d977b252 262 uint8_t fwVer_int = (uint8_t)(fwVer_float * 1000/10);
fahadmirza 28:c777d977b252 263
fahadmirza 28:c777d977b252 264 return fwVer_int;
fahadmirza 28:c777d977b252 265 }
fahadmirza 28:c777d977b252 266
fahadmirza 28:c777d977b252 267 /******************************************************************************
fahadmirza 0:a0c5877bd360 268 * @Brief : Send uplink packet using binary payload
fahadmirza 0:a0c5877bd360 269 * @Param : Pointer of sSendDataBinary_t variable
fahadmirza 0:a0c5877bd360 270 * @Retval : AT_OK or other eAtStatus_t statuses
fahadmirza 0:a0c5877bd360 271 ******************************************************************************/
fahadmirza 0:a0c5877bd360 272 static eAtStatus_t Lora_SendDataBinary(sSendDataBinary_t *binaryData)
fahadmirza 0:a0c5877bd360 273 {
fahadmirza 0:a0c5877bd360 274 return Modem_AT_Cmd(AT_SET, AT_SENDB, binaryData);
fahadmirza 0:a0c5877bd360 275 }
fahadmirza 0:a0c5877bd360 276
fahadmirza 0:a0c5877bd360 277 /******************************************************************************
fahadmirza 10:19da323c2bc0 278 * @Brief : Read the received downlink packet
fahadmirza 10:19da323c2bc0 279 * @Param : Pointer to sRecvDataBinary_t variable
fahadmirza 16:2179ec24dff8 280 * @Return : None
fahadmirza 10:19da323c2bc0 281 ******************************************************************************/
fahadmirza 10:19da323c2bc0 282 static void Lora_ReadData(sRecvDataBinary_t *receivedData)
fahadmirza 10:19da323c2bc0 283 {
fahadmirza 27:517ca3a30ad7 284 char receiveString[MAX_PAYLOAD_SIZE];
fahadmirza 10:19da323c2bc0 285
fahadmirza 10:19da323c2bc0 286 Modem_AT_Cmd(AT_GET, AT_RECVB, receiveString);
fahadmirza 10:19da323c2bc0 287
fahadmirza 10:19da323c2bc0 288 tiny_sscanf(receiveString, "%hhu,%hhu", &(receivedData->Ack), &(receivedData->Port));
fahadmirza 10:19da323c2bc0 289
fahadmirza 10:19da323c2bc0 290 // Find the position after the second comma
fahadmirza 10:19da323c2bc0 291 char *hexString = strchr((strchr(receiveString,',') + 1),',') + 1;
fahadmirza 10:19da323c2bc0 292
fahadmirza 27:517ca3a30ad7 293 receivedData->DataSize = stringHexToByteArray(hexString, receivedData->Buffer, MAX_PAYLOAD_SIZE);
fahadmirza 10:19da323c2bc0 294 }
fahadmirza 10:19da323c2bc0 295
fahadmirza 10:19da323c2bc0 296 /******************************************************************************
fahadmirza 10:19da323c2bc0 297 * @Brief : Change Lora_fsm() DeviceState
fahadmirza 10:19da323c2bc0 298 * @Param : Any of eDeviceState_t type
fahadmirza 10:19da323c2bc0 299 * @Return : None
fahadmirza 10:19da323c2bc0 300 ******************************************************************************/
fahadmirza 10:19da323c2bc0 301 void Lora_ChangeDeviceState(eDeviceState_t newDeviceState)
fahadmirza 10:19da323c2bc0 302 {
fahadmirza 10:19da323c2bc0 303 //ToDo: Check for valid eDeviceState_t states
fahadmirza 10:19da323c2bc0 304 DeviceState = newDeviceState;
fahadmirza 10:19da323c2bc0 305 }
fahadmirza 10:19da323c2bc0 306
fahadmirza 10:19da323c2bc0 307 /******************************************************************************
fahadmirza 0:a0c5877bd360 308 * @Brief : LoRa Modem state machine
fahadmirza 0:a0c5877bd360 309 * @Param : Void
fahadmirza 0:a0c5877bd360 310 * @Return : None
fahadmirza 0:a0c5877bd360 311 ******************************************************************************/
fahadmirza 0:a0c5877bd360 312 void Lora_fsm(void)
fahadmirza 0:a0c5877bd360 313 {
fahadmirza 0:a0c5877bd360 314 switch(DeviceState)
fahadmirza 0:a0c5877bd360 315 {
fahadmirza 26:176e648c03f6 316 case CLIENT_INIT:
fahadmirza 0:a0c5877bd360 317 {
fahadmirza 0:a0c5877bd360 318 if(is_modem_working() != AT_OK)
fahadmirza 0:a0c5877bd360 319 {
fahadmirza 1:168a6afffbff 320 DBG_PRINTF("AT failed. Resetting HW...\r\n");
fahadmirza 0:a0c5877bd360 321 // Modem isn't responding. Execute hard reset.
fahadmirza 0:a0c5877bd360 322 Modem_HardReset();
fahadmirza 27:517ca3a30ad7 323 // We stay in CLIENT_INIT state and try again.
fahadmirza 0:a0c5877bd360 324 }
fahadmirza 0:a0c5877bd360 325 else
fahadmirza 0:a0c5877bd360 326 {
fahadmirza 26:176e648c03f6 327 DeviceState = CLIENT_CONFIG;
fahadmirza 0:a0c5877bd360 328 }
fahadmirza 0:a0c5877bd360 329
fahadmirza 0:a0c5877bd360 330 break;
fahadmirza 0:a0c5877bd360 331 }
fahadmirza 26:176e648c03f6 332 case CLIENT_CONFIG:
fahadmirza 0:a0c5877bd360 333 {
fahadmirza 24:f3b987589609 334 eAtStatus_t loraModemRetCode = Lora_setDevEui(LoraConfigParam->DevEui);
fahadmirza 24:f3b987589609 335 loraModemRetCode |= Lora_setAppEui(LoraConfigParam->AppEui);
fahadmirza 24:f3b987589609 336 loraModemRetCode |= Lora_setAppKey(LoraConfigParam->AppKey);
fahadmirza 24:f3b987589609 337 loraModemRetCode |= Lora_setJoinMode(LoraConfigParam->JoinMode);
fahadmirza 24:f3b987589609 338 loraModemRetCode |= Lora_setClass(LoraConfigParam->Class);
fahadmirza 28:c777d977b252 339 loraModemRetCode |= Lora_setAdr(LoraConfigParam->AdrStatus);
fahadmirza 0:a0c5877bd360 340
fahadmirza 0:a0c5877bd360 341 if(loraModemRetCode == AT_OK)
fahadmirza 0:a0c5877bd360 342 {
fahadmirza 28:c777d977b252 343 // If users use AUTO as Device EUI, print the DevEUI
fahadmirza 28:c777d977b252 344 // so that they can add that on X-ON
fahadmirza 28:c777d977b252 345 Lora_printDevEui();
fahadmirza 28:c777d977b252 346 // Get the FW Version
fahadmirza 28:c777d977b252 347 FwVersion = Lora_getFwVersion();
fahadmirza 28:c777d977b252 348
fahadmirza 0:a0c5877bd360 349 setJoinRequestTimer();
fahadmirza 26:176e648c03f6 350 DeviceState = CLIENT_SLEEP;
fahadmirza 0:a0c5877bd360 351 }
fahadmirza 0:a0c5877bd360 352 else if(loraModemRetCode == AT_TIMEOUT)
fahadmirza 0:a0c5877bd360 353 {
fahadmirza 26:176e648c03f6 354 DeviceState = CLIENT_INIT;
fahadmirza 0:a0c5877bd360 355 }
fahadmirza 0:a0c5877bd360 356 else
fahadmirza 0:a0c5877bd360 357 {
fahadmirza 0:a0c5877bd360 358 DBG_PRINTF("Check your keys\r\n");
fahadmirza 27:517ca3a30ad7 359 while(1);
fahadmirza 0:a0c5877bd360 360 }
fahadmirza 0:a0c5877bd360 361
fahadmirza 0:a0c5877bd360 362 break;
fahadmirza 0:a0c5877bd360 363 }
fahadmirza 26:176e648c03f6 364 case CLIENT_JOIN:
fahadmirza 0:a0c5877bd360 365 {
fahadmirza 1:168a6afffbff 366 DBG_PRINTF("Joining...\r\n");
fahadmirza 0:a0c5877bd360 367 // Indicate a Join request using Nucleo LED
fahadmirza 0:a0c5877bd360 368 BSP_LED_On(LED_GREEN);
fahadmirza 0:a0c5877bd360 369 TimerStart(&NucleoLedTimer, 200);
fahadmirza 0:a0c5877bd360 370
fahadmirza 0:a0c5877bd360 371 switch(Lora_Join())
fahadmirza 0:a0c5877bd360 372 {
fahadmirza 0:a0c5877bd360 373 case AT_OK:
fahadmirza 0:a0c5877bd360 374 {
fahadmirza 0:a0c5877bd360 375 // Start the Join status request timer and go to sleep
fahadmirza 0:a0c5877bd360 376 TimerStart(&JoinStatusDelayTimer, JOIN_STATUS_REQ_DELAY);
fahadmirza 26:176e648c03f6 377 DeviceState = CLIENT_SLEEP;
fahadmirza 0:a0c5877bd360 378 break;
fahadmirza 0:a0c5877bd360 379 }
fahadmirza 0:a0c5877bd360 380 case AT_TIMEOUT:
fahadmirza 0:a0c5877bd360 381 {
fahadmirza 21:f1d561ed31a1 382 // The modem isn't responding. Execute hard reset
fahadmirza 26:176e648c03f6 383 DeviceState = CLIENT_INIT;
fahadmirza 0:a0c5877bd360 384 break;
fahadmirza 0:a0c5877bd360 385 }
fahadmirza 0:a0c5877bd360 386 default:
fahadmirza 0:a0c5877bd360 387 {
fahadmirza 0:a0c5877bd360 388 DBG_PRINTF("Join cmd failed\n");
fahadmirza 27:517ca3a30ad7 389 // We stay in CLIENT_JOIN state and redo Lora_Join()
fahadmirza 0:a0c5877bd360 390 break;
fahadmirza 0:a0c5877bd360 391 }
fahadmirza 0:a0c5877bd360 392 }
fahadmirza 0:a0c5877bd360 393
fahadmirza 0:a0c5877bd360 394 break;
fahadmirza 0:a0c5877bd360 395 }
fahadmirza 26:176e648c03f6 396 case CLIENT_JOIN_STATUS_CHECK:
fahadmirza 0:a0c5877bd360 397 {
fahadmirza 0:a0c5877bd360 398 if(Lora_getJoinStatus() == JOINED)
fahadmirza 0:a0c5877bd360 399 {
fahadmirza 12:f1fd61aa85e0 400 // Inidicate Join status using LED
fahadmirza 12:f1fd61aa85e0 401 GPIO_InitTypeDef GPIO_InitStruct;
fahadmirza 12:f1fd61aa85e0 402
fahadmirza 12:f1fd61aa85e0 403 GPIO_InitStruct.Pin = GPIO_PIN_7;
fahadmirza 12:f1fd61aa85e0 404 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
fahadmirza 12:f1fd61aa85e0 405 GPIO_InitStruct.Pull = GPIO_NOPULL;
fahadmirza 12:f1fd61aa85e0 406 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
fahadmirza 12:f1fd61aa85e0 407
fahadmirza 12:f1fd61aa85e0 408 GPIO_InitStruct.Pin = GPIO_PIN_6;
fahadmirza 12:f1fd61aa85e0 409 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
fahadmirza 12:f1fd61aa85e0 410 GPIO_InitStruct.Pull = GPIO_NOPULL;
fahadmirza 12:f1fd61aa85e0 411 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
fahadmirza 12:f1fd61aa85e0 412
fahadmirza 12:f1fd61aa85e0 413 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
fahadmirza 12:f1fd61aa85e0 414 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
fahadmirza 12:f1fd61aa85e0 415
fahadmirza 0:a0c5877bd360 416 DBG_PRINTF("Nwk Joined\n");
fahadmirza 17:e682a18c3914 417
fahadmirza 17:e682a18c3914 418 // Start timer for Re-Authentication
fahadmirza 17:e682a18c3914 419 TimerStart(&RejoinTimer, REJOIN_TIME);
fahadmirza 17:e682a18c3914 420
fahadmirza 0:a0c5877bd360 421 // Start timer for uplink transmission for sensor-data
fahadmirza 17:e682a18c3914 422 TimerStart(&SensorMeasureTimer, LoraDriverParam->SensorMeasureTime);
fahadmirza 26:176e648c03f6 423 DeviceState = CLIENT_SLEEP;
fahadmirza 0:a0c5877bd360 424 }
fahadmirza 0:a0c5877bd360 425 else
fahadmirza 0:a0c5877bd360 426 {
fahadmirza 0:a0c5877bd360 427 // Try joining again
fahadmirza 0:a0c5877bd360 428 setJoinRequestTimer();
fahadmirza 26:176e648c03f6 429 DeviceState = CLIENT_SLEEP;
fahadmirza 0:a0c5877bd360 430 }
fahadmirza 0:a0c5877bd360 431 break;
fahadmirza 0:a0c5877bd360 432 }
fahadmirza 26:176e648c03f6 433 case CLIENT_SLEEP:
fahadmirza 0:a0c5877bd360 434 {
fahadmirza 0:a0c5877bd360 435 /* Wake up through RTC events or asynchronous event coming from HXC modem*/
fahadmirza 0:a0c5877bd360 436 if(Modem_IsNewDataReceived() == true)
fahadmirza 0:a0c5877bd360 437 {
fahadmirza 26:176e648c03f6 438 DeviceState = CLIENT_DATA_RECEIVED;
fahadmirza 0:a0c5877bd360 439 }
fahadmirza 0:a0c5877bd360 440 break;
fahadmirza 0:a0c5877bd360 441 }
fahadmirza 26:176e648c03f6 442 case CLIENT_SEND:
fahadmirza 0:a0c5877bd360 443 {
fahadmirza 28:c777d977b252 444 SendDataBinary.DataSize = 0;
fahadmirza 28:c777d977b252 445 // Add Firmware version
fahadmirza 28:c777d977b252 446 SendDataBinary.Buffer[SendDataBinary.DataSize++] = FwVersion;
fahadmirza 0:a0c5877bd360 447 // Read sensor data and populate payload
fahadmirza 28:c777d977b252 448 SendDataBinary.DataSize = SendDataBinary.DataSize + LoraDriverParam->SendDataHandler(\
fahadmirza 28:c777d977b252 449 &SendDataBinary.Buffer[SendDataBinary.DataSize],\
fahadmirza 28:c777d977b252 450 (MAX_PAYLOAD_SIZE - SendDataBinary.DataSize), \
fahadmirza 28:c777d977b252 451 &SendDataBinary.Ack, &SendDataBinary.Port);
fahadmirza 28:c777d977b252 452
fahadmirza 0:a0c5877bd360 453 // Initiate uplink transmission
fahadmirza 0:a0c5877bd360 454 eAtStatus_t status = Lora_SendDataBinary(&SendDataBinary);
fahadmirza 28:c777d977b252 455
fahadmirza 0:a0c5877bd360 456 if (status == AT_OK)
fahadmirza 0:a0c5877bd360 457 {
fahadmirza 25:50414f44a431 458 DBG_PRINTF("Uplink sent\n");
fahadmirza 0:a0c5877bd360 459 // Schedule the next packet
fahadmirza 25:50414f44a431 460 TimerStart(&SensorMeasureTimer, LoraDriverParam->SensorMeasureTime);
fahadmirza 26:176e648c03f6 461 DeviceState = CLIENT_SLEEP;
fahadmirza 0:a0c5877bd360 462 }
fahadmirza 0:a0c5877bd360 463 else if(status == AT_TIMEOUT)
fahadmirza 0:a0c5877bd360 464 {
fahadmirza 0:a0c5877bd360 465 // Device isn't responding. Go to init.
fahadmirza 26:176e648c03f6 466 DeviceState = CLIENT_INIT;
fahadmirza 0:a0c5877bd360 467 }
fahadmirza 25:50414f44a431 468 else
fahadmirza 25:50414f44a431 469 {
fahadmirza 25:50414f44a431 470 DBG_PRINTF("Uplink Failed (Error: %d)\n", (uint8_t)status);
fahadmirza 25:50414f44a431 471 }
fahadmirza 0:a0c5877bd360 472
fahadmirza 0:a0c5877bd360 473 break;
fahadmirza 0:a0c5877bd360 474 }
fahadmirza 26:176e648c03f6 475 case CLIENT_DATA_RECEIVED:
fahadmirza 0:a0c5877bd360 476 {
fahadmirza 0:a0c5877bd360 477 uint8_t rBuffer[64];
fahadmirza 0:a0c5877bd360 478 sRecvDataBinary_t rxPacket = {.Buffer = rBuffer};
fahadmirza 0:a0c5877bd360 479
fahadmirza 0:a0c5877bd360 480 Lora_ReadData(&rxPacket);
fahadmirza 0:a0c5877bd360 481
fahadmirza 0:a0c5877bd360 482 // Execute users ReceivedPacketHandler function
fahadmirza 0:a0c5877bd360 483 LoraDriverParam->ReceiveDataHandler(rxPacket.Buffer, rxPacket.DataSize, rxPacket.Ack, rxPacket.Port);
fahadmirza 0:a0c5877bd360 484
fahadmirza 26:176e648c03f6 485 DeviceState = CLIENT_SLEEP;
fahadmirza 0:a0c5877bd360 486 break;
fahadmirza 0:a0c5877bd360 487 }
fahadmirza 0:a0c5877bd360 488 default:
fahadmirza 0:a0c5877bd360 489 {
fahadmirza 26:176e648c03f6 490 DeviceState = CLIENT_INIT;
fahadmirza 0:a0c5877bd360 491 break;
fahadmirza 0:a0c5877bd360 492 }
fahadmirza 0:a0c5877bd360 493 }
fahadmirza 0:a0c5877bd360 494 }
fahadmirza 0:a0c5877bd360 495
fahadmirza 16:2179ec24dff8 496 /******************************************************************************
fahadmirza 16:2179ec24dff8 497 * @Brief : Set Join request timer
fahadmirza 16:2179ec24dff8 498 * @Param : none
fahadmirza 16:2179ec24dff8 499 * @Return : none
fahadmirza 16:2179ec24dff8 500 ******************************************************************************/
fahadmirza 0:a0c5877bd360 501 static void setJoinRequestTimer(void)
fahadmirza 0:a0c5877bd360 502 {
fahadmirza 0:a0c5877bd360 503 // Use a random delay to avoid synchronized join request from all LoRa node after power up
fahadmirza 0:a0c5877bd360 504 uint32_t joinDelay = (HAL_RNG_GetRandomNumber(&RngHandle) % JOIN_SEND_DELAY_MAX) + 1;
fahadmirza 19:0c90e38fc9c5 505
fahadmirza 25:50414f44a431 506 TimerStart(&JoinRequestTimer, joinDelay);
fahadmirza 0:a0c5877bd360 507 }
fahadmirza 0:a0c5877bd360 508
fahadmirza 0:a0c5877bd360 509 /******************************************************************************
fahadmirza 0:a0c5877bd360 510 * @Brief : Function executed on JoinStatusDelayTimer Timeout event
fahadmirza 0:a0c5877bd360 511 * @Param : none
fahadmirza 0:a0c5877bd360 512 * @Return : none
fahadmirza 0:a0c5877bd360 513 ******************************************************************************/
fahadmirza 10:19da323c2bc0 514 static void OnJoinRequestTimerEvt(void)
fahadmirza 0:a0c5877bd360 515 {
fahadmirza 0:a0c5877bd360 516 TimerStop(&JoinRequestTimer);
fahadmirza 27:517ca3a30ad7 517
fahadmirza 27:517ca3a30ad7 518 // If this is a re-join SensorMeasureTimer is active
fahadmirza 27:517ca3a30ad7 519 TimerStop(&SensorMeasureTimer);
fahadmirza 27:517ca3a30ad7 520
fahadmirza 26:176e648c03f6 521 DeviceState = CLIENT_JOIN;
fahadmirza 0:a0c5877bd360 522 }
fahadmirza 0:a0c5877bd360 523
fahadmirza 0:a0c5877bd360 524 /******************************************************************************
fahadmirza 0:a0c5877bd360 525 * @Brief : Function executed on NucleoLedTimer Timeout event
fahadmirza 0:a0c5877bd360 526 * @Param : none
fahadmirza 0:a0c5877bd360 527 * @Return : none
fahadmirza 0:a0c5877bd360 528 ******************************************************************************/
fahadmirza 10:19da323c2bc0 529 static void OnLedTimerEvent(void)
fahadmirza 0:a0c5877bd360 530 {
fahadmirza 0:a0c5877bd360 531 TimerStop(&NucleoLedTimer);
fahadmirza 0:a0c5877bd360 532 BSP_LED_Off(LED_GREEN);
fahadmirza 0:a0c5877bd360 533 }
fahadmirza 0:a0c5877bd360 534
fahadmirza 0:a0c5877bd360 535 /******************************************************************************
fahadmirza 0:a0c5877bd360 536 * @Brief : Function executed on JoinStatusDelayTimer Timeout event
fahadmirza 0:a0c5877bd360 537 * @Param : none
fahadmirza 0:a0c5877bd360 538 * @Return : none
fahadmirza 0:a0c5877bd360 539 ******************************************************************************/
fahadmirza 10:19da323c2bc0 540 static void OnJoinStatusDelayTimerEvt(void)
fahadmirza 0:a0c5877bd360 541 {
fahadmirza 25:50414f44a431 542 TimerStop(&JoinStatusDelayTimer);
fahadmirza 26:176e648c03f6 543 DeviceState = CLIENT_JOIN_STATUS_CHECK;
fahadmirza 0:a0c5877bd360 544 }
fahadmirza 0:a0c5877bd360 545
fahadmirza 0:a0c5877bd360 546 /******************************************************************************
fahadmirza 0:a0c5877bd360 547 * @Brief : Function executed on SensorMeasureTimer Timeout event
fahadmirza 0:a0c5877bd360 548 * @Param : none
fahadmirza 0:a0c5877bd360 549 * @Return : none
fahadmirza 0:a0c5877bd360 550 ******************************************************************************/
fahadmirza 10:19da323c2bc0 551 static void OnSensorMeasureTimerEvt(void)
fahadmirza 0:a0c5877bd360 552 {
fahadmirza 25:50414f44a431 553 TimerStop(&SensorMeasureTimer);
fahadmirza 26:176e648c03f6 554 DeviceState = CLIENT_SEND;
fahadmirza 0:a0c5877bd360 555 }
fahadmirza 0:a0c5877bd360 556
fahadmirza 0:a0c5877bd360 557 /******************************************************************************
fahadmirza 17:e682a18c3914 558 * @Brief : Function executed on RejoinTimer Timeout event
fahadmirza 17:e682a18c3914 559 * @Param : none
fahadmirza 17:e682a18c3914 560 * @Return : none
fahadmirza 17:e682a18c3914 561 ******************************************************************************/
fahadmirza 17:e682a18c3914 562 static void OnRejoinTimerEvent(void)
fahadmirza 17:e682a18c3914 563 {
fahadmirza 27:517ca3a30ad7 564 TimerStop(&RejoinTimer);
fahadmirza 17:e682a18c3914 565 setJoinRequestTimer();
fahadmirza 17:e682a18c3914 566 }
fahadmirza 17:e682a18c3914 567
fahadmirza 17:e682a18c3914 568 /******************************************************************************
fahadmirza 0:a0c5877bd360 569 * @brief RNG MSP Initialization
fahadmirza 0:a0c5877bd360 570 * This function configures the hardware resources used in this example:
fahadmirza 0:a0c5877bd360 571 * - Peripheral's clock enable
fahadmirza 0:a0c5877bd360 572 * @param hrng: RNG handle pointer
fahadmirza 0:a0c5877bd360 573 * @retval None
fahadmirza 0:a0c5877bd360 574 ******************************************************************************/
fahadmirza 0:a0c5877bd360 575 void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
fahadmirza 0:a0c5877bd360 576 {
fahadmirza 0:a0c5877bd360 577 /* RNG Peripheral clock enable */
fahadmirza 0:a0c5877bd360 578 __HAL_RCC_RNG_CLK_ENABLE();
fahadmirza 0:a0c5877bd360 579 }
fahadmirza 0:a0c5877bd360 580
fahadmirza 0:a0c5877bd360 581