Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 10 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A simple ESP8266 Arduino library with built in re-connect functionality.


## Install
* Download the library as a zip from https://github.com/ekstrand/SerialESP8266wifi/archive/master.zip
* Download the library as a zip from https://github.com/ekstrand/SerialESP8266wifi/archive/master.zip
* Unzip and place in ARDUINO_HOME/libraries/ directory as SerialESP8266wifi
* Restart the Arduino IDE
* In your sketch do a `#include <SerialESP8266wifi.h>`
Expand Down Expand Up @@ -40,7 +40,9 @@ ESP8266#Module_Pin_Description
* **Example:** `boolean esp8266started = wifi.begin();`

## Connecting to an access point
**boolean connectToAP(char * ssid, char* password)** tells the ESP8266 to connect to an accesspoint
* **boolean setStaticIp(char\* ip, char\* gateway, char\* mask)** sets static ip. Use it before calling connectToAP to set static ip instead of using DHCP.

* **boolean connectToAP(char\* ssid, char\* password)** tells the ESP8266 to connect to an accesspoint
* **ssid** the ssid (station name) to be used. Note that this method uses char arrays as input. See http://arduino.cc/en/Reference/StringToCharArray for how to convert an arduino string object to a char array (max 15 chars)
* **password** the access point password wpa/wpa2 is assumed (max 15 chars)
* **return** will return a true if a valid IP was received within the time limit (15 seconds)
Expand All @@ -51,7 +53,7 @@ ESP8266#Module_Pin_Description
* **Example:** `boolean apConnected = wifi.isConnectedToAP();`

## Connecting to a server
**boolean connectToServer(char* ip, char* port)** tells the ESP8266 to open a connection to a server
**boolean connectToServer(char\* ip, char\* port)** tells the ESP8266 to open a connection to a server
* **ip** the IP-address of the server to connect to
* **port** the port number to be used
* **return** true if connection is established within 5 seconds
Expand All @@ -68,18 +70,18 @@ ESP8266#Module_Pin_Description
* **Example:** `wifi.disconnectFromServer();`

## 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)** sends a message - alias for send(char channel, char * message, true)
* **channel** Set to **SERVER** if you want to send to server. If we are the server, the value can be between '1'-'3'
* **message** a character array, max 25 characters long.
* **return** true if the message was sent
* **Example:** `boolean sendOk = wifi.send(SERVER, "Hello World!");`

**boolean send(char channel, char * message, boolean sendNow)** sends or queues a message for later sending
**boolean send(char channel, char\* message, boolean sendNow)** sends or queues a message for later sending
* **channel** Set to **SERVER** if you want to send to server. If we are the server, the value can be between '1'-'3'
* **message** a character array, max 25 characters long.
* **sendNow** if false, the message is appended to a buffer, if true the message is sent right away
* **return** true if the message was sent
* **Example:**
* **Example:**
```
wifi.send(SERVER, "You", false);
wifi.send(SERVER, " are ", false);
Expand Down Expand Up @@ -130,7 +132,7 @@ example for usage.
* **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:**
* **Example:**
```
void loop(){
WifiMessage in = wifi.listenForIncomingMessage(6000);
Expand All @@ -149,7 +151,7 @@ void loop(){
```

## 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 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
Expand Down Expand Up @@ -181,18 +183,3 @@ In SerialESP8266wifi.h you can change some stuff:
* char _password[16];
* char _localAPSSID[16];
* char _localAPPassword[16];















14 changes: 14 additions & 0 deletions SerialESP8266wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const char TCP[] PROGMEM = "TCP";
const char UDP[] PROGMEM = "UDP";

const char CWJAP[] PROGMEM = "AT+CWJAP=\"";
const char CIPSTA_CUR[] PROGMEM = "AT+CIPSTA_CUR=\"";


const char CWMODE_1[] PROGMEM = "AT+CWMODE=1";
const char CWMODE_3[] PROGMEM = "AT+CWMODE=3";
Expand Down Expand Up @@ -205,6 +207,18 @@ bool SerialESP8266wifi::connectToAP(){
return isConnectedToAP();
}

bool SerialESP8266wifi::setStaticIp(const char* ip, const char* gateway, const char* mask){
writeCommand(CIPSTA_CUR);
_serialOut -> print(ip);
writeCommand(COMMA_2);
_serialOut -> print(gateway);
writeCommand(COMMA_2);
_serialOut -> print(mask);
writeCommand(DOUBLE_QUOTE, EOL);
readCommand(1000, OK, FAIL);

}

bool SerialESP8266wifi::isConnectedToAP(){
writeCommand(CIFSR, EOL);
byte code = readCommand(350, NO_IP, ERROR);
Expand Down
59 changes: 30 additions & 29 deletions SerialESP8266wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,71 +49,72 @@ struct WifiConnection{

struct Flags // 1 byte value (on a system where 8 bits is a byte
{
bool started:1,
echoOnOff:1,
debug:1,
bool started:1,
echoOnOff:1,
debug:1,
serverConfigured:1, // true if a connection to a remote server is configured
connectedToServer:1, // true if a connection to a remote server is established
apConfigured:1, // true if the module is configured as a client station
localApConfigured:1,
localServerConfigured:1,
localApRunning:1,
localServerRunning:1,
endSendWithNewline:1,
localApRunning:1,
localServerRunning:1,
endSendWithNewline:1,
connectToServerUsingTCP:1;
};

class SerialESP8266wifi
{

public:
/*
* Will pull resetPin low then high to reset esp8266, connect this pin to CHPD pin
*/
SerialESP8266wifi(Stream &serialIn, Stream &serialOut, byte resetPin);


/*
* Will pull resetPin low then high to reset esp8266, connect this pin to CHPD pin
*/
SerialESP8266wifi(Stream &serialIn, Stream &serialOut, byte resetPin, Stream &dbgSerial);

/*
* Will do hw reset and set inital configuration, will try this HW_RESET_RETRIES times.
*/
bool begin(); // reset and set echo and other stuff

bool isStarted();

/*
* Connect to AP using wpa encryption
* (reconnect logic is applied, if conn lost or not established, or esp8266 restarted)
*/
bool connectToAP(String& ssid, String& password);
bool connectToAP(const char* ssid, const char* password);
bool setStaticIp(const char* ip, const char* gateway, const char* mask);
bool isConnectedToAP();
char* getIP();
char* getMAC();

/*
* Evaluate the connection and perform reconnects if needed. Eventually perform reset and restart.
*
*/
bool watchdog();

/*
* Connecting with TCP to server
* (reconnect logic is applied, if conn lost or not established, or esp8266 restarted)
*/

void setTransportToUDP();
//Default..
void setTransportToTCP();
bool connectToServer(String& ip, String& port);
bool connectToServer(const char* ip, const char* port);
void disconnectFromServer();
bool isConnectedToServer();

/*
* Starting local AP and local TCP-server
* (reconnect logic is applied, if conn lost or not established, or esp8266 restarted)
Expand All @@ -125,55 +126,55 @@ class SerialESP8266wifi
bool stopLocalAP();
bool stopLocalServer();
bool isLocalAPAndServerRunning();


/*
* Send string (if channel is connected of course)
*/
bool send(char channel, String& message, bool sendNow = true);
bool send(char channel, const char * message, bool sendNow = true);

/*
* Default is true.
*/
void endSendWithNewline(bool endSendWithNewline);

/*
* Scan for incoming message, do this as often and as long as you can (use as sleep in loop)
*/
WifiMessage listenForIncomingMessage(int timeoutMillis);
WifiMessage getIncomingMessage(void);
bool isConnection(void);
bool checkConnections(WifiConnection **pConnections);

private:
Stream* _serialIn;
Stream* _serialOut;
byte _resetPin;

Flags flags;

bool connectToServer();
char _ip[16];
char _port[6];

bool connectToAP();
char _ssid[16];
char _password[16];

bool startLocalAp();
bool startLocalServer();
char _localAPSSID[16];
char _localAPPassword[16];
char _localAPChannel[3];
char _localServerPort[6];
WifiConnection _connections[MAX_CONNECTIONS];

bool restart();

byte serverRetries;


char msgOut[MSG_BUFFER_MAX];//buffer for send method
char msgIn[MSG_BUFFER_MAX]; //buffer for listen method = limit of incoming message..

Expand Down