BLE Transmitter not working

Fork of Dealer_23Feb by kumar singh

Committer:
NarendraSingh
Date:
Tue Feb 21 13:33:29 2017 +0000
Revision:
20:f812f85cf97e
Parent:
16:7703b9d92326
Child:
21:a5fb0ae94dc6
packet length test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NarendraSingh 11:77e595130230 1 #include "Beacon.h"
NarendraSingh 11:77e595130230 2 #include "Lora.h"
NarendraSingh 11:77e595130230 3
NarendraSingh 16:7703b9d92326 4 RawSerial DEBUG_UART2(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10
NarendraSingh 11:77e595130230 5 RawSerial Beacon_Module_UART(PC_4, PC_5);//USART3_TX->PC4,USART3_RX->PC_5
NarendraSingh 11:77e595130230 6
NarendraSingh 11:77e595130230 7
NarendraSingh 11:77e595130230 8 uint8 Beacon_RxBuffer_Crnt_Pos,Beacon_RxBuffer_End_Pos; // must be volatile or the compiler may over-optimise.
NarendraSingh 11:77e595130230 9 uint8 Beacon_RX_Buffer[100];
NarendraSingh 11:77e595130230 10 char Beacon_RxIndex=0;
NarendraSingh 11:77e595130230 11 uint8 BLE_Adv_Module_Beacon_ID[6];
NarendraSingh 11:77e595130230 12
NarendraSingh 11:77e595130230 13 /*Instructions for operation(MS49SF1U)
NarendraSingh 11:77e595130230 14 1. UART command list(Commands for modifying configuration)
NarendraSingh 11:77e595130230 15 |-------------------|-----------------------------|---------------|---------------------------------------------------|
NarendraSingh 11:77e595130230 16 | Item | Command format | Length(data) | Returned value after successful receiving |
NarendraSingh 11:77e595130230 17 |-------------------|-----------------------------|---------------|---------------------------------------------------|
NarendraSingh 11:77e595130230 18 UUID 0x4154+0xFFF1+data+0x0d 16character 0xFFF1+received data
NarendraSingh 11:77e595130230 19 Major 0x4154+0xFFF2+data+0x0d 2character 0xFFF2+received data
NarendraSingh 11:77e595130230 20 Minor 0x4154+0xFFF3+data+0x0d 2character 0xFFF3+received data
NarendraSingh 11:77e595130230 21 Measured power 0x4154+0xFFF4+data+0x0d 1character 0xFFF4+received data
NarendraSingh 11:77e595130230 22 Transmission power 0x4154+0xFFF5+data+0x0d 1character 0xFFF5+received data
NarendraSingh 11:77e595130230 23 Pairing Password 0x4154+0xFFF6+data+0x0d 8character 0xFFF6+received data
NarendraSingh 11:77e595130230 24 Broadcasting Interval 0x4154+0xFFF7+data+0x0d 1character 0xFFF7+received data
NarendraSingh 11:77e595130230 25 Beacon_Serial_ID 0x4154+0xFFF8+data+0x0d 4character 0xFFF8+received data
NarendraSingh 11:77e595130230 26 iBeacon_name 0x4154+0xFFF9+data+0x0d 12character 0xFFF9+received data
NarendraSingh 11:77e595130230 27 Authentication 0x4154+0xFFFA+data+0x0d 12character 0xFFFA+received data
NarendraSingh 11:77e595130230 28 Connection Mode 0x4154+0xFFFE+data+0x0d 1character 0xFFFE+received data
NarendraSingh 11:77e595130230 29 Soft Reboot 0x4154+0xFFFF+data+0x0d 8character 0xFFFF+received data+0K or ERROR
NarendraSingh 11:77e595130230 30 open beacon 0x4154+0x6F70656E+0x0d return Beacon is open\n
NarendraSingh 11:77e595130230 31 close beacon 0x4154+0x636C6F7365+0x0d return Beacon is closed\n
NarendraSingh 11:77e595130230 32 app enable 0x4154+0x656E61626C65+0x0d return app enabled
NarendraSingh 11:77e595130230 33 app unable 0x4154+0x756E61626C65+0x0d
NarendraSingh 11:77e595130230 34 Read MAC ID 0x5244+0x6d6163+0x0d
NarendraSingh 11:77e595130230 35 -------------------------------------------------------------------------------------------------------------------
NarendraSingh 11:77e595130230 36 */
NarendraSingh 11:77e595130230 37
NarendraSingh 11:77e595130230 38 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};
NarendraSingh 20:f812f85cf97e 39 unsigned char SET_VIRTUAL_PACKET_UUID[23] = {0x01,0x01,0x41,0x54,0x01,0x01,0x02,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x0D};
NarendraSingh 20:f812f85cf97e 40 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};
NarendraSingh 11:77e595130230 41 unsigned char SET_MAJOR[7] = {0x41,0x54,0xFF,0xF2,0x00,0x00,0x0D};
NarendraSingh 11:77e595130230 42 unsigned char SET_MINOR[7] = {0x41,0x54,0xFF,0xF3,0x00,0x00,0x0D};
NarendraSingh 11:77e595130230 43 unsigned char SET_MEASURED_POWER[6] = {0x41,0x54,0xFF,0xF4,0x00,0x0D};
NarendraSingh 11:77e595130230 44 unsigned char SET_TRANSMISSION_POWER[6] = {0x41,0x54,0xFF,0xF5,0x00,0x0D};
NarendraSingh 11:77e595130230 45 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
NarendraSingh 11:77e595130230 46 unsigned char SET_BROADCAST_INTERVAL[6] = {0x41,0x54,0xFF,0xF7,0x00,0x0D};
NarendraSingh 11:77e595130230 47 unsigned char SET_IBEACON_NAME[17] = {0x41,0x54,0xFF,0xF9,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D};
NarendraSingh 11:77e595130230 48 unsigned char SOFT_REBOOT[13] = {0x41,0x54,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D};
NarendraSingh 11:77e595130230 49 unsigned char SET_OPEN_BEACON[7] = {0x41,0x54,0x6F,0x70,0x65,0x6E,0x0D};
NarendraSingh 11:77e595130230 50 unsigned char SET_CLOSE_BEACON[8] = {0x41,0x54,0x63,0x6C,0x6F,0x73,0x65,0x0D};
NarendraSingh 11:77e595130230 51 unsigned char READ_BEACON_MAC_ID[6] = {0x52,0x44,0x6D,0x61,0x63,0x0D};
NarendraSingh 11:77e595130230 52
NarendraSingh 20:f812f85cf97e 53 unsigned char SOFT_REBOOT1[15] = {0x01,0x0B,0x41,0x54,0xFF,0xFF,0x6D,0x69,0x6E,0x65,0x77,0x31,0x32,0x33,0x0D};
NarendraSingh 20:f812f85cf97e 54 unsigned char SOFT_REBOOT2[15] = {0x41,0x54,0xFF,0xFF,0x6D,0x69,0x6E,0x65,0x77,0x31,0x32,0x33,0x0D};
NarendraSingh 20:f812f85cf97e 55
NarendraSingh 11:77e595130230 56 void Process_Beacon_Command_Received(unsigned char* Command_Received);
NarendraSingh 11:77e595130230 57 void Change_Beacon_Parameter(unsigned char* Beacon_Parameter_To_Set);
NarendraSingh 11:77e595130230 58 void Get_Beacon_Response(void);
NarendraSingh 11:77e595130230 59 void Read_Beacon_Module_MAC_ID(void);
NarendraSingh 11:77e595130230 60 void Initialize_Beacon_Module(void);
NarendraSingh 11:77e595130230 61
NarendraSingh 11:77e595130230 62 void Process_Beacon_Command_Received(unsigned char* Command_Received)
NarendraSingh 11:77e595130230 63 {
NarendraSingh 11:77e595130230 64 static unsigned char Temp_Pos,Start_Position,End_Position,Data_Length;
NarendraSingh 20:f812f85cf97e 65 Start_Position = 0x02;
NarendraSingh 11:77e595130230 66 Temp_Pos = 0x02;
NarendraSingh 11:77e595130230 67 if((Command_Received[0] == SET_BEACON_UUID_CMD0) && (Command_Received[1] == SET_BEACON_UUID_CMD1)) //Check if command is receievd for setting UUID
NarendraSingh 11:77e595130230 68 {
NarendraSingh 20:f812f85cf97e 69 DEBUG_UART2.printf("Setting UUID");
NarendraSingh 11:77e595130230 70 Data_Length = 16;
NarendraSingh 11:77e595130230 71 End_Position = (Start_Position + Data_Length);
NarendraSingh 11:77e595130230 72 for(;Start_Position < End_Position;Start_Position++)
NarendraSingh 20:f812f85cf97e 73 {
NarendraSingh 11:77e595130230 74 SET_UUID[Start_Position] = Command_Received[Temp_Pos++];
NarendraSingh 20:f812f85cf97e 75 //DEBUG_UART2.putc(SET_UUID[Start_Position]);
NarendraSingh 20:f812f85cf97e 76 //DEBUG_UART2.printf("%s",SET_UUID);
NarendraSingh 20:f812f85cf97e 77
NarendraSingh 20:f812f85cf97e 78
NarendraSingh 20:f812f85cf97e 79 }
NarendraSingh 20:f812f85cf97e 80 Change_Beacon_Parameter(SET_VIRTUAL_PACKET_UUID1);
NarendraSingh 20:f812f85cf97e 81 DEBUG_UART2.printf("UUID Set");
NarendraSingh 11:77e595130230 82 }
NarendraSingh 11:77e595130230 83 else if((Command_Received[0] == SET_SET_MAJOR_CMD0) && (Command_Received[1] == SET_SET_MAJOR_CMD1)) //Check if command is received for Setting Major
NarendraSingh 11:77e595130230 84 {
NarendraSingh 11:77e595130230 85 SET_MAJOR[Start_Position++] = Command_Received[Temp_Pos++]; //MSB
NarendraSingh 11:77e595130230 86 SET_MAJOR[Start_Position] = Command_Received[Temp_Pos]; //LSB
NarendraSingh 11:77e595130230 87 DEBUG_UART2.printf("%s",SET_MAJOR);
NarendraSingh 11:77e595130230 88 Change_Beacon_Parameter(SET_MAJOR);
NarendraSingh 11:77e595130230 89 }
NarendraSingh 11:77e595130230 90 else if((Command_Received[0] == SET_SET_MINOR_CMD0) && (Command_Received[1] == SET_SET_MINOR_CMD1)) //Check if command is received for Setting Minor
NarendraSingh 11:77e595130230 91 {
NarendraSingh 11:77e595130230 92 SET_MINOR[Start_Position++] = Command_Received[Temp_Pos++]; //MSB
NarendraSingh 11:77e595130230 93 SET_MINOR[Start_Position] = Command_Received[Temp_Pos]; //LSB
NarendraSingh 11:77e595130230 94 DEBUG_UART2.printf("%s",SET_MINOR);
NarendraSingh 11:77e595130230 95 Change_Beacon_Parameter(SET_MINOR);
NarendraSingh 11:77e595130230 96 }
NarendraSingh 11:77e595130230 97 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
NarendraSingh 11:77e595130230 98 {
NarendraSingh 11:77e595130230 99 SET_MEASURED_POWER[Start_Position] = Command_Received[Temp_Pos];
NarendraSingh 11:77e595130230 100 DEBUG_UART2.printf("%s",SET_MEASURED_POWER);
NarendraSingh 11:77e595130230 101 Change_Beacon_Parameter(SET_MEASURED_POWER);
NarendraSingh 11:77e595130230 102 }
NarendraSingh 11:77e595130230 103 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
NarendraSingh 11:77e595130230 104 {
NarendraSingh 11:77e595130230 105 SET_TRANSMISSION_POWER[Start_Position] = Command_Received[Temp_Pos];
NarendraSingh 11:77e595130230 106 DEBUG_UART2.printf("%s",SET_TRANSMISSION_POWER);
NarendraSingh 11:77e595130230 107 Change_Beacon_Parameter(SET_TRANSMISSION_POWER);
NarendraSingh 11:77e595130230 108 }
NarendraSingh 11:77e595130230 109 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
NarendraSingh 11:77e595130230 110 {
NarendraSingh 11:77e595130230 111 Data_Length = 8;
NarendraSingh 11:77e595130230 112 End_Position = (Start_Position + Data_Length);
NarendraSingh 11:77e595130230 113 for(;Start_Position < End_Position;Start_Position++)
NarendraSingh 11:77e595130230 114 SET_PAIRING_PASSWORD[Start_Position] = Command_Received[Temp_Pos++];
NarendraSingh 11:77e595130230 115 DEBUG_UART2.printf("%s",SET_PAIRING_PASSWORD);
NarendraSingh 11:77e595130230 116 //Change_Beacon_Parameter(SET_PAIRING_PASSWORD);
NarendraSingh 11:77e595130230 117 Data_Length = 8;
NarendraSingh 11:77e595130230 118 Start_Position = 4;
NarendraSingh 11:77e595130230 119 End_Position = (Start_Position + Data_Length);
NarendraSingh 11:77e595130230 120 //for(Start_Position;Start_Position < End_Position;Start_Position++)
NarendraSingh 11:77e595130230 121 // SET_SOFT_REBOOT[Start_Position] = Command_Received[Temp_Pos++]; //write code to update this password in eeprom
NarendraSingh 11:77e595130230 122 DEBUG_UART2.printf("%s",SOFT_REBOOT);
NarendraSingh 11:77e595130230 123 }
NarendraSingh 11:77e595130230 124 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
NarendraSingh 11:77e595130230 125 {
NarendraSingh 11:77e595130230 126 SET_BROADCAST_INTERVAL[Start_Position] = Command_Received[Temp_Pos];
NarendraSingh 11:77e595130230 127 DEBUG_UART2.printf("%s",SET_BROADCAST_INTERVAL);
NarendraSingh 11:77e595130230 128 Change_Beacon_Parameter(SET_BROADCAST_INTERVAL);
NarendraSingh 11:77e595130230 129 }
NarendraSingh 11:77e595130230 130 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
NarendraSingh 11:77e595130230 131 {
NarendraSingh 11:77e595130230 132 Data_Length = 12;
NarendraSingh 11:77e595130230 133 End_Position = (Start_Position + Data_Length);
NarendraSingh 11:77e595130230 134 for(;Start_Position < End_Position;Start_Position++)
NarendraSingh 11:77e595130230 135 SET_IBEACON_NAME[Start_Position] = Command_Received[Temp_Pos++];
NarendraSingh 11:77e595130230 136 DEBUG_UART2.printf("%s",SET_IBEACON_NAME);
NarendraSingh 11:77e595130230 137 Change_Beacon_Parameter(SET_IBEACON_NAME);
NarendraSingh 11:77e595130230 138 }
NarendraSingh 11:77e595130230 139 else if((Command_Received[0] == SET_START_BEACON_CMD0) && (Command_Received[1] == SET_START_BEACON_CMD0)) //check if command is receievd for starting Beacon
NarendraSingh 11:77e595130230 140 {
NarendraSingh 11:77e595130230 141 DEBUG_UART2.printf("%s",SET_OPEN_BEACON);
NarendraSingh 11:77e595130230 142 Change_Beacon_Parameter(SET_OPEN_BEACON);
NarendraSingh 11:77e595130230 143 }
NarendraSingh 11:77e595130230 144 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
NarendraSingh 11:77e595130230 145 {
NarendraSingh 11:77e595130230 146 DEBUG_UART2.printf("%s",SET_CLOSE_BEACON);
NarendraSingh 11:77e595130230 147 Change_Beacon_Parameter(SET_CLOSE_BEACON);
NarendraSingh 11:77e595130230 148 }
NarendraSingh 11:77e595130230 149 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
NarendraSingh 11:77e595130230 150 {
NarendraSingh 11:77e595130230 151 DEBUG_UART2.printf("%s",SOFT_REBOOT);
NarendraSingh 20:f812f85cf97e 152 Beacon_Module_UART.printf("%s",SOFT_REBOOT2); //Soft Reboot Beacon Module
NarendraSingh 20:f812f85cf97e 153 DEBUG_UART2.printf("Beacon Device Resetted");
NarendraSingh 11:77e595130230 154 }
NarendraSingh 11:77e595130230 155 }
NarendraSingh 11:77e595130230 156
NarendraSingh 11:77e595130230 157 void Change_Beacon_Parameter(unsigned char* Beacon_Parameter_To_Set)
NarendraSingh 11:77e595130230 158 {
NarendraSingh 11:77e595130230 159 Beacon_Module_UART.printf("%s",Beacon_Parameter_To_Set);
NarendraSingh 11:77e595130230 160 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
NarendraSingh 20:f812f85cf97e 161 DEBUG_UART2.printf("%s",Beacon_Parameter_To_Set);
NarendraSingh 11:77e595130230 162 }
NarendraSingh 11:77e595130230 163
NarendraSingh 11:77e595130230 164 void Initialize_Beacon_Module(void)
NarendraSingh 11:77e595130230 165 {
NarendraSingh 11:77e595130230 166 Beacon_Module_UART.baud(9600);
NarendraSingh 11:77e595130230 167 Read_Beacon_Module_MAC_ID();
NarendraSingh 11:77e595130230 168 }
NarendraSingh 11:77e595130230 169
NarendraSingh 11:77e595130230 170 void Read_Beacon_Module_MAC_ID(void)
NarendraSingh 11:77e595130230 171 {
NarendraSingh 11:77e595130230 172 uint8 pos;
NarendraSingh 11:77e595130230 173 Beacon_Module_UART.printf("%s",READ_BEACON_MAC_ID);
NarendraSingh 11:77e595130230 174 Get_Beacon_Response();
NarendraSingh 11:77e595130230 175 for(pos=0;pos<6;pos++)
NarendraSingh 11:77e595130230 176 BLE_Adv_Module_Beacon_ID[pos] = Beacon_RX_Buffer[pos];
NarendraSingh 11:77e595130230 177 }
NarendraSingh 11:77e595130230 178
NarendraSingh 11:77e595130230 179 void Get_Beacon_Response(void)
NarendraSingh 11:77e595130230 180 {
NarendraSingh 11:77e595130230 181 char Beacon_inChar=0;
NarendraSingh 11:77e595130230 182 //Keep reading characters until we get a carriage return
NarendraSingh 11:77e595130230 183 while(Beacon_inChar != '\n')
NarendraSingh 11:77e595130230 184 {
NarendraSingh 11:77e595130230 185 //If a character comes in on the serial port, we need to act on it.
NarendraSingh 11:77e595130230 186 if(Beacon_Module_UART.readable())
NarendraSingh 11:77e595130230 187 {
NarendraSingh 11:77e595130230 188 //Get the new character from the Serial port.
NarendraSingh 11:77e595130230 189 Beacon_inChar = Beacon_Module_UART.getc();
NarendraSingh 11:77e595130230 190 //Start by checking if we've received the end of message character ('\r').
NarendraSingh 11:77e595130230 191 if(Beacon_inChar == '\n')
NarendraSingh 11:77e595130230 192 {
NarendraSingh 11:77e595130230 193 //Put the end of string character on our data string
NarendraSingh 11:77e595130230 194 Beacon_RX_Buffer[Beacon_RxIndex]='\0';
NarendraSingh 11:77e595130230 195 //Reset the buffer index so that the next character go es back at the beginning of the string.
NarendraSingh 11:77e595130230 196 Beacon_RxIndex=0;
NarendraSingh 11:77e595130230 197 }
NarendraSingh 11:77e595130230 198 //If we didn't get the end of message character, just add the new character to the string.
NarendraSingh 11:77e595130230 199 else
NarendraSingh 11:77e595130230 200 {
NarendraSingh 11:77e595130230 201 //Add the new character to the string, and increment the index variable.
NarendraSingh 11:77e595130230 202 Beacon_RX_Buffer[Beacon_RxIndex++]=Beacon_inChar;
NarendraSingh 11:77e595130230 203 }
NarendraSingh 11:77e595130230 204 }
NarendraSingh 11:77e595130230 205 }
NarendraSingh 11:77e595130230 206 }