Hi,
I am using some code from the wifi/ble scanner. I am logging the data on the sd card, on the BLE side, Iseem to drop the scan after about 500 records. I can’t seem to get the tft to display/load images that I have on the SD Card or dump the text to the screen. This is a sample of the code I am trying to run.
Preformatted text#
- List item
include <SPI.h>
#include <Seeed_FS.h>
#include "SD/Seeed_SD.h"
#include "rpcWiFi.h"
#include "rpcBLEDevice.h" //bluetooth library
#include "BLEScan.h" //bluetooth library
#include "BLEAdvertisedDevice.h" //bluetooth library
File myFile;
int scanTime = 2; //scan time for bluetooth
int counter = 1; //start count
BLEScan* pBLEScan; //create buffer to allocate memory
//get MAC addresses of bluetooth devices available
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks{
void onResult(BLEAdvertisedDevice advertisedDevice){
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str()); //print MAC addresses of bluetooth devices
myFile.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str()); //print MAC addresses of bluetooth devices
myFile.println("");
}
};
void setup() {
Serial.begin(115200);
while(!Serial); // Wait for Serial to be ready
delay(100);{
}
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA); //configure Wi-Fi as Station(STA) Mode
WiFi.disconnect(); //disconnect from an AP(access point) if it was previously connected
//Bluetooth setup
BLEDevice::init(""); //initialize bluetooth
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); //set device callbacks
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100); //scan interval
pBLEScan->setWindow(99); //less than or equal to setInterval value
delay(100);
// Setup for SD Card
Serial.print("Initializing SD card...");
if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("SensorData.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to SensorData.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening SensorData.txt");
}
// re-open the file for reading:
myFile = SD.open("SensorData.txt", FILE_READ);
if (myFile) {
Serial.println("SensorData.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening SensorData.txt");
Serial.println("Setup done");
}
}
// Setup done
// Before the loop
void loop() {
myFile = SD.open("SensorData.txt", FILE_APPEND);
Serial.println("Wi-Fi scan start");
myFile.println("Wi-Fi scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks(); //return the number of Wi-Fi networks found and store in variable
Serial.println("scan done");
myFile.println("scan done");
if (n == 0) {
Serial.println("no networks found"); //print string if no networks found
myFile.println("no networks found"); //print string if no networks found
} else {
Serial.print(n); //print the number of networks found
myFile.print(n);
Serial.println(" networks found");
myFile.println(" Networks Found");
for (int i = 0; i < n; ++i) { //iterate the network found in the loop
// Print SSID and RSSI for each network found
Serial.print(i + 1); //found network number
myFile.print(i + 1);
Serial.print(": ");
myFile.print(": ");
Serial.print(WiFi.SSID(i)); //print Wi-Fi name
myFile.print(WiFi.SSID(i));
Serial.print(" (");
myFile.print(" (");
Serial.print(WiFi.RSSI(i)); //print Wi-Fi signal strength
myFile.print(WiFi.RSSI(i));
Serial.print(")");
myFile.println(")");
Serial.print((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*"); //print "*" if password protected & print nothing if open
myFile.print((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
delay(10);
}
}
Serial.println("Wi-Fi scan done!");
myFile.println("Wi-Fi scan done!");
Serial.println(""); //print an empty line
myFile.println(""); //print an empty line
//Bluetooth scan
Serial.println("BLE scan start");
myFile.println("BLE scan start");
{
Serial.print("Loop Count: ");
Serial.println(counter);
myFile.print("Loop Count: ");
myFile.println(counter);
counter++;
}
BLEScanResults foundDevices = pBLEScan->start(scanTime, false); //scan for bluetooth devices and print MAC address
Serial.print(foundDevices.getCount()); //print number of bluetooth devices found
myFile.print(foundDevices.getCount()); //print number of bluetooth devices found
Serial.println(" BLE device(s) found");
myFile.println(" BLE device(s) found");
Serial.println("BLE scan done!");
myFile.println("BLE scan done!");
pBLEScan->clearResults(); //delete results from pBLEScan buffer to release memory
Serial.println("Networks Found");
myFile.println("Networks Found");
Serial.println("");
myFile.println("");
myFile.close();
// Wait a bit before scanning again
delay(500); //wait before scanning again
}
void saveData(){
myFile = SD.open("SensorData.txt",FILE_APPEND);
}
After about 92 loops through the BLE it stops, using the above code.
This code runs without using the TFT_eSPI display, when I add the TFT to it, it won’t run. I am sure I am doing something incorrectly, but need some good advice.
Thank you in advance!