Hello,
I have a void loop which contain a `variable int bleNumber
and hold sum number. it always has a data from foundDevices.getCount();
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
showParsedData();
newData = false;
}
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
int bleNumber = foundDevices.getCount();
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
Now i need to send this number to server so i am using spiffs it is in void setup()
server.on("/blenetworks", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(bleNumber).c_str());
});
But i am getting confused how should i store this value globally in the sketch so that it could get access by void setup
whenever the new data it should also be updated in that global .
You have not posted a complete sketch so the problem cannot be seen in context but you have answered your own question
Declare the variable as a global and don't declare a second variable with the same name anywhere else in the sketch
#include <HardwareSerial.h>
#include <WiFi.h>
#include <WiFiManager.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "SPIFFS.h"
#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());
}
};
boolean newData = false;
AsyncWebServer server(80);
HardwareSerial SerialPort(2);
//============
int bleNumber; // DEFINE HERE
String networkNumber = "";
void setup() {
Serial.begin(115200);
SerialPort.begin(9600,SERIAL_8N1, 16, 17);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
//SPIFFS
int numberOfNetworks = WiFi.scanNetworks();
networkNumber += numberOfNetworks;
Serial.println(networkNumber);
delay(1000);
if (WiFi.status() != WL_CONNECTED) {
WiFiManager manager;
bool success = manager.autoConnect("rootofpisniffer","12345678");
if(!success) {
Serial.println("Failed to connect");
}
else {
Serial.println("Connected");
}
}
Serial.println(WiFi.localIP());
if (!SPIFFS.begin()) {
Serial.println("An error has occurred while mounting SPIFFS");
}else {
Serial.println("SPIFFS mounted successfully");
}
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index.html", "text/html");
});
server.on("/noofnetworks", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", networkNumber.c_str());
});
server.on("/blenetworks", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(bleNumber).c_str());
});
server.serveStatic("/", SPIFFS, "/");
server.begin();
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() {
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
int bleNumber = foundDevices.getCount();
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
//============
i DID THIS I GET A VALUE AS 0 IN SERVER I AM USING JAVASCRIPT TO DO THIS
guix
November 17, 2022, 7:22am
4
int bleNumber = foundDevices.getCount();
This is a local variable, it's not the same variable as the one declared in the global scope
So how can i passed the value to the global variable ?
You don't need to pass a value to a global variable. If you update its value anywhere in the sketch then the new value is available to be used anywhere in the sketch
rootofpi:
bleNumber
so i provide varaible bleNumber to spiffs directly . but when it runs it will throw me error .
Post the code that causes an error where you declare bleNumber only as a global and not locally
system
Closed
May 16, 2023, 2:27pm
9
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.