BLE Transmitter not working

Fork of Dealer_23Feb by kumar singh

Committer:
NarendraSingh
Date:
Fri Jan 27 18:30:02 2017 +0000
Revision:
11:77e595130230
Child:
13:8955f2e95021
Before implementing queue;

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 11:77e595130230 8 RawSerial DEBUGING_UART(PA_9, PA_10);//USART1_TX->PA_9,USART1_RX->PA_10
NarendraSingh 11:77e595130230 9
NarendraSingh 11:77e595130230 10 uint8 Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //By Default Heart Beat PAckets should be sent
NarendraSingh 11:77e595130230 11 uint8 OBD_Protocol_Version = 0x01;
NarendraSingh 11:77e595130230 12 uint8 Vehicle_Identification_Number[17]; //Unique Vehicle_Identification_Number, Read using OBD
NarendraSingh 11:77e595130230 13 uint8 Motion_Packet_Sent_Count=0;
NarendraSingh 11:77e595130230 14 uint8 CheckIN_Packet_Sent_Count=0;
NarendraSingh 11:77e595130230 15 uint8 Lora_Packet_To_Send[100];
NarendraSingh 11:77e595130230 16 uint8 Send_Lora_Packet_Flag = FALSE;
NarendraSingh 11:77e595130230 17 int Lora_RxBuffer_Crnt_Pos,Lora_RxBuffer_End_Pos; // must be volatile or the compiler may over-optimise.
NarendraSingh 11:77e595130230 18 int receivedDataCount = 0;
NarendraSingh 11:77e595130230 19 char LORA_UART_RX_Buffer[LORA_UART_RX_Size];
NarendraSingh 11:77e595130230 20 int LORA_UART_RX_Crnt_Pos;
NarendraSingh 11:77e595130230 21 uint8 Lora_Command_Rcvd[100];
NarendraSingh 11:77e595130230 22 uint8 Lora_Cmd_Length = 0;
NarendraSingh 11:77e595130230 23 uint8 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 24
NarendraSingh 11:77e595130230 25 //Create Object for Type of Lora Packet to send
NarendraSingh 11:77e595130230 26 Heart_Beat_PacketType Heart_Beat_Lora_Packet; //Allocate Memory for HeartBeat Lora Packets
NarendraSingh 11:77e595130230 27 CheckIN_PacketType CheckIN_Lora_Packet; //Allocate Memory for CheckIN Lora Packets
NarendraSingh 11:77e595130230 28 Motion_PacketType Motion_Lora_Packet; //Allocate Memory for Movement Lora Packets
NarendraSingh 11:77e595130230 29 Vehicle_Status_PacketType Vehicle_Status_Lora_Packet; //Allocate Memory for Movement Lora Packets
NarendraSingh 11:77e595130230 30
NarendraSingh 11:77e595130230 31 //Lora AT Commands list
NarendraSingh 11:77e595130230 32 const char* Attention = {"AT\r"};
NarendraSingh 11:77e595130230 33 const char* Reset_Device = "ATZ\r";
NarendraSingh 11:77e595130230 34 const char* Reset_to_Factory_Defaults = "AT&F\r";
NarendraSingh 11:77e595130230 35 const char* Save_Configuration = "AT&W\r";
NarendraSingh 11:77e595130230 36 const char* Serial_Baud_Rate = "AT+IPR=";
NarendraSingh 11:77e595130230 37 const char* Join_Network = "AT+JOIN\r";
NarendraSingh 11:77e595130230 38 const char* Join_Retries = "AT+JR=";
NarendraSingh 11:77e595130230 39 const char* AES_Encryption = "AT+ENC=";
NarendraSingh 11:77e595130230 40 const char* Send_Lora_Packet = "AT+SEND ";
NarendraSingh 11:77e595130230 41 const char* Set_Frequency_Sub_Band = "AT+FSB=";
NarendraSingh 11:77e595130230 42 const char* Set_Network_ID = "AT+NI=";
NarendraSingh 11:77e595130230 43 const char* Set_Network_Key = "AT+NK=";
NarendraSingh 11:77e595130230 44 const char* Network_Key = "010203123";
NarendraSingh 11:77e595130230 45 const char* Network_ID = "010203040";
NarendraSingh 11:77e595130230 46 const char* Network_Join_Retries = "AT+JR=";
NarendraSingh 11:77e595130230 47 const char* Network_Join_Status = "AT+NJS=";
NarendraSingh 11:77e595130230 48 const char* Lora_Device_ID = "AT+DI\r";
NarendraSingh 11:77e595130230 49
NarendraSingh 11:77e595130230 50
NarendraSingh 11:77e595130230 51
NarendraSingh 11:77e595130230 52 uint8 Calculate_Lora_Frame_FCS(uint8* Packet_Data,uint8 Packet_Length);
NarendraSingh 11:77e595130230 53 void Set_Up_Lora_Network_Configuration(void);
NarendraSingh 11:77e595130230 54 void Get_Lora_Response(void);
NarendraSingh 11:77e595130230 55
NarendraSingh 11:77e595130230 56 //Set Up lora network
NarendraSingh 11:77e595130230 57 void Set_Up_Lora_Network_Configuration(void)
NarendraSingh 11:77e595130230 58 {
NarendraSingh 11:77e595130230 59 LORA_Module_UART.baud(115200);
NarendraSingh 11:77e595130230 60 LORA_Module_UART.printf(Attention);//Send Attention command
NarendraSingh 11:77e595130230 61 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 62 DEBUGING_UART.printf("Nwk set up started");
NarendraSingh 11:77e595130230 63 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 64 Get_Lora_Response();
NarendraSingh 11:77e595130230 65 DEBUGING_UART.printf("AT Response received");
NarendraSingh 11:77e595130230 66 LORA_Module_UART.printf("%s%d\r",Set_Frequency_Sub_Band,FREQUENCY_SUB_BAND_CHANNEL7);//set frequency sub band to 7
NarendraSingh 11:77e595130230 67 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 68 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 69 Get_Lora_Response();
NarendraSingh 11:77e595130230 70 DEBUGING_UART.printf("Frequency band response received");
NarendraSingh 11:77e595130230 71 LORA_Module_UART.printf("%s%d,%s\r",Set_Network_Key,STRING_PARAMETER,Network_Key); //set network key
NarendraSingh 11:77e595130230 72 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 73 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 74 Get_Lora_Response();
NarendraSingh 11:77e595130230 75 DEBUGING_UART.printf("Network key Response received");
NarendraSingh 11:77e595130230 76 LORA_Module_UART.printf("%s%d,%s\r",Set_Network_ID,STRING_PARAMETER,Network_ID); //set network id
NarendraSingh 11:77e595130230 77 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 78 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 79 Get_Lora_Response();
NarendraSingh 11:77e595130230 80 DEBUGING_UART.printf("Network Id response received");
NarendraSingh 11:77e595130230 81
NarendraSingh 11:77e595130230 82 //LORA_Module_UART.printf("%s",Reset_Device); //reset device
NarendraSingh 11:77e595130230 83 //wait_ms(3500);
NarendraSingh 11:77e595130230 84
NarendraSingh 11:77e595130230 85 LORA_Module_UART.printf("AT+TXDR=DR2\r");
NarendraSingh 11:77e595130230 86 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 87 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 88 Get_Lora_Response();
NarendraSingh 11:77e595130230 89 LORA_Module_UART.printf("AT+ADR=1\r"); //Enable adaptive data rate
NarendraSingh 11:77e595130230 90 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 91 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 92 Get_Lora_Response();
NarendraSingh 11:77e595130230 93 LORA_Module_UART.printf("%s",Save_Configuration); //save configuration
NarendraSingh 11:77e595130230 94 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 95 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 96 Get_Lora_Response();
NarendraSingh 11:77e595130230 97 DEBUGING_UART.printf("Configuration saved");
NarendraSingh 11:77e595130230 98 LORA_Module_UART.printf(Attention); //Send Attention command
NarendraSingh 11:77e595130230 99 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 100 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 101 Get_Lora_Response();
NarendraSingh 11:77e595130230 102 DEBUGING_UART.printf("AT Response received");
NarendraSingh 11:77e595130230 103 LORA_Module_UART.printf("%s",Join_Network); //join network with gateway
NarendraSingh 11:77e595130230 104 AT_Response_Receive_Status = FAILURE;
NarendraSingh 11:77e595130230 105 while(AT_Response_Receive_Status)
NarendraSingh 11:77e595130230 106 Get_Lora_Response();
NarendraSingh 11:77e595130230 107 DEBUGING_UART.printf("Join Response received");
NarendraSingh 11:77e595130230 108 }
NarendraSingh 11:77e595130230 109
NarendraSingh 11:77e595130230 110 void Initialize_lora_Packets()
NarendraSingh 11:77e595130230 111 {
NarendraSingh 11:77e595130230 112 uint8 i;
NarendraSingh 11:77e595130230 113 /******* Initialize Lora packet for HeartBeat *****/
NarendraSingh 11:77e595130230 114 Heart_Beat_Lora_Packet.Header = LORA_PACKET_HEADER;
NarendraSingh 11:77e595130230 115 Heart_Beat_Lora_Packet.Protocol_Version = OBD_Protocol_Version;
NarendraSingh 11:77e595130230 116 Heart_Beat_Lora_Packet.Packet_Type = HEART_BEAT_PACKET_CMD;
NarendraSingh 11:77e595130230 117 Heart_Beat_Lora_Packet.OBD_Battery_Voltage = 0x1234;
NarendraSingh 11:77e595130230 118 Heart_Beat_Lora_Packet.Car_Battery_Voltage = 0x5241;
NarendraSingh 11:77e595130230 119 Heart_Beat_Lora_Packet.OBD_Battery_Temperature = 0x00;
NarendraSingh 11:77e595130230 120 Heart_Beat_Lora_Packet.Car_Ambient_Temperature = 0x00;
NarendraSingh 11:77e595130230 121 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 122 Heart_Beat_Lora_Packet.Parking1_Beacon_ID[i] = 0x78; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 123 Heart_Beat_Lora_Packet.Parking1_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 124 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 125 Heart_Beat_Lora_Packet.Parking2_Beacon_ID[i] = 0x12; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 126 Heart_Beat_Lora_Packet.Parking2_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 127 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 128 Heart_Beat_Lora_Packet.Parking3_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 129 Heart_Beat_Lora_Packet.Parking3_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 130 Heart_Beat_Lora_Packet.FCS = 0x00; //FCS of all packets
NarendraSingh 11:77e595130230 131
NarendraSingh 11:77e595130230 132 /******* Initialize Lora packet for Vehicle Status *****/
NarendraSingh 11:77e595130230 133 Vehicle_Status_Lora_Packet.Header = LORA_PACKET_HEADER;
NarendraSingh 11:77e595130230 134 Vehicle_Status_Lora_Packet.Protocol_Version = OBD_Protocol_Version;
NarendraSingh 11:77e595130230 135 Vehicle_Status_Lora_Packet.Packet_Type = CHECKIN_PACKET_CMD;
NarendraSingh 11:77e595130230 136 for(i=0;i<17;i++)
NarendraSingh 11:77e595130230 137 Vehicle_Status_Lora_Packet.VIN[i] = Vehicle_Identification_Number[i];
NarendraSingh 11:77e595130230 138 for(i=0;i<3;i++)
NarendraSingh 11:77e595130230 139 Vehicle_Status_Lora_Packet.ODO_METER_READING[i] = 0x00; //Dummyy data, To be read using OBD
NarendraSingh 11:77e595130230 140 Vehicle_Status_Lora_Packet.OBD_Battery_Voltage = 0x1234;
NarendraSingh 11:77e595130230 141 Vehicle_Status_Lora_Packet.Car_Battery_Voltage = 0x5241;
NarendraSingh 11:77e595130230 142 Vehicle_Status_Lora_Packet.OBD_Battery_Temperature = 0x00;
NarendraSingh 11:77e595130230 143 Vehicle_Status_Lora_Packet.Car_Ambient_Temperature = 0x00;
NarendraSingh 11:77e595130230 144 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 145 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 146 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 147 Vehicle_Status_Lora_Packet.Parking1_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 148 Vehicle_Status_Lora_Packet.Parking1_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 149 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 150 Vehicle_Status_Lora_Packet.Parking2_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 151 Vehicle_Status_Lora_Packet.Parking2_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 152 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 153 Vehicle_Status_Lora_Packet.Parking3_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 154 Vehicle_Status_Lora_Packet.Parking3_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 155 Vehicle_Status_Lora_Packet.FCS = 0x00; //FCS of all packets
NarendraSingh 11:77e595130230 156
NarendraSingh 11:77e595130230 157 /******* Initialize Lora packet for CheckIn *****/
NarendraSingh 11:77e595130230 158 CheckIN_Lora_Packet.Header = LORA_PACKET_HEADER;
NarendraSingh 11:77e595130230 159 CheckIN_Lora_Packet.Protocol_Version = OBD_Protocol_Version;
NarendraSingh 11:77e595130230 160 CheckIN_Lora_Packet.Packet_Type = CHECKIN_PACKET_CMD;
NarendraSingh 11:77e595130230 161 for(i=0;i<17;i++)
NarendraSingh 11:77e595130230 162 CheckIN_Lora_Packet.VIN[i] = Vehicle_Identification_Number[i];
NarendraSingh 11:77e595130230 163 for(i=0;i<3;i++)
NarendraSingh 11:77e595130230 164 CheckIN_Lora_Packet.ODO_METER_READING[i] = 0x00; //Dummyy data, To be read using OBD
NarendraSingh 11:77e595130230 165 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 166 CheckIN_Lora_Packet.Parking1_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 167 CheckIN_Lora_Packet.Parking1_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 168 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 169 CheckIN_Lora_Packet.Parking2_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 170 CheckIN_Lora_Packet.Parking2_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 171 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 172 CheckIN_Lora_Packet.Parking3_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 173 CheckIN_Lora_Packet.Parking3_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 174 CheckIN_Lora_Packet.FCS = 0x00; //FCS of all packets
NarendraSingh 11:77e595130230 175
NarendraSingh 11:77e595130230 176 /******* Initialize Lora packet for Movement *****/
NarendraSingh 11:77e595130230 177 Motion_Lora_Packet.Header = LORA_PACKET_HEADER;
NarendraSingh 11:77e595130230 178 Motion_Lora_Packet.Protocol_Version = OBD_Protocol_Version;
NarendraSingh 11:77e595130230 179 Motion_Lora_Packet.Packet_Type = MOTION_PACKET_CMD;
NarendraSingh 11:77e595130230 180 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 181 Motion_Lora_Packet.Parking1_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 182 Motion_Lora_Packet.Parking1_Beacon_Signal_Strength = 0x00; //Signal Strength of 1st NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 183 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 184 Motion_Lora_Packet.Parking2_Beacon_ID[i] = 0x00; //MAC ID of 2nd NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 185 Motion_Lora_Packet.Parking2_Beacon_Signal_Strength = 0x00; //Signal Strength of 2nd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 186 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 187 Motion_Lora_Packet.Parking3_Beacon_ID[i] = 0x00; //MAC ID of 3rd NearBy Beacon Device with Highest Signal Strength,dummy data
NarendraSingh 11:77e595130230 188 Motion_Lora_Packet.Parking3_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength
NarendraSingh 11:77e595130230 189 Motion_Lora_Packet.Acceleration_Type = 0x00; //Type of acceleration, Vehicle Started/Vehicle Stopped/Sudden Vehicle Movement
NarendraSingh 11:77e595130230 190 Motion_Lora_Packet.FCS = 0x00; //FCS of all packets
NarendraSingh 11:77e595130230 191 }
NarendraSingh 11:77e595130230 192
NarendraSingh 11:77e595130230 193 //HeartBeat Packet should be sent every 30sec
NarendraSingh 11:77e595130230 194 void Send_HeartBeat_Packet(void)
NarendraSingh 11:77e595130230 195 {
NarendraSingh 11:77e595130230 196 //write code to read obd data,temperature,beacon data
NarendraSingh 11:77e595130230 197 uint8 Pos = 0,i;
NarendraSingh 11:77e595130230 198 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 11:77e595130230 199 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Protocol_Version;
NarendraSingh 11:77e595130230 200 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Packet_Type; //MSB of Motion Packet Type
NarendraSingh 11:77e595130230 201 Lora_Packet_To_Send[Pos++] = (Heart_Beat_Lora_Packet.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage
NarendraSingh 11:77e595130230 202 Lora_Packet_To_Send[Pos++] = (Heart_Beat_Lora_Packet.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 11:77e595130230 203 Lora_Packet_To_Send[Pos++] = (Heart_Beat_Lora_Packet.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature
NarendraSingh 11:77e595130230 204 Lora_Packet_To_Send[Pos++] = (Heart_Beat_Lora_Packet.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature
NarendraSingh 11:77e595130230 205 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.OBD_Battery_Temperature; //Get Battery Temperature
NarendraSingh 11:77e595130230 206 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Car_Ambient_Temperature; //Get Ambient Temperature
NarendraSingh 11:77e595130230 207 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 208 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 209 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 210 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 211 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 212 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 213 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 214 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 215 Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 216 Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes
NarendraSingh 11:77e595130230 217 Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET;
NarendraSingh 11:77e595130230 218 Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos);
NarendraSingh 11:77e595130230 219 }
NarendraSingh 11:77e595130230 220
NarendraSingh 11:77e595130230 221 //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 222 void Send_Vehicle_Status_Packet(void)
NarendraSingh 11:77e595130230 223 {
NarendraSingh 11:77e595130230 224 //write code to read OBD data,temperature,beacon data
NarendraSingh 11:77e595130230 225 uint8 Pos = 0,i;
NarendraSingh 11:77e595130230 226 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 11:77e595130230 227 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Protocol_Version; //MSB of Motion Packet Type
NarendraSingh 11:77e595130230 228 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Packet_Type; //MSB of Motion Packet Type
NarendraSingh 11:77e595130230 229 for(i=0;i < 17;i++)
NarendraSingh 11:77e595130230 230 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.VIN[i]; //Get OBD_ID
NarendraSingh 11:77e595130230 231 for(i=0;i < 3;i++)
NarendraSingh 11:77e595130230 232 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.ODO_METER_READING[i]; //Get OBD_ID
NarendraSingh 11:77e595130230 233 Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.Fuel_Level >> 8); //Get OBD_Battery Voltage
NarendraSingh 11:77e595130230 234 Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.Fuel_Level & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 11:77e595130230 235 Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage
NarendraSingh 11:77e595130230 236 Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 11:77e595130230 237 Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature
NarendraSingh 11:77e595130230 238 Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature
NarendraSingh 11:77e595130230 239 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.OBD_Battery_Temperature; //Get Battery Temperature
NarendraSingh 11:77e595130230 240 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Car_Ambient_Temperature; //Get Ambient Temperature
NarendraSingh 11:77e595130230 241 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 242 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 243 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 244 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 245 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 246 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 247 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 248 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 249 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 250 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 251 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 252 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 253 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 254 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 255 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 256 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 257 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 258 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 259 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 260 Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 261 Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes
NarendraSingh 11:77e595130230 262 CheckIN_Packet_Sent_Count++;
NarendraSingh 11:77e595130230 263 Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos);
NarendraSingh 11:77e595130230 264 }
NarendraSingh 11:77e595130230 265
NarendraSingh 11:77e595130230 266 //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 267 void Send_CheckIN_Packet(void)
NarendraSingh 11:77e595130230 268 {
NarendraSingh 11:77e595130230 269 //write code to read OBD data,temperature,beacon data
NarendraSingh 11:77e595130230 270 uint8 Pos = 0,i;
NarendraSingh 11:77e595130230 271 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 11:77e595130230 272 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Protocol_Version; //MSB of Motion Packet Type
NarendraSingh 11:77e595130230 273 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Packet_Type; //MSB of Motion Packet Type
NarendraSingh 11:77e595130230 274 for(i=0;i < 17;i++)
NarendraSingh 11:77e595130230 275 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.VIN[i]; //Get OBD_ID
NarendraSingh 11:77e595130230 276 for(i=0;i < 3;i++)
NarendraSingh 11:77e595130230 277 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.ODO_METER_READING[i]; //Get OBD_ID
NarendraSingh 11:77e595130230 278 Lora_Packet_To_Send[Pos++] = (CheckIN_Lora_Packet.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage
NarendraSingh 11:77e595130230 279 Lora_Packet_To_Send[Pos++] = (CheckIN_Lora_Packet.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage
NarendraSingh 11:77e595130230 280 Lora_Packet_To_Send[Pos++] = (CheckIN_Lora_Packet.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature
NarendraSingh 11:77e595130230 281 Lora_Packet_To_Send[Pos++] = (CheckIN_Lora_Packet.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature
NarendraSingh 11:77e595130230 282 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.OBD_Battery_Temperature; //Get Battery Temperature
NarendraSingh 11:77e595130230 283 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Car_Ambient_Temperature; //Get Ambient Temperature
NarendraSingh 11:77e595130230 284 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 285 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 286 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 287 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 288 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 289 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 290 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 291 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 292 Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 293 Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes
NarendraSingh 11:77e595130230 294 CheckIN_Packet_Sent_Count++;
NarendraSingh 11:77e595130230 295 Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos);
NarendraSingh 11:77e595130230 296 }
NarendraSingh 11:77e595130230 297
NarendraSingh 11:77e595130230 298 //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 299 // It should be sent every 30sec for 2minutes and afterthat it should stop sending
NarendraSingh 11:77e595130230 300 void Send_Motion_Packet(void)
NarendraSingh 11:77e595130230 301 {
NarendraSingh 11:77e595130230 302 //write code to read accelerometer data,temperature,beacon data
NarendraSingh 11:77e595130230 303 uint8 Pos = 0,i;
NarendraSingh 11:77e595130230 304 Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Header; //Header of Lora Packet,0xFE
NarendraSingh 11:77e595130230 305 Lora_Packet_To_Send[Pos++] = (Motion_Lora_Packet.Packet_Type >> 8); //MSB of Motion Packet Type
NarendraSingh 11:77e595130230 306 Lora_Packet_To_Send[Pos++] = (Motion_Lora_Packet.Packet_Type & 0xFF); //LSB of Motion Packet Type
NarendraSingh 11:77e595130230 307 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 308 Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 309 Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device
NarendraSingh 11:77e595130230 310 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 311 Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 312 Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device
NarendraSingh 11:77e595130230 313 for(i=0;i<6;i++)
NarendraSingh 11:77e595130230 314 Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 315 Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device
NarendraSingh 11:77e595130230 316 Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Acceleration_Type; //get Type of Acceleration
NarendraSingh 11:77e595130230 317 Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes
NarendraSingh 11:77e595130230 318 Motion_Packet_Sent_Count++;
NarendraSingh 11:77e595130230 319 Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos);
NarendraSingh 11:77e595130230 320 }
NarendraSingh 11:77e595130230 321
NarendraSingh 11:77e595130230 322
NarendraSingh 11:77e595130230 323 void Get_Lora_Response(void)
NarendraSingh 11:77e595130230 324 {
NarendraSingh 11:77e595130230 325 static uint16 Temp_Pos1;
NarendraSingh 11:77e595130230 326 static uint8 Lora_Response_Found = 0x00;
NarendraSingh 11:77e595130230 327 static uint8 Response_Start_Pos[5];
NarendraSingh 11:77e595130230 328 Lora_Response_Found = 0;
NarendraSingh 11:77e595130230 329 Temp_Pos1 = LORA_UART_RX_Crnt_Pos = 0;
NarendraSingh 11:77e595130230 330 while(Temp_Pos1 < Lora_RxBuffer_End_Pos)
NarendraSingh 11:77e595130230 331 { //check for end of AT response to calculate length
NarendraSingh 11:77e595130230 332 if((LORA_UART_RX_Buffer[Temp_Pos1] != 0x0D) || (LORA_UART_RX_Buffer[Temp_Pos1+1] != 0x0A))
NarendraSingh 11:77e595130230 333 { //check for AT end response <cr><lf> (i.e. 0x0D,0x0A)
NarendraSingh 11:77e595130230 334 Temp_Pos1++;
NarendraSingh 11:77e595130230 335 }
NarendraSingh 11:77e595130230 336 else
NarendraSingh 11:77e595130230 337 {
NarendraSingh 11:77e595130230 338 Temp_Pos1+=2;
NarendraSingh 11:77e595130230 339 Response_Start_Pos[Lora_Response_Found++] = Temp_Pos1;
NarendraSingh 11:77e595130230 340 //DEBUGING_UART.printf("%c",Temp_Pos1);
NarendraSingh 11:77e595130230 341 if(Lora_Response_Found >= 4)
NarendraSingh 11:77e595130230 342 break;
NarendraSingh 11:77e595130230 343 }
NarendraSingh 11:77e595130230 344 }
NarendraSingh 11:77e595130230 345 if(Lora_Response_Found >= 3)
NarendraSingh 11:77e595130230 346 {
NarendraSingh 11:77e595130230 347 if((Response_Start_Pos[1] - Response_Start_Pos[0]) > 0x02) //Response received without data
NarendraSingh 11:77e595130230 348 {
NarendraSingh 11:77e595130230 349 LORA_UART_RX_Crnt_Pos = (Response_Start_Pos[0]);
NarendraSingh 11:77e595130230 350 Response_Start_Pos[1]-=2;
NarendraSingh 11:77e595130230 351 for(Temp_Pos1=0; LORA_UART_RX_Crnt_Pos < Response_Start_Pos[1]; Temp_Pos1++)
NarendraSingh 11:77e595130230 352 {
NarendraSingh 11:77e595130230 353 Lora_Command_Rcvd[Temp_Pos1] = LORA_UART_RX_Buffer[LORA_UART_RX_Crnt_Pos++];
NarendraSingh 11:77e595130230 354 DEBUGING_UART.putc(Lora_Command_Rcvd[Temp_Pos1]);
NarendraSingh 11:77e595130230 355 }
NarendraSingh 11:77e595130230 356 Lora_RxBuffer_End_Pos = 0;
NarendraSingh 11:77e595130230 357 AT_Response_Receive_Status = SUCCESS;
NarendraSingh 11:77e595130230 358 }
NarendraSingh 11:77e595130230 359 else if((Response_Start_Pos[1] - Response_Start_Pos[0]) == 0x02) //Response received along with data
NarendraSingh 11:77e595130230 360 {
NarendraSingh 11:77e595130230 361 LORA_UART_RX_Crnt_Pos = (Response_Start_Pos[1]);
NarendraSingh 11:77e595130230 362 Response_Start_Pos[2]-=2;
NarendraSingh 11:77e595130230 363 for(Temp_Pos1=0; LORA_UART_RX_Crnt_Pos < Response_Start_Pos[2]; Temp_Pos1++)
NarendraSingh 11:77e595130230 364 {
NarendraSingh 11:77e595130230 365 Lora_Command_Rcvd[Temp_Pos1] = LORA_UART_RX_Buffer[LORA_UART_RX_Crnt_Pos++];
NarendraSingh 11:77e595130230 366 DEBUGING_UART.putc(Lora_Command_Rcvd[Temp_Pos1]);
NarendraSingh 11:77e595130230 367 }
NarendraSingh 11:77e595130230 368 Lora_RxBuffer_End_Pos = 0;
NarendraSingh 11:77e595130230 369 AT_Response_Receive_Status = SUCCESS;
NarendraSingh 11:77e595130230 370 }
NarendraSingh 11:77e595130230 371 }
NarendraSingh 11:77e595130230 372 }
NarendraSingh 11:77e595130230 373
NarendraSingh 11:77e595130230 374 //Function to send general Lora packets using "AT+SEND" Command
NarendraSingh 11:77e595130230 375 void Send_Lora_Packet_To_Gateway(uint8* Command_To_Send,uint8 Length)
NarendraSingh 11:77e595130230 376 {
NarendraSingh 11:77e595130230 377 uint8 i=0;
NarendraSingh 11:77e595130230 378 LORA_Module_UART.printf("%s",Send_Lora_Packet);//write to serial port for sending through lora module
NarendraSingh 11:77e595130230 379 for(i=0;i<Length;i++)
NarendraSingh 11:77e595130230 380 LORA_Module_UART.putc(Command_To_Send[i]);//write to serial port for sending through lora module
NarendraSingh 11:77e595130230 381 LORA_Module_UART.printf("\r");
NarendraSingh 11:77e595130230 382 }
NarendraSingh 11:77e595130230 383
NarendraSingh 11:77e595130230 384
NarendraSingh 11:77e595130230 385 unsigned char Calculate_Lora_Frame_FCS(unsigned char* Packet_Data,unsigned char Packet_Length)
NarendraSingh 11:77e595130230 386 {
NarendraSingh 11:77e595130230 387 uint8 i,FCS = 0x00;
NarendraSingh 11:77e595130230 388 for(i=0; i < Packet_Length; i++)
NarendraSingh 11:77e595130230 389 {
NarendraSingh 11:77e595130230 390 FCS ^= (Packet_Data[i]);
NarendraSingh 11:77e595130230 391 }
NarendraSingh 11:77e595130230 392 return(FCS);
NarendraSingh 11:77e595130230 393 }