kumar singh
/
Dealer_20Mar
BLE Transmitter not working
Fork of Dealer_23Feb by
Beacon.cpp@25:0ac2680d594c, 2017-03-03 (annotated)
- Committer:
- NarendraSingh
- Date:
- Fri Mar 03 08:48:39 2017 +0000
- Revision:
- 25:0ac2680d594c
- Parent:
- 22:c2f034a13108
03Mar2017;
Who changed what in which revision?
User | Revision | Line number | New 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 | 25:0ac2680d594c | 38 | unsigned char SET_UUID[23] = {0x01,0x01,0x41,0x54,0xFF,0xF1,0xFD,0xA5,0x06,0x93,0xA4,0xE2,0x4F,0xB1,0xAF,0xCF,0xC6,0xEB,0x07,0x54,0x87,0x02,0x0D}; |
NarendraSingh | 11:77e595130230 | 39 | unsigned char SET_MAJOR[7] = {0x41,0x54,0xFF,0xF2,0x00,0x00,0x0D}; |
NarendraSingh | 11:77e595130230 | 40 | unsigned char SET_MINOR[7] = {0x41,0x54,0xFF,0xF3,0x00,0x00,0x0D}; |
NarendraSingh | 11:77e595130230 | 41 | unsigned char SET_MEASURED_POWER[6] = {0x41,0x54,0xFF,0xF4,0x00,0x0D}; |
NarendraSingh | 11:77e595130230 | 42 | unsigned char SET_TRANSMISSION_POWER[6] = {0x41,0x54,0xFF,0xF5,0x00,0x0D}; |
NarendraSingh | 11:77e595130230 | 43 | 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 | 44 | unsigned char SET_BROADCAST_INTERVAL[6] = {0x41,0x54,0xFF,0xF7,0x00,0x0D}; |
NarendraSingh | 11:77e595130230 | 45 | unsigned char SET_IBEACON_NAME[17] = {0x41,0x54,0xFF,0xF9,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D}; |
NarendraSingh | 25:0ac2680d594c | 46 | unsigned char SOFT_REBOOT[13] = {0x41,0x54,0xFF,0xFF,0x6D,0x69,0x6E,0x65,0x77,0x31,0x32,0x33,0x0D}; |
NarendraSingh | 11:77e595130230 | 47 | unsigned char SET_OPEN_BEACON[7] = {0x41,0x54,0x6F,0x70,0x65,0x6E,0x0D}; |
NarendraSingh | 11:77e595130230 | 48 | unsigned char SET_CLOSE_BEACON[8] = {0x41,0x54,0x63,0x6C,0x6F,0x73,0x65,0x0D}; |
NarendraSingh | 11:77e595130230 | 49 | unsigned char READ_BEACON_MAC_ID[6] = {0x52,0x44,0x6D,0x61,0x63,0x0D}; |
NarendraSingh | 11:77e595130230 | 50 | |
NarendraSingh | 11:77e595130230 | 51 | void Process_Beacon_Command_Received(unsigned char* Command_Received); |
NarendraSingh | 11:77e595130230 | 52 | void Change_Beacon_Parameter(unsigned char* Beacon_Parameter_To_Set); |
NarendraSingh | 11:77e595130230 | 53 | void Get_Beacon_Response(void); |
NarendraSingh | 11:77e595130230 | 54 | void Read_Beacon_Module_MAC_ID(void); |
NarendraSingh | 11:77e595130230 | 55 | void Initialize_Beacon_Module(void); |
NarendraSingh | 11:77e595130230 | 56 | |
NarendraSingh | 11:77e595130230 | 57 | void Process_Beacon_Command_Received(unsigned char* Command_Received) |
NarendraSingh | 11:77e595130230 | 58 | { |
NarendraSingh | 11:77e595130230 | 59 | static unsigned char Temp_Pos,Start_Position,End_Position,Data_Length; |
NarendraSingh | 25:0ac2680d594c | 60 | static unsigned char Beacon_Data[25],j; |
NarendraSingh | 25:0ac2680d594c | 61 | static unsigned char Seq_No; |
NarendraSingh | 20:f812f85cf97e | 62 | Start_Position = 0x02; |
NarendraSingh | 11:77e595130230 | 63 | Temp_Pos = 0x02; |
NarendraSingh | 11:77e595130230 | 64 | 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 | 65 | { |
NarendraSingh | 25:0ac2680d594c | 66 | DEBUG_UART2.printf("Setting UUID"); |
NarendraSingh | 25:0ac2680d594c | 67 | Data_Length = 21; |
NarendraSingh | 11:77e595130230 | 68 | End_Position = (Start_Position + Data_Length); |
NarendraSingh | 25:0ac2680d594c | 69 | for(j=0;Start_Position < End_Position;Start_Position++) |
NarendraSingh | 20:f812f85cf97e | 70 | { |
NarendraSingh | 25:0ac2680d594c | 71 | Beacon_Data[j++] = Command_Received[Start_Position]; |
NarendraSingh | 20:f812f85cf97e | 72 | } |
NarendraSingh | 25:0ac2680d594c | 73 | Beacon_Data[4] = Seq_No++; |
NarendraSingh | 25:0ac2680d594c | 74 | Change_Beacon_Parameter(Beacon_Data,Data_Length); |
NarendraSingh | 25:0ac2680d594c | 75 | wait(.5); |
NarendraSingh | 25:0ac2680d594c | 76 | Change_Beacon_Parameter(SOFT_REBOOT,13); |
NarendraSingh | 11:77e595130230 | 77 | } |
NarendraSingh | 11:77e595130230 | 78 | 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 | 79 | { |
NarendraSingh | 11:77e595130230 | 80 | SET_MAJOR[Start_Position++] = Command_Received[Temp_Pos++]; //MSB |
NarendraSingh | 11:77e595130230 | 81 | SET_MAJOR[Start_Position] = Command_Received[Temp_Pos]; //LSB |
NarendraSingh | 11:77e595130230 | 82 | DEBUG_UART2.printf("%s",SET_MAJOR); |
NarendraSingh | 25:0ac2680d594c | 83 | //Change_Beacon_Parameter(SET_MAJOR); |
NarendraSingh | 11:77e595130230 | 84 | } |
NarendraSingh | 11:77e595130230 | 85 | 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 | 86 | { |
NarendraSingh | 11:77e595130230 | 87 | SET_MINOR[Start_Position++] = Command_Received[Temp_Pos++]; //MSB |
NarendraSingh | 11:77e595130230 | 88 | SET_MINOR[Start_Position] = Command_Received[Temp_Pos]; //LSB |
NarendraSingh | 11:77e595130230 | 89 | DEBUG_UART2.printf("%s",SET_MINOR); |
NarendraSingh | 25:0ac2680d594c | 90 | //Change_Beacon_Parameter(SET_MINOR); |
NarendraSingh | 11:77e595130230 | 91 | } |
NarendraSingh | 11:77e595130230 | 92 | 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 | 93 | { |
NarendraSingh | 11:77e595130230 | 94 | SET_MEASURED_POWER[Start_Position] = Command_Received[Temp_Pos]; |
NarendraSingh | 11:77e595130230 | 95 | DEBUG_UART2.printf("%s",SET_MEASURED_POWER); |
NarendraSingh | 25:0ac2680d594c | 96 | //Change_Beacon_Parameter(SET_MEASURED_POWER); |
NarendraSingh | 11:77e595130230 | 97 | } |
NarendraSingh | 11:77e595130230 | 98 | 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 | 99 | { |
NarendraSingh | 11:77e595130230 | 100 | SET_TRANSMISSION_POWER[Start_Position] = Command_Received[Temp_Pos]; |
NarendraSingh | 11:77e595130230 | 101 | DEBUG_UART2.printf("%s",SET_TRANSMISSION_POWER); |
NarendraSingh | 25:0ac2680d594c | 102 | //Change_Beacon_Parameter(SET_TRANSMISSION_POWER); |
NarendraSingh | 11:77e595130230 | 103 | } |
NarendraSingh | 11:77e595130230 | 104 | 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 | 105 | { |
NarendraSingh | 11:77e595130230 | 106 | Data_Length = 8; |
NarendraSingh | 11:77e595130230 | 107 | End_Position = (Start_Position + Data_Length); |
NarendraSingh | 11:77e595130230 | 108 | for(;Start_Position < End_Position;Start_Position++) |
NarendraSingh | 11:77e595130230 | 109 | SET_PAIRING_PASSWORD[Start_Position] = Command_Received[Temp_Pos++]; |
NarendraSingh | 11:77e595130230 | 110 | DEBUG_UART2.printf("%s",SET_PAIRING_PASSWORD); |
NarendraSingh | 11:77e595130230 | 111 | //Change_Beacon_Parameter(SET_PAIRING_PASSWORD); |
NarendraSingh | 11:77e595130230 | 112 | Data_Length = 8; |
NarendraSingh | 11:77e595130230 | 113 | Start_Position = 4; |
NarendraSingh | 11:77e595130230 | 114 | End_Position = (Start_Position + Data_Length); |
NarendraSingh | 11:77e595130230 | 115 | //for(Start_Position;Start_Position < End_Position;Start_Position++) |
NarendraSingh | 11:77e595130230 | 116 | // SET_SOFT_REBOOT[Start_Position] = Command_Received[Temp_Pos++]; //write code to update this password in eeprom |
NarendraSingh | 11:77e595130230 | 117 | DEBUG_UART2.printf("%s",SOFT_REBOOT); |
NarendraSingh | 11:77e595130230 | 118 | } |
NarendraSingh | 11:77e595130230 | 119 | 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 | 120 | { |
NarendraSingh | 11:77e595130230 | 121 | SET_BROADCAST_INTERVAL[Start_Position] = Command_Received[Temp_Pos]; |
NarendraSingh | 11:77e595130230 | 122 | DEBUG_UART2.printf("%s",SET_BROADCAST_INTERVAL); |
NarendraSingh | 25:0ac2680d594c | 123 | //Change_Beacon_Parameter(SET_BROADCAST_INTERVAL); |
NarendraSingh | 11:77e595130230 | 124 | } |
NarendraSingh | 11:77e595130230 | 125 | 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 | 126 | { |
NarendraSingh | 11:77e595130230 | 127 | Data_Length = 12; |
NarendraSingh | 11:77e595130230 | 128 | End_Position = (Start_Position + Data_Length); |
NarendraSingh | 11:77e595130230 | 129 | for(;Start_Position < End_Position;Start_Position++) |
NarendraSingh | 11:77e595130230 | 130 | SET_IBEACON_NAME[Start_Position] = Command_Received[Temp_Pos++]; |
NarendraSingh | 11:77e595130230 | 131 | DEBUG_UART2.printf("%s",SET_IBEACON_NAME); |
NarendraSingh | 25:0ac2680d594c | 132 | //Change_Beacon_Parameter(SET_IBEACON_NAME); |
NarendraSingh | 11:77e595130230 | 133 | } |
NarendraSingh | 11:77e595130230 | 134 | 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 | 135 | { |
NarendraSingh | 11:77e595130230 | 136 | DEBUG_UART2.printf("%s",SET_OPEN_BEACON); |
NarendraSingh | 25:0ac2680d594c | 137 | //Change_Beacon_Parameter(SET_OPEN_BEACON); |
NarendraSingh | 11:77e595130230 | 138 | } |
NarendraSingh | 11:77e595130230 | 139 | 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 | 140 | { |
NarendraSingh | 11:77e595130230 | 141 | DEBUG_UART2.printf("%s",SET_CLOSE_BEACON); |
NarendraSingh | 25:0ac2680d594c | 142 | //Change_Beacon_Parameter(SET_CLOSE_BEACON); |
NarendraSingh | 11:77e595130230 | 143 | } |
NarendraSingh | 11:77e595130230 | 144 | } |
NarendraSingh | 11:77e595130230 | 145 | |
NarendraSingh | 25:0ac2680d594c | 146 | void Change_Beacon_Parameter(unsigned char* Beacon_Parameter_To_Set,unsigned char Length) |
NarendraSingh | 11:77e595130230 | 147 | { |
NarendraSingh | 21:a5fb0ae94dc6 | 148 | uint8 i; |
NarendraSingh | 25:0ac2680d594c | 149 | for(i = 0;i<Length;i++) |
NarendraSingh | 21:a5fb0ae94dc6 | 150 | { |
NarendraSingh | 21:a5fb0ae94dc6 | 151 | Beacon_Module_UART.putc(Beacon_Parameter_To_Set[i]); |
NarendraSingh | 25:0ac2680d594c | 152 | DEBUG_UART2.printf("%2x",Beacon_Parameter_To_Set[i]); |
NarendraSingh | 21:a5fb0ae94dc6 | 153 | } |
NarendraSingh | 11:77e595130230 | 154 | } |
NarendraSingh | 11:77e595130230 | 155 | |
NarendraSingh | 11:77e595130230 | 156 | void Initialize_Beacon_Module(void) |
NarendraSingh | 11:77e595130230 | 157 | { |
NarendraSingh | 21:a5fb0ae94dc6 | 158 | Beacon_Module_UART.baud(9600); //set Beacon transmitter uart baud rate to default 9600 |
NarendraSingh | 22:c2f034a13108 | 159 | //Read_Beacon_Module_MAC_ID(); |
NarendraSingh | 11:77e595130230 | 160 | } |
NarendraSingh | 11:77e595130230 | 161 | |
NarendraSingh | 11:77e595130230 | 162 | void Read_Beacon_Module_MAC_ID(void) |
NarendraSingh | 11:77e595130230 | 163 | { |
NarendraSingh | 11:77e595130230 | 164 | uint8 pos; |
NarendraSingh | 11:77e595130230 | 165 | Beacon_Module_UART.printf("%s",READ_BEACON_MAC_ID); |
NarendraSingh | 11:77e595130230 | 166 | Get_Beacon_Response(); |
NarendraSingh | 11:77e595130230 | 167 | for(pos=0;pos<6;pos++) |
NarendraSingh | 11:77e595130230 | 168 | BLE_Adv_Module_Beacon_ID[pos] = Beacon_RX_Buffer[pos]; |
NarendraSingh | 11:77e595130230 | 169 | } |
NarendraSingh | 11:77e595130230 | 170 | |
NarendraSingh | 11:77e595130230 | 171 | void Get_Beacon_Response(void) |
NarendraSingh | 11:77e595130230 | 172 | { |
NarendraSingh | 11:77e595130230 | 173 | char Beacon_inChar=0; |
NarendraSingh | 11:77e595130230 | 174 | //Keep reading characters until we get a carriage return |
NarendraSingh | 11:77e595130230 | 175 | while(Beacon_inChar != '\n') |
NarendraSingh | 11:77e595130230 | 176 | { |
NarendraSingh | 11:77e595130230 | 177 | //If a character comes in on the serial port, we need to act on it. |
NarendraSingh | 11:77e595130230 | 178 | if(Beacon_Module_UART.readable()) |
NarendraSingh | 11:77e595130230 | 179 | { |
NarendraSingh | 11:77e595130230 | 180 | //Get the new character from the Serial port. |
NarendraSingh | 11:77e595130230 | 181 | Beacon_inChar = Beacon_Module_UART.getc(); |
NarendraSingh | 11:77e595130230 | 182 | //Start by checking if we've received the end of message character ('\r'). |
NarendraSingh | 11:77e595130230 | 183 | if(Beacon_inChar == '\n') |
NarendraSingh | 11:77e595130230 | 184 | { |
NarendraSingh | 11:77e595130230 | 185 | //Put the end of string character on our data string |
NarendraSingh | 11:77e595130230 | 186 | Beacon_RX_Buffer[Beacon_RxIndex]='\0'; |
NarendraSingh | 11:77e595130230 | 187 | //Reset the buffer index so that the next character go es back at the beginning of the string. |
NarendraSingh | 11:77e595130230 | 188 | Beacon_RxIndex=0; |
NarendraSingh | 11:77e595130230 | 189 | } |
NarendraSingh | 11:77e595130230 | 190 | //If we didn't get the end of message character, just add the new character to the string. |
NarendraSingh | 11:77e595130230 | 191 | else |
NarendraSingh | 11:77e595130230 | 192 | { |
NarendraSingh | 11:77e595130230 | 193 | //Add the new character to the string, and increment the index variable. |
NarendraSingh | 11:77e595130230 | 194 | Beacon_RX_Buffer[Beacon_RxIndex++]=Beacon_inChar; |
NarendraSingh | 11:77e595130230 | 195 | } |
NarendraSingh | 11:77e595130230 | 196 | } |
NarendraSingh | 11:77e595130230 | 197 | } |
NarendraSingh | 11:77e595130230 | 198 | } |