Hi,
I have written (or copy/paste) my first small sketch for starting and stopping the wifi on my ESP8266.
Is this a good way to do it? What is to be improved? I thought it might be a good thing, because its seemed easier to me for use in the future for other sketches. Same sketch as the wifi below I will write for my mqtt server. So I can copy and paste both into an sketch when I need them.
Thank you!! Kind regards.
#include <ESP8266WiFi.h>
#ifndef STASSID
#define STASSID "ssid"
#define STAPSK "<Passwort>"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
void setup() {
Serial.begin(115200);
}
void loop() {
//if (WiFi.status() != WL_CONNECTED) {WiFiSwitch("on");}
//if (WiFi.status() == WL_CONNECTED) {WiFiSwitch("off");}
}
void WiFiSwitch(String Event) {
if (Event == "on") {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
} else if (Event == "off") {
while (WiFi.status() == WL_CONNECTED) {
WiFi.mode(WIFI_OFF);
delay(500);
}
}
}