I am working on a project that involves a BLE scanner using ESP32. I use the installed BLE scanner sample code in Arduino IDE to program the ESP32 but the device is unable to scan for the names of iBeacon just like any normal android BLE scanner application. I also tried to modify the code to specifically extract the names of iBeacons only but I still end up with the same result. Any suggestions are welcomed. Here is my modified code:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
//Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
Serial.print("Name :");
Serial.println(advertisedDevice.getName().c_str());
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
// put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
Related
I bought SEN0188 sensor and I tried to use but it gives the error: "Did not find fingerprint sensor :(". After some search and datasheet checking, I found an error and updated "FINGERPRINT_VERIFYPASSWORD". After some usage and testing I finished the project, I put it in a box and after some trying, I again got "Did not find fingerprint sensor :(". I checked my code, wiring schematic, etc.. But it doesn't work.
Library: github.com/adafruit/Adafruit-Fingerprint-Sensor-Library/
#include <Adafruit_Fingerprint.h>
#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)
SoftwareSerial mySerial(2, 3);
#else
#define mySerial Serial1
#endif
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
void setup()
{
Serial.begin(9600);
while (!Serial); // For Yun/Leo/Micro/Zero/...
delay(100);
Serial.println("\n\nAdafruit finger detect test");
finger.begin(57600);
while (true) {
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
break;
} else {
Serial.println("Did not find fingerprint sensor :(");
delay(1000);
}
}
}
void loop() {}
I have the following code:
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEAdvertisedDevice.h>
static void log(String message) {
Serial.println(message);
}
const char* deviceName = "BLEScanner";
BLEScan* pBLEScan;
void setup() {
Serial.begin(115200);
log("Setup!");
BLEDevice::init(deviceName);
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
log("Listing BLE Sensors");
BLEScanResults foundSensors = pBLEScan->start(30, false);
int count = foundSensors.getCount();
for (int i = 0; i < count; i++) {
BLEAdvertisedDevice bleSensor = foundSensors.getDevice(i);
String sensorName = bleSensor.getName().c_str();
String address = bleSensor.getAddress().toString().c_str();
log(address + " " + sensorName);
}
log("Sleeping");
sleep(10);
}
And when running it I get the following output:
Setup!
Listing BLE Sensors
00:09:b0:09:e7:ca
41:59:8f:07:ec:96
ac:5d:5c:e1:72:89 LM0845
c4:7c:8d:6a:36:cf Flower care
c4:7c:8d:6a:45:6b Flower care
c4:7c:8d:6a:46:2c Flower care
de:d5:2a:8e:1e:fd Tile
Sleeping
Listing BLE Sensors
00:09:b0:09:e7:ca
41:59:8f:07:ec:96
ac:5d:5c:e1:72:89
dc4:7c:8d:6a:36:cf
dc4:7c:8d:6a:45:6b
dc4:7c:8d:6a:46:2c
de:d5:2a:8e:1e:fd Tile
Sleeping
Listing BLE Sensors
00:09:b0:09:e7:ca
41:59:8f:07:ec:96
ac:5d:5c:e1:72:89
c4:7c:8d:6a:36:cf
dc4:7c:8d:6a:45:6b
dc4:7c:8d:6a:46:2c
de:d5:2a:8e:1e:fd Tile
... and so on
For some reason I only get the names of some of the devices the first time, is this a known error and what can be done about it?
I have tried to put a pBLEScan->clearResults(); before I start a scan but that did not help.
I'm using an ESP32 Adafruit feather.
The only solution I have found is to put the BLEDevice::init(deviceName); inside the loop and then run BLEDevice::deinit(); before sleep!
I have been using the ESP32 to scan for nearby iBeacon but I was unable to retrieve the UUID of beacons. The serial monitor keeps displaying "N/a". However, I was able to get the UUIDs of beacons when using the normal android BLE scanner app. Is there anything missing in my code?
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEUUID.h>
#include <BLEAdvertisedDevice.h>
BLEScan* pBLEScan;
int scanTime = 5; //In seconds
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice)
{
//Print Name
Serial.print("Name :");
if (advertisedDevice.haveName())
{
Serial.println(advertisedDevice.getName().c_str());
}
else
{
Serial.println("N/a");
}
//Print RSSI
Serial.print("RSSI :");
Serial.println(advertisedDevice.getRSSI());
//Print UUID
Serial.print("UUID :");
if (advertisedDevice.haveServiceUUID())
{
Serial.println(advertisedDevice.getServiceUUID().toString().c_str());
}
else
{
Serial.println("N/a");
}
Serial.println("");
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
}
void loop() {
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
BLEScanResults foundDevices = pBLEScan->start(scanTime);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
delay(1000);
}
I using the esp32 to detection beacon through rssi of beacon, but i meet some problem, first is rssi value is no stable so i need more sample of rssi, the second problem is esp32 scan sample rate is too slow, that faster speed only have one second scan rate, below is my used code
#include <BLEAdvertisedDevice.h>
#include <BLEDevice.h>
#include <BLEScan.h>
const int PIN = 2;
const int CUTOFF = -60;
void setup() {
pinMode(PIN, OUTPUT);
BLEDevice::init("");
}
void loop() {
BLEScan *scan = BLEDevice::getScan();
scan->setActiveScan(true);
BLEScanResults results = scan->start(1);
int best = CUTOFF;
for (int i = 0; i < results.getCount(); i++)
{
BLEAdvertisedDevice device =
results.getDevice(i);
int rssi = device.getRSSI();
if (rssi > best) {
best = rssi;
}
}
digitalWrite(PIN, best > CUTOFF ? HIGH :
LOW);
}
but i want to mor rssi sample, i tried change scan->start(1) to scan->start(0), but result is not return, how to do can solve lower sample problem, or use another board?
The problem is that you do all of the "work" in the loop.
Are you aware of the fact that an ESP32 microcontroller runs at 240 MHz
I guess your loop time is about 400 Nano seconds.
So how would your device know if and how much devices are found.
You are trying to do something with the scan results just after scan start.
To get around this problem there is a BLEAdvertisedDeviceCallbacks in the Arduino for ESP32 code.
This callback will return a result for every device it finds.
Here is an example from GITHUB (ESP32_BLE_Arduino by Neil Kolban)https://github.com/nkolban/ESP32_BLE_Arduino/blob/master/examples/BLE_scan/BLE_scan.ino
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets
/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results
//faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
// put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
This to give you an idea on how it works, tweak it to meet your own needs.
I'm trying to copy a file from an SD card to an adafruit FRAM module. I'm wondering if I'm going about it the right way. I'm trying to read the file one byte at a time and then write that byte to a specific location on the Fram module.
I've been trying that approach using the sketch below and haven't been successful. I'm wondering if I'm approaching it the right way, and if so, where have I gone wrong with my sketch. Thanks.
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include "Adafruit_FRAM_I2C.h"
Adafruit_FRAM_I2C fram = Adafruit_FRAM_I2C();
uint16_t framAddr = 0;
void setup() {
Serial.begin(9600);
// setup SD-card
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println(" failed!");
while(true);
}
Serial.println(" done.");
}
void loop() {
uint16_t count = 0;
File myFile = SD.open("test.txt");
if (!myFile) {
// if the file didn't open, print an error and stop
Serial.println("error opening");
while (true);
}
const int S = 1;
byte buffer[S];
while (myFile.available()) {
// read from the file into buffer
myFile.read(buffer, sizeof(buffer));
Serial.print("0x"); Serial.print(count, HEX); Serial.print(": ");
Serial.println(buffer[count]);
//write fram (address,value)
fram.write8(count,buffer[count]);
}
myFile.close();
while (true) ;
}
I don't familiar with this FRAM, but you don't promote your address.
So the device write to the same address all the time and rewrite the memory.
Hope i help.
yoav