BLE Transmitter not working

Fork of Dealer_23Feb by kumar singh

Committer:
NarendraSingh
Date:
Tue Feb 21 13:33:29 2017 +0000
Revision:
20:f812f85cf97e
Parent:
19:886d50ecc718
Child:
21:a5fb0ae94dc6
packet length test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NarendraSingh 11:77e595130230 1 #include "Lora.h"
NarendraSingh 11:77e595130230 2 #include "OBD.h"
NarendraSingh 11:77e595130230 3 #include "Common_Defs.h"
NarendraSingh 11:77e595130230 4 #include "Beacon.h"
NarendraSingh 11:77e595130230 5
NarendraSingh 11:77e595130230 6 //Configure Lora Packet
NarendraSingh 11:77e595130230 7 RawSerial LORA_Module_UART(PA_0, PA_1);//USART4_TX->PA_0,USART4_RX->PA_1
NarendraSingh 17:758fb8454ab0 8 RawSerial pc2(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10
NarendraSingh 17:758fb8454ab0 9 Serial pc_2(USBTX, USBRX);
NarendraSingh 11:77e595130230 10
NarendraSingh 11:77e595130230 11 uint8 Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //By Default Heart Beat PAckets should be sent
NarendraSingh 11:77e595130230 12 uint8 OBD_Protocol_Version = 0x01;
NarendraSingh 11:77e595130230 13 uint8 Vehicle_Identification_Number[17]; //Unique Vehicle_Identification_Number, Read using OBD
NarendraSingh 11:77e595130230 14 uint8 Motion_Packet_Sent_Count=0;
NarendraSingh 11:77e595130230 15 uint8 CheckIN_Packet_Sent_Count=0;
NarendraSingh 11:77e595130230 16 uint8 Lora_Packet_To_Send[100];
NarendraSingh 11:77e595130230 17 uint8 Send_Lora_Packet_Flag = FALSE;
NarendraSingh 11:77e595130230 18 int Lora_RxBuffer_Crnt_Pos,Lora_RxBuffer_End_Pos; // must be volatile or the compiler may over-optimise.
NarendraSingh 11:77e595130230 19 int receivedDataCount = 0;
NarendraSingh 11:77e595130230 20 char LORA_UART_RX_Buffer[LORA_UART_RX_Size];
NarendraSingh 11:77e595130230 21 int LORA_UART_RX_Crnt_Pos;
NarendraSingh 11:77e595130230 22 uint8 Lora_Command_Rcvd[100];
NarendraSingh 11:77e595130230 23 uint8 Lora_Cmd_Length = 0;
NarendraSingh 11:77e595130230 24 uint8 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 25
NarendraSingh 11:77e595130230 26 //Create Object for Type of Lora Packet to send
NarendraSingh 11:77e595130230 27 Heart_Beat_PacketType Heart_Beat_Lora_Packet; //Allocate Memory for HeartBeat Lora Packets
NarendraSingh 11:77e595130230 28 CheckIN_PacketType CheckIN_Lora_Packet; //Allocate Memory for CheckIN Lora Packets
NarendraSingh 18:86f069689502 29 CheckOUT_PacketType CheckOUT_Packet; //Allocate Memory for CheckOUT Lora Packets
NarendraSingh 11:77e595130230 30 Motion_PacketType Motion_Lora_Packet; //Allocate Memory for Movement Lora Packets
NarendraSingh 11:77e595130230 31 Vehicle_Status_PacketType Vehicle_Status_Lora_Packet; //Allocate Memory for Movement Lora Packets
NarendraSingh 16:7703b9d92326 32 Near_Car_Beacon Near_Car_Beacon_Packet;
NarendraSingh 16:7703b9d92326 33 Fixed_Beacon Fixed_Beacon_Packet;
NarendraSingh 19:886d50ecc718 34 Misc_Packets Misc_Packet_Data;
NarendraSingh 19:886d50ecc718 35
NarendraSingh 11:77e595130230 36 //Lora AT Commands list
NarendraSingh 11:77e595130230 37 const char* Attention = {"AT\r"};
NarendraSingh 11:77e595130230 38 const char* Reset_Device = "ATZ\r";
NarendraSingh 11:77e595130230 39 const char* Reset_to_Factory_Defaults = "AT&F\r";
NarendraSingh 11:77e595130230 40 const char* Save_Configuration = "AT&W\r";
NarendraSingh 11:77e595130230 41 const char* Serial_Baud_Rate = "AT+IPR=";
NarendraSingh 11:77e595130230 42 const char* Join_Network = "AT+JOIN\r";
NarendraSingh 11:77e595130230 43 const char* Join_Retries = "AT+JR=";
NarendraSingh 11:77e595130230 44 const char* AES_Encryption = "AT+ENC=";
NarendraSingh 11:77e595130230 45 const char* Send_Lora_Packet = "AT+SEND ";
NarendraSingh 11:77e595130230 46 const char* Set_Frequency_Sub_Band = "AT+FSB=";
NarendraSingh 11:77e595130230 47 const char* Set_Network_ID = "AT+NI=";
NarendraSingh 11:77e595130230 48 const char* Set_Network_Key = "AT+NK=";
NarendraSingh 11:77e595130230 49 const char* Network_Key = "010203123";
NarendraSingh 11:77e595130230 50 const char* Network_ID = "010203040";
NarendraSingh 11:77e595130230 51 const char* Network_Join_Retries = "AT+JR=";
NarendraSingh 11:77e595130230 52 const char* Network_Join_Status = "AT+NJS=";
NarendraSingh 11:77e595130230 53 const char* Lora_Device_ID = "AT+DI\r";
NarendraSingh 11:77e595130230 54
NarendraSingh 16:7703b9d92326 55 #define CHECK_IN_PACKET 0x00
NarendraSingh 16:7703b9d92326 56 #define CHECK_OUT_PACKET 0x01
NarendraSingh 11:77e595130230 57
NarendraSingh 11:77e595130230 58 uint8 Calculate_Lora_Frame_FCS(uint8* Packet_Data,uint8 Packet_Length);
NarendraSingh 11:77e595130230 59 void Set_Up_Lora_Network_Configuration(void);
NarendraSingh 11:77e595130230 60 void Get_Lora_Response(void);
NarendraSingh 11:77e595130230 61
NarendraSingh 11:77e595130230 62 //Set Up lora network
NarendraSingh 11:77e595130230 63 void Set_Up_Lora_Network_Configuration(void)
NarendraSingh 11:77e595130230 64 {
NarendraSingh 11:77e595130230 65 LORA_Module_UART.baud(115200);
NarendraSingh 11:77e595130230 66 LORA_Module_UART.printf(Attention);//Send Attention command
NarendraSingh 11:77e595130230 67 AT_Response_Receive_Status = FAILURE;
NarendraSingh 17:758fb8454ab0 68 pc2.printf("Nwk set up started");
NarendraSingh 11:77e595130230 69 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 70 Get_Lora_Response();
NarendraSingh 17:758fb8454ab0 71 pc2.printf("AT Response received");
NarendraSingh 11:77e595130230 72 LORA_Module_UART.printf("%s%d\r",Set_Frequency_Sub_Band,FREQUENCY_SUB_BAND_CHANNEL7);//set frequency sub band to 7
NarendraSingh 11:77e595130230 73 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 74 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 75 Get_Lora_Response();
NarendraSingh 17:758fb8454ab0 76 pc2.printf("Frequency band response received");
NarendraSingh 11:77e595130230 77 LORA_Module_UART.printf("%s%d,%s\r",Set_Network_Key,STRING_PARAMETER,Network_Key); //set network key
NarendraSingh 11:77e595130230 78 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 79 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 80 Get_Lora_Response();
NarendraSingh 17:758fb8454ab0 81 pc2.printf("Network key Response received");
NarendraSingh 11:77e595130230 82 LORA_Module_UART.printf("%s%d,%s\r",Set_Network_ID,STRING_PARAMETER,Network_ID); //set network id
NarendraSingh 11:77e595130230 83 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 84 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 85 Get_Lora_Response();
NarendraSingh 17:758fb8454ab0 86 pc2.printf("Network Id response received");
NarendraSingh 11:77e595130230 87
NarendraSingh 20:f812f85cf97e 88 LORA_Module_UART.printf("AT+TXDR\r");
NarendraSingh 20:f812f85cf97e 89 AT_Response_Receive_Status = FAILURE;
NarendraSingh 20:f812f85cf97e 90 while(AT_Response_Receive_Status)
NarendraSingh 20:f812f85cf97e 91 Get_Lora_Response();
NarendraSingh 20:f812f85cf97e 92 LORA_Module_UART.printf("AT+TXDR=DR3\r");
NarendraSingh 11:77e595130230 93 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 94 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 95 Get_Lora_Response();
NarendraSingh 19:886d50ecc718 96 LORA_Module_UART.printf("AT+RXDR=10\r");
NarendraSingh 19:886d50ecc718 97 AT_Response_Receive_Status = FAILURE;
NarendraSingh 19:886d50ecc718 98 while(AT_Response_Receive_Status)
NarendraSingh 19:886d50ecc718 99 Get_Lora_Response();
NarendraSingh 20:f812f85cf97e 100 LORA_Module_UART.printf("AT+ADR=0\r"); //Enable adaptive data rate
NarendraSingh 11:77e595130230 101 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 102 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 103 Get_Lora_Response();
NarendraSingh 11:77e595130230 104 LORA_Module_UART.printf("%s",Save_Configuration); //save configuration
NarendraSingh 11:77e595130230 105 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 106 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 107 Get_Lora_Response();
NarendraSingh 20:f812f85cf97e 108 LORA_Module_UART.printf("AT+TXDR\r");
NarendraSingh 20:f812f85cf97e 109 AT_Response_Receive_Status = FAILURE;
NarendraSingh 20:f812f85cf97e 110 while(AT_Response_Receive_Status)
NarendraSingh 20:f812f85cf97e 111 Get_Lora_Response();
NarendraSingh 20:f812f85cf97e 112
NarendraSingh 17:758fb8454ab0 113 pc2.printf("Configuration saved");
NarendraSingh 13:8955f2e95021 114 LORA_Module_UART.printf("%s",Reset_Device); //reset device
NarendraSingh 13:8955f2e95021 115 wait_ms(3500);
NarendraSingh 11:77e595130230 116 LORA_Module_UART.printf(Attention); //Send Attention command
NarendraSingh 11:77e595130230 117 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 118 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 119 Get_Lora_Response();
NarendraSingh 17:758fb8454ab0 120 pc2.printf("AT Response received");
NarendraSingh 11:77e595130230 121 LORA_Module_UART.printf("%s",Join_Network); //join network with gateway
NarendraSingh 11:77e595130230 122 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 123 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 124 Get_Lora_Response();
NarendraSingh 17:758fb8454ab0 125 pc2.printf("Join Response received");
NarendraSingh 11:77e595130230 126 }
NarendraSingh 11:77e595130230 127
NarendraSingh 11:77e595130230 128 void Initialize_lora_Packets()
NarendraSingh 11:77e595130230 129 {
NarendraSingh 11:77e595130230 130 uint8 i;
NarendraSingh 11:77e595130230 131 /******* Initialize Lora packet for HeartBeat *****/
NarendraSingh 11:77e595130230 132 Heart_Beat_Lora_Packet.Header = LORA_PACKET_HEADER;
NarendraSingh 19:886d50ecc718 133 Misc_Packet_Data.Protocol_Version = OBD_Protocol_Version;
NarendraSingh 11:77e595130230 134 Heart_Beat_Lora_Packet.Packet_Type = HEART_BEAT_PACKET_CMD;
NarendraSingh 19:886d50ecc718 135 Misc_Packet_Data.OBD_Battery_Voltage = 350; //3.50V, dummy data
NarendraSingh 19:886d50ecc718 136 Misc_Packet_Data.Car_Battery_Voltage = 1250; //12.50V, dummy data
NarendraSingh 19:886d50ecc718 137 Misc_Packet_Data.OBD_Battery_Temperature = 95; //95'F, dummy data
NarendraSingh 19:886d50ecc718 138 Misc_Packet_Data.Car_Ambient_Temperature = 104; //104'F, dummy data
NarendraSingh 11:77e595130230 139 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 140 Fixed_Beacon_Packet.Parking1_Beacon_ID[i] = (0x01+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 141 Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = 23; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 142 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 143 Fixed_Beacon_Packet.Parking2_Beacon_ID[i] = (10+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 144 Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = 45; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 145 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 146 Fixed_Beacon_Packet.Parking3_Beacon_ID[i] = (20+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 147 Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = 12; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 148 Heart_Beat_Lora_Packet.FCS = 0x00; //FCS of all packets
NarendraSingh 11:77e595130230 149
NarendraSingh 11:77e595130230 150 /******* Initialize Lora packet for Vehicle Status *****/
NarendraSingh 11:77e595130230 151 Vehicle_Status_Lora_Packet.Header = LORA_PACKET_HEADER;
NarendraSingh 19:886d50ecc718 152 Misc_Packet_Data.Protocol_Version = OBD_Protocol_Version;
NarendraSingh 13:8955f2e95021 153 Vehicle_Status_Lora_Packet.Packet_Type = STATUS_PACKET_CMD;
NarendraSingh 11:77e595130230 154 for(i=0;i<17;i++)
NarendraSingh 19:886d50ecc718 155 Misc_Packet_Data.VIN[i] = (30+i);//Vehicle_Identification_Number[i];
NarendraSingh 11:77e595130230 156 for(i=0;i<3;i++)
NarendraSingh 19:886d50ecc718 157 Misc_Packet_Data.ODO_METER_READING[i] = 0x05; //Dummyy data, To be read using OBD
NarendraSingh 13:8955f2e95021 158 Vehicle_Status_Lora_Packet.Fuel_Level = 1050;//10.5 litre
NarendraSingh 19:886d50ecc718 159 Misc_Packet_Data.OBD_Battery_Voltage = 350;
NarendraSingh 19:886d50ecc718 160 Misc_Packet_Data.Car_Battery_Voltage = 1250;
NarendraSingh 19:886d50ecc718 161 Misc_Packet_Data.OBD_Battery_Temperature = 95;
NarendraSingh 19:886d50ecc718 162 Misc_Packet_Data.Car_Ambient_Temperature = 104;
NarendraSingh 11:77e595130230 163 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 164 Vehicle_Status_Lora_Packet.BLE_Adv_Beacon_ID[i] = BLE_Adv_Module_Beacon_ID[i]; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 165 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 166 Fixed_Beacon_Packet.Parking1_Beacon_ID[i] = (30+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 167 Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = 0x07; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 13:8955f2e95021 168 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 169 Fixed_Beacon_Packet.Parking2_Beacon_ID[i] = (40+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 170 Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = 0x08; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 13:8955f2e95021 171 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 172 Fixed_Beacon_Packet.Parking3_Beacon_ID[i] = (50+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 173 Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = 0x09; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 174 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 175 Near_Car_Beacon_Packet.Near_Car1_Beacon_ID[i] = (60+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 176 Near_Car_Beacon_Packet.Near_Car1_Beacon_Signal_Strength = 0x09; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 177 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 178 Near_Car_Beacon_Packet.Near_Car2_Beacon_ID[i] = (70+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 179 Near_Car_Beacon_Packet.Near_Car2_Beacon_Signal_Strength = 0x09; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 13:8955f2e95021 180 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 181 Near_Car_Beacon_Packet.Near_Car3_Beacon_ID[i] = (80+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 182 Near_Car_Beacon_Packet.Near_Car3_Beacon_Signal_Strength = 0x09; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 183 Vehicle_Status_Lora_Packet.FCS = 0x00; //FCS of all packets
NarendraSingh 11:77e595130230 184
NarendraSingh 11:77e595130230 185 /******* Initialize Lora packet for CheckIn *****/
NarendraSingh 11:77e595130230 186 CheckIN_Lora_Packet.Header = LORA_PACKET_HEADER;
NarendraSingh 19:886d50ecc718 187 Misc_Packet_Data.Protocol_Version = OBD_Protocol_Version;
NarendraSingh 11:77e595130230 188 CheckIN_Lora_Packet.Packet_Type = CHECKIN_PACKET_CMD;
NarendraSingh 11:77e595130230 189 for(i=0;i<17;i++)
NarendraSingh 19:886d50ecc718 190 Misc_Packet_Data.VIN[i] = Vehicle_Identification_Number[i];
NarendraSingh 11:77e595130230 191 for(i=0;i<3;i++)
NarendraSingh 19:886d50ecc718 192 Misc_Packet_Data.ODO_METER_READING[i] = 0x00; //Dummyy data, To be read using OBD
NarendraSingh 11:77e595130230 193 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 194 Fixed_Beacon_Packet.Parking1_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 195 Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 196 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 197 Fixed_Beacon_Packet.Parking2_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 198 Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 199 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 200 Fixed_Beacon_Packet.Parking3_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 201 Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 202 CheckIN_Lora_Packet.FCS = 0x00; //FCS of all packets
NarendraSingh 11:77e595130230 203
NarendraSingh 19:886d50ecc718 204 /******* Initialize Lora packet for CheckOUT *****/
NarendraSingh 19:886d50ecc718 205 CheckOUT_Packet.Header = LORA_PACKET_HEADER;
NarendraSingh 19:886d50ecc718 206 Misc_Packet_Data.Protocol_Version = OBD_Protocol_Version;
NarendraSingh 19:886d50ecc718 207 CheckOUT_Packet.Packet_Type = CHECKIN_PACKET_CMD;
NarendraSingh 19:886d50ecc718 208 for(i=0;i<17;i++)
NarendraSingh 19:886d50ecc718 209 Misc_Packet_Data.VIN[i] = Vehicle_Identification_Number[i];
NarendraSingh 19:886d50ecc718 210 for(i=0;i<3;i++)
NarendraSingh 19:886d50ecc718 211 Misc_Packet_Data.ODO_METER_READING[i] = 0x00; //Dummyy data, To be read using OBD
NarendraSingh 19:886d50ecc718 212 for(i=0;i<6;i++)
NarendraSingh 19:886d50ecc718 213 Fixed_Beacon_Packet.Parking1_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 19:886d50ecc718 214 Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 19:886d50ecc718 215 for(i=0;i<6;i++)
NarendraSingh 19:886d50ecc718 216 Fixed_Beacon_Packet.Parking2_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 19:886d50ecc718 217 Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 19:886d50ecc718 218 for(i=0;i<6;i++)
NarendraSingh 19:886d50ecc718 219 Fixed_Beacon_Packet.Parking3_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 19:886d50ecc718 220 Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 19:886d50ecc718 221 CheckOUT_Packet.FCS = 0x00; //FCS of all packets
NarendraSingh 19:886d50ecc718 222
NarendraSingh 11:77e595130230 223 /******* Initialize Lora packet for Movement *****/
NarendraSingh 11:77e595130230 224 Motion_Lora_Packet.Header = LORA_PACKET_HEADER;
NarendraSingh 19:886d50ecc718 225 Misc_Packet_Data.Protocol_Version = OBD_Protocol_Version;
NarendraSingh 11:77e595130230 226 Motion_Lora_Packet.Packet_Type = MOTION_PACKET_CMD;
NarendraSingh 11:77e595130230 227 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 228 Fixed_Beacon_Packet.Parking1_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 229 Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = 0x00; //Signal Strength of 1st NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 230 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 231 Fixed_Beacon_Packet.Parking2_Beacon_ID[i] = 0x00; //MAC ID of 2nd NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 232 Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = 0x00; //Signal Strength of 2nd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 233 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 234 Fixed_Beacon_Packet.Parking3_Beacon_ID[i] = 0x00; //MAC ID of 3rd NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 16:7703b9d92326 235 Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 236 Motion_Lora_Packet.Acceleration_Type = 0x00; //Type of acceleration, Vehicle Started/Vehicle Stopped/Sudden Vehicle Movement
NarendraSingh 11:77e595130230 237 Motion_Lora_Packet.FCS = 0x00; //FCS of all packets
NarendraSingh 14:144ed8b74713 238
NarendraSingh 11:77e595130230 239 }
NarendraSingh 16:7703b9d92326 240
NarendraSingh 11:77e595130230 241 //HeartBeat Packet should be sent every 30sec
NarendraSingh 11:77e595130230 242 void Send_HeartBeat_Packet(void)
NarendraSingh 11:77e595130230 243 {
NarendraSingh 11:77e595130230 244 //write code to read obd data,temperature,beacon data
NarendraSingh 11:77e595130230 245 uint8 Pos = 0,i;
NarendraSingh 20:f812f85cf97e 246 Lora_Packet_To_Send[Pos++] = 0x41;//Heart_Beat_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 20:f812f85cf97e 247 Lora_Packet_To_Send[Pos++] = 0x54;//Misc_Packet_Data.Protocol_Version;
NarendraSingh 20:f812f85cf97e 248 Lora_Packet_To_Send[Pos++] = 0x2b;//Heart_Beat_Lora_Packet.Packet_Type; //MSB of Motion Packet Type
NarendraSingh 20:f812f85cf97e 249 Lora_Packet_To_Send[Pos++] = 0x53;//(Misc_Packet_Data.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage
NarendraSingh 20:f812f85cf97e 250 Lora_Packet_To_Send[Pos++] = 0x45;//(Misc_Packet_Data.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 20:f812f85cf97e 251 Lora_Packet_To_Send[Pos++] = 0x4e;//(Misc_Packet_Data.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature
NarendraSingh 20:f812f85cf97e 252 Lora_Packet_To_Send[Pos++] = 0x44;//(Misc_Packet_Data.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature
NarendraSingh 20:f812f85cf97e 253 Lora_Packet_To_Send[Pos++] = 0x20;//Vehicle_Status_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 11:77e595130230 254 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 19:886d50ecc718 255 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Protocol_Version;
NarendraSingh 11:77e595130230 256 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Packet_Type; //MSB of Motion Packet Type
NarendraSingh 19:886d50ecc718 257 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage
NarendraSingh 19:886d50ecc718 258 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 19:886d50ecc718 259 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature
NarendraSingh 19:886d50ecc718 260 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature
NarendraSingh 19:886d50ecc718 261 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.OBD_Battery_Temperature; //Get Battery Temperature
NarendraSingh 19:886d50ecc718 262 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Car_Ambient_Temperature; //Get Ambient Temperature
NarendraSingh 11:77e595130230 263 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 264 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 16:7703b9d92326 265 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 266 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 267 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 16:7703b9d92326 268 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 269 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 270 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 20:f812f85cf97e 271 Lora_Packet_To_Send[Pos++] = 0x04;//Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 20:f812f85cf97e 272 Lora_Packet_To_Send[Pos++] = 0x05;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes
NarendraSingh 11:77e595130230 273 Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET;
NarendraSingh 11:77e595130230 274 Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos);
NarendraSingh 11:77e595130230 275 }
NarendraSingh 11:77e595130230 276
NarendraSingh 11:77e595130230 277 //CheckIN packets sending should be started when device is plugged in to the vehicle. It should be sent every 5sec for 2minutes and afterthat it should stop sending
NarendraSingh 11:77e595130230 278 void Send_Vehicle_Status_Packet(void)
NarendraSingh 11:77e595130230 279 {
NarendraSingh 11:77e595130230 280 //write code to read OBD data,temperature,beacon data
NarendraSingh 11:77e595130230 281 uint8 Pos = 0,i;
NarendraSingh 20:f812f85cf97e 282 Lora_Packet_To_Send[Pos++] = 0x41;//Heart_Beat_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 20:f812f85cf97e 283 Lora_Packet_To_Send[Pos++] = 0x54;//Misc_Packet_Data.Protocol_Version;
NarendraSingh 20:f812f85cf97e 284 Lora_Packet_To_Send[Pos++] = 0x2b;//Heart_Beat_Lora_Packet.Packet_Type; //MSB of Motion Packet Type
NarendraSingh 20:f812f85cf97e 285 Lora_Packet_To_Send[Pos++] = 0x53;//(Misc_Packet_Data.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage
NarendraSingh 20:f812f85cf97e 286 Lora_Packet_To_Send[Pos++] = 0x45;//(Misc_Packet_Data.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 20:f812f85cf97e 287 Lora_Packet_To_Send[Pos++] = 0x4e;//(Misc_Packet_Data.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature
NarendraSingh 20:f812f85cf97e 288 Lora_Packet_To_Send[Pos++] = 0x44;//(Misc_Packet_Data.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature
NarendraSingh 20:f812f85cf97e 289 Lora_Packet_To_Send[Pos++] = 0x20;//Vehicle_Status_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 20:f812f85cf97e 290 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 20:f812f85cf97e 291 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Protocol_Version;
NarendraSingh 20:f812f85cf97e 292 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Packet_Type; //MSB of Motion Packet Type
NarendraSingh 20:f812f85cf97e 293 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage
NarendraSingh 20:f812f85cf97e 294 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 20:f812f85cf97e 295 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature
NarendraSingh 20:f812f85cf97e 296 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature
NarendraSingh 11:77e595130230 297 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 19:886d50ecc718 298 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Protocol_Version; //MSB of Motion Packet Type
NarendraSingh 11:77e595130230 299 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Packet_Type; //MSB of Motion Packet Type
NarendraSingh 11:77e595130230 300 for(i=0;i < 17;i++)
NarendraSingh 19:886d50ecc718 301 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.VIN[i]; //Get OBD_ID
NarendraSingh 11:77e595130230 302 for(i=0;i < 3;i++)
NarendraSingh 19:886d50ecc718 303 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.ODO_METER_READING[i]; //Get OBD_ID
NarendraSingh 11:77e595130230 304 Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.Fuel_Level >> 8); //Get OBD_Battery Voltage
NarendraSingh 11:77e595130230 305 Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.Fuel_Level & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 19:886d50ecc718 306 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage
NarendraSingh 19:886d50ecc718 307 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 19:886d50ecc718 308 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature
NarendraSingh 19:886d50ecc718 309 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature
NarendraSingh 19:886d50ecc718 310 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.OBD_Battery_Temperature; //Get Battery Temperature
NarendraSingh 19:886d50ecc718 311 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Car_Ambient_Temperature; //Get Ambient Temperature
NarendraSingh 11:77e595130230 312 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 313 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.BLE_Adv_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 314 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 315 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 20:f812f85cf97e 316 /*Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 317 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 318 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 16:7703b9d92326 319 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 320 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 321 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 16:7703b9d92326 322 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 323 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 324 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 16:7703b9d92326 325 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 326 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 327 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 16:7703b9d92326 328 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 329 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 330 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 20:f812f85cf97e 331 */Lora_Packet_To_Send[Pos++] = 0x06;//Near_Car_Beacon_Packet.Near_Car3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 20:f812f85cf97e 332 Lora_Packet_To_Send[Pos++] = 0x04;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes
NarendraSingh 11:77e595130230 333 Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos);
NarendraSingh 11:77e595130230 334 }
NarendraSingh 11:77e595130230 335
NarendraSingh 11:77e595130230 336 //CheckIN packets sending should be started when device is plugged in to the vehicle. It should be sent every 5sec for 2minutes and afterthat it should stop sending
NarendraSingh 11:77e595130230 337 void Send_CheckIN_Packet(void)
NarendraSingh 11:77e595130230 338 {
NarendraSingh 11:77e595130230 339 //write code to read OBD data,temperature,beacon data
NarendraSingh 11:77e595130230 340 uint8 Pos = 0,i;
NarendraSingh 11:77e595130230 341 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 19:886d50ecc718 342 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Protocol_Version; //MSB of Motion Packet Type
NarendraSingh 16:7703b9d92326 343 if(OBD_PlugIN_State)
NarendraSingh 16:7703b9d92326 344 Lora_Packet_To_Send[Pos++] = CHECK_IN_PACKET; //Check detected. Set packet type to Checkout type
NarendraSingh 16:7703b9d92326 345 else
NarendraSingh 16:7703b9d92326 346 Lora_Packet_To_Send[Pos++] = CHECK_OUT_PACKET; //Checkout detected. Set packet type to Checkout type
NarendraSingh 11:77e595130230 347 for(i=0;i < 17;i++)
NarendraSingh 19:886d50ecc718 348 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.VIN[i]; //Get OBD_ID
NarendraSingh 11:77e595130230 349 for(i=0;i < 3;i++)
NarendraSingh 19:886d50ecc718 350 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.ODO_METER_READING[i]; //Get OBD_ID
NarendraSingh 19:886d50ecc718 351 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage
NarendraSingh 19:886d50ecc718 352 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 19:886d50ecc718 353 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature
NarendraSingh 19:886d50ecc718 354 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature
NarendraSingh 19:886d50ecc718 355 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.OBD_Battery_Temperature; //Get Battery Temperature
NarendraSingh 19:886d50ecc718 356 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Car_Ambient_Temperature; //Get Ambient Temperature
NarendraSingh 11:77e595130230 357 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 358 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 16:7703b9d92326 359 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 360 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 361 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 16:7703b9d92326 362 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 363 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 364 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 16:7703b9d92326 365 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 366 Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes
NarendraSingh 11:77e595130230 367 CheckIN_Packet_Sent_Count++;
NarendraSingh 11:77e595130230 368 Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos);
NarendraSingh 19:886d50ecc718 369 pc2.printf("CheckIN Packet Sent");
NarendraSingh 11:77e595130230 370 }
NarendraSingh 11:77e595130230 371
NarendraSingh 18:86f069689502 372
NarendraSingh 18:86f069689502 373 //CheckIN packets sending should be started when device is plugged in to the vehicle. It should be sent every 5sec for 2minutes and afterthat it should stop sending
NarendraSingh 18:86f069689502 374 void Send_CheckOUT_Packet(void)
NarendraSingh 18:86f069689502 375 {
NarendraSingh 18:86f069689502 376 //write code to read OBD data,temperature,beacon data
NarendraSingh 18:86f069689502 377 uint8 Pos = 0,i;
NarendraSingh 18:86f069689502 378 Lora_Packet_To_Send[Pos++] = CheckOUT_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 19:886d50ecc718 379 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Protocol_Version; //MSB of Motion Packet Type
NarendraSingh 18:86f069689502 380 Lora_Packet_To_Send[Pos++] = CHECK_OUT_PACKET; //Checkout detected. Set packet type to Checkout type
NarendraSingh 18:86f069689502 381 for(i=0;i < 17;i++)
NarendraSingh 19:886d50ecc718 382 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.VIN[i]; //Get OBD_ID
NarendraSingh 19:886d50ecc718 383 for(i=0;i < 3;i++)
NarendraSingh 19:886d50ecc718 384 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.ODO_METER_READING[i]; //Get OBD_ID
NarendraSingh 19:886d50ecc718 385 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage
NarendraSingh 19:886d50ecc718 386 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 19:886d50ecc718 387 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature
NarendraSingh 19:886d50ecc718 388 Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature
NarendraSingh 19:886d50ecc718 389 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.OBD_Battery_Temperature; //Get Battery Temperature
NarendraSingh 19:886d50ecc718 390 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Car_Ambient_Temperature; //Get Ambient Temperature
NarendraSingh 18:86f069689502 391 Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes
NarendraSingh 18:86f069689502 392 CheckIN_Packet_Sent_Count++;
NarendraSingh 18:86f069689502 393 Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos);
NarendraSingh 19:886d50ecc718 394 pc2.printf("CheckOUT Packet Sent");
NarendraSingh 19:886d50ecc718 395
NarendraSingh 18:86f069689502 396 }
NarendraSingh 18:86f069689502 397
NarendraSingh 11:77e595130230 398 //Motion packets sending should be started when vehicle acceleration changes like when it starts moving,stops moving and gets sudden jurk in case of theft.
NarendraSingh 11:77e595130230 399 // It should be sent every 30sec for 2minutes and afterthat it should stop sending
NarendraSingh 11:77e595130230 400 void Send_Motion_Packet(void)
NarendraSingh 11:77e595130230 401 {
NarendraSingh 11:77e595130230 402 //write code to read accelerometer data,temperature,beacon data
NarendraSingh 11:77e595130230 403 uint8 Pos = 0,i;
NarendraSingh 11:77e595130230 404 Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 19:886d50ecc718 405 Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Protocol_Version; //Protocol Version
NarendraSingh 11:77e595130230 406 Lora_Packet_To_Send[Pos++] = (Motion_Lora_Packet.Packet_Type & 0xFF); //LSB of Motion Packet Type
NarendraSingh 11:77e595130230 407 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 408 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 16:7703b9d92326 409 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 410 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 411 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 16:7703b9d92326 412 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 413 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 414 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 16:7703b9d92326 415 Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 14:144ed8b74713 416 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 417 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 16:7703b9d92326 418 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 14:144ed8b74713 419 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 420 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 16:7703b9d92326 421 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 14:144ed8b74713 422 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 423 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 16:7703b9d92326 424 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 425 Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Acceleration_Type; //get Type of Acceleration
NarendraSingh 14:144ed8b74713 426 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 427 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 16:7703b9d92326 428 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 14:144ed8b74713 429 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 430 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 16:7703b9d92326 431 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 14:144ed8b74713 432 for(i=0;i<6;i++)
NarendraSingh 16:7703b9d92326 433 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 16:7703b9d92326 434 Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 435 Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes
NarendraSingh 11:77e595130230 436 Motion_Packet_Sent_Count++;
NarendraSingh 11:77e595130230 437 Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos);
NarendraSingh 11:77e595130230 438 }
NarendraSingh 11:77e595130230 439
NarendraSingh 11:77e595130230 440
NarendraSingh 11:77e595130230 441 void Get_Lora_Response(void)
NarendraSingh 11:77e595130230 442 {
NarendraSingh 11:77e595130230 443 static uint16 Temp_Pos1;
NarendraSingh 11:77e595130230 444 static uint8 Lora_Response_Found = 0x00;
NarendraSingh 11:77e595130230 445 static uint8 Response_Start_Pos[5];
NarendraSingh 11:77e595130230 446 Lora_Response_Found = 0;
NarendraSingh 11:77e595130230 447 Temp_Pos1 = LORA_UART_RX_Crnt_Pos = 0;
NarendraSingh 11:77e595130230 448 while(Temp_Pos1 < Lora_RxBuffer_End_Pos)
NarendraSingh 11:77e595130230 449 { //check for end of AT response to calculate length
NarendraSingh 11:77e595130230 450 if((LORA_UART_RX_Buffer[Temp_Pos1] != 0x0D) || (LORA_UART_RX_Buffer[Temp_Pos1+1] != 0x0A))
NarendraSingh 11:77e595130230 451 { //check for AT end response <cr><lf> (i.e. 0x0D,0x0A)
NarendraSingh 11:77e595130230 452 Temp_Pos1++;
NarendraSingh 11:77e595130230 453 }
NarendraSingh 11:77e595130230 454 else
NarendraSingh 11:77e595130230 455 {
NarendraSingh 11:77e595130230 456 Temp_Pos1+=2;
NarendraSingh 11:77e595130230 457 Response_Start_Pos[Lora_Response_Found++] = Temp_Pos1;
NarendraSingh 17:758fb8454ab0 458 //pc2.printf("%c",Temp_Pos1);
NarendraSingh 11:77e595130230 459 if(Lora_Response_Found >= 4)
NarendraSingh 11:77e595130230 460 break;
NarendraSingh 11:77e595130230 461 }
NarendraSingh 11:77e595130230 462 }
NarendraSingh 11:77e595130230 463 if(Lora_Response_Found >= 3)
NarendraSingh 11:77e595130230 464 {
NarendraSingh 11:77e595130230 465 if((Response_Start_Pos[1] - Response_Start_Pos[0]) > 0x02) //Response received without data
NarendraSingh 11:77e595130230 466 {
NarendraSingh 11:77e595130230 467 LORA_UART_RX_Crnt_Pos = (Response_Start_Pos[0]);
NarendraSingh 11:77e595130230 468 Response_Start_Pos[1]-=2;
NarendraSingh 11:77e595130230 469 for(Temp_Pos1=0; LORA_UART_RX_Crnt_Pos < Response_Start_Pos[1]; Temp_Pos1++)
NarendraSingh 11:77e595130230 470 {
NarendraSingh 11:77e595130230 471 Lora_Command_Rcvd[Temp_Pos1] = LORA_UART_RX_Buffer[LORA_UART_RX_Crnt_Pos++];
NarendraSingh 17:758fb8454ab0 472 pc2.putc(Lora_Command_Rcvd[Temp_Pos1]);
NarendraSingh 11:77e595130230 473 }
NarendraSingh 11:77e595130230 474 Lora_RxBuffer_End_Pos = 0;
NarendraSingh 11:77e595130230 475 AT_Response_Receive_Status = SUCCESS;
NarendraSingh 11:77e595130230 476 }
NarendraSingh 11:77e595130230 477 else if((Response_Start_Pos[1] - Response_Start_Pos[0]) == 0x02) //Response received along with data
NarendraSingh 11:77e595130230 478 {
NarendraSingh 11:77e595130230 479 LORA_UART_RX_Crnt_Pos = (Response_Start_Pos[1]);
NarendraSingh 11:77e595130230 480 Response_Start_Pos[2]-=2;
NarendraSingh 11:77e595130230 481 for(Temp_Pos1=0; LORA_UART_RX_Crnt_Pos < Response_Start_Pos[2]; Temp_Pos1++)
NarendraSingh 11:77e595130230 482 {
NarendraSingh 11:77e595130230 483 Lora_Command_Rcvd[Temp_Pos1] = LORA_UART_RX_Buffer[LORA_UART_RX_Crnt_Pos++];
NarendraSingh 17:758fb8454ab0 484 pc2.putc(Lora_Command_Rcvd[Temp_Pos1]);
NarendraSingh 11:77e595130230 485 }
NarendraSingh 11:77e595130230 486 Lora_RxBuffer_End_Pos = 0;
NarendraSingh 11:77e595130230 487 AT_Response_Receive_Status = SUCCESS;
NarendraSingh 11:77e595130230 488 }
NarendraSingh 11:77e595130230 489 }
NarendraSingh 11:77e595130230 490 }
NarendraSingh 11:77e595130230 491
NarendraSingh 11:77e595130230 492 //Function to send general Lora packets using "AT+SEND" Command
NarendraSingh 11:77e595130230 493 void Send_Lora_Packet_To_Gateway(uint8* Command_To_Send,uint8 Length)
NarendraSingh 11:77e595130230 494 {
NarendraSingh 11:77e595130230 495 uint8 i=0;
NarendraSingh 20:f812f85cf97e 496 //pc2.printf("Sending data");
NarendraSingh 20:f812f85cf97e 497 // LORA_Module_UART.printf("%s",Send_Lora_Packet);//write to serial port for sending through lora module
NarendraSingh 20:f812f85cf97e 498 /*for(i=0; i < 7; i++)
NarendraSingh 20:f812f85cf97e 499 {
NarendraSingh 20:f812f85cf97e 500 LORA_Module_UART.putc(Send_Lora_Packet[i]);
NarendraSingh 20:f812f85cf97e 501
NarendraSingh 20:f812f85cf97e 502 }*/
NarendraSingh 20:f812f85cf97e 503 for(i=0; i < Length; i++)
NarendraSingh 20:f812f85cf97e 504 {
NarendraSingh 20:f812f85cf97e 505 LORA_Module_UART.putc(Command_To_Send[i]);
NarendraSingh 20:f812f85cf97e 506 pc2.putc(Command_To_Send[i]);
NarendraSingh 20:f812f85cf97e 507 }
NarendraSingh 20:f812f85cf97e 508 for(i=0; i < Length; i++)
NarendraSingh 20:f812f85cf97e 509 {
NarendraSingh 20:f812f85cf97e 510 pc2.putc(Command_To_Send[i]);
NarendraSingh 20:f812f85cf97e 511
NarendraSingh 20:f812f85cf97e 512 }
NarendraSingh 20:f812f85cf97e 513
NarendraSingh 20:f812f85cf97e 514 // LORA_Module_UART.printf("%s",Command_To_Send);
NarendraSingh 11:77e595130230 515 LORA_Module_UART.printf("\r");
NarendraSingh 20:f812f85cf97e 516 pc2.printf("Total Bytes %d",Length);
NarendraSingh 11:77e595130230 517 }
NarendraSingh 11:77e595130230 518
NarendraSingh 11:77e595130230 519
NarendraSingh 11:77e595130230 520 unsigned char Calculate_Lora_Frame_FCS(unsigned char* Packet_Data,unsigned char Packet_Length)
NarendraSingh 11:77e595130230 521 {
NarendraSingh 11:77e595130230 522 uint8 i,FCS = 0x00;
NarendraSingh 11:77e595130230 523 for(i=0; i < Packet_Length; i++)
NarendraSingh 11:77e595130230 524 {
NarendraSingh 11:77e595130230 525 FCS ^= (Packet_Data[i]);
NarendraSingh 11:77e595130230 526 }
NarendraSingh 11:77e595130230 527 return(FCS);
NarendraSingh 11:77e595130230 528 }