Configuring the cheap bluetooth module from ebay is more difficult than appears at first, especially since datasheets provide conflicting instructions for programming the chip. I found useful another blog post, although they do not provide explicit Arduino code for configuring the bluetooth module.
Arduino Mega 2560 | JY-MCU |
---|---|
TX1 (18) | RXD |
RX1 (19) | TXD |
+5V | VCC |
GND | GND |
Code for Arduino
int led = 13;
/* used help from http://byron76.blogspot.com/2011/09/one-board-several-firmwares.html */
void setup() {
pinMode(led, OUTPUT);
Serial.begin(115200);
Serial1.begin(115200);//use 9600 if first set up
}
void loop() {
digitalWrite(led, HIGH);//sanity check
command("AT",2);// response: OK
command("AT+VERSION",12);// response: OKlinvorV1.5
command("AT+NAMEArduino",9);//response: OKsetname
command("AT+BAUD8",8);//response: OK115200
while(1);//stop execution
}
void command(const char* cmd, int num_bytes_response) {
delay(1000);
Serial1.print(cmd);
delay(1500);
for (int i=0;i<num_bytes_response;i++)
Serial.write(Serial1.read());
}
Edit 2013-08-21: Fixed a typo in the code (character "<" not displayed properly)
Compile and upload the above program to the Arduino. Open the serial monitor (at 115200 baud rate) to check whether the module responds properly. Do not pair to the bluetooth module until after configuration is complete. Without the delay commands, it appears that the bluetooth chip cannot keep up. This code can be easily expanded to send other commands to the module as well.
Thank you for this code. But "lt" generates an error.
ReplyDeleteWhat kind of error are you getting?
ReplyDeleteSorry, i got no Reply on Mail. The Error was myself. Is this a permanent adjustment?
DeleteI'm not 100% clear, were you able to get this to work successfully? The parameters (name and baud rate in this case) are changed permanently, and will remain if the device is unpowered.
Deleteerror: Serial1 was not declared in scope. aparece essa mensagem, o que fazer?
ReplyDeleteSerial1 is for Arduino Mega only. See more detailed description here:
Deletehttp://arduino.cc/en/Serial/Begin. It seems that you are using a different board, or did not specify the correct board during compilation. If you only have 1 serial on your board, you can try to use "SoftwareSerial" library http://arduino.cc/en/Reference/SoftwareSerial as a replacement for Serial1.
When reporting errors, please include line numbers so that it's obvious where they are coming from.
Now it works fine. You have to add 2 lines.
ReplyDeletepinMode(40, OUTPUT);
digitalWrite(40, HIGH);
PIN 40 is connect to KEY. After the setting, disconnect PIN 40.
Can you tell me the second Parameters. "command (" AT + PIN1956, "?);
Glad it's working for you. The second argument to the "command" function is the number of bytes you expect to receive in response. You can just set it to a large number if you are not sure, count how many bytes you receive, then change program accordingly. Also, no spaces. You can try:
Delete----------
command("AT+PIN1956",8);
----------
It seems that the response should be "OKsetPIN".
Why do i need to set it to 115200 baud rate?
ReplyDeleteIt depends on your application. For situations where data comes in quickly and needs to be sent to the PC real-time, serial port speed makes a difference.
DeleteCasino Review & Promo Codes (2021) | JTM Hub
ReplyDeleteRead our detailed 부천 출장안마 review of Casino.Casino. Details. Security, Games, Deposit Methods, Withdrawal 군산 출장안마 Methods, and Security.Casino Games: 400+Deposit 성남 출장샵 Methods: Bank Transfer, Pulsa, Skrill, EcoPayz, PayzWithdrawal Methods: 대전광역 출장안마 Neosurf, Paypal, eCheck, 계룡 출장샵 ApMinimum Deposit: C$10