Skip to content

The WIFI Manager

Tinkerspy edited this page Nov 20, 2016 · 6 revisions

The WIFi Manager

The Atm_esp8266_wifi component connects to a wifi network and generates actions on connect and disconnect events.

Synopsis

#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();
}

Atm_esp8266_wifi & begin( const char ssid[], const char password[] )

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" );

}

Atm_esp8266_wifi & onChange( int state, {connector}, {connector-arg} )

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();

}

Atm_esp8266_wifi & onChange( {connector}, {connector-arg} )

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();

}

IPAddress ip( void )

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() );
    });

Atm_esp8266_wifi & led( int pin, bool activeLow = false )

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();
}

Atm_esp8266_wifi & start( void )

Starts up the WIFI connection (equivalent to triggering an EVT_START event).

void setup() {

  wifi.begin( ap_ssid, ap_password )
    .start();
}

EVT_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 );

}

Atm_esp8266_wifi & trace( Stream & stream )

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 );