kumar singh
/
Dealer_20Mar
BLE Transmitter not working
Fork of Dealer_23Feb by
Diff: Lora.cpp
- Revision:
- 11:77e595130230
- Child:
- 13:8955f2e95021
diff -r dc33cd3f4eb9 -r 77e595130230 Lora.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lora.cpp Fri Jan 27 18:30:02 2017 +0000 @@ -0,0 +1,393 @@ + #include "Lora.h" + #include "OBD.h" + #include "Common_Defs.h" + #include "Beacon.h" + + //Configure Lora Packet + RawSerial LORA_Module_UART(PA_0, PA_1);//USART4_TX->PA_0,USART4_RX->PA_1 + RawSerial DEBUGING_UART(PA_9, PA_10);//USART1_TX->PA_9,USART1_RX->PA_10 + + uint8 Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //By Default Heart Beat PAckets should be sent + uint8 OBD_Protocol_Version = 0x01; + uint8 Vehicle_Identification_Number[17]; //Unique Vehicle_Identification_Number, Read using OBD + uint8 Motion_Packet_Sent_Count=0; + uint8 CheckIN_Packet_Sent_Count=0; + uint8 Lora_Packet_To_Send[100]; + uint8 Send_Lora_Packet_Flag = FALSE; + int Lora_RxBuffer_Crnt_Pos,Lora_RxBuffer_End_Pos; // must be volatile or the compiler may over-optimise. + int receivedDataCount = 0; + char LORA_UART_RX_Buffer[LORA_UART_RX_Size]; + int LORA_UART_RX_Crnt_Pos; + uint8 Lora_Command_Rcvd[100]; + uint8 Lora_Cmd_Length = 0; + uint8 AT_Response_Receive_Status = FAILURE; + + //Create Object for Type of Lora Packet to send + Heart_Beat_PacketType Heart_Beat_Lora_Packet; //Allocate Memory for HeartBeat Lora Packets + CheckIN_PacketType CheckIN_Lora_Packet; //Allocate Memory for CheckIN Lora Packets + Motion_PacketType Motion_Lora_Packet; //Allocate Memory for Movement Lora Packets + Vehicle_Status_PacketType Vehicle_Status_Lora_Packet; //Allocate Memory for Movement Lora Packets + + //Lora AT Commands list + const char* Attention = {"AT\r"}; + const char* Reset_Device = "ATZ\r"; + const char* Reset_to_Factory_Defaults = "AT&F\r"; + const char* Save_Configuration = "AT&W\r"; + const char* Serial_Baud_Rate = "AT+IPR="; + const char* Join_Network = "AT+JOIN\r"; + const char* Join_Retries = "AT+JR="; + const char* AES_Encryption = "AT+ENC="; + const char* Send_Lora_Packet = "AT+SEND "; + const char* Set_Frequency_Sub_Band = "AT+FSB="; + const char* Set_Network_ID = "AT+NI="; + const char* Set_Network_Key = "AT+NK="; + const char* Network_Key = "010203123"; + const char* Network_ID = "010203040"; + const char* Network_Join_Retries = "AT+JR="; + const char* Network_Join_Status = "AT+NJS="; + const char* Lora_Device_ID = "AT+DI\r"; + + + + uint8 Calculate_Lora_Frame_FCS(uint8* Packet_Data,uint8 Packet_Length); + void Set_Up_Lora_Network_Configuration(void); + void Get_Lora_Response(void); + +//Set Up lora network +void Set_Up_Lora_Network_Configuration(void) +{ + LORA_Module_UART.baud(115200); + LORA_Module_UART.printf(Attention);//Send Attention command + AT_Response_Receive_Status = FAILURE; + DEBUGING_UART.printf("Nwk set up started"); + while(AT_Response_Receive_Status) + Get_Lora_Response(); + DEBUGING_UART.printf("AT Response received"); + LORA_Module_UART.printf("%s%d\r",Set_Frequency_Sub_Band,FREQUENCY_SUB_BAND_CHANNEL7);//set frequency sub band to 7 + AT_Response_Receive_Status = FAILURE; + while(AT_Response_Receive_Status) + Get_Lora_Response(); + DEBUGING_UART.printf("Frequency band response received"); + LORA_Module_UART.printf("%s%d,%s\r",Set_Network_Key,STRING_PARAMETER,Network_Key); //set network key + AT_Response_Receive_Status = FAILURE; + while(AT_Response_Receive_Status) + Get_Lora_Response(); + DEBUGING_UART.printf("Network key Response received"); + LORA_Module_UART.printf("%s%d,%s\r",Set_Network_ID,STRING_PARAMETER,Network_ID); //set network id + AT_Response_Receive_Status = FAILURE; + while(AT_Response_Receive_Status) + Get_Lora_Response(); + DEBUGING_UART.printf("Network Id response received"); + + //LORA_Module_UART.printf("%s",Reset_Device); //reset device + //wait_ms(3500); + + LORA_Module_UART.printf("AT+TXDR=DR2\r"); + AT_Response_Receive_Status = FAILURE; + while(AT_Response_Receive_Status) + Get_Lora_Response(); + LORA_Module_UART.printf("AT+ADR=1\r"); //Enable adaptive data rate + AT_Response_Receive_Status = FAILURE; + while(AT_Response_Receive_Status) + Get_Lora_Response(); + LORA_Module_UART.printf("%s",Save_Configuration); //save configuration + AT_Response_Receive_Status = FAILURE; + while(AT_Response_Receive_Status) + Get_Lora_Response(); + DEBUGING_UART.printf("Configuration saved"); + LORA_Module_UART.printf(Attention); //Send Attention command + AT_Response_Receive_Status = FAILURE; + while(AT_Response_Receive_Status) + Get_Lora_Response(); + DEBUGING_UART.printf("AT Response received"); + LORA_Module_UART.printf("%s",Join_Network); //join network with gateway + AT_Response_Receive_Status = FAILURE; + while(AT_Response_Receive_Status) + Get_Lora_Response(); + DEBUGING_UART.printf("Join Response received"); +} + + void Initialize_lora_Packets() +{ + uint8 i; + /******* Initialize Lora packet for HeartBeat *****/ + Heart_Beat_Lora_Packet.Header = LORA_PACKET_HEADER; + Heart_Beat_Lora_Packet.Protocol_Version = OBD_Protocol_Version; + Heart_Beat_Lora_Packet.Packet_Type = HEART_BEAT_PACKET_CMD; + Heart_Beat_Lora_Packet.OBD_Battery_Voltage = 0x1234; + Heart_Beat_Lora_Packet.Car_Battery_Voltage = 0x5241; + Heart_Beat_Lora_Packet.OBD_Battery_Temperature = 0x00; + Heart_Beat_Lora_Packet.Car_Ambient_Temperature = 0x00; + for(i=0;i<6;i++) + Heart_Beat_Lora_Packet.Parking1_Beacon_ID[i] = 0x78; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Heart_Beat_Lora_Packet.Parking1_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + Heart_Beat_Lora_Packet.Parking2_Beacon_ID[i] = 0x12; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Heart_Beat_Lora_Packet.Parking2_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + Heart_Beat_Lora_Packet.Parking3_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Heart_Beat_Lora_Packet.Parking3_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + Heart_Beat_Lora_Packet.FCS = 0x00; //FCS of all packets + + /******* Initialize Lora packet for Vehicle Status *****/ + Vehicle_Status_Lora_Packet.Header = LORA_PACKET_HEADER; + Vehicle_Status_Lora_Packet.Protocol_Version = OBD_Protocol_Version; + Vehicle_Status_Lora_Packet.Packet_Type = CHECKIN_PACKET_CMD; + for(i=0;i<17;i++) + Vehicle_Status_Lora_Packet.VIN[i] = Vehicle_Identification_Number[i]; + for(i=0;i<3;i++) + Vehicle_Status_Lora_Packet.ODO_METER_READING[i] = 0x00; //Dummyy data, To be read using OBD + Vehicle_Status_Lora_Packet.OBD_Battery_Voltage = 0x1234; + Vehicle_Status_Lora_Packet.Car_Battery_Voltage = 0x5241; + Vehicle_Status_Lora_Packet.OBD_Battery_Temperature = 0x00; + Vehicle_Status_Lora_Packet.Car_Ambient_Temperature = 0x00; + for(i=0;i<6;i++) + 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 + for(i=0;i<6;i++) + Vehicle_Status_Lora_Packet.Parking1_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Vehicle_Status_Lora_Packet.Parking1_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + Vehicle_Status_Lora_Packet.Parking2_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Vehicle_Status_Lora_Packet.Parking2_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + Vehicle_Status_Lora_Packet.Parking3_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Vehicle_Status_Lora_Packet.Parking3_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + Vehicle_Status_Lora_Packet.FCS = 0x00; //FCS of all packets + + /******* Initialize Lora packet for CheckIn *****/ + CheckIN_Lora_Packet.Header = LORA_PACKET_HEADER; + CheckIN_Lora_Packet.Protocol_Version = OBD_Protocol_Version; + CheckIN_Lora_Packet.Packet_Type = CHECKIN_PACKET_CMD; + for(i=0;i<17;i++) + CheckIN_Lora_Packet.VIN[i] = Vehicle_Identification_Number[i]; + for(i=0;i<3;i++) + CheckIN_Lora_Packet.ODO_METER_READING[i] = 0x00; //Dummyy data, To be read using OBD + for(i=0;i<6;i++) + CheckIN_Lora_Packet.Parking1_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + CheckIN_Lora_Packet.Parking1_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + CheckIN_Lora_Packet.Parking2_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + CheckIN_Lora_Packet.Parking2_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + CheckIN_Lora_Packet.Parking3_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + CheckIN_Lora_Packet.Parking3_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + CheckIN_Lora_Packet.FCS = 0x00; //FCS of all packets + + /******* Initialize Lora packet for Movement *****/ + Motion_Lora_Packet.Header = LORA_PACKET_HEADER; + Motion_Lora_Packet.Protocol_Version = OBD_Protocol_Version; + Motion_Lora_Packet.Packet_Type = MOTION_PACKET_CMD; + for(i=0;i<6;i++) + Motion_Lora_Packet.Parking1_Beacon_ID[i] = 0x00; //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Motion_Lora_Packet.Parking1_Beacon_Signal_Strength = 0x00; //Signal Strength of 1st NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + Motion_Lora_Packet.Parking2_Beacon_ID[i] = 0x00; //MAC ID of 2nd NearBy Beacon Device with Highest Signal Strength,dummy data + Motion_Lora_Packet.Parking2_Beacon_Signal_Strength = 0x00; //Signal Strength of 2nd NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + Motion_Lora_Packet.Parking3_Beacon_ID[i] = 0x00; //MAC ID of 3rd NearBy Beacon Device with Highest Signal Strength,dummy data + Motion_Lora_Packet.Parking3_Beacon_Signal_Strength = 0x00; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + Motion_Lora_Packet.Acceleration_Type = 0x00; //Type of acceleration, Vehicle Started/Vehicle Stopped/Sudden Vehicle Movement + Motion_Lora_Packet.FCS = 0x00; //FCS of all packets +} + + //HeartBeat Packet should be sent every 30sec +void Send_HeartBeat_Packet(void) +{ + //write code to read obd data,temperature,beacon data + uint8 Pos = 0,i; + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Header; //Header of Lora Packet,0xFE + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Protocol_Version; + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Packet_Type; //MSB of Motion Packet Type + Lora_Packet_To_Send[Pos++] = (Heart_Beat_Lora_Packet.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Heart_Beat_Lora_Packet.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Heart_Beat_Lora_Packet.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = (Heart_Beat_Lora_Packet.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.OBD_Battery_Temperature; //Get Battery Temperature + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Car_Ambient_Temperature; //Get Ambient Temperature + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; + Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos); +} + +//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 +void Send_Vehicle_Status_Packet(void) +{ + //write code to read OBD data,temperature,beacon data + uint8 Pos = 0,i; + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Header; //Header of Lora Packet,0xFE + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Protocol_Version; //MSB of Motion Packet Type + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Packet_Type; //MSB of Motion Packet Type + for(i=0;i < 17;i++) + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.VIN[i]; //Get OBD_ID + for(i=0;i < 3;i++) + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.ODO_METER_READING[i]; //Get OBD_ID + Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.Fuel_Level >> 8); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.Fuel_Level & 0xFF); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = (Vehicle_Status_Lora_Packet.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.OBD_Battery_Temperature; //Get Battery Temperature + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Car_Ambient_Temperature; //Get Ambient Temperature + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.BLE_Adv_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Near_Car3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + CheckIN_Packet_Sent_Count++; + Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos); +} + +//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 +void Send_CheckIN_Packet(void) +{ + //write code to read OBD data,temperature,beacon data + uint8 Pos = 0,i; + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Header; //Header of Lora Packet,0xFE + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Protocol_Version; //MSB of Motion Packet Type + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Packet_Type; //MSB of Motion Packet Type + for(i=0;i < 17;i++) + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.VIN[i]; //Get OBD_ID + for(i=0;i < 3;i++) + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.ODO_METER_READING[i]; //Get OBD_ID + Lora_Packet_To_Send[Pos++] = (CheckIN_Lora_Packet.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (CheckIN_Lora_Packet.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (CheckIN_Lora_Packet.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = (CheckIN_Lora_Packet.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.OBD_Battery_Temperature; //Get Battery Temperature + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Car_Ambient_Temperature; //Get Ambient Temperature + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + CheckIN_Packet_Sent_Count++; + Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos); +} + +//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. +// It should be sent every 30sec for 2minutes and afterthat it should stop sending +void Send_Motion_Packet(void) +{ + //write code to read accelerometer data,temperature,beacon data + uint8 Pos = 0,i; + Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Header; //Header of Lora Packet,0xFE + Lora_Packet_To_Send[Pos++] = (Motion_Lora_Packet.Packet_Type >> 8); //MSB of Motion Packet Type + Lora_Packet_To_Send[Pos++] = (Motion_Lora_Packet.Packet_Type & 0xFF); //LSB of Motion Packet Type + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Acceleration_Type; //get Type of Acceleration + Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Motion_Packet_Sent_Count++; + Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos); +} + + +void Get_Lora_Response(void) +{ + static uint16 Temp_Pos1; + static uint8 Lora_Response_Found = 0x00; + static uint8 Response_Start_Pos[5]; + Lora_Response_Found = 0; + Temp_Pos1 = LORA_UART_RX_Crnt_Pos = 0; + while(Temp_Pos1 < Lora_RxBuffer_End_Pos) + { //check for end of AT response to calculate length + if((LORA_UART_RX_Buffer[Temp_Pos1] != 0x0D) || (LORA_UART_RX_Buffer[Temp_Pos1+1] != 0x0A)) + { //check for AT end response <cr><lf> (i.e. 0x0D,0x0A) + Temp_Pos1++; + } + else + { + Temp_Pos1+=2; + Response_Start_Pos[Lora_Response_Found++] = Temp_Pos1; + //DEBUGING_UART.printf("%c",Temp_Pos1); + if(Lora_Response_Found >= 4) + break; + } + } + if(Lora_Response_Found >= 3) + { + if((Response_Start_Pos[1] - Response_Start_Pos[0]) > 0x02) //Response received without data + { + LORA_UART_RX_Crnt_Pos = (Response_Start_Pos[0]); + Response_Start_Pos[1]-=2; + for(Temp_Pos1=0; LORA_UART_RX_Crnt_Pos < Response_Start_Pos[1]; Temp_Pos1++) + { + Lora_Command_Rcvd[Temp_Pos1] = LORA_UART_RX_Buffer[LORA_UART_RX_Crnt_Pos++]; + DEBUGING_UART.putc(Lora_Command_Rcvd[Temp_Pos1]); + } + Lora_RxBuffer_End_Pos = 0; + AT_Response_Receive_Status = SUCCESS; + } + else if((Response_Start_Pos[1] - Response_Start_Pos[0]) == 0x02) //Response received along with data + { + LORA_UART_RX_Crnt_Pos = (Response_Start_Pos[1]); + Response_Start_Pos[2]-=2; + for(Temp_Pos1=0; LORA_UART_RX_Crnt_Pos < Response_Start_Pos[2]; Temp_Pos1++) + { + Lora_Command_Rcvd[Temp_Pos1] = LORA_UART_RX_Buffer[LORA_UART_RX_Crnt_Pos++]; + DEBUGING_UART.putc(Lora_Command_Rcvd[Temp_Pos1]); + } + Lora_RxBuffer_End_Pos = 0; + AT_Response_Receive_Status = SUCCESS; + } + } +} + +//Function to send general Lora packets using "AT+SEND" Command +void Send_Lora_Packet_To_Gateway(uint8* Command_To_Send,uint8 Length) +{ + uint8 i=0; + LORA_Module_UART.printf("%s",Send_Lora_Packet);//write to serial port for sending through lora module + for(i=0;i<Length;i++) + LORA_Module_UART.putc(Command_To_Send[i]);//write to serial port for sending through lora module + LORA_Module_UART.printf("\r"); +} + + + unsigned char Calculate_Lora_Frame_FCS(unsigned char* Packet_Data,unsigned char Packet_Length) + { + uint8 i,FCS = 0x00; + for(i=0; i < Packet_Length; i++) + { + FCS ^= (Packet_Data[i]); + } + return(FCS); + }