Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HardwareSerial & BTserial = Serial1; //SETTING SERIAL1
- // BTrx --> 18tx1 sending data to BT orange wire
- // BTtx --> 19rx1 recieving data from BT green wire
- int BTtest;
- char c = ' ';
- char lineBuffer[1000];
- char subBuffer[30];
- unsigned int index = 0;
- unsigned int index2 = 0;
- byte BTtotal;
- byte BTresult = 0;
- String send = "";
- void setup()
- {
- // ////BT PINS /////////////
- pinMode(8, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
- digitalWrite(8, HIGH); // SET this to HIGH, unplug ppower, unplug USB > plug USB > plug power, to enter AT mode
- pinMode(45, OUTPUT); // give power to BT HC-05
- digitalWrite(45, HIGH); // give power to BT HC-05
- Serial.begin(9600);
- // setting up mega serial
- BTserial.begin(38400); // HC-05 default serial speed for AT mode is 38400
- // Wait for hardware to initialize
- delay(1000);
- // ////Starting BT /////////////
- initHC05ToInq(); // Set correct states for inq
- initMessage();
- BTserial.println("AT+INQ");
- // ////Starting BT /////////////
- }
- void delayAndRead()
- {
- delay(50);
- while (BTserial.available())
- {
- char c = BTserial.read();
- Serial.print(c);
- }
- delay(800);
- }
- void initHC05ToInq()
- {
- Serial.println(F("Starting BT module..."));
- BTserial.println("AT+CMODE=1"); // Enable connect to any device
- delayAndRead();
- BTserial.println("AT+ROLE=1"); // Set to master in order to enable scanning
- delayAndRead();
- BTserial.println("AT+INQM=1,100,12"); // RSSI, Max 10 devices, ~30s
- delayAndRead();
- BTserial.println("AT+CLASS=0"); // Disable COD filter
- delayAndRead();
- BTserial.println("AT+INIT"); // Init.
- delayAndRead();
- }
- Voind loop() {
- BTtotal = updateBTmodule();
- }
- void initMessage()
- {
- //send = "Bluetooth1;{\"state\":{\"reported\":{\"devices\":[";
- send = "B";
- }
- int updateBTmodule() {
- // Keep reading from HC-05 and send to Arduino Serial Monitor
- if (BTserial.available())
- {
- // Read character and append into buffer
- c = BTserial.read();
- lineBuffer[index] = c;
- index++;
- // When line ends
- if (c == '\n')
- {
- // Remove line end characters from buffer
- lineBuffer[index - 1] = 0; // \r\n
- // Reset buffer index for next line
- index = 0;
- if (lineBuffer[0] == 'O' && lineBuffer[1] == 'K')
- {
- // Finish message
- //send += "]";
- // DEBUG / TODO actually send this message
- //if (BTresult > 0)
- //{
- //Serial.println(BTresult);
- //Serial.println(send);
- //}
- //else
- //{
- //Serial.println(BTresult);
- //Serial.println(send);
- //}
- // Restart INQ
- BTserial.println("AT+INQ");
- BTresult = 0;
- initMessage();
- }
- else
- {
- capture = false;
- index2 = 0;
- for (index = 0; index < 30; index++)
- {
- if (!capture)
- {
- if (lineBuffer[index] == ':')
- {
- capture = true;
- }
- }
- else
- {
- subBuffer[index2] = lineBuffer[index];
- if (lineBuffer[index] == ',')
- {
- subBuffer[index2] = 0;
- break;
- }
- index2++;
- }
- }
- index = 0;
- // Add this line buffer
- String str((char *) subBuffer);
- if (send.indexOf(str) <= 0)
- {
- // If not first then add comma
- // if (BTresult > 0)
- //{
- // send += ",";
- //}
- //send += "\"";
- send += str;
- // send += "\"";
- // Keep count
- BTresult++;
- }
- }
- }
- }
- return BTresult;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement