Hi all,
I'm trying to write my first library but am struggling to get it to compile properly. It's a relatively simple class that's supposed to store a list of known good Wifi networks along with their passwords and then connect to the strongest one. Note that it's nowhere near finished, I just wanted to test what I had so far, but ran into some problems... Here's the contents of the .h file:
#ifndef BCN_WiFi
#define BCN_WiFi
#include "Arduino.h"
#include <ESP8266WiFi.h>
class BCN_WiFi {
public:
//BCN_WiFi;
void addNetwork(char* sSSID, char* sPWD);
bool setup_wifi();
void list_networks();
private:
static WiFiClient espClient;
char* mySSIDs[5];
char* myPWDs[5];
int iUsed;
int32_t myRSSI[5];
};
#endif
and here's the corresponding .cpp file:
/*
This is a file to manage WiFi connections on the ESP8266
*/
#include <ESP8266WiFi.h>
#include "BCN_WiFi.h"
#include "Arduino.h"
/*
// Default constructor
BCN_WiFi::BCN_WiFi() {
//Do nothing
}
*/
void BCN_WiFi::addNetwork(char* sSSID, char* sPWD) {
if (iUsed < 5){
mySSIDs[iUsed] = sSSID;
myPWDs[iUsed] = sPWD;
iUsed += 1;
}
}
void BCN_WiFi::list_networks() {
//List stored networks - mainly for debugging
for (int j = 0; j < iUsed; ++j){
Serial.println(mySSIDs(j));
Serial.println(myPWDs(j));
Serial.println("");
}
}
bool BCN_WiFi::setup_wifi() {
//Scan for networks
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int n = WiFi.scanNetworks();
if (n == 0)
//Wait for a second before trying again
delay(1000);
else {
for (int i = 0; i < n; ++i){
for (int j = 0; j < iUsed; ++j){
if (strcmp(WiFi.SSID(i), mySSIDs(j)) == 0) {
myRSSI(j) = WiFi.RSSI(i);
}
}
}
}
}
Here's the code I'm using in the Arduino IDE to test:
#include "BCN_WiFi.h"
BCN_WiFi myWiFi;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
When I hit compile I get the following errors:
C:\Users\Me\Documents\Arduino\libraries\BCN_WiFi/BCN_WiFi.h:7:16: error: an anonymous struct cannot have function members
class BCN_WiFi {
^
C:\Users\Me\Documents\Arduino\libraries\BCN_WiFi/BCN_WiFi.h:20:1: error: abstract declarator '<anonymous class>' used as declaration
};
^
sketch_aug23a:3: error: 'myWiFi' does not name a type
BCN_WiFi myWiFi;
^
exit status 1
'myWiFi' does not name a type
I'm stuck on what the compiler means by 'anonymous' both in terms of struct and class. If I'm honest, I have no idea what it means by 'myWifi' does not name a type either... If possible, could someone tell me what I'm doing wrong as well as point me in the direction of some literature that would help me understand what I've done wrong? I've tried Dr. Google but he's not been of much use.
Thanks in advance,
Ben.