Threading Implemented.Memory Size Problem09_Mar2017
Fork of rtos_queue by
Revision 7:22c4d2681cc9, committed 2017-03-09
- Comitter:
- NarendraSingh
- Date:
- Thu Mar 09 15:00:37 2017 +0000
- Parent:
- 6:0cb43a362538
- Commit message:
- Threading Implemented(Memory Size Problem
Changed in this revision
diff -r 0cb43a362538 -r 22c4d2681cc9 Beacon.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Beacon.cpp Thu Mar 09 15:00:37 2017 +0000 @@ -0,0 +1,231 @@ +#include "Beacon.h" +#include "Lora.h" + + RawSerial DEBUG_UART2(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 + RawSerial Beacon_Module_UART(PC_4, PC_5);//USART3_TX->PC4,USART3_RX->PC_5 + + + uint8 Beacon_RxBuffer_Crnt_Pos,Beacon_RxBuffer_End_Pos; // must be volatile or the compiler may over-optimise. + uint8 Beacon_RX_Buffer[50]; + char Beacon_RxIndex=0; + uint8 BLE_Adv_Module_Beacon_ID[6]; + +/*Instructions for operation(MS49SF1U) +1. UART command list(Commands for modifying configuration) +|-------------------|-----------------------------|---------------|---------------------------------------------------| +| Item | Command format | Length(data) | Returned value after successful receiving | +|-------------------|-----------------------------|---------------|---------------------------------------------------| +UUID 0x4154+0xFFF1+data+0x0d 16character 0xFFF1+received data +Major 0x4154+0xFFF2+data+0x0d 2character 0xFFF2+received data +Minor 0x4154+0xFFF3+data+0x0d 2character 0xFFF3+received data +Measured power 0x4154+0xFFF4+data+0x0d 1character 0xFFF4+received data +Transmission power 0x4154+0xFFF5+data+0x0d 1character 0xFFF5+received data +Pairing Password 0x4154+0xFFF6+data+0x0d 8character 0xFFF6+received data +Broadcasting Interval 0x4154+0xFFF7+data+0x0d 1character 0xFFF7+received data +Beacon_Serial_ID 0x4154+0xFFF8+data+0x0d 4character 0xFFF8+received data +iBeacon_name 0x4154+0xFFF9+data+0x0d 12character 0xFFF9+received data +Authentication 0x4154+0xFFFA+data+0x0d 12character 0xFFFA+received data +Connection Mode 0x4154+0xFFFE+data+0x0d 1character 0xFFFE+received data +Soft Reboot 0x4154+0xFFFF+data+0x0d 8character 0xFFFF+received data+0K or ERROR +open beacon 0x4154+0x6F70656E+0x0d return Beacon is open\n +close beacon 0x4154+0x636C6F7365+0x0d return Beacon is closed\n +app enable 0x4154+0x656E61626C65+0x0d return app enabled +app unable 0x4154+0x756E61626C65+0x0d +Read MAC ID 0x5244+0x6d6163+0x0d +------------------------------------------------------------------------------------------------------------------- +*/ + +unsigned char SET_UUID[21] = {0x41,0x54,0xFF,0xF1,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D}; +unsigned char SET_VIRTUAL_PACKET_UUID[23] = {0x01,0x01,0x41,0x54,0x01,0x01,0x02,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x03,0x0B,0x0C,0x05,0x0E,0x0F,0x0D};// +//unsigned char SET_VIRTUAL_PACKET_UUID1[23] = {0x41,0x54,0xFF,0xF1,0x02,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x02,0x0B,0x0C,0x05,0x0E,0x0F,0x0D}; +//unsigned char SET_VIRTUAL_PACKET_UUID2[23] = {0x12,0x21,0x41,0x54,0xFF,0xF1,0x0F,0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00,0x0D}; +//unsigned char SET_VIRTUAL_PACKET_UUID3[23] = {0x41,0x54,0xFF,0xF1,0x0F,0x0E,0x0D,0x0C,0x0B,0x04,0x05,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00,0x0D}; +unsigned char SET_MAJOR[7] = {0x41,0x54,0xFF,0xF2,0x00,0x00,0x0D}; +unsigned char SET_MINOR[7] = {0x41,0x54,0xFF,0xF3,0x00,0x00,0x0D}; +unsigned char SET_MEASURED_POWER[6] = {0x41,0x54,0xFF,0xF4,0x00,0x0D}; +unsigned char SET_TRANSMISSION_POWER[6] = {0x41,0x54,0xFF,0xF5,0x00,0x0D}; +unsigned char SET_PAIRING_PASSWORD[13] = {0x41,0x54,0xFF,0xF6,0x4F,0x42,0x44,0x21,0x30,0x30,0x30,0x31,0x0D};//Set Password here, Now set as OBD!1234 +unsigned char SET_BROADCAST_INTERVAL[6] = {0x41,0x54,0xFF,0xF7,0x00,0x0D}; +unsigned char SET_IBEACON_NAME[17] = {0x41,0x54,0xFF,0xF9,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D}; +unsigned char SOFT_REBOOT[13] = {0x41,0x54,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D}; +unsigned char SET_OPEN_BEACON[7] = {0x41,0x54,0x6F,0x70,0x65,0x6E,0x0D}; +unsigned char SET_CLOSE_BEACON[8] = {0x41,0x54,0x63,0x6C,0x6F,0x73,0x65,0x0D}; +unsigned char READ_BEACON_MAC_ID[6] = {0x52,0x44,0x6D,0x61,0x63,0x0D}; + +unsigned char SOFT_REBOOT1[15] = {0x01,0x0B,0x41,0x54,0xFF,0xFF,0x6D,0x69,0x6E,0x65,0x77,0x31,0x32,0x33,0x0D}; +unsigned char SOFT_REBOOT2[15] = {0x41,0x54,0xFF,0xFF,0x6D,0x69,0x6E,0x65,0x77,0x31,0x32,0x33,0x0D}; + +void Process_Beacon_Command_Received(unsigned char* Command_Received); +void Change_Beacon_Parameter(unsigned char* Beacon_Parameter_To_Set); +void Get_Beacon_Response(void); +void Read_Beacon_Module_MAC_ID(void); +void Initialize_Beacon_Module(void); + +void Process_Beacon_Command_Received(unsigned char* Command_Received) +{ + static unsigned char Temp_Pos,Start_Position,End_Position,Data_Length; + Start_Position = 0x02; + Temp_Pos = 0x02; + if((Command_Received[0] == SET_BEACON_UUID_CMD0) && (Command_Received[1] == SET_BEACON_UUID_CMD1)) //Check if command is receievd for setting UUID + { + //DEBUG_UART2.printf("Setting UUID"); + Data_Length = 16; + End_Position = (Start_Position + Data_Length); + for(;Start_Position < End_Position;Start_Position++) + { + SET_UUID[Start_Position] = Command_Received[Temp_Pos++]; + //DEBUG_UART2.putc(SET_UUID[Start_Position]); + //DEBUG_UART2.printf("%s",SET_UUID); + + + } + DEBUG_UART2.printf("UUID Set"); + //Change_Beacon_Parameter(SET_VIRTUAL_PACKET_UUID1); + } + else if((Command_Received[0] == 0x12) && (Command_Received[1] == 0x21)) //Check if command is receievd for setting UUID + { + DEBUG_UART2.printf("Setting UUID2"); + Data_Length = 16; + End_Position = (Start_Position + Data_Length); + for(;Start_Position < End_Position;Start_Position++) + { + SET_UUID[Start_Position] = Command_Received[Temp_Pos++]; + //DEBUG_UART2.putc(SET_UUID[Start_Position]); + //DEBUG_UART2.printf("%s",SET_UUID); + + + } + DEBUG_UART2.printf("UUID2 Set"); + //Change_Beacon_Parameter(SET_VIRTUAL_PACKET_UUID3); + } + else if((Command_Received[0] == SET_SET_MAJOR_CMD0) && (Command_Received[1] == SET_SET_MAJOR_CMD1)) //Check if command is received for Setting Major + { + SET_MAJOR[Start_Position++] = Command_Received[Temp_Pos++]; //MSB + SET_MAJOR[Start_Position] = Command_Received[Temp_Pos]; //LSB + DEBUG_UART2.printf("%s",SET_MAJOR); + Change_Beacon_Parameter(SET_MAJOR); + } + else if((Command_Received[0] == SET_SET_MINOR_CMD0) && (Command_Received[1] == SET_SET_MINOR_CMD1)) //Check if command is received for Setting Minor + { + SET_MINOR[Start_Position++] = Command_Received[Temp_Pos++]; //MSB + SET_MINOR[Start_Position] = Command_Received[Temp_Pos]; //LSB + DEBUG_UART2.printf("%s",SET_MINOR); + Change_Beacon_Parameter(SET_MINOR); + } + else if((Command_Received[0] == SET_SET_MEASURED_POWER_CMD0) && (Command_Received[1] == SET_SET_MEASURED_POWER_CMD1)) //Check if command is received for Setting Measured power + { + SET_MEASURED_POWER[Start_Position] = Command_Received[Temp_Pos]; + DEBUG_UART2.printf("%s",SET_MEASURED_POWER); + Change_Beacon_Parameter(SET_MEASURED_POWER); + } + else if((Command_Received[0] == SET_TRANSMISSION_POWER_CMD0) && (Command_Received[1] == SET_TRANSMISSION_POWER_CMD1)) //Check if command is received for settting Transmission power + { + SET_TRANSMISSION_POWER[Start_Position] = Command_Received[Temp_Pos]; + DEBUG_UART2.printf("%s",SET_TRANSMISSION_POWER); + Change_Beacon_Parameter(SET_TRANSMISSION_POWER); + } + else if((Command_Received[0] == SET_PAIRING_PASSWORD_CMD0) && (Command_Received[1] == SET_PAIRING_PASSWORD_CMD1)) //Check if command is received for Changing Pairing Password + { + Data_Length = 8; + End_Position = (Start_Position + Data_Length); + for(;Start_Position < End_Position;Start_Position++) + SET_PAIRING_PASSWORD[Start_Position] = Command_Received[Temp_Pos++]; + DEBUG_UART2.printf("%s",SET_PAIRING_PASSWORD); + //Change_Beacon_Parameter(SET_PAIRING_PASSWORD); + Data_Length = 8; + Start_Position = 4; + End_Position = (Start_Position + Data_Length); + //for(Start_Position;Start_Position < End_Position;Start_Position++) + // SET_SOFT_REBOOT[Start_Position] = Command_Received[Temp_Pos++]; //write code to update this password in eeprom + DEBUG_UART2.printf("%s",SOFT_REBOOT); + } + else if((Command_Received[0] == SET_BROADCAST_INTERVAL_CMD0) && (Command_Received[1] == SET_BROADCAST_INTERVAL_CMD1)) //Check if command is received for setting BroadCast Interval + { + SET_BROADCAST_INTERVAL[Start_Position] = Command_Received[Temp_Pos]; + DEBUG_UART2.printf("%s",SET_BROADCAST_INTERVAL); + Change_Beacon_Parameter(SET_BROADCAST_INTERVAL); + } + else if((Command_Received[0] == SET_IBEACON_NAME_CMD0) && (Command_Received[1] == SET_IBEACON_NAME_CMD1)) //Check if command is received for setting ibeacon name + { + Data_Length = 12; + End_Position = (Start_Position + Data_Length); + for(;Start_Position < End_Position;Start_Position++) + SET_IBEACON_NAME[Start_Position] = Command_Received[Temp_Pos++]; + DEBUG_UART2.printf("%s",SET_IBEACON_NAME); + Change_Beacon_Parameter(SET_IBEACON_NAME); + } + else if((Command_Received[0] == SET_START_BEACON_CMD0) && (Command_Received[1] == SET_START_BEACON_CMD0)) //check if command is receievd for starting Beacon + { + DEBUG_UART2.printf("%s",SET_OPEN_BEACON); + Change_Beacon_Parameter(SET_OPEN_BEACON); + } + else if((Command_Received[0] == SET_STOP_BEACON_CMD0) && (Command_Received[1] == SET_STOP_BEACON_CMD1)) //Check if command is received for Stopping beacon and go to sleep mode + { + DEBUG_UART2.printf("%s",SET_CLOSE_BEACON); + Change_Beacon_Parameter(SET_CLOSE_BEACON); + } + else if((Command_Received[0] == SET_SOFT_REBOOT_CMD0) && (Command_Received[1] == SET_SOFT_REBOOT_CMD1)) //Check if command is received for Stopping beacon and go to sleep mode + { + Beacon_Module_UART.printf("%s",SOFT_REBOOT2); //Soft Reboot Beacon Module + DEBUG_UART2.printf("Beacon Device Resetted %s",SOFT_REBOOT2); + } +} + +void Change_Beacon_Parameter(unsigned char* Beacon_Parameter_To_Set) +{ + uint8 i; + for(i = 0;i<21;i++) + { + Beacon_Module_UART.putc(Beacon_Parameter_To_Set[i]); + DEBUG_UART2.putc(Beacon_Parameter_To_Set[i]); + } + //Beacon_Module_UART.printf('%x',0x0d); + //DEBUG_UART2.putc('%x',0x0d); + Beacon_Module_UART.printf("%s",Beacon_Parameter_To_Set); + //Beacon_Module_UART.printf("%s",SOFT_REBOOT); //Every Ibeacon command must be followed by Soft reset command in order to make the changes take place instantly + //DEBUG_UART2.printf("%s",Beacon_Parameter_To_Set); +} + +void Initialize_Beacon_Module(void) +{ + Beacon_Module_UART.baud(9600); //set Beacon transmitter uart baud rate to default 9600 + //Read_Beacon_Module_MAC_ID(); +} + +void Read_Beacon_Module_MAC_ID(void) +{ + uint8 pos; + Beacon_Module_UART.printf("%s",READ_BEACON_MAC_ID); + Get_Beacon_Response(); + for(pos=0;pos<6;pos++) + BLE_Adv_Module_Beacon_ID[pos] = Beacon_RX_Buffer[pos]; +} + +void Get_Beacon_Response(void) +{ + char Beacon_inChar=0; + //Keep reading characters until we get a carriage return + while(Beacon_inChar != '\n') + { + //If a character comes in on the serial port, we need to act on it. + if(Beacon_Module_UART.readable()) + { + //Get the new character from the Serial port. + Beacon_inChar = Beacon_Module_UART.getc(); + //Start by checking if we've received the end of message character ('\r'). + if(Beacon_inChar == '\n') + { + //Put the end of string character on our data string + Beacon_RX_Buffer[Beacon_RxIndex]='\0'; + //Reset the buffer index so that the next character go es back at the beginning of the string. + Beacon_RxIndex=0; + } + //If we didn't get the end of message character, just add the new character to the string. + else + { + //Add the new character to the string, and increment the index variable. + Beacon_RX_Buffer[Beacon_RxIndex++]=Beacon_inChar; + } + } + } +}
diff -r 0cb43a362538 -r 22c4d2681cc9 Beacon.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Beacon.h Thu Mar 09 15:00:37 2017 +0000 @@ -0,0 +1,50 @@ +#include "mbed.h" +#include "Common_Defs.h" + +//Macro Definitions +#define SET_BEACON_UUID_CMD0 0x01 +#define SET_BEACON_UUID_CMD1 0x01 +#define SET_SET_MAJOR_CMD0 0x01 +#define SET_SET_MAJOR_CMD1 0x02 +#define SET_SET_MINOR_CMD0 0x01 +#define SET_SET_MINOR_CMD1 0x03 +#define SET_SET_MEASURED_POWER_CMD0 0x01 +#define SET_SET_MEASURED_POWER_CMD1 0x04 +#define SET_TRANSMISSION_POWER_CMD0 0x01 +#define SET_TRANSMISSION_POWER_CMD1 0x05 +#define SET_PAIRING_PASSWORD_CMD0 0x01 +#define SET_PAIRING_PASSWORD_CMD1 0x06 +#define SET_BROADCAST_INTERVAL_CMD0 0x01 +#define SET_BROADCAST_INTERVAL_CMD1 0x07 +#define SET_IBEACON_NAME_CMD0 0x01 +#define SET_IBEACON_NAME_CMD1 0x08 +#define SET_START_BEACON_CMD0 0x01 //Open Beacon +#define SET_START_BEACON_CMD1 0x09 +#define SET_STOP_BEACON_CMD0 0x01 //Close Beacon +#define SET_STOP_BEACON_CMD1 0x0A +#define SET_SOFT_REBOOT_CMD0 0x01 +#define SET_SOFT_REBOOT_CMD1 0x0B + +extern unsigned char SOFT_REBOOT1[15]; +extern unsigned char SET_VIRTUAL_PACKET_UUID[23]; +extern unsigned char SET_VIRTUAL_PACKET_UUID2[23]; + +extern unsigned char SET_UUID[21]; +extern unsigned char SET_MAJOR[7]; +extern unsigned char SET_MINOR[7]; +extern unsigned char SET_MEASURED_POWER[6]; +extern unsigned char SET_TRANSMISSION_POWER[6]; +extern unsigned char SET_PAIRING_PASSWORD[13]; +extern unsigned char SET_BROADCAST_INTERVAL[6]; +extern unsigned char SET_IBEACON_NAME[17]; +extern unsigned char SOFT_REBOOT[13]; +extern unsigned char SET_OPEN_BEACON[7]; +extern unsigned char SET_CLOSE_BEACON[8]; +extern uint8 BLE_Adv_Module_Beacon_ID[6]; + + extern uint8 Beacon_RxBuffer_Crnt_Pos,Beacon_RxBuffer_End_Pos; // must be volatile or the compiler may over-optimise. + extern uint8 Beacon_RX_Buffer[50]; + +extern void Process_Beacon_Command_Received(unsigned char* Command_Received); +extern void Change_Beacon_Parameter(unsigned char* Beacon_Parameter_To_Set); +extern void Initialize_Beacon_Module(void); \ No newline at end of file
diff -r 0cb43a362538 -r 22c4d2681cc9 Common_Defs.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Common_Defs.h Thu Mar 09 15:00:37 2017 +0000 @@ -0,0 +1,9 @@ + typedef unsigned char uint8; + typedef unsigned int uint16; + extern uint8 OBD_PlugIN_State; + #define TRUE 1 + #define FALSE 0 + + #define SUCCESS 0 + #define FAILURE 1 + \ No newline at end of file
diff -r 0cb43a362538 -r 22c4d2681cc9 Lora.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lora.cpp Thu Mar 09 15:00:37 2017 +0000 @@ -0,0 +1,560 @@ + #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 pc2(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 + Serial pc_2(USBTX, USBRX); + + 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 + CheckOUT_PacketType CheckOUT_Packet; //Allocate Memory for CheckOUT 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 + Near_Car_Beacon Near_Car_Beacon_Packet; + Fixed_Beacon Fixed_Beacon_Packet; + Misc_Packets Misc_Packet_Data; + + //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"; + + #define CHECK_IN_PACKET 0x00 + #define CHECK_OUT_PACKET 0x01 + + 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) +{ + pc_2.baud(115200); + LORA_Module_UART.baud(115200); + LORA_Module_UART.printf(Attention);//Send Attention command + AT_Response_Receive_Status = FAILURE; + pc2.printf("\nNwk set up started"); + while(AT_Response_Receive_Status) + Get_Lora_Response(); + pc2.printf("\nAT 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(); + pc2.printf("\nFrequency band response received"); + LORA_Module_UART.printf("\n%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(); + pc2.printf("\nNetwork key Response received"); + LORA_Module_UART.printf("\n%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(); + pc2.printf("\nNetwork Id response received"); + + LORA_Module_UART.printf("AT+TXDR=DR3\r"); + AT_Response_Receive_Status = FAILURE; + while(AT_Response_Receive_Status) + Get_Lora_Response(); + LORA_Module_UART.printf("AT+RXDR=10\r"); + AT_Response_Receive_Status = FAILURE; + while(AT_Response_Receive_Status) + Get_Lora_Response(); + LORA_Module_UART.printf("AT+ADR=0\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(); + + pc2.printf("\nConfiguration saved"); + LORA_Module_UART.printf("%s",Reset_Device); //reset device + wait_ms(3500); + LORA_Module_UART.printf(Attention); //Send Attention command + AT_Response_Receive_Status = FAILURE; + while(AT_Response_Receive_Status) + Get_Lora_Response(); + pc2.printf("\nAT 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(); + pc2.printf("\nJoin Response received"); +} + + void Initialize_lora_Packets() +{ + uint8 i; + /******* Initialize Lora packet for HeartBeat *****/ + Misc_Packet_Data.Header = LORA_PACKET_HEADER; + Misc_Packet_Data.Protocol_Version = OBD_Protocol_Version; + Heart_Beat_Lora_Packet.Packet_Type = HEART_BEAT_PACKET_CMD; + Misc_Packet_Data.OBD_Battery_Voltage = 350; //3.50V, dummy data + Misc_Packet_Data.Car_Battery_Voltage = 1250; //12.50V, dummy data + Misc_Packet_Data.OBD_Battery_Temperature = 95; //95'F, dummy data + Misc_Packet_Data.Car_Ambient_Temperature = 104; //104'F, dummy data + for(i=0;i<6;i++) + Fixed_Beacon_Packet.Parking1_Beacon_ID[i] = (0x01+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = 23; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + Fixed_Beacon_Packet.Parking2_Beacon_ID[i] = (2+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = 45; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + Fixed_Beacon_Packet.Parking3_Beacon_ID[i] = (3+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = 12; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + Heart_Beat_Lora_Packet.Sequence_No = 0x01; //Sent Packet Sequence Number + Heart_Beat_Lora_Packet.FCS = 0x00; //FCS of all packets + + /******* Initialize Lora packet for Vehicle Status *****/ + Misc_Packet_Data.Header = LORA_PACKET_HEADER; + Misc_Packet_Data.Protocol_Version = OBD_Protocol_Version; + Vehicle_Status_Lora_Packet.Packet_Type = STATUS_PACKET_CMD; + for(i=0;i<17;i++) + Misc_Packet_Data.VIN[i] = 0x06;//Vehicle_Identification_Number[i]; + for(i=0;i<3;i++) + Misc_Packet_Data.ODO_METER_READING[i] = 0x05; //Dummyy data, To be read using OBD + Vehicle_Status_Lora_Packet.Fuel_Level = 1050;//10.5 litre + Misc_Packet_Data.OBD_Battery_Voltage = 200; + Misc_Packet_Data.Car_Battery_Voltage = 1000; + Misc_Packet_Data.OBD_Battery_Temperature = 100; + Misc_Packet_Data.Car_Ambient_Temperature = 200; + for(i=0;i<6;i++) + Vehicle_Status_Lora_Packet.BLE_Adv_Beacon_ID[i] = 0x01;//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++) + Near_Car_Beacon_Packet.Near_Car1_Beacon_ID[i] = (3+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Near_Car_Beacon_Packet.Near_Car1_Beacon_Signal_Strength = 0x07; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + Near_Car_Beacon_Packet.Near_Car2_Beacon_ID[i] = (2+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Near_Car_Beacon_Packet.Near_Car2_Beacon_Signal_Strength = 0x08; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + for(i=0;i<6;i++) + Near_Car_Beacon_Packet.Near_Car3_Beacon_ID[i] = (1+i); //MAC ID of 1st NearBy Beacon Device with Highest Signal Strength,dummy data + Near_Car_Beacon_Packet.Near_Car3_Beacon_Signal_Strength = 0x09; //Signal Strength of 3rd NearBy Beacon Device with Highest Signal Strength + Vehicle_Status_Lora_Packet.Sequence_No = 0x01; //Sent Packet Sequence Number + Vehicle_Status_Lora_Packet.FCS = 0x00; //FCS of all packets + + /******* Initialize Lora packet for CheckIn *****/ + Misc_Packet_Data.Header = LORA_PACKET_HEADER; + Misc_Packet_Data.Protocol_Version = OBD_Protocol_Version; + CheckIN_Lora_Packet.Packet_Type = CHECKIN_PACKET_CMD; + for(i=0;i<5;i++) + CheckIN_Lora_Packet.DTC[i] = 0x00; //Get Beacon_ID of 1st nearby Beacon Device + CheckIN_Lora_Packet.Sequence_No = 0x01; //Sent Packet Sequence Number + CheckIN_Lora_Packet.FCS = 0x00; //FCS of all packets + + /******* Initialize Lora packet for CheckOUT *****/ + Misc_Packet_Data.Header = LORA_PACKET_HEADER; + Misc_Packet_Data.Protocol_Version = OBD_Protocol_Version; + CheckOUT_Packet.Packet_Type = CHECKIN_PACKET_CMD; + CheckOUT_Packet.Sequence_No = 0x01; //Sent Packet Sequence Number + CheckOUT_Packet.FCS = 0x00; //FCS of all packets + + /******* Initialize Lora packet for Movement *****/ + Misc_Packet_Data.Header = LORA_PACKET_HEADER; + Misc_Packet_Data.Protocol_Version = OBD_Protocol_Version; + Motion_Lora_Packet.Packet_Type = MOTION_PACKET_CMD; + Motion_Lora_Packet.Acceleration_Type = 0x00; //Type of acceleration, Vehicle Started/Vehicle Stopped/Sudden Vehicle Movement + Motion_Lora_Packet.Sequence_No = 0x01; //Sent Packet Sequence Number + Motion_Lora_Packet.FCS = 0x00; //FCS of all packets + Lora_Packet_To_Send[0] = 0x41;//'A' + Lora_Packet_To_Send[1] = 0x54;//'T' + Lora_Packet_To_Send[2] = 0x2b;//'+' + Lora_Packet_To_Send[3] = 0x53;//'S' + Lora_Packet_To_Send[4] = 0x45;//'E' + Lora_Packet_To_Send[5] = 0x4e;//'N' + Lora_Packet_To_Send[6] = 0x44;//'D' + Lora_Packet_To_Send[7] = 0x20;//' ' Blank Space + Lora_Packet_To_Send[8] = Misc_Packet_Data.Header; //Header of Lora Packet,0xFE + Lora_Packet_To_Send[9] = Misc_Packet_Data.Protocol_Version; + pc2.printf("\nLora Packets Initialized"); +} + + //HeartBeat Packet should be sent every 30sec +void Send_HeartBeat_Packet(void) +{ + //write code to read obd data,temperature,beacon data + uint8 Pos = 10,i; + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Packet_Type; //MSB of Motion Packet Type + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.OBD_Battery_Temperature; //Get Battery Temperature + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Car_Ambient_Temperature; //Get Ambient Temperature + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Heart_Beat_Lora_Packet.Sequence_No; //Packet Sequence number + //Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xFA;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0x0D;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + /*Lora_Packet_To_Send[Pos++] = 0xB1;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xB2;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xB3;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xB4;//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); + if(Heart_Beat_Lora_Packet.Sequence_No < 0x08) + Heart_Beat_Lora_Packet.Sequence_No++; + else + Heart_Beat_Lora_Packet.Sequence_No = 0x01; + pc2.printf("\nHeartbeat Packet Sequence Number %d",Heart_Beat_Lora_Packet.Sequence_No); + pc_2.printf("\nHeartbeat Packet Sequence Number %d",Heart_Beat_Lora_Packet.Sequence_No); +} + +//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 = 10,i; + Lora_Packet_To_Send[Pos++] = STATUS_PACKET_CMD; //MSB of Motion Packet Type + for(i=0;i < 17;i++) + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.VIN[i]; //Get OBD_ID + for(i=0;i < 3;i++) + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.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++] = (Misc_Packet_Data.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.OBD_Battery_Temperature; //Get Battery Temperature + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Car_Ambient_Temperature; //Get Ambient Temperature + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_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++] = Near_Car_Beacon_Packet.Near_Car2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_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++] = Near_Car_Beacon_Packet.Near_Car3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = 0x23;//Near_Car_Beacon_Packet.Near_Car3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Vehicle_Status_Lora_Packet.Sequence_No; //Packet Sequence number + /*Lora_Packet_To_Send[Pos++] = 0xEE;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEa;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEb;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEc;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEE;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xE3;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEb;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xE1;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xE5;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEa;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEb;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEf;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEE;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEa;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEb;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEc;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEE;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xE3;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEb;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xE1;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xE5;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEE;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEa;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEb;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xE9;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEE;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xE8;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xEb;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xE7;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + */Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0x0D;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + /*Lora_Packet_To_Send[Pos++] = 0xA1;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xA2;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xA3;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0xA4;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + */Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos); + if(Vehicle_Status_Lora_Packet.Sequence_No < 0x08) + Vehicle_Status_Lora_Packet.Sequence_No++; + else + Vehicle_Status_Lora_Packet.Sequence_No = 0x01; + pc2.printf("\nStatus Packet sending"); + pc2.printf("\nStatus Packet Sequence Number %d",Vehicle_Status_Lora_Packet.Sequence_No); + pc_2.printf("\nStatus Packet Sequence Number %d",Vehicle_Status_Lora_Packet.Sequence_No); +} + +//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 = 10,i; + Lora_Packet_To_Send[Pos++] = CHECKIN_PACKET_CMD; //Check detected. Set packet type to Checkout type + for(i=0;i < 17;i++) + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.VIN[i]; //Get OBD_ID + for(i=0;i < 3;i++) + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.ODO_METER_READING[i]; //Get OBD_ID + for(i=0;i<5;i++) + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.DTC[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.OBD_Battery_Temperature; //Get Battery Temperature + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Car_Ambient_Temperature; //Get Ambient Temperature + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = CheckIN_Lora_Packet.Sequence_No; //Packet Sequence number + Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0x0D;//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); + pc2.printf("\nCheckIN Packet Sent"); + if(CheckIN_Lora_Packet.Sequence_No < 0x08) + CheckIN_Lora_Packet.Sequence_No++; + else + CheckIN_Lora_Packet.Sequence_No = 0x01; + pc2.printf("\nStatus Packet sending"); + pc2.printf("\nCheckIN Packet Sequence Number %d",CheckIN_Lora_Packet.Sequence_No); +} + + +//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_CheckOUT_Packet(void) +{ + //write code to read OBD data,temperature,beacon data + uint8 Pos = 10,i; + Lora_Packet_To_Send[Pos++] = CHECKOUT_PACKET_CMD; //Checkout detected. Set packet type to Checkout type + for(i=0;i < 17;i++) + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.VIN[i]; //Get OBD_ID + for(i=0;i < 3;i++) + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.ODO_METER_READING[i]; //Get OBD_ID + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage >> 8); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.OBD_Battery_Voltage & 0xFF); //Get OBD_Battery Voltage + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage >> 8); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = (Misc_Packet_Data.Car_Battery_Voltage & 0xFF); //Get Vehicle_Battery Temperature + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.OBD_Battery_Temperature; //Get Battery Temperature + Lora_Packet_To_Send[Pos++] = Misc_Packet_Data.Car_Ambient_Temperature; //Get Ambient Temperature + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = CheckOUT_Packet.Sequence_No; //Packet Sequence number + Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Lora_Packet_To_Send[Pos++] = 0x0D;//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); + pc2.printf("\nCheckOUT Packet Sent"); + if(CheckOUT_Packet.Sequence_No < 0x08) + CheckOUT_Packet.Sequence_No++; + else + CheckOUT_Packet.Sequence_No = 0x01; + pc2.printf("\CheckOUT Packet Sequence Number %d",CheckOUT_Packet.Sequence_No); +} + +//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 = 10,i; + Lora_Packet_To_Send[Pos++] = MOTION_PACKET_CMD;//(Motion_Lora_Packet.Packet_Type & 0xFF); //LSB of Motion Packet Type + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength; //Get Signal Strength of 2nd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car1_Beacon_Signal_Strength; //Get Signal Strength of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Acceleration_Type; //get Type of Acceleration + /*for(i=0;i<6;i++) + Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car1_Beacon_ID[i]; //Get Beacon_ID of 1st nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_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++] = Near_Car_Beacon_Packet.Near_Car2_Beacon_ID[i]; //Get Beacon_ID of 2nd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_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++] = Near_Car_Beacon_Packet.Near_Car3_Beacon_ID[i]; //Get Beacon_ID of 3rd nearby Beacon Device + Lora_Packet_To_Send[Pos++] = Near_Car_Beacon_Packet.Near_Car3_Beacon_Signal_Strength; //Get Signal Strength of 3rd nearby Beacon Device + */Lora_Packet_To_Send[Pos++] = Motion_Lora_Packet.Sequence_No; //Packet Sequence number + Lora_Packet_To_Send[Pos++] = Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Motion_Packet_Sent_Count++; + Lora_Packet_To_Send[Pos++] = 0x0D;//Calculate_Lora_Frame_FCS(Lora_Packet_To_Send,Pos); //Calculate FCS of all bytes + Send_Lora_Packet_To_Gateway(Lora_Packet_To_Send,Pos); + if(Motion_Lora_Packet.Sequence_No < 0x08) + Motion_Lora_Packet.Sequence_No++; + else + Motion_Lora_Packet.Sequence_No = 0x01; + pc2.printf("\n Motion Packet Sent"); + pc2.printf("\Motion Packet Sequence Number %d",Motion_Lora_Packet.Sequence_No); +} + + +void Get_Lora_Response(void) +{ + static uint16 Temp_Pos1,Temp_pos2; + 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; + /*pc2.printf("\nEcho data\n"); + for(Temp_pos2 = LORA_UART_RX_Crnt_Pos;Temp_pos2<Temp_Pos1;Temp_pos2++) + pc2.printf("0x%2x ",(LORA_UART_RX_Buffer[Temp_pos2]));*/ + 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++]; + } + 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++]; + } + 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; + pc2.printf("\nSending command"); + for(i=0; i < Length; i++) + { + LORA_Module_UART.putc(Command_To_Send[i]); + pc2.printf("0x%2x ",(Command_To_Send[i])); + } + //LORA_Module_UART.printf("\r"); + /* + pc2.printf("\n Sending packet\n"); + for(i=0; i < Length; i++) + { + pc2.printf("0x%2x ",(Command_To_Send[i])); + } + */ + // LORA_Module_UART.printf("%s",Command_To_Send); + pc2.printf("\nTotal Bytes %d\n",Length); +} + +/* +//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; + //pc2.printf("Sending data"); + // LORA_Module_UART.printf("%s",Send_Lora_Packet);//write to serial port for sending through lora module + /*for(i=0; i < 7; i++) + { + LORA_Module_UART.putc(Send_Lora_Packet[i]); + + } + for(i=0; i < Length; i++) + { + LORA_Module_UART.putc(Command_To_Send[i]); + pc2.putc(Command_To_Send[i]); + } + for(i=0; i < Length; i++) + { + pc2.putc(Command_To_Send[i]); + + } + + // LORA_Module_UART.printf("%s",Command_To_Send); + LORA_Module_UART.printf("\r"); + pc2.printf("Total Bytes %d",Length); +}*/ + + 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); + }
diff -r 0cb43a362538 -r 22c4d2681cc9 Lora.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lora.h Thu Mar 09 15:00:37 2017 +0000 @@ -0,0 +1,176 @@ + #include "mbed.h" + #include "Common_Defs.h" + + // mDOT AT COMMANDS DEFINITIONS + #define ENABLED 1 + #define Disabled 0 + #define NWK_PARA_HEX 0 + #define NWK_PARA_STRING 1 + #define LORA_UART_RX_Size 2000//Buffer for receiving Lora responses + + #define LORA_PACKET_HEADER 0xFE //Lora Header,SOF,Every packet sent starts with 0xFE + + #define HEART_BEAT_PACKET_CMD 0x01 //MSB,HeartBeat Identification Packet + #define CHECKIN_PACKET_CMD 0x02 //MSB,CheckIn Identification Packet + #define CHECKOUT_PACKET_CMD 0x03 //MSB,CheckIn Identification Packet + #define MOTION_PACKET_CMD 0x04 //MSB,Motion Identification Packet + #define STATUS_PACKET_CMD 0x05 //MSB,Status Packet + #define NEW_DEV_PACKET_CMD 0x06 //MSB,New Device Identification Packet + + + //Lora Frequency SubBand + #define FREQUENCY_SUB_BAND_CHANNEL0 0 + #define FREQUENCY_SUB_BAND_CHANNEL1 1 + #define FREQUENCY_SUB_BAND_CHANNEL2 2 + #define FREQUENCY_SUB_BAND_CHANNEL3 3 + #define FREQUENCY_SUB_BAND_CHANNEL4 4 + #define FREQUENCY_SUB_BAND_CHANNEL5 5 + #define FREQUENCY_SUB_BAND_CHANNEL6 6 + #define FREQUENCY_SUB_BAND_CHANNEL7 7 + #define FREQUENCY_SUB_BAND_CHANNEL8 8 + + #define HEX_PARAMETER 0 + #define STRING_PARAMETER 1 + + //Network Join Status + #define NETWORK_STATUS_JOINED 1 + #define NETWORK_STATUS_NOT_JOINED 0 + + //Netwrork Join Retries Definitions + #define NETWORK_JOIN_RETRIES_Disable 0 + #define NETWORK_JOIN_RETRIES_1 1 + #define NETWORK_JOIN_RETRIES_2 2 + #define NETWORK_JOIN_RETRIES_3 3 + #define NETWORK_JOIN_RETRIES_4 4 + + #define HEARTBEAT_TYPE_PACKET 0x01 + #define MOTION_TYPE_PACKET 0x02 + #define CHECKIN_TYPE_PACKET 0x03 + #define STATUS_TYPE_PACKET 0x04 + + + extern uint8 Packet_Type_To_Send; //By Default Heart Beat PAckets should be sent + + +struct Fixed_Beacon +{ + uint8 Parking1_Beacon_ID[6]; + uint8 Parking1_Beacon_Signal_Strength; + uint8 Parking2_Beacon_ID[6]; + uint8 Parking2_Beacon_Signal_Strength; + uint8 Parking3_Beacon_ID[6]; + uint8 Parking3_Beacon_Signal_Strength; +}; + +struct Near_Car_Beacon +{ + uint8 Near_Car1_Beacon_ID[6]; + uint8 Near_Car1_Beacon_Signal_Strength; + uint8 Near_Car2_Beacon_ID[6]; + uint8 Near_Car2_Beacon_Signal_Strength; + uint8 Near_Car3_Beacon_ID[6]; + uint8 Near_Car3_Beacon_Signal_Strength; +}; + +struct Misc_Packets +{ + uint8 Header; + uint8 Protocol_Version; + uint16 OBD_Battery_Voltage; + uint16 Car_Battery_Voltage; + uint8 OBD_Battery_Temperature; + uint8 Car_Ambient_Temperature; + uint8 VIN[17]; //Vehicle Identification Number + uint8 ODO_METER_READING[3]; +}; + +struct Heart_Beat_PacketType +{ + Misc_Packets Misc_Packet_Data; + uint8 Packet_Type; + Fixed_Beacon Fixed_Beacon_Packet; + uint8 Sequence_No; + uint8 FCS; +}; + +struct Vehicle_Status_PacketType +{ + Misc_Packets Misc_Packet_Data; + uint8 Packet_Type; + uint16 Fuel_Level; + uint8 BLE_Adv_Beacon_ID[6]; + Fixed_Beacon Fixed_Beacon_Packet; + Near_Car_Beacon Near_Car_Beacon_Packet; + uint8 Sequence_No; + uint8 FCS; +}; + +struct CheckIN_PacketType +{ + Misc_Packets Misc_Packet_Data; + uint8 Packet_Type; + uint8 DTC[5]; //Diagnostic trouble code + Fixed_Beacon Fixed_Beacon_Packet; + uint8 Sequence_No; + uint8 FCS; +}; + +struct CheckOUT_PacketType +{ + Misc_Packets Misc_Packet_Data; + uint8 Packet_Type; + uint8 Sequence_No; + uint8 FCS; +}; + +struct Motion_PacketType +{ + Misc_Packets Misc_Packet_Data; + uint8 Packet_Type; + Fixed_Beacon Fixed_Beacon_Packet; + Near_Car_Beacon Near_Car_Beacon_Packet; + uint8 Acceleration_Type; + uint8 Sequence_No; + uint8 FCS; +}; + + + extern uint8 Send_Lora_Packet_Flag; + extern int Lora_RxBuffer_Crnt_Pos,Lora_RxBuffer_End_Pos; // must be volatile or the compiler may over-optimise. + extern int receivedDataCount; + extern char LORA_UART_RX_Buffer[LORA_UART_RX_Size]; + extern int LORA_UART_RX_Crnt_Pos; + extern uint8 Lora_Command_Rcvd[100]; + extern uint8 Lora_Cmd_Length; + extern uint8 AT_Response_Receive_Status; + extern uint8 Vehicle_Identification_Number[17]; //Unique Vehicle_Identification_Number, Read using OBD + extern uint8 Motion_Packet_Sent_Count; + extern uint8 CheckIN_Packet_Sent_Count; + extern uint8 Lora_Packet_To_Send[100]; + + extern uint8 OBD_Protocol_Version; + extern Heart_Beat_PacketType Heart_Beat_Lora_Packet; //Allocate Memory for HeartBeat Lora Packets + extern CheckIN_PacketType CheckIN_Lora_Packet; //Allocate Memory for CheckIN Lora Packets + extern Motion_PacketType Motion_Lora_Packet; //Allocate Memory for Movement Lora Packets + extern Vehicle_Status_PacketType Vehicle_Status_Lora_Packet; //Allocate Memory for Movement Lora Packets + extern Near_Car_Beacon Near_Car_Packet; + extern Fixed_Beacon Fixed_Beacon_Packet; + extern CheckOUT_PacketType CheckOUT_Packet; + extern Misc_Packets Misc_Packet_Data; + +void Send_Lora_Packet_To_Gateway(uint8* Command_To_Send,uint8 Length); +extern void Flip_Lora_Packet_Sending(); +extern void Lora_Periodic_Packet_Sending_thread(void const *arg); +void Initialize_lora_Packets(); +static void Process_Received_Response(uint8 Response_Length); +void Read_Lora_END_Node_ID(uint8* Buffer,uint8 Start_Position,uint8 Response_Length); +//extern void Lora_Rcvd_Cmd_Processing_thread(void const *args); +void Get_Acceleration_Type(void); +void Send_Motion_Packet(void); +void Send_CheckIN_Packet(void); +void Send_CheckOUT_Packet(void); +void Send_HeartBeat_Packet(void); +void Send_Vehicle_Status_Packet(void); +extern uint8 Calculate_Lora_Frame_FCS(uint8* Packet_Data,uint8 Packet_Length); +extern void Set_Up_Lora_Network_Configuration(void); +extern void Get_Lora_Response(void);
diff -r 0cb43a362538 -r 22c4d2681cc9 main.cpp --- a/main.cpp Fri Jan 13 19:49:53 2017 +0000 +++ b/main.cpp Thu Mar 09 15:00:37 2017 +0000 @@ -1,41 +1,350 @@ + #include "mbed.h" +#include "rtos.h" +#include "Lora.h" +#include "Beacon.h" + +#define LORA_SEND_THREAD "lora_send_thread" +#define HEART_BEAT_PACKET_SENDING 0x01 +#define STATUS_PACKET_SENDING 0x02 + +InterruptIn checkinIntr(PC_13); +InterruptIn motionIntr(PB_14); +InterruptIn testIntr(PB_5); + + +DigitalOut led1(LED1); +DigitalOut led2(PA_11); +DigitalOut led3(PA_12); + +/* reserve the debbuger uart to shell interface */ +Serial pc_serial(USBTX,USBRX); + RawSerial pc3(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 + RawSerial LORA_UART(PA_0, PA_1);//USART4_TX->PA_0,USART4_RX->PA_1 : Used for Lora module command sending and reception from gateway +//RawSerial pc1(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only +//RawSerial DEBUG_UART(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only +RawSerial Beacon_UART(PC_4, PC_5);//USART3_TX->PC4,USART3_RX->PC_5 : Used for sending command to beacon module +RawSerial BLE_RECEIVER_UART(PA_9, PA_10);//USART1_TX->PA_0,USART1_RX->PA_1 : Used for Lora module command sending and reception from gateway +Serial pc1(USBTX, USBRX); +RawSerial DEBUG_UART(USBTX, USBRX);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only + +/* declares threads for this demo: */ +const size_t a_stk_size = 384; +uint8_t a_stk[a_stk_size]; +Thread loraSendThread(osPriorityNormal, a_stk_size, &a_stk[0]); + +const size_t c_stk_size = 384; +uint8_t c_stk[c_stk_size]; + +const size_t e_stk_size = 384; +uint8_t e_stk[e_stk_size]; +Thread checkinEventThread(osPriorityNormal, e_stk_size, &e_stk[0]); +Thread motionEventThread(osPriorityNormal, c_stk_size, &c_stk[0]); + +// create an event queue +EventQueue chechinEventQueue; +EventQueue motionEventQueue; -typedef struct { - float voltage; /* AD result of measured voltage */ - float current; /* AD result of measured current */ - uint32_t counter; /* A counter value */ -} message_t; +Queue<int, 16> loraQueue; +int message2; +int message3; + +uint32_t count = 0; + +//Datatype typecasting +typedef unsigned char uint8; +typedef unsigned int uint16; + +uint8 OBD_Plug_In_Status = FALSE; + +//peripheral connection +//DigitalOut led1(PB_11); +//DigitalOut led2(LED2); +DigitalIn Switch2(PB_13); + + +//InterruptIn OBD_PLUGIN_INTERRUPT_PIN(PC_13); +InterruptIn CheckIn_Interrupt(PB_7);//(PC_13); +InterruptIn Motion_Start_To_Stop(PB_5);//(PC_13); +InterruptIn Motion_Stop_To_Start(PB_14);//(PC_13); +InterruptIn Motion_Sudden_Jerk(PC_13);//(PC_13); -MemoryPool<message_t, 16> mpool; -Queue<message_t, 16> queue; -Thread thread; + +uint8 Ticker_Count = 0; //Variable to count for timer overflows + +uint8 OBD_Plugin_Detected = FALSE; + +//Create Object for structure of Lora Packet to be sent + +static uint16 Calculate_Wheels_RPM(uint8* Buffer); +void flip_Packet_Sending_Flag(); +void Lora_Periodic_Packet_Sending_thread(void const *args); + +void Lora_Rcvd_Cmd_Processing_thread(void);// const *args); +void Enable_CheckIN_Packet_Sending(); + +const char GET_RSSI[]= {0x41,0x54,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x0D,0x0A}; +const char SET_BEACON_VENDOR_ID[]= {0x41,0x54,0xF1,0x01,0x02,0x00,0x00,0x00,0x00,0xF2,0x0D,0x0A}; +const char SET_BEACON_MESSAGE_TYPE[]= {0x41,0x54,0xF2,0x01,0x00,0x00,0x00,0x00,0x00,0xF3,0x0D,0x0A}; +/*************************Accelerometer related definitions***********************************/ +//Accelerometer related definitions + +#define DOUBLE_TAP_INTERRUPT 0x20 +#define ACTIVITY_INTERRUPT 0x10 +#define INACTIVITY_INTERRUPT 0x08 -/* Send Thread */ -void send_thread (void) { - uint32_t i = 0; - while (true) { - i++; // fake data update - message_t *message = mpool.alloc(); - message->voltage = (i * 0.1) * 33; - message->current = (i * 0.1) * 11; - message->counter = i; - queue.put(message); - wait(1); +#define TAP_THRESHOLD 75 +#define ACTIVITY_THRESHOLD 64 // THRES_ACT register value 62.5mg/LSB , therfore value 32 indicates 2g activity +#define INACTIVITY_THRESHOLD 50 + +#define DUR_TIME 0x15 // DUR Register value providing maximum time to be held to generate an interrupt +#define LATENT_TIME 0x15 // The interrupt latency +#define WINDOW_TIME 0x45 // The time of the interrupt window in which the next tap will be detected +#define INACTIVITY_VALIDATION_TIME 5 // The time until which the acceleration must be held below the inactivity threshold to generate an inactvity interrupt +// Here the value 5 indicates literally 5 secs +#define X_AXIS_OFFSET 0x7F +#define Y_AXIS_OFFSET 0x7F +#define Z_AXIS_OFFSET 0x05 + +Serial pc(USBTX, USBRX); +I2C i2c(PB_9, PB_8); + +InterruptIn activity(PB_0); +InterruptIn inactivity(PA_4); // As for now only this is used +DigitalOut led(LED1); + +const int slave_address_acc = 0xA6; +char axis_data[6] = {0,0,0,0,0,0}; + +char interrupt_source[2]; +char axis_data_start_address[2] = {0x32, 0}; +char intr_source_address[2] = {0x30, 0}; +char all_interrupt_clear_command[2] = {0x2E, 0x00}; +char all_interrupt_enable_command[2] = {0x2E, 0x38}; +char activity_interrupt_disable_command[2] = {0x2E, 0x08}; +char inactivity_interrupt_disable_command[2] = {0x2E, 0x30}; +char accelerometer_status_registered = 0; +unsigned int interrupt_source_duplicate; + +char threshold_offset_command[5]; +char act_inact_time_config_command[8]; +char interrupt_enable_command[3]; +char tap_axis_enable_command[2]; +char baud_rate_command[2]; +char data_format_command[2]; +char measure_bit_on_command[2]; + + +unsigned char vehicle_speed = 25; // Kmph +unsigned char current_speed, previous_speed, speed_threshold = 10; // Kmph + +unsigned char x_axis, y_axis, z_axis; + +unsigned char Motion_Detect_Status = FALSE; +uint8 OBD_PlugInOut_IOC_Status = FALSE; +unsigned char Motion_Type_Detected = 0x00;//MOTION_TYPE_UNKNOWN; //By default set motion type as unknown +void Accelerometer_Process_thread();//void const *args) ; + +/************************************************************************/ + +uint8 Command_Length_Sent; +uint8 Checkin_Detect_Status = FALSE; +void Extract_Received_Lora_Response(void); +void Send_Command_To_BLE_Receiver(const char* Command); + +char previous_state = 0; +char current_state = 0; + +uint8 OBD_PlugIN_State=0; +uint8 OBD_PlugIN_State1=0; +uint8 OBD_PlugIN_State2=0; +uint8 OBD_PlugIN_Temp_State=1; + +uint8 Start_To_Stop_Status = FALSE; +uint8 Stop_To_Start_Status = FALSE; +uint8 Jerk_Status = FALSE; +#define BLE_RECEIVER_UART_RX_Size 100 +uint8 BLE_RxBuffer_End_Pos = 0; +char BLE_Receiver_UART_RX_Buffer[BLE_RECEIVER_UART_RX_Size]; + +void Start_To_Stop_Interrupt() +{ + Start_To_Stop_Status = TRUE; + Motion_Detect_Status = TRUE; +} + +void Stop_To_Start_Interrupt() +{ + Stop_To_Start_Status = TRUE; + Motion_Detect_Status = TRUE; +} + +void Sudden_Jerk_Interrupt() +{ + Jerk_Status = TRUE; + Motion_Detect_Status = TRUE; +} +/* +void OBD_Plug_IN_Interrupt() +{ + if(OBD_PlugIN_State1!=OBD_PlugIN_Temp_State) { + OBD_PlugIN_State1=1; + OBD_PlugIN_State=!OBD_PlugIN_State; + OBD_PlugIN_Temp_State=OBD_PlugIN_State; + OBD_PlugInOut_IOC_Status = TRUE; } } -int main (void) { - thread.start(callback(send_thread)); +void OBD_Plug_OUT_Interrupt() +{ - while (true) { - osEvent evt = queue.get(); - if (evt.status == osEventMessage) { - message_t *message = (message_t*)evt.value.p; - printf("\nVoltage: %.2f V\n\r" , message->voltage); - printf("Current: %.2f A\n\r" , message->current); - printf("Number of cycles: %u\n\r", message->counter); - - mpool.free(message); + if(OBD_PlugIN_State2!=OBD_PlugIN_Temp_State) { + OBD_PlugIN_State2=0; + OBD_PlugIN_State=!OBD_PlugIN_State; + OBD_PlugIN_Temp_State=OBD_PlugIN_State; + OBD_PlugInOut_IOC_Status = TRUE; + } +} + +//This function is Interrupt routine for detecting OBD Plugin and Out +void Handle_CheckIn_Interrupt() +{ + OBD_PlugInOut_IOC_Status = TRUE; + pc.printf("\nMovement_Detected\n"); +} + +//Declare Ticker for sending lora packet +Ticker Lora_Packet_Sending_Ticker; +void flip_Packet_Sending_Flag(void) +{ + led1=!led1; + //flip function + if(Ticker_Count < 5) { + Ticker_Count++; + } else { + Ticker_Count = 0; + Send_Lora_Packet_Flag = TRUE; + } +} +*/ +// called every time a byte is received from lora module. +void Lora_onDataRx() +{ + while (LORA_UART.readable()) { + // while there is data waiting + LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos++] = LORA_UART.getc(); // put it in the buffer + //pc1.putc(LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos-1]); + if(Lora_RxBuffer_End_Pos >= LORA_UART_RX_Size) { + // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. + // For now just throw everything away. + Lora_RxBuffer_End_Pos = 0; + } + } +} +/* +// called every time a byte is received from Beacon Module. +void Beacon_onDataRx() +{ + while (Beacon_UART.readable()) { + // while there is data waiting + Beacon_RX_Buffer[Beacon_RxBuffer_End_Pos++] = Beacon_UART.getc(); // put it in the buffer + pc1.printf("%2x",Beacon_RX_Buffer[Beacon_RxBuffer_End_Pos-1]); + if(Beacon_RxBuffer_End_Pos >= 100) { + // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. + // For now just throw everything away. + Beacon_RxBuffer_End_Pos = 0; + } + } +} + +void BLE_Receiver_onDataRx(void) +{ + while (BLE_RECEIVER_UART.readable()) { + // while there is data waiting + //BLE_Receiver_UART_RX_Buffer[BLE_RxBuffer_End_Pos++] = BLE_RECEIVER_UART.getc(); // put it in the buffer + //pc1.putc(BLE_Receiver_UART_RX_Buffer[BLE_RxBuffer_End_Pos-1]); + if(BLE_RxBuffer_End_Pos >= BLE_RECEIVER_UART_RX_Size) { + // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. + // For now just throw everything away. + BLE_RxBuffer_End_Pos = 0; } } } +*/ + +void checkinFunction() { + // this now runs in the context of checkinEventThread, instead of in the ISR + count++; + printf("Toggling LED 2!\r\n"); + for(int i = 0 ; i < 0xFFFFFF; i++); + led2 = !led2; + message2 = 1; + loraQueue.put(&message2); + printf("Toggled LED 2 count = %d \r\n", count); +} + +void motionFunction() { + // this now runs in the context of motionEventThread, instead of in the ISR + count++; + printf("Toggling LED 3!\r\n"); + for(int i = 0 ; i < 0xFFFFFF; i++); + led3 = !led3; + message3 = 2; + loraQueue.put(&message3); + printf("Toggled LED 3 count = %d \r\n", count); +} + +/** + * @brief thread a function + */ +static void loraSendThreadFunc(void const *buf) +{ + uint32_t execs = 0; + int *message1; + osEvent evt; + pc_serial.printf("## started %s execution! ##\n\r", (char *)buf); + + for(;;) { + execs++; + /* adds dummy processing */ + for(int i = 0 ; i < 0xFFFFFF; i++); + evt = loraQueue.get(); + if (evt.status == osEventMessage) { + message1 = (int*)evt.value.p; + } + if( *message1 == HEART_BEAT_PACKET_SENDING) + Send_HeartBeat_Packet(); //call function to send heartbeat packet + else if( *message1 == STATUS_PACKET_SENDING) + Send_Vehicle_Status_Packet(); //call function to send heartbeat packet + pc_serial.printf("## %s executed %d times! p = %d ##\n\r", (char *)buf, execs, *message1); + led1 = !led1; + Thread::yield(); + } +} + +/** + * @brief main application loop + */ +int main(void) +{ + pc_serial.baud(115200); + pc3.baud(115200); + pc_serial.printf("Hello"); + LORA_UART.attach(&Lora_onDataRx, Serial::RxIrq); + Set_Up_Lora_Network_Configuration(); + Initialize_lora_Packets(); + loraSendThread.start(callback(loraSendThreadFunc, (void *)LORA_SEND_THREAD)); + //b_thread.start(callback(thread2, (void *)THREAD_B)); + /*c_thread.start(callback(thread3, (void *)THREAD_C));*/ + + checkinEventThread.start(callback(&chechinEventQueue, &EventQueue::dispatch_forever)); + motionEventThread.start(callback(&motionEventQueue, &EventQueue::dispatch_forever)); + + // wrap calls in queue.event to automatically defer to the queue's thread + checkinIntr.fall(chechinEventQueue.event(&checkinFunction)); + motionIntr.fall(motionEventQueue.event(&motionFunction)); + + return 0; +} + +
diff -r 0cb43a362538 -r 22c4d2681cc9 mbed-os.lib --- a/mbed-os.lib Fri Jan 13 19:49:53 2017 +0000 +++ b/mbed-os.lib Thu Mar 09 15:00:37 2017 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-os/#2885c1b41e63158cb6faf5f107cd821ae06ef26c +https://github.com/ARMmbed/mbed-os/#34c1facf42a174f47fdf9002cd8c6bf10ac41744