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);
}
Related
I am using the below code with arduino-uno, but often getting "Could not find a valid MPU6050 sensor
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
Serial.begin(115200);
Serial.println("Initialize MPU6050");
while (!mpu.begin()) {
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
}
void loop() {
}
My Arduino is working fine,
So, I checked MPU6050 using below code,
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(115200);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
Got Output as expected
Scanning...
I2C device found at address 0x68 !
done
From the above output I hope GPU6050 is working
How can I get values from GPU6050?
Your code is working as expected, it tells you in setup() when it can't connect to the device, until it can.
So when it stops printing the message, it is connected.
Now in loop() you should write your code to negotiate with the device.
Here's an excellent place to start with.
Hello I am programming a bluetooth connection from Android Studio to Arduino. The connection works and it sends the String. I only get something like this tho on my Arduino (x?xx?xx??xxx?xxx?x) the questionmarks are the other way the string I send is on
This is my code:
#include <SoftwareSerial.h>
#define rxPort 11
#define txPort 10
char btData;
String string;
SoftwareSerial btSerial(rxPort, txPort);
void setup(){
Serial.begin(9600);
btSerial.begin(38400);
Serial.println("bluetooth available");
pinMode(LED_BUILTIN, OUTPUT);
}
void loop(){
if(btSerial.available()>0){
string = "";
}
while(btSerial.available()>0){
btData = (byte)btSerial.read();
if(btData==":"){
break;
}else{
string += btData;
}
delay(1);
Serial.println(string);
}
if(string == "on"){
digitalWrite(LED_BUILTIN,HIGH);
}
}
why do you cast the read to byte??
char it is :)
it should work with 9600 BAUD rate as well
example from my project (I didn't build String though - used single char as command)
SoftwareSerial blue(3, 5); // BlueTooth RX, TX;
void BluetoothSetup()
{
blue.begin(9600);
blue.print("AT+NAMEKuku"); // give it a name
delay(2000);
Serial.println("got BT");
}
void setup() {
....
BluetoothSetup();
....
}
void loop() {
if (blue.available()) {
char r = blue.read();
ProcessRemoteCommand(r);
}
}
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);
}
I have a UBLOX Neo 6M Module to get the latitude and longitude of a device. I am planning to send these data to Firebase. From my research, it requires a Wifi Module for it to be able to pass the data to Firebase. I was planning to use a SIM card that may provide internet data.
#include <FirebaseArduino.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define FIREBASE_HOST "FIREBASE URL"
#define FIREBASE_AUTH "DB SECRET"
//SIM
#define APN "internet"
void setup() {
Serial.begin(9600);
ss.begin(GPSBaud);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
int n = 0;
void loop(){
// This sketch displays information every time a new sentence is correctly encoded.
while (ss.available() > 0){
gps.encode(ss.read());
if (gps.location.isUpdated()){
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
}
}
}
delay(1000);
// set bool value
Firebase.setBool("truth", false);
// handle error
if (Firebase.failed()) {
Serial.print("setting /truth failed:");
Serial.println(Firebase.error());
return;
}
delay(1000);
// append a new value to /logs
String name = Firebase.push("location", gps);
// handle error
if (Firebase.failed()) {
Serial.print("pushing /location failed:");
Serial.println(Firebase.error());
return;
}
Serial.print("pushed: /location/");
Serial.println(name);
delay(1000);
}
--
I'm sure that this code block wont lead me to a working system. But what I want to know if this sequence may lead me to somewhere. Please drop ideas on what I lack and I should search. Thank you.
I'm not sure about how Firebase works but what you need to do is to first get a valid location and then pass it to Firebase, something like this:
#include <FirebaseArduino.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define FIREBASE_HOST "FIREBASE URL"
#define FIREBASE_AUTH "DB SECRET"
//SIM
#define APN "internet"
float lat, lng;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
unsigned long last_millis = millis();
}
void feed_gps()
{
unsigned long last_millis = millis();
if (ss.available() > 0)
{
//feed gps for 1 seconds
while (millis() - last_millis < 1000)
{
gps.encode(ss.read());
if (gps.location.isValid())
{
lat = gps.location.lat();
lng = gps.location.lng();
Serial.print("Latitude= ");
Serial.print(lat, 6);
Serial.print(" Longitude= ");
Serial.println(lng, 6);
}
else
{
Serial.print("GPS still unavailable, Chars processed: ");
Serial.println(gps.charsProcessed());
}
}
}
}
void loop()
{
feed_gps();
// set bool value
Firebase.setBool("truth", false);
// handle error
if (Firebase.failed())
{
Serial.print("setting /truth failed:");
Serial.println(Firebase.error());
return;
}
char location_str[25] = {0};
// store string of lat,lng
sprintf(location_str, "%f, %f", lat, lng);
String name = Firebase.push("location", location_str);
// handle error
if (Firebase.failed())
{
Serial.print("pushing /location failed:");
Serial.println(Firebase.error());
return;
}
Serial.print("pushed: /location/");
Serial.println(name);
}
I'm trying to request temperatures from my DS18B20 sensor to post on plot.ly, but it seems my loop function is only running once; after connecting to plot.ly and creating the graph, the temperature is printed once in the serial monitor and does not seem to continue! Any help is greatly appreciated. Here is my code:
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <plotly_streaming_cc3000.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define WLAN_SSID "wifi"
#define WLAN_PASS "********"
#define WLAN_SECURITY WLAN_SEC_WPA2
OneWire oneWire(10);
DallasTemperature sensors(&oneWire);
#define nTraces 1
char *tokens[nTraces] = {"token"};
plotly graph("username", "token", tokens, "filename", nTraces);
void wifi_connect(){
/* Initialise the module */
Serial.println(F("\n... Initializing..."));
if (!graph.cc3000.begin())
{
Serial.println(F("... Couldn't begin()! Check your wiring?"));
while(1);
}
// Optional SSID scan
// listSSIDResults();
if (!graph.cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("... Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("... Request DHCP"));
while (!graph.cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
unsigned long aucDHCP = 14400;
unsigned long aucARP = 3600;
unsigned long aucKeepalive = 10;
unsigned long aucInactivity = 20;
if (netapp_timeout_values(&aucDHCP, &aucARP, &aucKeepalive, &aucInactivity) != 0) {
Serial.println("Error setting inactivity timeout!");
}
}
}
void setup() {
graph.maxpoints = 100;
Serial.begin(9600);
sensors.begin();
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
wifi_connect();
bool success;
success = graph.init();
if(!success){while(true){}}
graph.openStream();
}
void loop(void) {
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
Serial.print("Temperature for Device 1 is: ");
Serial.print(sensors.getTempFByIndex(0));
graph.plot(millis(), sensors.getTempFByIndex(0), tokens[0]);
delay(500);
}