Skip to content

Commit

Permalink
SSL support
Browse files Browse the repository at this point in the history
  • Loading branch information
bportaluri committed Mar 2, 2016
1 parent fda7e19 commit 61aa610
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2.1 (2016-xx-x)
* SSL connection support
* Implementation of config and configAP methods
* Fixed read methods WiFiEspClient.read(buf, size) and WiFiEspUDP::read(buf, size)
* Fixed possible buffer overflow in EspDrv.sendCmdGet
* Added beginAP methods similar to WiFi101 library - parameters order for beginAP is now different!

1.6 (2016-02-21)
* Improved UDP support
* Added WiFiEspClient.remoteIP() method
Expand Down
106 changes: 106 additions & 0 deletions examples/WebClientSSL/WebClientSSL.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
WiFiEsp example: WebClient
This sketch connects to google website using an ESP8266 module to
perform a simple web search.
For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-client.html
*/

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

char ssid[] = "Twim"; // your network SSID (name)
char pass[] = "12345678"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status

char server[] = "www.google.com";

// Initialize the Ethernet client object
WiFiEspClient client;

void setup()
{
// initialize serial for debugging
Serial.begin(115200);
// initialize serial for ESP module
Serial1.begin(9600);
// initialize ESP module
WiFi.init(&Serial1);

// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}

// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}

// you're connected now, so print out the data
Serial.println("You're connected to the network");

printWifiStatus();

Serial.println();
Serial.println("Starting connection to server...");
// if you get a connection, report back via serial
if (client.connectSSL(server, 443)) {
Serial.println("Connected to server");
// Make a HTTP request
client.println("GET / HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
}

void loop()
{
// if there are incoming bytes available
// from the server, read them and print them
while (client.available()) {
char c = client.read();
Serial.write(c);
}

// if the server's disconnected, stop the client
if (!client.connected()) {
Serial.println();
Serial.println("Disconnecting from server...");
client.stop();

// do nothing forevermore
while (true);
}
}


void printWifiStatus()
{
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
22 changes: 8 additions & 14 deletions src/WiFiEspClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,39 @@ size_t WiFiEspClient::println(const __FlashStringHelper *ifsh)

int WiFiEspClient::connectSSL(const char* host, uint16_t port)
{
// SSL support in SDK 1.5.2 is still broken
return 0;
//return connect(host, port, true);
return connect(host, port, SSL_MODE);
}

int WiFiEspClient::connectSSL(IPAddress ip, uint16_t port)
{
// SSL support in SDK 1.5.2 is still broken
return 0;

//char s[16];
//sprintf(s, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);

//return connect(s, port, true);
char s[16];
sprintf(s, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
return connect(s, port, SSL_MODE);
}

int WiFiEspClient::connect(const char* host, uint16_t port)
{
return connect(host, port, false);
return connect(host, port, TCP_MODE);
}

int WiFiEspClient::connect(IPAddress ip, uint16_t port)
{
char s[16];
sprintf(s, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);

return connect(s, port, false);
return connect(s, port, TCP_MODE);
}

/* Private method */
int WiFiEspClient::connect(const char* host, uint16_t port, bool ssl)
int WiFiEspClient::connect(const char* host, uint16_t port, uint8_t protMode)
{
LOGINFO1(F("Connecting to"), host);

_sock = getFirstSocket();

if (_sock != NO_SOCKET_AVAIL)
{
if (!EspDrv::startClient(host, port, _sock))
if (!EspDrv::startClient(host, port, _sock, protMode))
return 0;

WiFiEspClass::_state[_sock] = _sock;
Expand Down
2 changes: 1 addition & 1 deletion src/WiFiEspClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class WiFiEspClient : public Client
uint8_t _sock; // connection id

uint8_t getFirstSocket();
int connect(const char* host, uint16_t port, bool ssl);
int connect(const char* host, uint16_t port, uint8_t protMode);

size_t printFSH(const __FlashStringHelper *ifsh, bool appendCrLf);

Expand Down
1 change: 1 addition & 0 deletions src/WiFiEspServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ WiFiEspClient WiFiEspServer::available(byte* status)
int bytes = EspDrv::availData(0);
if (bytes>0)
{
LOGINFO1(F("New client"), EspDrv::_connId);
WiFiEspClient client(EspDrv::_connId);
return client;
}
Expand Down
12 changes: 10 additions & 2 deletions src/utility/EspDrv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void EspDrv::wifiDriverInit(Stream *espSerial)
if (sendCmd(F("AT")) != TAG_OK)
{
LOGERROR(F("Cannot initialize ESP module"));
delay(8000);
delay(5000);
return;
}

Expand Down Expand Up @@ -112,6 +112,7 @@ void EspDrv::reset()

// set station mode
sendCmd(F("AT+CWMODE=1"));
delay(200);

// set multiple connections mode
sendCmd(F("AT+CIPMUX=1"));
Expand All @@ -125,6 +126,7 @@ void EspDrv::reset()

// enable DHCP
sendCmd(F("AT+CWDHCP=1,1"));
delay(200);
}


Expand Down Expand Up @@ -572,7 +574,13 @@ bool EspDrv::startClient(const char* host, uint16_t port, uint8_t sock, uint8_t
int ret;
if (protMode==TCP_MODE)
ret = sendCmd(F("AT+CIPSTART=%d,\"TCP\",\"%s\",%u"), 5000, sock, host, port);
else
else if (protMode==SSL_MODE)
{
// better to put the CIPSSLSIZE here because it is not supported before firmware 1.4
sendCmd(F("AT+CIPSSLSIZE=4096"));
ret = sendCmd(F("AT+CIPSTART=%d,\"SSL\",\"%s\",%u"), 5000, sock, host, port);
}
else if (protMode==UDP_MODE)
ret = sendCmd(F("AT+CIPSTART=%d,\"UDP\",\"%s\",0,%u,2"), 5000, sock, host, port);

return ret==TAG_OK;
Expand Down
4 changes: 2 additions & 2 deletions src/utility/EspDrv.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ along with The Arduino WiFiEsp library. If not, see
#define CMD_BUFFER_SIZE 200


typedef enum eProtMode {TCP_MODE, UDP_MODE} tProtMode;
typedef enum eProtMode {TCP_MODE, UDP_MODE, SSL_MODE} tProtMode;


typedef enum {
Expand Down Expand Up @@ -249,7 +249,7 @@ class EspDrv


static bool startServer(uint16_t port);
static bool startClient(const char* host, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
static bool startClient(const char* host, uint16_t port, uint8_t sock, uint8_t protMode);
static void stopClient(uint8_t sock);
static uint8_t getServerState(uint8_t sock);
static uint8_t getClientState(uint8_t sock);
Expand Down
48 changes: 48 additions & 0 deletions test/EspDebug/EspDebug.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// EspDebug - Test sketch for ESP8266 module

#include "Arduino.h"

// Emulate Serial1 on pins 7/6 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

void setup()
{
Serial.begin(115200); // serial port used for debugging
Serial1.begin(9600); // your ESP's baud rate might be different
}

void loop()
{
if(Serial1.available()) // check if the ESP is sending a message
{
while(Serial1.available())
{
int c = Serial1.read(); // read the next character
Serial.write((char)c); // writes data to the serial monitor
}
}

if(Serial.available())
{
// wait to let all the input command in the serial buffer
delay(10);

// read the input command in a string
String cmd = "";
while(Serial.available())
{
cmd += (char)Serial.read();
}

// print the command and send it to the ESP
Serial.println();
Serial.print(">>>> ");
Serial.println(cmd);

// send the read character to the ESP
Serial1.print(cmd);
}
}

0 comments on commit 61aa610

Please sign in to comment.