ekstrand / ESP8266wifi

ESP8266 Arduino library with built in reconnect functionality
MIT License
453 stars 236 forks source link

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

Checking Client Connections

boolean checkConnections(&connections) - Updates pre-initialised pointer to 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..
* **timeoutMillis** the maximum number of milliseconds to look for a new incoming message
* **return** WifiMessage contains:
 * **boolean hasData** true if a message was received
 * **char channel** tells you if the message was received from the server (channel == SERVER) or another source
 * **char * message** the message as a character array (up to the first 25 characters)
* **Example:** 

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
* **ssid** the name for your access point, max 15 characters
* **password** the password for your access point, max 15 characters
* **channel** the channel for your access point
* **port** the port for your local server, TCP only
* **return** true if the local access point and server was configured and started
* **Example:** `boolean localAPAndServerStarted = wifi.startLocalAPAndServer("my_ap", "secret_pwd", "5", "2121");`

**boolean stopLocalAPAndServer()** disable the accesspoint (the server will not be stopped, since a restart is needed)
* **return** true if the local access point was stopped
* **Example:** `boolean localAPAndServerStopped = wifi.stopLocalAPAndServer();`

**boolean isLocalAPAndServerRunning()** check if local access point and server is running
* **return** true if the local access point and local server is running
* **Example:** `boolean localAPAndServerRunning = wifi.isLocalAPAndServerRunning();`

## 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:
* **HW_RESET_RETRIES 3** - is the maximum number of times begin() will try to start the ESP8266 module
* **SERVER_CONNECT_RETRIES_BEFORE_HW_RESET 30** - is the nr of time the watchdog will try to establish connection to a server before a hardware reset of the ESP8266 is performed
* The maximum number of characters for incoming and outgoing messages can be changes by editing:
    * char msgOut[26];
    * char msgIn[26];
* If the limit for ssid and password length does not suite you, please change:
    * char _ssid[16];
    * char _password[16];
    *  char _localAPSSID[16];
    *  char _localAPPassword[16];