I am learning to code in cpp.
the problem I am facing is the function i wrote returns array of strings. but when I assign variable I get an error saying:
#include "ESP8266WiFi.h"
#define BLINK_PERIOD 250
long lastBlinkMillis;
boolean ledState;
#define SCAN_PERIOD 5000
long lastScanMillis;
String* getWifiHotspots() {
// String* arr = new String[];
String arr[100];
Serial.print("Scan start ... ");
int mn = WiFi.scanNetworks();
for (int i = 0; i < mn; i++)
{
arr[i] = WiFi.SSID(i).c_str();
}
return arr;
}
void setup()
{
Serial.begin(115200);
Serial.println();
pinMode(LED_BUILTIN, OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
}
void loop()
{
long currentMillis = millis();
// blink LED
if (currentMillis - lastBlinkMillis > BLINK_PERIOD)
{
digitalWrite(LED_BUILTIN, ledState);
ledState = !ledState;
lastBlinkMillis = currentMillis;
}
// trigger Wi-Fi network scan
if (currentMillis - lastScanMillis > SCAN_PERIOD)
{
WiFi.scanNetworks(true);
Serial.print("\nScan start ... ");
lastScanMillis = currentMillis;
}
// print out Wi-Fi network scan result uppon completion
// int n = WiFi.scanComplete();
int n = WiFi.scanNetworks();
if (n >= 0)
{
Serial.printf("%d network(s) found\n", n);
String* results = getWifiHotspots();
Serial.println(results);
WiFi.scanDelete();
}
How do get an array containg hotspot names like this :
One way would be to use malloc(...) but I do not know if that works on an Arduino.
Another way would be to pass in the array (and the size of the array) as parameters.
Does that help, or do you need more?
(By the way, the usual advice is to NOT use String on an Arduino because it can lead to inefficient usage of memory. C strings (arrays of char ended with \0) are the recommended alternative.)
Where have I used single quotes in the code I posted in the first post ?
if you are pointing to this ['ESP8266', 'network18', 'Jio'] then that is just an output I am wanting to have may it be
single or double output can be anyone, but in my code I have not used single quotes.
seaurchin:
How do get an array containg hotspot names like this :
['ESP8266', 'network18', 'Jio']
please help !
i should have said "you missed this" instead of "pay attention"
Colleagues at work have asked for help to find problems such as a ";" immediately after the closing parenthesis of an if. we don't see it because ... whatever.