-
Notifications
You must be signed in to change notification settings - Fork 7
The WIFI Manager
The Atm_esp8266_wifi component connects to a wifi network and generates actions on connect and disconnect events.
#include <Automaton.h>
#include <Atm_esp8266.h>
char ap_ssid[] = "MySSID";
char ap_password[] = "MyPASSWORD";
void setup() {
Serial.begin( 9600 );
Serial.println( "Connecting to WIFI" );
wifi.begin( ap_ssid, ap_password )
.onChange( true, [] ( int idx, int v, int up ) {
Serial.print( "Connected to Wifi, browse to http://" );
Serial.println( wifi.ip() );
});
.onChange( false, [] ( int idx, int v, int up ) {
Serial.println( "Lost WIFI connection" );
})
.led( LED_BUILTIN, true ) // Led indicates WIFI status
.start();
}
void loop() {
automaton.run();
}
Initializes the Atm_esp8266_wifi state machine and sets the SSID and WPA2 password. The connection is not made until an EVT_START event is received or the start() method gets called.
void setup() {
wifi.begin( "MySSID", "MyPASSWORD" );
}
Called whenever a WIFI connection is created or lost. The state argument is be true
for a connection made and
false
for a connection lost.
The connector argument can be either a component and an event or a callback.
void setup() {
...
wifi.begin( ap_ssid, ap_password )
.onChange( true, server, server.EVT_START )
.start();
}
A callback can also be a lambda expression:
void disconnect( int idx, int v, int up ) {
Serial.println( "WIFI connection lost" );
}
void setup() {
...
wifi.begin( ap_ssid, ap_password )
.onChange( true, [] ( int idx, int v, int up ) {
Serial.println( "WIFI connection up" );
server.start();
})
.onChange( true, disconnect )
.start();
}
This version of the onChange() method is called whenever the wifi status changes. The new state is in the v argument.
void setup() {
...
wifi.begin( ap_ssid, ap_password )
.onChange( [] ( int idx, int v, int up ) {
switch ( v ) {
case 0:
Serial.println( "WIFI connection lost" );
break;
case 1:
Serial.println( "WIFI connection up" );
break;
}
})
.start();
}
Returns the ip address assigned to us by the WIFI network.
wifi.begin( ap_ssid, ap_password )
.onChange( true, [] ( int idx, int v, int up ) {
Serial.print( "Connected to Wifi, browse to http://" );
Serial.println( wifi.ip() );
});
Connects a digital pin to the WIFI state machine so that a led connected to it follows the status of the WIFI connection. Note that the built in led in the Wemos is active low.
void setup() {
wifi.begin( ap_ssid, ap_password )
.led( LED_BUILTIN, true ) // Led indicates WIFI status
.start();
}
Starts up the WIFI connection (equivalent to triggering an EVT_START event).
void setup() {
wifi.begin( ap_ssid, ap_password )
.start();
}
Starts up the WIFI connection (equivalent to triggering an EVT_START event).
void setup() {
Serial.begin( 9600 );
Serial.println( "Press the button to connect to the WIFI network" );
wifi.begin( ap_ssid, ap_password );
button.begin( D2 )
.onPress( wifi, wifi.EVT_START );
}
To monitor the behavior of this machine you may log state change events to a Stream object like Serial.
Serial.begin( 9600 );
wifi.trace( Serial );