Home

Awesome

SerialESP8266wifi

A simple ESP8266 Arduino library with built in re-connect functionality.

Memory footprint and more

Install

Constructor

SerialESP8266wifi(Stream serialIn, Stream serialOut, byte resetPin)

SerialESP8266wifi(Stream serialIn, Stream serialOut, byte resetPin, Stream debugSerial)

Starting the module

boolean begin() calling this method will do a hw reset on the ESP8266 and set basic parameters

Connecting to an access point

boolean connectToAP(char * ssid, char password)* tells the ESP8266 to connect to an accesspoint

boolean isConnectedToAP() checks if the module is connected with a valid IP

Connecting to a server

boolean connectToServer(char ip, char port)** tells the ESP8266 to open a connection to a server

boolean isConnectedToServer() checks if a server is connected

setTransportToTCP() AND setTransportToUDP() tells the ESP8266 which transport to use when connecting to a server. Default is TCP.

Disconnecting from a server

disconnectFromServer() tells the ESP8266 to close the server connection

Sending a message

boolean send(char channel, char * message) sends a message - alias for send(char channel, char * message, true)

boolean send(char channel, char * message, boolean sendNow) sends or queues a message for later sending

wifi.send(SERVER, "You", false);
wifi.send(SERVER, " are ", false);
wifi.send(SERVER, "fantastic!", true); // ie wifi.send(SERVER, "fantastic!");

endSendWithNewline(bool endSendWithNewline) by default all messages are sent with newline and carrage return (println), you can disable this

Checking Client Connections

boolean checkConnections(&connections) - Updates pre-initialised pointer to WifiConnection *connections.

WifiConnection *connections;

wifi.checkConnections(&connections);
for (int i = 0; i < MAX_CONNECTIONS; i++) {
  if (connections[i].connected) {
    // See if there is a message
    WifiMessage msg = wifi.getIncomingMessage();
    // Check message is there
    if (msg.hasData) {
      processCommand(msg);
    }
  }
}

Check Connection

boolean isConnection(void) - Returns true if client is connected, otherwise false. Use as above without WifiConnection pointer if not bothered about multi-client.

Get Incoming Message From Connected Client

WifiMessage getIncomingMessage(void) - checks serial buffer for messages. Return is WifiMessage type as below. See example Check Client Connection example for usage.

Receiving messages

WifiMessage listenForIncomingMessage(int timeoutMillis) will listen for new messages up to timeoutMillis milliseconds. Call this method as often as possible and with as large timeoutMillis as possible to be able to catch as many messages as possible..

void loop(){
    WifiMessage in = wifi.listenForIncomingMessage(6000);
    if (in.hasData) {
        Serial.print("Incoming message:");
        Serial.println(in.message);
        if(in.channel == SERVER)
            Serial.println("From server");
        else{
            Serial.print("From channel:");
            Serial.println(in.channel);
        }
    }
    // Do other stuff
 }

Local access point and local server

boolean startLocalAPAndServer(char ssid, char password, char* channel, char* port)** will create an local access point and start a local server

boolean stopLocalAPAndServer() disable the accesspoint (the server will not be stopped, since a restart is needed)

boolean isLocalAPAndServerRunning() check if local access point and server is running

Re-connect functionality

Everytime send(...) and listenForIncomingMessage(..) is called a watchdog checks that the configured access point, server and local access point and server is still running, if not they will be restarted or re-connected. The same thing happens if the ESP8266 should reset. Note: It is really only the send method that can detect a lost connection to the server. To be sure you are connected, do a send once in a while..

Avanced configuration

In SerialESP8266wifi.h you can change some stuff: