In short, I am trying to send BLE scan results (only MAC Address and RSSI value in my case) to Ubidots. So, I tried to use ubidots.add(String, int) to send the addresses and the respective RSSI, but the variables don’t appear properly on Ubidots. I am using the ble_scan example from arduino.
Here are the results on the Ubidots variables page. The RSSI value (negative int)
appears correctly, but the Address doesn’t.
Example of scan results in Serial Port: (no Ubidots just raw data)
MAC Address: 1c:f3:67:78:63:96
RSSI: -84
MAC Address: 1c:f3:67:78:63:96
RSSI: -92
MAC Address: 7c:ab:ef:f7:f7:79
RSSI: -82
MAC Address: 1c:f3:67:78:63:96
RSSI: -84
MAC Address: 5d:9f:e4:7b:35:33
RSSI: -82
This is the output when I use Ubidots
As you can see, the ubidots.add() does indeed receive the Address and its MAC, but doesnt send over to Ubidots
.....WiFi connected
IP address:
192.168.1.11
Starting Scan...
Scan Started
----------
payload:
UbidotsESP32/0.0.1|POST|BBFF-yRZeguGTJQXLKQIx2cOzEkEsWTQhk0|2462ABE081BC:2462ABE081BC=>4e:7e:45:e1:47:8a:-88|end
----------
Sending data...
----------
Server's response:
ERROR|400
----------
----------
payload:
UbidotsESP32/0.0.1|POST|BBFF-yRZeguGTJQXLKQIx2cOzEkEsWTQhk0|2462ABE081BC:2462ABE081BC=>4e:7e:45:e1:47:8a:-88|end
----------
Sending data...
Trying to connect to industrial.api.ubidots.com , attempt number: 0
Trying to connect to industrial.api.ubidots.com , attempt number: 1
Trying to connect to industrial.api.ubidots.com , attempt number: 2
Trying to connect to industrial.api.ubidots.com , attempt number: 3
Trying to connect to industrial.api.ubidots.com , attempt number: 4
[ERROR] Could not connect to the host
----------
payload:
UbidotsESP32/0.0.1|POST|BBFF-yRZeguGTJQXLKQIx2cOzEkEsWTQhk0|2462ABE081BC:2462ABE081BC=>TJQXLKQIx2cOzEkEsWTQhk0|2462ABE081BC:2462ABE081BC=>:-88|end
----------
Below is my edited 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 <UbiBuilder.h>
#include <UbiConstants.h>
#include <Ubidots.h>
#include <UbiHttp.h>
#include <UbiProtocol.h>
#include <UbiProtocolHandler.h>
#include <UbiTcp.h>
#include <UbiTypes.h>
#include <UbiUdp.h>
#include <UbiUtils.h>
#include <iostream>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <WiFi.h>
#include <Ubidots.h>
/* Network credentials*/
const char* UBIDOTS_TOKEN = "token inserted";
const char* WIFI_SSID = "network1";
const char* WIFI_PASS = "password1";
Ubidots ubidots(UBIDOTS_TOKEN, UBI_TCP);
/*Initializing scan variables*/
int scanTime = 2; //In seconds
BLEScan* pBLEScan;
String readString;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
/* Printing MACs and resp RSSI
Serial.print("MAC Address: ");
Serial.println(advertisedDevice.getAddress().toString().c_str());
Serial.print("RSSI: ");
Serial.println(advertisedDevice.getRSSI());
delay(200); */
/* Sending to Ubidots*/
delay(2500);
ubidots.add(advertisedDevice.getAddress().toString().c_str(), advertisedDevice.getRSSI());
// delay(5500);
bool bufferSent = false;
bufferSent = ubidots.send();
delay(1000);
}
};
void setup() {
Serial.begin(115200);
ubidots.wifiConnect(WIFI_SSID, WIFI_PASS);
ubidots.setDebug(true); // Uncomment to debug
Serial.println("Starting Scan...");
BLEDevice::init("ESP32");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but gets results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
ESP.restart();
Serial.println("Scan Started");
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(1000);
}
Any Help is appreciated. Thanks in advance!