I've been trying to connect my arduino to a gopro hero 11, which should support BLE communication. The ArduinoBLE example "PeripheralExplorer" finds it and lists all the BLE services contained, roughly a dozen.
However, when trying to connect to a specific Service UUID, it fails and when I use advertisedServiceUuidCount(), it returns 1. Is there something I should do to switch the advertised Service from the Gopro? Or am I doing something wrong to access all of the GoPro's services?
Also, other minor question : should I advertise with my arduino as a client?
here's the BLE part of the code I tried :
`
while (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
}
BLEDevice peripheral;
do{
BLE.scan();
delay(500);
peripheral = BLE.available();
}while (peripheral.localName()!="GoPro 3167");
Serial.println("trouvé le truc");
BLE.stopScan();
// if(peripheral.hasService(WifiServiceUUID)) Serial.println("ah?");
Serial.println(peripheral.advertisedServiceUuidCount());
`
thanks!
Related
I am making BLE echo system between Arduino and Android.
I have finished to make read and write function. But I want Android can automatically detect value when BluetoothGattCharacteristic value change. So I made set notification on Android.
When I tried to check the fucntion is working properly or not, It was not working properly. And I use nRFConnect App to check what is the problem. the problem is the status of descriptor on Arduino is Notification and indications disabled. And I googled how to enable notification and indication on Arduino. But on the Arduino's document, I cannot find the function which can make enable descriptor's notification and indication. this is the document link. So is it possible to enable notification and indication on Arduino BLE??
Code of Arduino nano 33 IOT.
#include <ArduinoBLE.h>
BLEService echoService("00000000-0000-1000-8000-00805f9b34fb");
BLEStringCharacteristic charac ("741c12b9-e13c-4992-8a5e-fce46dec0bff", BLERead | BLEWrite | BLENotify,40);
BLEDescriptor Descriptor("beca6057-955c-4f8a-e1e3-56a1633f04b1","Descriptor");
String var = "";
void setup(){
Serial.begin(9600);
while(!Serial);
if(!BLE.begin()){
Serial.println("starting BLE failed.");
while(1);
}
BLE.setLocalName("Arduino BLE Echo");
BLE.setAdvertisedService(echoService);
charac.addDescriptor(Descriptor);
echoService.addCharacteristic(charac);
BLE.addService(echoService);
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
Serial.println(" ");
}
void loop(){
BLEDevice central = BLE.central();
if(central){
Serial.println("* Connected to central device!");
Serial.print("Connected to central : ");
Serial.println(central.address());
Serial.println(" ");
while(central.connected()){
if(charac.written()){
var = charac.value();
Serial.println(String(var));
delay(500);
charac.writeValue(var);
Serial.println("write stringCharacteristic");
}
}
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
picture of nRFConnection APP.
You need to enable the notifications from the Android side in order to receive the BLE notifications. This is because according to the BLE specification, BLE notifications are disabled by default. If you want BLE notifications to remain enabled after the first time you enable them, you can bond with the Arduino device. This way, the status of the notification will persist across power cycles and disconnection/reconnection, and as soon as you reconnect to the Arduino device, notifications will be enabled by default (but again, you still have to enable them the first time).
This can be seen in the paragraph below (Bluetooth Specification v5.3, Vol 3, Part G, Section 3.3.3.3 - Clinet Characteristic Configuration - Page 1489):-
"The Client Characteristic Configuration declaration is an optional
characteristic descriptor that defines how the characteristic may be
configured by a specific client. The Client Characteristic
Configuration descriptor value shall be persistent across connections
for bonded devices. The Client Characteristic Configuration descriptor
value shall be set to the default value at each connection with
non-bonded devices.The characteristic descriptor value is a bit
field. When a bit is set, that action shall be enabled, otherwise it
will not be used. The Client Characteristic Configuration descriptor
may occur in any position within the characteristic definition after
the Characteristic Value. Only one Client Characteristic Configuration
declaration shall exist in a characteristic definition.
The default value for the Client Characteristic Configuration descriptor value shall be 0x0000."
Note that the Client Characteristic Descriptor (CCCD) is the attribute in the characteristic that handles notification/indications. You can read more about this here:-
Bluetooth Low Energy: A Primer
Bluetooth GATT
Bluetooth Low Energy Characteristics Tutorial
As an introduction, I bought myself an arduino and a few modules to learn some software stuff. The project is to eventually connect to a bluetooth OBD2 reader on my car to display real time data on a small LCD.
The problem
I am either not able to connect to, or not write to, my HC05 module via software serial. I think I have narrowed this down to a couple possibilities.
I am unable to connect to the module in the first place.
I have a Mega 2560 and HC05.
5V <-> VCC
GND <-> GND
D2 <-> RXD
D3 <-> TXD
Note that I have seen 9600 and 38400 baud rates for connecting but neither worked, so I made this function to try them all for me...
//set up serial relay into HC05.
SoftwareSerial hc05(2,3);
//computer serial baud rate 115200
bool hc05_connect() {
long baud_list[] = {300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400};
Serial.println("Attempting to connect to HC05 bluetooth module...");
bool success = 0;
for (int i=0; i<(sizeof(baud_list) / sizeof(baud_list[0])); i++) {
Serial.print("Baud rate ");
Serial.print(baud_list[i]);
Serial.print("...");
hc05.begin(baud_list[i]);
hc05.write("AT");
delay(1000);
if (hc05.available()) {
Serial.println(" successful!");
success = 1;
return success;
} else {
Serial.println(" failed");
}
}
return success;
}
Notes:
This has always returned failed for every baud rate.
I have the bluetooth module in command mode, initiated by pressing the button as I supply power.
I have tried unplugging the TX/RX pins while uploading the sketch. No difference noted.
My attempts to send commands to the HC05 are failing.
Below is my function for sending commands to the module.
void loop() {
// listen for communication from the ESP8266 and then write it to the serial monitor
if (hc05.available()) {
Serial.write(hc05.read());
}
// listen for user input and send it to the ESP8266
if (Serial.available() > 0) {
Serial.println("Writing to hc05...");
Serial.println(Serial.available());
Serial.println(Serial.read());
hc05.write(Serial.read());
}
}
I have added in a few lines which write back to Serial so I can see what's being sent, and the monitor returns weird stuff. For example, if I send "AT", this is what the monitor reads:
Writing to hc05...
3
65
Writing to hc05...
1
10
Notes:
Why is it sending 2 different items?
Why is it sending integers rather than the characters I said?
Does this indicate I'm just sending it nonsense commands so it's not responding?
I can provide full code if you want, this is already a huge textwall though. Please help me!
Edit
So I have been able to get communication two ways via the bluetooth module using a modified version of the code in this instructable: https://www.instructables.com/How-to-Set-Up-and-Test-Arduino-Bluetooth-Connectio/
I was able to send from PC only and not receive to an android bluetooth terminal using SoftwareSerial with HC05's RX - TX0 / TX - RX0.
And I was able to receive to PC and not send using hardware serial / Serial1 with HC05's RX - TX1 / TX - RX1.
So now I have RX - TX0 / TX - RX1. It seems to communicate through terminal like this.
void setup() {
Serial.begin(9600); //open the serial port
Serial1.begin(9600);
}
void loop() {
if (Serial1.available()) {
Serial.print("(Received)");
Serial.println(Serial1.readString()); // send from serial to bluetooth
}
if (Serial.available()) {
Serial.print("(Sent)");
Serial.println(Serial.readString());
Serial1.println(Serial.readString()); // send from bluetooth to serial
}
}
But if I apply this to my code, I still can't get it to work.
Before I try to hack this together, why am I getting serial to work across 2 different serial channels? Weird...
Okay, so I figured it out. (I can't say I fully understand, but maybe this will help people in future.)
1. Unable to connect to module
Thanks #ukBaz for suggesting I connect with the terminal app on my phone, this allowed me to debug the connection to the module in the first place. and #Juraj for suggesting that the Mega uses hardware serial.
Serial1 apparently is broken on my board, so I am using Serial3. I bluetoothed to the device with my phone, and was able to send commands back and forth between Serial and Serial3 both on 9600 baud rate. Here is the code I used:
void setup() {
Serial.begin(9600); //open the serial port to PC
Serial3.begin(9600); //open serial port to HC05. TX -> 15, RX -> 14
}
void loop() {
if(Serial3.available()){
Serial.print(Serial3.readString()); // send from serial to bluetooth
}
if(Serial.available()){
Serial3.print(Serial.readString()); // send from bluetooth to serial
}
}
I suspect I was using the wrong read/readString and write/print/println for my purpose initially.
2. Unable to issue commands to the module
Once I got that working, I changed the baud rate to 38400, and tied the STATE pin of the module to VCC (rather than using the button). Uploaded code, disconnected 5V, reconnected 5V, reset arduino.
At that point, I could issue "AT" to the module via Serial Monitor, and receive "OK" back. Woohoo!
I think I understand now that #hlovdal was suggesting that I was issuing a command to the module and never parsing a response I got, so it was.. clogged parhaps? In any case. I can now successfully issue commands and receive responses from the module.
Thanks everyone for your help.
There are some problems with how you are communicating AT command lines to your device. For instance:
hc05.write("AT");
delay(1000);
You do not send an AT command to a modem, you send an AT command line that contains zero or more AT commands, followed by a command line terminating character (that always should be '\r', aka carriage return (or <CR>)).
You are missing that terminating character here, so the modem will never send you a reply, because you have not sent it a command line.
And also, you should never, ever use delay as a substitute for reading and parsing the responses that the modem sends back. See this answer for some more details.
So I recently bought the Arduino Wifi Shield 101. I went through the getting started steps, and just copied and pasted their "scan for networks" code. The code that I used is down below. So to help you guys help me, I should say my school's wifi is wpa2 encrypted. However, the code is simply looking for possible networks to connect to. The code runs fine until it gets to the "Wifi.macAddress(mac)" line. I don't understand why the code stops working here. There aren't any errors in compiling or uploading, the code just seems to not be working. Im obviously a beginner with the arduino wifi board, so any help at all would be great.
Here's the code:
`#include <SPI.h>
#include <WiFi101.h>
void setup() {
// initialize serial and wait for the port to open:
Serial.begin(9600);
while(!Serial);
// attempt to connect using WEP encryption:
Serial.println("Initializing Wifi...");
printMacAddress();
// scan for existing networks:
Serial.println("Scanning available networks...");
listNetworks();
}
void loop() {
delay(10000);
// scan for existing networks:
Serial.println("Scanning available networks...");
listNetworks();
}
void printMacAddress() {
// the MAC address of your Wifi shield
byte mac[6];
// print your MAC address:
Serial.print("The code got to here");
WiFi.macAddress(mac); //why won't this method work?
Serial.print("The code never reaches this point ... Why?!?!?");
Serial.print("MAC: ");
Serial.print(mac[5],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
}
void listNetworks() {
// scan for nearby networks:
Serial.println("** Scan Networks **");
byte numSsid = WiFi.scanNetworks();
// print the list of networks seen:
Serial.print("number of available networks:");
Serial.println(numSsid);
// print the network number and name for each network found:
for (int thisNet = 0; thisNet<numSsid; thisNet++) {
Serial.print(thisNet);
Serial.print(") ");
Serial.print(WiFi.SSID(thisNet));
Serial.print("\tSignal: ");
Serial.print(WiFi.RSSI(thisNet));
Serial.print(" dBm");
Serial.print("\tEncryption: ");
Serial.println(WiFi.encryptionType(thisNet));
}
}`
Are you powering the boards via USB? If so, you're likely experiencing an under-power issue.
My original answer was deleted (so I don't know what you can and cannot see), but the link to the Arduino bug I filed is here:
Arduino 101 + Wifi 101 Shield board freeze. #50
As it turns out, my board was simply under-powered as I was using a USB port rather than a wall outlet. I actually ended up using a different USB port and the scanNetworks example now works for me.
EDIT Actually, as it turns out, it was the USB cable. Either way, power was the issue.
I would recommend powering the board via a wall wart or choosing a different USB port and trying again.
I have a USB 2 Serial adapter and the device is working fine. I see the device perfectly configured in my System.
I have connected the TX0 pin of Arduino to DB 2 pin (read pin) of the adapter. Below is my Arduino code:
int i = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
for(i=0;i<6;i++)
{
Serial.write('H');
delay(100);
}
}
But when I try to read thru terminal software of data received at my COM port I see some junk character incoming. I am pretty sure that I am using same baud rate / flow setting both side. Why am I facing this issue - do I need to connect any other pins also as I just need to receive data at system side?
You should connect the ground pin with the ground pin
Arduino Communicate with Ur computer using pins 0 and 1( Tx0 and Rx0)
you shouldn't be connected to the Tx0 pin to another serial device because arduino use it to communicate with computer.
if you are using Uno. Check for Software Serial. and two grounds should be connected.
I have an arduino GSM shield sitting on top of an arduino uno. I have the code below. The shield just shows me it is connecting but it never shows me it is connected.I want to know why it is not connecting.I will be grateful for any help.Am using arduino 1.0.5 IDE.
#include <GSM.h>
#define PINNUMBER ""
GSM gsmAccess(true);
GSM_SMS sms;
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
char code = 'X';
while(true) {
Serial.println("try Access");
code=gsmAccess.begin("",true,false);
Serial.println("\nAfter Access");
if(code==GSM_READY){
Serial.println("connected");
break;
}
if(code==CONNECTING) {
Serial.println("code is CONNECTING");
} else {
Serial.println(code);
delay(1000);
}
}
}
You are starting up the modem in asynchronous mode with:
code=gsmAccess.begin("",true,false);
Looking at the GSMBegin documentation, you are going to get a return value of 0 always which does not correspond to the GSM_READY enum type which is 3 I believe. Try:
code=gsmAccess.begin("",true);
try connecting a 9v battery to it. for me that solved the problem. i had the same issue. apparently, the gsm shield uses a lot of power. some computers can deliver enough, some cant.
I had the same problem. I solved it by connecting a 680 μF capacitor between 5 V and GND. This is only a temporary fix because it is going to create a huge current spike when connecting the power supply. A better solution would be to connect an external power supply or a more capable USB power supply.