I was looking for a simple way to register an ESP8266 device on a WiFi-network.
Normally it requires setting the device in Access Point mode at connect to its network. Thereafter the SSID and credentials must be entered in a web-page. Then reset the device and boot it up with the configured SSID and password.
I did not like this predure so I decided to use WSP.
Your router has an WSP-button. Pressing this button, enable the router to autoconnect to a device, that is in "WSP-mode". The router returns to normal operation after no more than 3 minutes, if no connection is made.
My implementation requres an ESP8266 with a button connected between pin4 (configurable) and ground. This is the button that sets the ESP8266 in "WSP-mode" at any time when pressed.
Further I have connected a LED to pin 12 (configurable) in order to indicate when the device is in "WSP-mode".
To use the software, include the following file:
WSP.h
#include <ESP8266WiFi.h>
class WPS {
private:
int cnt;
int _cflag = false;
int _maxcount = 30;
int _BUTTON = 4;
int _LED = 12;
public:
void handleClient() {
cnt = 0;
if ((digitalRead(_BUTTON)==LOW)) {
while ((digitalRead(_BUTTON)==LOW)) delay(100);
_cflag = false;
cnt = _maxcount;
WiFi.disconnect();
delay(100);
}
while (WiFi.status() != WL_CONNECTED) {
delay(200);
if (WiFi.status() == WL_CONNECTED) break;
Serial.print(".");
if (cnt++ >= _maxcount) {
digitalWrite(_LED, HIGH);
Serial.println("\nAttempting connection ...");
WiFi.beginWPSConfig();
cnt = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print("*");
delay(200);
if (cnt++ >= (_maxcount >> 1)) break;
}
cnt = _maxcount >> 1;
digitalWrite(_LED, LOW);
}
}
if (_cflag) return;
_cflag = true;
digitalWrite(_LED, LOW);
Serial.println("\r\nConnected!");
Serial.println(WiFi.localIP());
Serial.println(WiFi.SSID());
Serial.println(WiFi.macAddress());
}
WPS(int BUTTON, int LED) {
_BUTTON = BUTTON;
_LED = LED;
pinMode(_BUTTON, INPUT_PULLUP);
pinMode(_LED, OUTPUT);
}
};
Use it as in the following sample program:
|
|
WSPtest.ino
#define button 4
#define led 12
#include "WPS.h"
WPS wps(button, led);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin("", "");
}
void loop() {
wps.handleClient();
}
Downloads:
|