BLE Transmitter not working

Fork of Dealer_23Feb by kumar singh

Committer:
NarendraSingh
Date:
Thu Feb 23 08:15:58 2017 +0000
Revision:
23:688ee106c385
Parent:
22:c2f034a13108
Child:
24:1063cfc311e5
basic lora packet sending working.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:491820ee784d 1 #include "mbed.h"
NarendraSingh 11:77e595130230 2 #include "rtos.h"
NarendraSingh 11:77e595130230 3 #include "OBD.h"
NarendraSingh 11:77e595130230 4 #include "Lora.h"
NarendraSingh 11:77e595130230 5 #include "Accelerometer.h"
NarendraSingh 11:77e595130230 6 #include "Beacon.h"
NarendraSingh 11:77e595130230 7 #include "main.h"
NarendraSingh 11:77e595130230 8
NarendraSingh 11:77e595130230 9 //Datatype typecasting
NarendraSingh 11:77e595130230 10 typedef unsigned char uint8;
NarendraSingh 11:77e595130230 11 typedef unsigned int uint16;
NarendraSingh 11:77e595130230 12
NarendraSingh 16:7703b9d92326 13 uint8 OBD_Plug_In_Status = FALSE;
NarendraSingh 16:7703b9d92326 14
NarendraSingh 16:7703b9d92326 15 //peripheral connection
emilmont 1:491820ee784d 16 DigitalOut led1(LED1);
emilmont 1:491820ee784d 17 DigitalOut led2(LED2);
NarendraSingh 11:77e595130230 18
NarendraSingh 11:77e595130230 19 //Configure Serial port
NarendraSingh 16:7703b9d92326 20 RawSerial LORA_UART(PA_0, PA_1);//USART4_TX->PA_0,USART4_RX->PA_1 : Used for Lora module command sending and reception from gateway
NarendraSingh 23:688ee106c385 21 //RawSerial pc1(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only
NarendraSingh 23:688ee106c385 22 //RawSerial DEBUG_UART(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only
NarendraSingh 16:7703b9d92326 23 RawSerial Beacon_UART(PC_4, PC_5);//USART3_TX->PC4,USART3_RX->PC_5 : Used for sending command to beacon module
NarendraSingh 16:7703b9d92326 24 RawSerial BLE_RECEIVER_UART(PA_9, PA_10);//USART1_TX->PA_0,USART1_RX->PA_1 : Used for Lora module command sending and reception from gateway
NarendraSingh 23:688ee106c385 25 Serial pc1(USBTX, USBRX);
NarendraSingh 23:688ee106c385 26 RawSerial DEBUG_UART(USBTX, USBRX);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only
NarendraSingh 16:7703b9d92326 27
NarendraSingh 16:7703b9d92326 28 //InterruptIn OBD_PLUGIN_INTERRUPT_PIN(PC_13);
NarendraSingh 21:a5fb0ae94dc6 29 InterruptIn CheckIn_Interrupt(PB_7);//(PC_13);
NarendraSingh 16:7703b9d92326 30
NarendraSingh 16:7703b9d92326 31 uint8 Ticker_Count = 0; //Variable to count for timer overflows
NarendraSingh 16:7703b9d92326 32
NarendraSingh 16:7703b9d92326 33 uint8 OBD_Plugin_Detected = FALSE;
NarendraSingh 16:7703b9d92326 34
NarendraSingh 16:7703b9d92326 35 //Create Object for structure of Lora Packet to be sent
NarendraSingh 11:77e595130230 36
NarendraSingh 16:7703b9d92326 37 static uint16 Calculate_Wheels_RPM(uint8* Buffer);
NarendraSingh 16:7703b9d92326 38 void flip_Packet_Sending_Flag();
NarendraSingh 16:7703b9d92326 39 void Lora_Periodic_Packet_Sending_thread(void const *args);
NarendraSingh 11:77e595130230 40
NarendraSingh 16:7703b9d92326 41 void Lora_Rcvd_Cmd_Processing_thread(void);// const *args);
NarendraSingh 16:7703b9d92326 42 void Enable_CheckIN_Packet_Sending();
NarendraSingh 16:7703b9d92326 43
NarendraSingh 23:688ee106c385 44 const char GET_RSSI[]= {0x41,0x54,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x0D,0x0A};
NarendraSingh 23:688ee106c385 45 const char SET_BEACON_VENDOR_ID[]= {0x41,0x54,0xF1,0x01,0x02,0x00,0x00,0x00,0x00,0xF2,0x0D,0x0A};
NarendraSingh 23:688ee106c385 46 const char SET_BEACON_MESSAGE_TYPE[]= {0x41,0x54,0xF2,0x01,0x00,0x00,0x00,0x00,0x00,0xF3,0x0D,0x0A};
NarendraSingh 11:77e595130230 47 /*************************Accelerometer related definitions***********************************/
NarendraSingh 11:77e595130230 48 //Accelerometer related definitions
NarendraSingh 13:8955f2e95021 49
NarendraSingh 11:77e595130230 50 #define DOUBLE_TAP_INTERRUPT 0x20
NarendraSingh 11:77e595130230 51 #define ACTIVITY_INTERRUPT 0x10
NarendraSingh 11:77e595130230 52 #define INACTIVITY_INTERRUPT 0x08
NarendraSingh 11:77e595130230 53
NarendraSingh 16:7703b9d92326 54 #define TAP_THRESHOLD 75
NarendraSingh 11:77e595130230 55 #define ACTIVITY_THRESHOLD 64 // THRES_ACT register value 62.5mg/LSB , therfore value 32 indicates 2g activity
NarendraSingh 11:77e595130230 56 #define INACTIVITY_THRESHOLD 50
NarendraSingh 11:77e595130230 57
NarendraSingh 11:77e595130230 58 #define DUR_TIME 0x15 // DUR Register value providing maximum time to be held to generate an interrupt
NarendraSingh 11:77e595130230 59 #define LATENT_TIME 0x15 // The interrupt latency
NarendraSingh 11:77e595130230 60 #define WINDOW_TIME 0x45 // The time of the interrupt window in which the next tap will be detected
NarendraSingh 11:77e595130230 61 #define INACTIVITY_VALIDATION_TIME 5 // The time until which the acceleration must be held below the inactivity threshold to generate an inactvity interrupt
NarendraSingh 16:7703b9d92326 62 // Here the value 5 indicates literally 5 secs
NarendraSingh 11:77e595130230 63 #define X_AXIS_OFFSET 0x7F
NarendraSingh 11:77e595130230 64 #define Y_AXIS_OFFSET 0x7F
NarendraSingh 11:77e595130230 65 #define Z_AXIS_OFFSET 0x05
NarendraSingh 11:77e595130230 66
NarendraSingh 17:758fb8454ab0 67 Serial pc(USBTX, USBRX);
NarendraSingh 16:7703b9d92326 68 I2C i2c(PB_9, PB_8);
NarendraSingh 11:77e595130230 69
NarendraSingh 16:7703b9d92326 70 InterruptIn activity(PB_0);
NarendraSingh 11:77e595130230 71 InterruptIn inactivity(PA_4); // As for now only this is used
NarendraSingh 11:77e595130230 72 DigitalOut led(LED1);
NarendraSingh 11:77e595130230 73
NarendraSingh 11:77e595130230 74 const int slave_address_acc = 0xA6;
NarendraSingh 11:77e595130230 75 char axis_data[6] = {0,0,0,0,0,0};
NarendraSingh 11:77e595130230 76
NarendraSingh 11:77e595130230 77 char interrupt_source[2];
NarendraSingh 11:77e595130230 78 char axis_data_start_address[2] = {0x32, 0};
NarendraSingh 11:77e595130230 79 char intr_source_address[2] = {0x30, 0};
NarendraSingh 11:77e595130230 80 char all_interrupt_clear_command[2] = {0x2E, 0x00};
NarendraSingh 11:77e595130230 81 char all_interrupt_enable_command[2] = {0x2E, 0x38};
NarendraSingh 11:77e595130230 82 char activity_interrupt_disable_command[2] = {0x2E, 0x08};
NarendraSingh 11:77e595130230 83 char inactivity_interrupt_disable_command[2] = {0x2E, 0x30};
NarendraSingh 11:77e595130230 84 char accelerometer_status_registered = 0;
NarendraSingh 11:77e595130230 85 unsigned int interrupt_source_duplicate;
NarendraSingh 11:77e595130230 86
NarendraSingh 11:77e595130230 87 char threshold_offset_command[5];
NarendraSingh 11:77e595130230 88 char act_inact_time_config_command[8];
NarendraSingh 11:77e595130230 89 char interrupt_enable_command[3];
NarendraSingh 11:77e595130230 90 char tap_axis_enable_command[2];
NarendraSingh 11:77e595130230 91 char baud_rate_command[2];
NarendraSingh 11:77e595130230 92 char data_format_command[2];
NarendraSingh 11:77e595130230 93 char measure_bit_on_command[2];
NarendraSingh 11:77e595130230 94
NarendraSingh 11:77e595130230 95
NarendraSingh 11:77e595130230 96 unsigned char vehicle_speed = 25; // Kmph
NarendraSingh 11:77e595130230 97 unsigned char current_speed, previous_speed, speed_threshold = 10; // Kmph
NarendraSingh 11:77e595130230 98
NarendraSingh 11:77e595130230 99 unsigned char x_axis, y_axis, z_axis;
NarendraSingh 11:77e595130230 100
NarendraSingh 11:77e595130230 101 unsigned char Motion_Detect_Status = FALSE;
NarendraSingh 11:77e595130230 102 uint8 OBD_PlugInOut_IOC_Status = FALSE;
NarendraSingh 11:77e595130230 103 unsigned char Motion_Type_Detected = MOTION_TYPE_UNKNOWN; //By default set motion type as unknown
NarendraSingh 11:77e595130230 104 void Accelerometer_Process_thread();//void const *args) ;
NarendraSingh 11:77e595130230 105
NarendraSingh 16:7703b9d92326 106 #define BLE_RECEIVER_UART_RX_Size 100
NarendraSingh 16:7703b9d92326 107 uint8 BLE_RxBuffer_End_Pos = 0;
NarendraSingh 16:7703b9d92326 108 char BLE_Receiver_UART_RX_Buffer[BLE_RECEIVER_UART_RX_Size];
NarendraSingh 13:8955f2e95021 109
NarendraSingh 11:77e595130230 110 //This function is Interrupt routine for detecting motion(acceleartion), i.e. either starts from rest/vehicle stops afeter running or if sudden jurk is detected
NarendraSingh 11:77e595130230 111 void interrupt_activity_inactivity()
NarendraSingh 16:7703b9d92326 112 {
NarendraSingh 16:7703b9d92326 113 i2c.write(slave_address_acc, all_interrupt_clear_command, 2);
NarendraSingh 16:7703b9d92326 114 Motion_Detect_Status = TRUE;
NarendraSingh 11:77e595130230 115 }
NarendraSingh 13:8955f2e95021 116
NarendraSingh 13:8955f2e95021 117
NarendraSingh 16:7703b9d92326 118 /************************************************************************/
NarendraSingh 11:77e595130230 119 uint8 Command_Sent[100];
NarendraSingh 11:77e595130230 120 uint8 Command_Length_Sent;
NarendraSingh 12:6107b32b0729 121 uint8 Checkin_Detect_Status = FALSE;
NarendraSingh 11:77e595130230 122 void Extract_Received_Lora_Response(void);
NarendraSingh 23:688ee106c385 123 void Send_Command_To_BLE_Receiver(const char* Command);
NarendraSingh 16:7703b9d92326 124
NarendraSingh 16:7703b9d92326 125 char previous_state = 0;
NarendraSingh 16:7703b9d92326 126 char current_state = 0;
NarendraSingh 16:7703b9d92326 127
NarendraSingh 16:7703b9d92326 128 uint8 OBD_PlugIN_State=0;
NarendraSingh 16:7703b9d92326 129 uint8 OBD_PlugIN_State1=0;
NarendraSingh 16:7703b9d92326 130 uint8 OBD_PlugIN_State2=0;
NarendraSingh 16:7703b9d92326 131 uint8 OBD_PlugIN_Temp_State=1;
NarendraSingh 16:7703b9d92326 132
NarendraSingh 16:7703b9d92326 133 void OBD_Plug_IN_Interrupt()
NarendraSingh 16:7703b9d92326 134 {
NarendraSingh 17:758fb8454ab0 135 if(OBD_PlugIN_State1!=OBD_PlugIN_Temp_State) {
NarendraSingh 16:7703b9d92326 136 OBD_PlugIN_State1=1;
NarendraSingh 16:7703b9d92326 137 OBD_PlugIN_State=!OBD_PlugIN_State;
NarendraSingh 16:7703b9d92326 138 OBD_PlugIN_Temp_State=OBD_PlugIN_State;
NarendraSingh 16:7703b9d92326 139 OBD_PlugInOut_IOC_Status = TRUE;
NarendraSingh 16:7703b9d92326 140 }
NarendraSingh 16:7703b9d92326 141 }
NarendraSingh 17:758fb8454ab0 142
NarendraSingh 16:7703b9d92326 143 void OBD_Plug_OUT_Interrupt()
NarendraSingh 16:7703b9d92326 144 {
NarendraSingh 22:c2f034a13108 145
NarendraSingh 17:758fb8454ab0 146 if(OBD_PlugIN_State2!=OBD_PlugIN_Temp_State) {
NarendraSingh 16:7703b9d92326 147 OBD_PlugIN_State2=0;
NarendraSingh 16:7703b9d92326 148 OBD_PlugIN_State=!OBD_PlugIN_State;
NarendraSingh 16:7703b9d92326 149 OBD_PlugIN_Temp_State=OBD_PlugIN_State;
NarendraSingh 16:7703b9d92326 150 OBD_PlugInOut_IOC_Status = TRUE;
NarendraSingh 17:758fb8454ab0 151 }
NarendraSingh 16:7703b9d92326 152 }
NarendraSingh 16:7703b9d92326 153
NarendraSingh 16:7703b9d92326 154 //This function is Interrupt routine for detecting OBD Plugin and Out
NarendraSingh 11:77e595130230 155 void Handle_CheckIn_Interrupt()
NarendraSingh 16:7703b9d92326 156 {
NarendraSingh 11:77e595130230 157 OBD_PlugInOut_IOC_Status = TRUE;
NarendraSingh 17:758fb8454ab0 158 pc.printf("Movement_Detected");
NarendraSingh 11:77e595130230 159 }
NarendraSingh 11:77e595130230 160
NarendraSingh 16:7703b9d92326 161 //Declare Ticker for sending lora packet
NarendraSingh 16:7703b9d92326 162 Ticker Lora_Packet_Sending_Ticker;
NarendraSingh 16:7703b9d92326 163 void flip_Packet_Sending_Flag(void)
NarendraSingh 16:7703b9d92326 164 {
NarendraSingh 16:7703b9d92326 165 //flip function
NarendraSingh 16:7703b9d92326 166 if(Ticker_Count < 5) {
NarendraSingh 11:77e595130230 167 Ticker_Count++;
NarendraSingh 16:7703b9d92326 168 } else {
NarendraSingh 11:77e595130230 169 Ticker_Count = 0;
NarendraSingh 11:77e595130230 170 Send_Lora_Packet_Flag = TRUE;
NarendraSingh 11:77e595130230 171 }
NarendraSingh 16:7703b9d92326 172 }
NarendraSingh 11:77e595130230 173
NarendraSingh 11:77e595130230 174 // called every time a byte is received from lora module.
NarendraSingh 11:77e595130230 175 void Lora_onDataRx()
NarendraSingh 11:77e595130230 176 {
NarendraSingh 16:7703b9d92326 177 while (LORA_UART.readable()) {
NarendraSingh 16:7703b9d92326 178 // while there is data waiting
NarendraSingh 11:77e595130230 179 LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos++] = LORA_UART.getc(); // put it in the buffer
NarendraSingh 20:f812f85cf97e 180 //pc1.putc(LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos-1]);
NarendraSingh 16:7703b9d92326 181 if(Lora_RxBuffer_End_Pos >= LORA_UART_RX_Size) {
NarendraSingh 11:77e595130230 182 // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation.
NarendraSingh 11:77e595130230 183 // For now just throw everything away.
NarendraSingh 11:77e595130230 184 Lora_RxBuffer_End_Pos = 0;
NarendraSingh 11:77e595130230 185 }
emilmont 1:491820ee784d 186 }
NarendraSingh 16:7703b9d92326 187 }
NarendraSingh 11:77e595130230 188
NarendraSingh 11:77e595130230 189 // called every time a byte is received from Beacon Module.
NarendraSingh 11:77e595130230 190 void Beacon_onDataRx()
NarendraSingh 11:77e595130230 191 {
NarendraSingh 16:7703b9d92326 192 while (Beacon_UART.readable()) {
NarendraSingh 16:7703b9d92326 193 // while there is data waiting
NarendraSingh 11:77e595130230 194 Beacon_RX_Buffer[Beacon_RxBuffer_End_Pos++] = Beacon_UART.getc(); // put it in the buffer
NarendraSingh 23:688ee106c385 195 pc1.printf("%2x",Beacon_RX_Buffer[Beacon_RxBuffer_End_Pos-1]);
NarendraSingh 16:7703b9d92326 196 if(Beacon_RxBuffer_End_Pos >= 100) {
NarendraSingh 11:77e595130230 197 // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation.
NarendraSingh 11:77e595130230 198 // For now just throw everything away.
NarendraSingh 11:77e595130230 199 Beacon_RxBuffer_End_Pos = 0;
NarendraSingh 11:77e595130230 200 }
emilmont 1:491820ee784d 201 }
emilmont 1:491820ee784d 202 }
NarendraSingh 11:77e595130230 203
NarendraSingh 16:7703b9d92326 204 void BLE_Receiver_onDataRx(void)
NarendraSingh 13:8955f2e95021 205 {
NarendraSingh 16:7703b9d92326 206 while (BLE_RECEIVER_UART.readable()) {
NarendraSingh 16:7703b9d92326 207 // while there is data waiting
NarendraSingh 13:8955f2e95021 208 BLE_Receiver_UART_RX_Buffer[BLE_RxBuffer_End_Pos++] = BLE_RECEIVER_UART.getc(); // put it in the buffer
NarendraSingh 22:c2f034a13108 209 //pc1.putc(BLE_Receiver_UART_RX_Buffer[BLE_RxBuffer_End_Pos-1]);
NarendraSingh 16:7703b9d92326 210 if(BLE_RxBuffer_End_Pos >= BLE_RECEIVER_UART_RX_Size) {
NarendraSingh 13:8955f2e95021 211 // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation.
NarendraSingh 13:8955f2e95021 212 // For now just throw everything away.
NarendraSingh 13:8955f2e95021 213 BLE_RxBuffer_End_Pos = 0;
NarendraSingh 13:8955f2e95021 214 }
NarendraSingh 12:6107b32b0729 215 }
NarendraSingh 16:7703b9d92326 216 }
NarendraSingh 16:7703b9d92326 217
NarendraSingh 16:7703b9d92326 218 int main()
NarendraSingh 11:77e595130230 219 {
NarendraSingh 17:758fb8454ab0 220 pc1.baud(115200);
NarendraSingh 16:7703b9d92326 221 BLE_RECEIVER_UART.baud(115200);
NarendraSingh 17:758fb8454ab0 222 pc1.printf("%s","Debugging started");
NarendraSingh 16:7703b9d92326 223 BLE_RECEIVER_UART.attach(&BLE_Receiver_onDataRx, Serial::RxIrq);
NarendraSingh 11:77e595130230 224 LORA_UART.attach(&Lora_onDataRx, Serial::RxIrq);
NarendraSingh 22:c2f034a13108 225 CheckIn_Interrupt.fall(&OBD_Plug_IN_Interrupt);
NarendraSingh 22:c2f034a13108 226 CheckIn_Interrupt.rise(&OBD_Plug_OUT_Interrupt);
NarendraSingh 22:c2f034a13108 227
NarendraSingh 11:77e595130230 228 //Create a thread to read vehicle data
NarendraSingh 11:77e595130230 229 //Thread OBD_thread(OBD_Rcvd_Cmd_Processing_thread);
NarendraSingh 11:77e595130230 230
NarendraSingh 11:77e595130230 231 Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 3.0); // call flip_Packet_Sending_Flag function every 5 seconds
NarendraSingh 11:77e595130230 232
NarendraSingh 11:77e595130230 233 // OBD_PLUGIN_INTERRUPT_PIN.rise(&Enable_CheckIN_Packet_Sending); // call toggle function on the rising edge
NarendraSingh 11:77e595130230 234 //led2_thread is executing concurrently with main at this point
NarendraSingh 22:c2f034a13108 235 //inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge
NarendraSingh 19:886d50ecc718 236 Initialize_Beacon_Module();
NarendraSingh 23:688ee106c385 237 Send_Command_To_BLE_Receiver(SET_BEACON_VENDOR_ID);
NarendraSingh 23:688ee106c385 238 Send_Command_To_BLE_Receiver(SET_BEACON_MESSAGE_TYPE);
NarendraSingh 20:f812f85cf97e 239 pc1.printf("%s","Transmitter MAC ID received");
NarendraSingh 11:77e595130230 240 Lora_Periodic_Packet_Sending(); //Infinite loop for sending and receiving lora response, no return from here
NarendraSingh 16:7703b9d92326 241 }
NarendraSingh 13:8955f2e95021 242
NarendraSingh 23:688ee106c385 243 void Send_Command_To_BLE_Receiver(const char* Command)
NarendraSingh 23:688ee106c385 244 {
NarendraSingh 23:688ee106c385 245 uint8 i;
NarendraSingh 23:688ee106c385 246 for(i=0;i<12;i++)
NarendraSingh 23:688ee106c385 247 BLE_RECEIVER_UART.putc(Command[i]);
NarendraSingh 23:688ee106c385 248 }
NarendraSingh 16:7703b9d92326 249 //Function to be called when Interrupt is genearted for motion sensing, checkin
NarendraSingh 16:7703b9d92326 250 void Initialize_Packets_Sent_Count(void)
NarendraSingh 16:7703b9d92326 251 {
NarendraSingh 11:77e595130230 252 Motion_Packet_Sent_Count = 0x00;
NarendraSingh 11:77e595130230 253 CheckIN_Packet_Sent_Count = 0x00;
NarendraSingh 16:7703b9d92326 254 }
NarendraSingh 16:7703b9d92326 255
NarendraSingh 16:7703b9d92326 256 uint8 Status_Packet_Wait_Count = 0;
NarendraSingh 16:7703b9d92326 257 void Lora_Periodic_Packet_Sending()
NarendraSingh 11:77e595130230 258 {
NarendraSingh 17:758fb8454ab0 259 pc1.printf("Periodic packet sending intiialized");
NarendraSingh 11:77e595130230 260 Set_Up_Lora_Network_Configuration();
NarendraSingh 16:7703b9d92326 261 Initialize_lora_Packets();
NarendraSingh 19:886d50ecc718 262 Send_Lora_Packet_Flag = TRUE;
NarendraSingh 16:7703b9d92326 263 while (true) {
NarendraSingh 16:7703b9d92326 264 if(Packet_Type_To_Send == HEARTBEAT_TYPE_PACKET) { //check if packet to be sent is Heartbeat packet
NarendraSingh 16:7703b9d92326 265 if(Send_Lora_Packet_Flag) { //Check if packet sending is enabled, Packet should be sent only when enabled after timeout period
NarendraSingh 12:6107b32b0729 266 Status_Packet_Wait_Count++;
NarendraSingh 21:a5fb0ae94dc6 267 if(Status_Packet_Wait_Count < 4) {
NarendraSingh 23:688ee106c385 268 Send_Command_To_BLE_Receiver(GET_RSSI);
NarendraSingh 21:a5fb0ae94dc6 269 Send_RSSI_Request_Command(GET_RSSI);
NarendraSingh 21:a5fb0ae94dc6 270 pc1.printf("Sending heartbeat packet"); //call function to send periodic motion packet
NarendraSingh 12:6107b32b0729 271 Send_HeartBeat_Packet(); //call function to send heartbeat packet
NarendraSingh 17:758fb8454ab0 272 pc.printf("Sent HeartBeat Packet");
NarendraSingh 12:6107b32b0729 273 AT_Response_Receive_Status = FAILURE;
NarendraSingh 12:6107b32b0729 274 while(AT_Response_Receive_Status)
NarendraSingh 12:6107b32b0729 275 Get_Lora_Response();
NarendraSingh 12:6107b32b0729 276 Send_Lora_Packet_Flag = FALSE;
NarendraSingh 21:a5fb0ae94dc6 277 //pc1.printf("Heartbeat Packet Response Received");
NarendraSingh 22:c2f034a13108 278 } else {
NarendraSingh 20:f812f85cf97e 279 //Send_RSSI_Request_Command(GET_RSSI);
NarendraSingh 12:6107b32b0729 280 Status_Packet_Wait_Count = 0;
NarendraSingh 21:a5fb0ae94dc6 281 pc1.printf("Sending Vehicle status packets"); //call function to send periodic motion packet
NarendraSingh 13:8955f2e95021 282 Send_Vehicle_Status_Packet(); //call function to send heartbeat packet
NarendraSingh 21:a5fb0ae94dc6 283 pc1.printf("Sent Vehicle Status Packet");
NarendraSingh 19:886d50ecc718 284 //AT_Response_Receive_Status = FAILURE;
NarendraSingh 21:a5fb0ae94dc6 285 // while(AT_Response_Receive_Status)
NarendraSingh 19:886d50ecc718 286 // Get_Lora_Response();
NarendraSingh 12:6107b32b0729 287 Send_Lora_Packet_Flag = FALSE;
NarendraSingh 20:f812f85cf97e 288 //pc1.printf("Status Packet Response Received");
NarendraSingh 12:6107b32b0729 289 }
NarendraSingh 11:77e595130230 290 }
NarendraSingh 16:7703b9d92326 291 } else if(Packet_Type_To_Send == MOTION_TYPE_PACKET) { //check if packet to be sent is motion packet
NarendraSingh 16:7703b9d92326 292 if(Send_Lora_Packet_Flag) { //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period
NarendraSingh 21:a5fb0ae94dc6 293 //Send_RSSI_Request_Command(GET_RSSI);
NarendraSingh 21:a5fb0ae94dc6 294 pc1.printf("Sending Motion Packet"); //call function to send periodic motion packet
NarendraSingh 11:77e595130230 295 Send_Motion_Packet();
NarendraSingh 17:758fb8454ab0 296 pc1.printf("Sent Motion Packet"); //call function to send periodic motion packet
NarendraSingh 11:77e595130230 297 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 298 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 299 Get_Lora_Response();
NarendraSingh 17:758fb8454ab0 300 pc1.printf("Motion Packet Response Received");
NarendraSingh 11:77e595130230 301 Send_Lora_Packet_Flag = FALSE;
NarendraSingh 16:7703b9d92326 302 if(Motion_Packet_Sent_Count >= 5) { //Stop Sending Motion Packets if after sending for 2 minute
NarendraSingh 17:758fb8454ab0 303 pc1.printf("Packet Type Sending Changed to HeartBeat");
NarendraSingh 11:77e595130230 304 Motion_Packet_Sent_Count = 0;
NarendraSingh 11:77e595130230 305 Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet
NarendraSingh 16:7703b9d92326 306 Lora_Packet_Sending_Ticker.detach(); //destroy ticker
NarendraSingh 11:77e595130230 307 Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 10.0); //create new ticker
NarendraSingh 11:77e595130230 308 }
NarendraSingh 16:7703b9d92326 309 }
NarendraSingh 16:7703b9d92326 310 } else if(Packet_Type_To_Send == CHECKIN_TYPE_PACKET) { //check if packet to be sent is Checkin packet
NarendraSingh 16:7703b9d92326 311 if(Send_Lora_Packet_Flag) { //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period
NarendraSingh 19:886d50ecc718 312 Send_Lora_Packet_Flag = FALSE;
NarendraSingh 21:a5fb0ae94dc6 313 //pc1.printf("Sent Beacon ID request");
NarendraSingh 20:f812f85cf97e 314 // Send_RSSI_Request_Command(GET_RSSI);
NarendraSingh 19:886d50ecc718 315 if(OBD_PlugIN_State)
NarendraSingh 19:886d50ecc718 316 {
NarendraSingh 21:a5fb0ae94dc6 317 pc1.printf("Sening Checkin Packet");
NarendraSingh 19:886d50ecc718 318 Send_CheckIN_Packet(); //call function to send periodic checkIn packet
NarendraSingh 19:886d50ecc718 319 pc1.printf("Sent Checkin Packet");
NarendraSingh 21:a5fb0ae94dc6 320 Process_Beacon_Command_Received(SET_VIRTUAL_PACKET_UUID);
NarendraSingh 22:c2f034a13108 321 wait(.2);
NarendraSingh 21:a5fb0ae94dc6 322 Process_Beacon_Command_Received(SOFT_REBOOT1);
NarendraSingh 19:886d50ecc718 323 }
NarendraSingh 19:886d50ecc718 324 else
NarendraSingh 19:886d50ecc718 325 {
NarendraSingh 21:a5fb0ae94dc6 326 pc1.printf("Sending Checkout Packet");
NarendraSingh 19:886d50ecc718 327 Send_CheckOUT_Packet();
NarendraSingh 19:886d50ecc718 328 pc1.printf("Sent CheckOut Packet");
NarendraSingh 21:a5fb0ae94dc6 329 Process_Beacon_Command_Received(SET_VIRTUAL_PACKET_UUID2);
NarendraSingh 22:c2f034a13108 330 wait(.2);
NarendraSingh 21:a5fb0ae94dc6 331 Process_Beacon_Command_Received(SOFT_REBOOT1);
NarendraSingh 19:886d50ecc718 332 }
NarendraSingh 22:c2f034a13108 333 /* AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 334 while(AT_Response_Receive_Status)
NarendraSingh 22:c2f034a13108 335 Get_Lora_Response();*/
NarendraSingh 16:7703b9d92326 336 if(CheckIN_Packet_Sent_Count >= 5) { //Stop Sending Motion Packets if after sending for 2 minute
NarendraSingh 17:758fb8454ab0 337 pc1.printf("Packet Type Sending Changed to HeartBeat");
NarendraSingh 20:f812f85cf97e 338 CheckIN_Packet_Sent_Count = 0;
NarendraSingh 20:f812f85cf97e 339 Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet
NarendraSingh 20:f812f85cf97e 340 Initialize_Packets_Sent_Count();
NarendraSingh 11:77e595130230 341 }
NarendraSingh 11:77e595130230 342 }
NarendraSingh 16:7703b9d92326 343 }
NarendraSingh 16:7703b9d92326 344 if(OBD_PlugInOut_IOC_Status) { //Check if Accelerometer Interrupt is generated
NarendraSingh 13:8955f2e95021 345 //Enable_CheckIN_Packet_Sending();
NarendraSingh 11:77e595130230 346 //Get_Acceleration_Type();
NarendraSingh 21:a5fb0ae94dc6 347 // Process_Beacon_Command_Received(SET_VIRTUAL_PACKET_UUID);
NarendraSingh 21:a5fb0ae94dc6 348 // Process_Beacon_Command_Received(SOFT_REBOOT1);
NarendraSingh 22:c2f034a13108 349 pc.putc('j');
NarendraSingh 11:77e595130230 350 OBD_PlugInOut_IOC_Status = FALSE;
NarendraSingh 13:8955f2e95021 351 Packet_Type_To_Send = CHECKIN_TYPE_PACKET;
NarendraSingh 13:8955f2e95021 352 Send_Lora_Packet_Flag = TRUE;
NarendraSingh 19:886d50ecc718 353 CheckIN_Packet_Sent_Count = 0;
NarendraSingh 21:a5fb0ae94dc6 354 CheckIN_Lora_Packet.Sequence_No = 0x00; //Reset counter
NarendraSingh 21:a5fb0ae94dc6 355 CheckOUT_Packet.Sequence_No = 0x00; //Reset counter
NarendraSingh 16:7703b9d92326 356 wait(1); //wait for 1sec to avoid detecting plugin debounce
NarendraSingh 11:77e595130230 357 }
NarendraSingh 16:7703b9d92326 358 if(Motion_Detect_Status) { //Check if Accelerometer Interrupt is generated
NarendraSingh 21:a5fb0ae94dc6 359 Motion_Lora_Packet.Sequence_No = 0x00; //Reset counter
NarendraSingh 11:77e595130230 360 Get_Acceleration_Type();
NarendraSingh 11:77e595130230 361 Motion_Detect_Status = FALSE;
NarendraSingh 11:77e595130230 362 }
NarendraSingh 16:7703b9d92326 363 }
NarendraSingh 11:77e595130230 364 }
NarendraSingh 11:77e595130230 365
NarendraSingh 11:77e595130230 366 void Get_Acceleration_Type(void)
NarendraSingh 11:77e595130230 367 {
NarendraSingh 11:77e595130230 368 Packet_Type_To_Send = MOTION_TYPE_PACKET; //whenever acceleration is detected change the packet type to be sent to motion type
NarendraSingh 16:7703b9d92326 369 Lora_Packet_Sending_Ticker.detach(); //destroy ticker
NarendraSingh 11:77e595130230 370 Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 5.0); //create new ticker, time inetrval value to be changed
NarendraSingh 11:77e595130230 371 Send_Lora_Packet_Flag = 1; //set flag to send motion packet as soon as motion is detecetd
NarendraSingh 11:77e595130230 372 Motion_Lora_Packet.Acceleration_Type = Motion_Type_Detected; //Read Type of motion deteceted
NarendraSingh 11:77e595130230 373 //write code to read acceleration value for every 10sec after any of the acceleration is found
NarendraSingh 11:77e595130230 374 }
NarendraSingh 19:886d50ecc718 375
NarendraSingh 13:8955f2e95021 376 /*
NarendraSingh 11:77e595130230 377 //This function is used to enable checkin packet sending once OBD device is plugged in
NarendraSingh 16:7703b9d92326 378 void Enable_CheckIN_Packet_Sending()
NarendraSingh 11:77e595130230 379 {
NarendraSingh 12:6107b32b0729 380 // Enable_CheckIN_Packet_Sending = TRUE; //set status to true
NarendraSingh 13:8955f2e95021 381 if(Command_Received[0] == 0xFE) //check start of frame of packet received. every packet must start with 0xFE
NarendraSingh 11:77e595130230 382 {
NarendraSingh 11:77e595130230 383 if(Calculate_Lora_Frame_FCS((Command_Received + 1),(Command_Length + 1)) == (Command_Received[Command_Length + 2])) //Check for packet inegrity by checking FCS
NarendraSingh 11:77e595130230 384 {
NarendraSingh 11:77e595130230 385 if((Command_Received[0] == SET_BEACON_CMD0) && (Command_Received[1] == SET_BEACON_CMD1)) //Check if command is received for setting beacon parameters
NarendraSingh 11:77e595130230 386 {
NarendraSingh 11:77e595130230 387 Process_Beacon_Command_Received((Command_Received + 2));
NarendraSingh 11:77e595130230 388 }
NarendraSingh 11:77e595130230 389 }
NarendraSingh 13:8955f2e95021 390 }
NarendraSingh 15:a448e955b8f3 391 } */
NarendraSingh 15:a448e955b8f3 392
NarendraSingh 16:7703b9d92326 393 void Send_RSSI_Request_Command(const char* Command_To_Send)
NarendraSingh 15:a448e955b8f3 394 {
NarendraSingh 23:688ee106c385 395 //BLE_RECEIVER_UART.printf(Command_To_Send);
NarendraSingh 16:7703b9d92326 396 Get_Fixed_Beacon_RSSI();
NarendraSingh 15:a448e955b8f3 397 }
NarendraSingh 15:a448e955b8f3 398
NarendraSingh 16:7703b9d92326 399 void Get_Fixed_Beacon_RSSI(void)
NarendraSingh 15:a448e955b8f3 400 {
NarendraSingh 16:7703b9d92326 401 uint8 i;
NarendraSingh 16:7703b9d92326 402 uint8 Temp_Pos=0;
NarendraSingh 19:886d50ecc718 403 /*Fixed_Beacon_Packet.Parking1_Beacon_ID[0] = 0xA0;
NarendraSingh 16:7703b9d92326 404 Fixed_Beacon_Packet.Parking1_Beacon_ID[1] = 0xAC;
NarendraSingh 16:7703b9d92326 405 Fixed_Beacon_Packet.Parking1_Beacon_ID[2] = 0x51;
NarendraSingh 16:7703b9d92326 406 Fixed_Beacon_Packet.Parking1_Beacon_ID[3] = 0x3F;
NarendraSingh 16:7703b9d92326 407 Fixed_Beacon_Packet.Parking1_Beacon_ID[4] = 0x91;
NarendraSingh 16:7703b9d92326 408 Fixed_Beacon_Packet.Parking1_Beacon_ID[5] = 0xE0;
NarendraSingh 16:7703b9d92326 409 Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = 0xC3;
NarendraSingh 16:7703b9d92326 410 Fixed_Beacon_Packet.Parking2_Beacon_ID[0] = 0xDC;
NarendraSingh 16:7703b9d92326 411 Fixed_Beacon_Packet.Parking2_Beacon_ID[1] = 0xA3;
NarendraSingh 16:7703b9d92326 412 Fixed_Beacon_Packet.Parking2_Beacon_ID[2] = 0x6D;
NarendraSingh 16:7703b9d92326 413 Fixed_Beacon_Packet.Parking2_Beacon_ID[3] = 0x30;
NarendraSingh 16:7703b9d92326 414 Fixed_Beacon_Packet.Parking2_Beacon_ID[4] = 0x38;
NarendraSingh 16:7703b9d92326 415 Fixed_Beacon_Packet.Parking2_Beacon_ID[5] = 0xDF;
NarendraSingh 16:7703b9d92326 416 Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = 0xB4;
NarendraSingh 16:7703b9d92326 417 Fixed_Beacon_Packet.Parking3_Beacon_ID[0] = 0x6C;
NarendraSingh 16:7703b9d92326 418 Fixed_Beacon_Packet.Parking3_Beacon_ID[1] = 0x29;
NarendraSingh 16:7703b9d92326 419 Fixed_Beacon_Packet.Parking3_Beacon_ID[2] = 0xB6;
NarendraSingh 16:7703b9d92326 420 Fixed_Beacon_Packet.Parking3_Beacon_ID[3] = 0x27;
NarendraSingh 16:7703b9d92326 421 Fixed_Beacon_Packet.Parking3_Beacon_ID[4] = 0x3A;
NarendraSingh 16:7703b9d92326 422 Fixed_Beacon_Packet.Parking3_Beacon_ID[5] = 0xC9;
NarendraSingh 16:7703b9d92326 423 Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = 0xB0;
NarendraSingh 19:886d50ecc718 424 */
NarendraSingh 21:a5fb0ae94dc6 425 pc1.printf("Reading MACID from Receiver");
NarendraSingh 17:758fb8454ab0 426 while(true)
NarendraSingh 15:a448e955b8f3 427 {
NarendraSingh 21:a5fb0ae94dc6 428 wait_ms(2);
NarendraSingh 21:a5fb0ae94dc6 429 if(Temp_Pos <= BLE_RxBuffer_End_Pos)
NarendraSingh 15:a448e955b8f3 430 {
NarendraSingh 17:758fb8454ab0 431 if(BLE_Receiver_UART_RX_Buffer[Temp_Pos] == 0x0D)
NarendraSingh 16:7703b9d92326 432 {
NarendraSingh 17:758fb8454ab0 433 if(BLE_Receiver_UART_RX_Buffer[Temp_Pos+1] == 0x0A)
NarendraSingh 16:7703b9d92326 434 {
NarendraSingh 16:7703b9d92326 435 Temp_Pos = 2;
NarendraSingh 21:a5fb0ae94dc6 436 //pc1.printf("Fixed Beacon1 MAC ID");
NarendraSingh 16:7703b9d92326 437 for(i=0; i<6; i++)
NarendraSingh 21:a5fb0ae94dc6 438 {
NarendraSingh 21:a5fb0ae94dc6 439 Fixed_Beacon_Packet.Parking1_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++];
NarendraSingh 21:a5fb0ae94dc6 440 pc1.putc(Fixed_Beacon_Packet.Parking1_Beacon_ID[i]);
NarendraSingh 21:a5fb0ae94dc6 441 }
NarendraSingh 19:886d50ecc718 442 Temp_Pos+=2;
NarendraSingh 16:7703b9d92326 443 Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++];
NarendraSingh 21:a5fb0ae94dc6 444 //pc1.printf("Fixed Beacon2 MAC ID");
NarendraSingh 16:7703b9d92326 445 for(i=0; i<6; i++)
NarendraSingh 21:a5fb0ae94dc6 446 {
NarendraSingh 16:7703b9d92326 447 Fixed_Beacon_Packet.Parking2_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++];
NarendraSingh 21:a5fb0ae94dc6 448 pc1.putc(Fixed_Beacon_Packet.Parking2_Beacon_ID[i]);
NarendraSingh 21:a5fb0ae94dc6 449 }
NarendraSingh 19:886d50ecc718 450 Temp_Pos+=2;
NarendraSingh 16:7703b9d92326 451 Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++];
NarendraSingh 21:a5fb0ae94dc6 452 //pc1.printf("Fixed Beacon3 MAC ID");
NarendraSingh 16:7703b9d92326 453 for(i=0; i<6; i++)
NarendraSingh 21:a5fb0ae94dc6 454 {
NarendraSingh 21:a5fb0ae94dc6 455 Fixed_Beacon_Packet.Parking3_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++];
NarendraSingh 21:a5fb0ae94dc6 456 pc1.putc(Fixed_Beacon_Packet.Parking3_Beacon_ID[i]);
NarendraSingh 21:a5fb0ae94dc6 457 }
NarendraSingh 19:886d50ecc718 458 Temp_Pos+=2;
NarendraSingh 16:7703b9d92326 459 Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++];
NarendraSingh 21:a5fb0ae94dc6 460 BLE_RxBuffer_End_Pos = 0x00; //Start Next adat at 0th location
NarendraSingh 16:7703b9d92326 461 break;
NarendraSingh 16:7703b9d92326 462 }
NarendraSingh 16:7703b9d92326 463 Temp_Pos++;
NarendraSingh 15:a448e955b8f3 464 }
NarendraSingh 16:7703b9d92326 465 else
NarendraSingh 16:7703b9d92326 466 Temp_Pos++;
NarendraSingh 15:a448e955b8f3 467 }
NarendraSingh 15:a448e955b8f3 468 else
NarendraSingh 16:7703b9d92326 469 Temp_Pos = 0x00;
NarendraSingh 19:886d50ecc718 470 }
NarendraSingh 21:a5fb0ae94dc6 471 pc1.printf("Reading MACID finished");
NarendraSingh 15:a448e955b8f3 472 }