A battery powered (2 x AA) Arduino LilyPad should switch a BlueSmirf v2.11 Bluetooth modem to/from command mode (see source code below). The BlueSmirf has been set to 9600 baud.
If the PC connects via Bluetooth (see source code below), the Arduino program runs fine at the beginning (sending multiple "ping\n"). After some time it (LilyPad/BlueSmirf) starts to also send "$$$" and "---\n" over the Bluetooth connection instead of switching to/from command mode.
Any ideas?
Regards,
tamberg
// Arduino source code:
void setup () {
Serial.begin(9600);
}
void loop () {
Serial.print("$$$");
delay(2000); // TODO: Inquiry, etc.
Serial.print("---\n");
delay(100);
Serial.print("ping\n");
delay(2000);
}
// C# source code (runs on PC)
using System;
using System.IO.Ports;
class Program {
static void Main () {
SerialPort p = new SerialPort(
"COM20", 9600, Parity.None, 8, StopBits.One);
using (p) {
p.Open();
while (p.IsOpen) {
Console.Write((char) p.ReadChar());
}
}
}
}
From the datasheet, page 6:
NOTE1 : You can enter command mode
locally over the serial port at any
time when not connected. Once a
connection is made, you can only enter
command mode if the config timer has
not expired. To enable continuous
configuration, set the config timer to
255. Also, if the device is in Auto Master mode 3, you will NOT be able to
enter command mode when connected over
Bluetooth.
My guess would be that the config timer is expiring.
Related
I am able to connect a Raspberry Pi 4 an and Arduino Nano BLE through bluepy for Rpi4 and ArduinoBLE.h for Arduino Nano BLE. Unfortunately when I try to write from Rpi4 to Arduino Nano BLE, I'm not seeing the expected output for Read and Write. I don't see any perfect example for Arduino Nano BLE since it is recently released hardware with built-in BLE. It would be very helpful if anyone could help me achieve communication between them. Thanks in advance. Below is my code for Raspberry Pi.
import bluepy.btle as btle
p = btle.Peripheral("de:fc:54:87:b0:04")
services=p.getServices()
s = p.getServiceByUUID(list(services)[2].uuid)
c = s.getCharacteristics()[0]
c.write(bytes("2", "utf-8"))
p.disconnect()
And I'm using the Arduino built-in example from the Arduino Nano BLE Library.
#include <ArduinoBLE.h>
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int ledPin = LED_BUILTIN; // pin to use for the LED
void setup() {
Serial.begin(9600);
while (!Serial);
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("LED");
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
// set the initial value for the characeristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE LED Peripheral");
}
void loop() {
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
//prints the centrals MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
I figured out myself, it was the value in the write which was going wrong all this time. Below is the right one. I hope now you can find this as a perfect solution to connect raspberry Pi 4 and Arduino Nano BLE wirelessly Via Bluetooth.
c.write(bytes("0001".encode())
I am trying to communicate with the HC-05 Bluetooth module for quite long time now, but with no success.
I am using the Nodemcu(V3) ESP8266 module.
I connect the HC-05 to Nodemcu in following sequence:
HC-05 Nodemcu
----- -----------
RX --> Pin 1 (Tx)
TX --> Pin 3 (RX)
Vcc --> +3.3V
GND --> GND
For starters, i want to check if my Nodemcu is communicating properly with my HC-05 module.
I have written the following code to read the response of AT commands:
#include <SoftwareSerial.h>
SoftwareSerial BTserial(3, 1); // RX | TX
char Bluetooth_Name = ' ';
void setup()
{
// Arduino IDE serial monitor
Serial.begin(115200);
// HC-05 default serial speed for AT mode is 38400
BTserial.begin(38400);
// Wait for hardware to initialize
delay(1000);
// Print debug string
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTserial.available())
{
reading = BTserial.read();
Serial.println(reading);
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
reading = Serial.read();
BTserial.write(reading);
}
}
However, i do not get response for any AT command at all. The serial monitor just shows blank.
Thanks in advance
EDIT:- I connected the "EN" pin on the HC-05 to Vcc. Noe, the led on the HC-05 blinks slowly, which means that the HC-05 is configured in command mode. However, i am still not able to receive the response for any AT commands. I have also selected "Both NL & CR" in the serial monitor, configured the baud rate correctly and double-checked the hardware connections.
Everything seems to be correct except that i don't get response for the AT commands.
Please help!!!
I changed the line
SoftwareSerial BTserial(3, 1); // RX, TX
to
SoftwareSerial BTserial(D4, D3); // RX, TX
And got it to work!
I have been trying to establish connection between two HC-06 Bluetooth modules. Pairing has been done. The two modules are communicating. My aim is to send a letter from one module and receive acknowledgment from the other module. The code for the master module is below.
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2,3); // RX, TX
char c;
char s[]="Matched";
int t[]="NotMatched";
void setup()
{
// start the serial communication with the computer
Serial.begin(9600);
Serial.println("Arduino with HC-06 is ready");
// start communication with the HC-06 using 38400
BTserial.begin(38400);
Serial.println("Bluetooth serial started at 38400");
}
void loop()
{
// Read from HC-06 and send to Arduino Serial Monitor
if (BTserial.available())
{
c=(BTserial.read());
if (c=='a')
{
Serial.write(s);
}
else
{
Serial.write(t);
}
}
// Read from Arduino Serial Monitor and send to HC-06
if (Serial.available())
{
c = Serial.read();
Serial.write(c);
BTserial.write(c);
}
}
Similar code is used for the slave module. Except for the 'else' part in the code everything runs right. I receive acknowledgement along with the else portion being printed twice for both the if and else portion of the code i.e 'matched not matched not matched' is printed when it receives char 'a' and 'not matched not matched not matched' is printed when it receives anything other than 'a' . Can you please give me suggestions on what could be wrong.
Have you printed the characters that you are reading?
I had a similar problem and I found that the Bluetooth was receiving garbage characters. The reason was because the standard baudrate, of HC-06 modules, is 9600, so if you change:
BTserial.begin(38400);
to
BTserial.begin(9600);
In both master and slave, it might work.
I am trying out Arduino Uno + HC-05 using this library. I don't think it is working properly. This is the Serial Monitor's output of the example "echo":
findBaud
Trying 4800... x
Trying 9600... x
Trying 19200... x
Trying 38400... x
Trying 57600... x
Trying 115200... x
No connection
No Connection, waiting...OK
None of the Communication speed works, but I manage to connect my Android phone (w/ Bluetooth Terminal) to the HC-05, which is why you see "OK" at the end of the output. But it is not able to echo my input from the Bluetooth Terminal.
The Arduino code:
#include <Arduino.h>
#include "HC05.h"
#include <SoftwareSerial.h>
HC05 btSerial = HC05(A2, A5, A3, A4); // cmd, state, rx, tx
void setup()
{
DEBUG_BEGIN(57600);
btSerial.findBaud();
}
void loop()
{
btSerial.println("Echo Server- type something");
while (btSerial.connected())
{
if (btSerial.available())
{
btSerial.write(btSerial.read());
}
}
}
How I connect the HC-05 to Arduino:
I just follow the instruction(5V and GND; State, Rx and Tx to A5, A3 and A4 respectively) , except I don't have pin "cmd", but I do have pin "CFG", so I just assume that should be cmd and connect it to A2
(I know I should comment instead of writting an answer, but I need 50 reputation)
Same here.
I tried with and without the lib and with different pins. I also tried an arduino micro and uno.
Always the same: the module's LED blink (when I send data trough RX/TX it seems) but I can't have any answer (neither when switching to command mode with "$$$" and neither with AT commands with KEY pin - on my module it is named EN, maybe for enable, I assume it is the same)
EDIT:
I should have RTFM. Especially this.
Here are my findings :
The bluetooth module is called HC-05 and the whole board I have is ZS-040.
The EN pin is used to switch off the module, if set to LOW
The small button switch can be pushed to enter command mode (I don't have a KEY pin)
Speed is 9600 bauds by default and stay the same when entering command mode
To debug your connection you can use the Android app called BlueSerial
For the record, here is my code :
#include <SoftwareSerial.h>
#define HC05_STATE 5
#define HC05_RXD_ARDUINO_TXD 4
#define HC05_TXD_ARDUINO_RXD 3
#define HC05_EN 2
SoftwareSerial BTSerial(HC05_TXD_ARDUINO_RXD, HC05_RXD_ARDUINO_TXD); // RX | TX
void setup(void)
{
pinMode(HC05_EN, OUTPUT);
digitalWrite(HC05_EN, HIGH); // just to be sure to enable the module, if not plugged it still works
Serial.begin(9600);
BTSerial.begin(9600); // default speed
Serial.println("Ready");
}
void loop(void)
{
if (BTSerial.available())
{
Serial.print("< ");
while (BTSerial.available())
Serial.write(BTSerial.read());
Serial.print("\n");
}
if (Serial.available())
{
Serial.print("> ");
while (Serial.available())
{
char c = Serial.read();
BTSerial.write(c);
Serial.write(c);
}
Serial.print("\n");
}
}
Here's my problem. I have an Arduino Mega 2560 connected with USB on my pc (windows 7).On the arduino i have connected a bluetooth device HC-06. I upload the following the program to my arduino:
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial Genotronex(14, 15); // RX, TX
int BluetoothData; // the data given from Computer
void setup() {
// put your setup code here, to run once:
Genotronex.begin(9600);
}
void loop() {
BluetoothData=Genotronex.read(); //read incoming data
Genotronex.println(BluetoothData); //print data received from bluetooth
delay(100);// prepare for next data ...
}
I successfully connect my arduino with the bluetooth. Next i use putty and connect to the bluetooth but the problem is that it prints "-1" meaning that the incoming data to the bluetooth is "-1" although i do not send any data from any other progamm. I also tried to send other data from putty but it didn't work. Thanks and sorry for my english.
Try something like that to be sure that you received some data before send them to computer
void loop()
{
if (Genotronex.available())
{
BluetoothData=Genotronex.read();
Genotronex.write(BluetoothData);
}
delay(100);
}
And Have you check the HC-06 configuration ?
Here
and Here