kumar singh
/
Dealer_20Mar
BLE Transmitter not working
Fork of Dealer_23Feb by
Diff: main.cpp
- Revision:
- 24:1063cfc311e5
- Parent:
- 23:688ee106c385
- Child:
- 25:0ac2680d594c
- Child:
- 26:506380fccce2
diff -r 688ee106c385 -r 1063cfc311e5 main.cpp --- a/main.cpp Thu Feb 23 08:15:58 2017 +0000 +++ b/main.cpp Thu Feb 23 13:21:08 2017 +0000 @@ -13,8 +13,9 @@ uint8 OBD_Plug_In_Status = FALSE; //peripheral connection -DigitalOut led1(LED1); +DigitalOut led1(PB_11); DigitalOut led2(LED2); +DigitalIn Switch2(PB_13); //Configure Serial port 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 @@ -27,6 +28,10 @@ //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); + uint8 Ticker_Count = 0; //Variable to count for timer overflows @@ -130,6 +135,27 @@ 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; + +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) { @@ -155,13 +181,14 @@ void Handle_CheckIn_Interrupt() { OBD_PlugInOut_IOC_Status = TRUE; - pc.printf("Movement_Detected"); + 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++; @@ -205,7 +232,7 @@ { 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 + //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. @@ -219,12 +246,17 @@ { pc1.baud(115200); BLE_RECEIVER_UART.baud(115200); - pc1.printf("%s","Debugging started"); - BLE_RECEIVER_UART.attach(&BLE_Receiver_onDataRx, Serial::RxIrq); + pc1.printf("%s","Debugging started\n"); + //BLE_RECEIVER_UART.attach(&BLE_Receiver_onDataRx, Serial::RxIrq); LORA_UART.attach(&Lora_onDataRx, Serial::RxIrq); CheckIn_Interrupt.fall(&OBD_Plug_IN_Interrupt); CheckIn_Interrupt.rise(&OBD_Plug_OUT_Interrupt); - + if(Switch2) + { + } + Motion_Start_To_Stop.fall(&Start_To_Stop_Interrupt); + Motion_Stop_To_Start.fall(&Stop_To_Start_Interrupt); + Motion_Sudden_Jerk.fall(&Sudden_Jerk_Interrupt); //Create a thread to read vehicle data //Thread OBD_thread(OBD_Rcvd_Cmd_Processing_thread); @@ -234,9 +266,9 @@ //led2_thread is executing concurrently with main at this point //inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge Initialize_Beacon_Module(); - Send_Command_To_BLE_Receiver(SET_BEACON_VENDOR_ID); - Send_Command_To_BLE_Receiver(SET_BEACON_MESSAGE_TYPE); - pc1.printf("%s","Transmitter MAC ID received"); + // Send_Command_To_BLE_Receiver(SET_BEACON_VENDOR_ID); + // Send_Command_To_BLE_Receiver(SET_BEACON_MESSAGE_TYPE); + pc1.printf("\n%s","Transmitter MAC ID received\n"); Lora_Periodic_Packet_Sending(); //Infinite loop for sending and receiving lora response, no return from here } @@ -256,51 +288,53 @@ uint8 Status_Packet_Wait_Count = 0; void Lora_Periodic_Packet_Sending() { - pc1.printf("Periodic packet sending intiialized"); + pc1.printf("\nPeriodic packet sending intiialized\n"); Set_Up_Lora_Network_Configuration(); Initialize_lora_Packets(); Send_Lora_Packet_Flag = TRUE; while (true) { if(Packet_Type_To_Send == HEARTBEAT_TYPE_PACKET) { //check if packet to be sent is Heartbeat packet if(Send_Lora_Packet_Flag) { //Check if packet sending is enabled, Packet should be sent only when enabled after timeout period + pc1.printf("\nStatus_Packet_Wait_Count %d\n",Status_Packet_Wait_Count); Status_Packet_Wait_Count++; if(Status_Packet_Wait_Count < 4) { - Send_Command_To_BLE_Receiver(GET_RSSI); - Send_RSSI_Request_Command(GET_RSSI); - pc1.printf("Sending heartbeat packet"); //call function to send periodic motion packet + // Send_Command_To_BLE_Receiver(GET_RSSI); + //Send_RSSI_Request_Command(GET_RSSI); + pc1.printf("\nSending heartbeat packet\n"); //call function to send periodic motion packet Send_HeartBeat_Packet(); //call function to send heartbeat packet - pc.printf("Sent HeartBeat Packet"); - AT_Response_Receive_Status = FAILURE; + pc.printf("\nSent HeartBeat Packet\n"); + /*AT_Response_Receive_Status = FAILURE; while(AT_Response_Receive_Status) Get_Lora_Response(); - Send_Lora_Packet_Flag = FALSE; - //pc1.printf("Heartbeat Packet Response Received"); + */Send_Lora_Packet_Flag = FALSE; + pc1.printf("\nHeartbeat Packet Response Received\n"); } else { //Send_RSSI_Request_Command(GET_RSSI); Status_Packet_Wait_Count = 0; - pc1.printf("Sending Vehicle status packets"); //call function to send periodic motion packet + pc1.printf("\nSending Vehicle status packets\n"); //call function to send periodic motion packet Send_Vehicle_Status_Packet(); //call function to send heartbeat packet - pc1.printf("Sent Vehicle Status Packet"); + pc1.printf("\nSent Vehicle Status Packet\n"); //AT_Response_Receive_Status = FAILURE; // while(AT_Response_Receive_Status) // Get_Lora_Response(); Send_Lora_Packet_Flag = FALSE; - //pc1.printf("Status Packet Response Received"); + Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; + //pc1.printf("\nStatus Packet Response Received\n"); } } } else if(Packet_Type_To_Send == MOTION_TYPE_PACKET) { //check if packet to be sent is motion packet if(Send_Lora_Packet_Flag) { //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period //Send_RSSI_Request_Command(GET_RSSI); - pc1.printf("Sending Motion Packet"); //call function to send periodic motion packet + pc1.printf("\nSending Motion Packet\n"); //call function to send periodic motion packet Send_Motion_Packet(); - pc1.printf("Sent Motion Packet"); //call function to send periodic motion packet + pc1.printf("\nSent Motion Packet\n"); //call function to send periodic motion packet AT_Response_Receive_Status = FAILURE; while(AT_Response_Receive_Status) Get_Lora_Response(); - pc1.printf("Motion Packet Response Received"); + pc1.printf("\nMotion Packet Response Received\n"); Send_Lora_Packet_Flag = FALSE; if(Motion_Packet_Sent_Count >= 5) { //Stop Sending Motion Packets if after sending for 2 minute - pc1.printf("Packet Type Sending Changed to HeartBeat"); + pc1.printf("\nPacket Type Sending Changed to HeartBeat\n"); Motion_Packet_Sent_Count = 0; Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet Lora_Packet_Sending_Ticker.detach(); //destroy ticker @@ -310,31 +344,31 @@ } else if(Packet_Type_To_Send == CHECKIN_TYPE_PACKET) { //check if packet to be sent is Checkin packet if(Send_Lora_Packet_Flag) { //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period Send_Lora_Packet_Flag = FALSE; - //pc1.printf("Sent Beacon ID request"); + //pc1.printf("\nSent Beacon ID request\n"); // Send_RSSI_Request_Command(GET_RSSI); if(OBD_PlugIN_State) { - pc1.printf("Sening Checkin Packet"); + pc1.printf("\nSening Checkin Packet\n"); Send_CheckIN_Packet(); //call function to send periodic checkIn packet - pc1.printf("Sent Checkin Packet"); - Process_Beacon_Command_Received(SET_VIRTUAL_PACKET_UUID); - wait(.2); - Process_Beacon_Command_Received(SOFT_REBOOT1); + pc1.printf("\nSent Checkin Packet\n"); + //Process_Beacon_Command_Received(SET_VIRTUAL_PACKET_UUID); + //wait(.2); + //Process_Beacon_Command_Received(SOFT_REBOOT1); } else { - pc1.printf("Sending Checkout Packet"); + pc1.printf("\nSending Checkout Packet\n"); Send_CheckOUT_Packet(); - pc1.printf("Sent CheckOut Packet"); - Process_Beacon_Command_Received(SET_VIRTUAL_PACKET_UUID2); - wait(.2); - Process_Beacon_Command_Received(SOFT_REBOOT1); + pc1.printf("\nSent CheckOut Packet\n"); + //Process_Beacon_Command_Received(SET_VIRTUAL_PACKET_UUID2); + //wait(.2); + //Process_Beacon_Command_Received(SOFT_REBOOT1); } /* AT_Response_Receive_Status = FAILURE; while(AT_Response_Receive_Status) Get_Lora_Response();*/ if(CheckIN_Packet_Sent_Count >= 5) { //Stop Sending Motion Packets if after sending for 2 minute - pc1.printf("Packet Type Sending Changed to HeartBeat"); + pc1.printf("\nPacket Type Sending Changed to HeartBeat\n"); CheckIN_Packet_Sent_Count = 0; Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet Initialize_Packets_Sent_Count(); @@ -346,19 +380,23 @@ //Get_Acceleration_Type(); // Process_Beacon_Command_Received(SET_VIRTUAL_PACKET_UUID); // Process_Beacon_Command_Received(SOFT_REBOOT1); - pc.putc('j'); OBD_PlugInOut_IOC_Status = FALSE; Packet_Type_To_Send = CHECKIN_TYPE_PACKET; Send_Lora_Packet_Flag = TRUE; CheckIN_Packet_Sent_Count = 0; - CheckIN_Lora_Packet.Sequence_No = 0x00; //Reset counter - CheckOUT_Packet.Sequence_No = 0x00; //Reset counter + CheckIN_Lora_Packet.Sequence_No = 0x01; //Reset counter + CheckOUT_Packet.Sequence_No = 0x01; //Reset counter wait(1); //wait for 1sec to avoid detecting plugin debounce } if(Motion_Detect_Status) { //Check if Accelerometer Interrupt is generated - Motion_Lora_Packet.Sequence_No = 0x00; //Reset counter + Motion_Lora_Packet.Sequence_No = 0x01; //Reset counter + Motion_Packet_Sent_Count = 0x00; //reset counetr Get_Acceleration_Type(); Motion_Detect_Status = FALSE; + Start_To_Stop_Status = FALSE; + Jerk_Status = FALSE; + Stop_To_Start_Status = FALSE; + wait(1); //wait for 1sec to avoid detecting switch debounce } } } @@ -369,7 +407,12 @@ Lora_Packet_Sending_Ticker.detach(); //destroy ticker Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 5.0); //create new ticker, time inetrval value to be changed Send_Lora_Packet_Flag = 1; //set flag to send motion packet as soon as motion is detecetd - Motion_Lora_Packet.Acceleration_Type = Motion_Type_Detected; //Read Type of motion deteceted + if(Start_To_Stop_Status) + Motion_Lora_Packet.Acceleration_Type = MOTION_TYPE_START_TO_STOP; //Read Type of motion deteceted + if(Stop_To_Start_Status) + Motion_Lora_Packet.Acceleration_Type = MOTION_TYPE_STOP_TO_START; //Read Type of motion deteceted + if(Jerk_Status) + Motion_Lora_Packet.Acceleration_Type = MOTION_TYPE_TAP; //Read Type of motion deteceted //write code to read acceleration value for every 10sec after any of the acceleration is found } @@ -422,7 +465,7 @@ Fixed_Beacon_Packet.Parking3_Beacon_ID[5] = 0xC9; Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = 0xB0; */ - pc1.printf("Reading MACID from Receiver"); + pc1.printf("\nReading MACID from Receiver\n"); while(true) { wait_ms(2); @@ -433,7 +476,7 @@ if(BLE_Receiver_UART_RX_Buffer[Temp_Pos+1] == 0x0A) { Temp_Pos = 2; - //pc1.printf("Fixed Beacon1 MAC ID"); + //pc1.printf("\nFixed Beacon1 MAC ID\n"); for(i=0; i<6; i++) { Fixed_Beacon_Packet.Parking1_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; @@ -441,7 +484,7 @@ } Temp_Pos+=2; Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; - //pc1.printf("Fixed Beacon2 MAC ID"); + //pc1.printf("\nFixed Beacon2 MAC ID\n"); for(i=0; i<6; i++) { Fixed_Beacon_Packet.Parking2_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; @@ -449,7 +492,7 @@ } Temp_Pos+=2; Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; - //pc1.printf("Fixed Beacon3 MAC ID"); + //pc1.printf("\nFixed Beacon3 MAC ID\n"); for(i=0; i<6; i++) { Fixed_Beacon_Packet.Parking3_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; @@ -468,5 +511,5 @@ else Temp_Pos = 0x00; } - pc1.printf("Reading MACID finished"); + pc1.printf("\nReading MACID finished\n"); }