Skip to content

Commit

Permalink
Re-implement WiFi socket features (#16)
Browse files Browse the repository at this point in the history
Current implementation is based on asynchronous LwIP APIs, and there are memory management issues that cause #13.

We re-implmemented the sockets with LwIP BSD-style socket APIs, which are blocking calls that are better suited to Arduino calling styles and easier memory management. This fixed #13.

This pull request also have following improveent:

 * Reduced waiting time for WiFi.begin().
 * Remove redundant checks and waits in WiFi example sketches. (The 10-second wait after `begin()` is unnecessary).
 * Adds MCS classes' syntax coloring to fix issue #15.
  • Loading branch information
pablosun committed May 23, 2017
1 parent 8db6ec2 commit 3f08204
Show file tree
Hide file tree
Showing 34 changed files with 575 additions and 2,403 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
the IP address obtained, and other network details.
Circuit:
* WiFi shield attached
* LinkIt 7697 HDK
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 23 May 2017
by MediaTek Labs
*/
#include <LWiFi.h>

Expand All @@ -23,18 +25,6 @@ void setup() {
; // wait for serial port to connect. Needed for native USB port only
}

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

String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to open SSID: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
all in the 0-9, A-F range.
Circuit:
* WiFi shield attached
* LinkIt 7697 HDK
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 23 May 2017
by MediaTek Labs
*/
#include <LWiFi.h>

Expand All @@ -35,26 +37,11 @@ void setup() {
; // wait for serial port to connect. Needed for native USB port only
}

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

String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WEP network, SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, keyIndex, key);

// wait 10 seconds for connection:
delay(10000);
}

// once you are connected :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
the IP address obtained, and other network details.
Circuit:
* WiFi shield attached
* LinkIt 7697 HDK
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 23 May 2017
by MediaTek Labs
*/
#include <LWiFi.h>

Expand All @@ -24,27 +26,12 @@ void setup() {
; // wait for serial port to connect. Needed for native USB port only
}

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

String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}

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

// wait 10 seconds for connection:
delay(10000);
}

// you're connected now, so print out the data:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
connect to any network, so no encryption scheme is specified.
Circuit:
* WiFi shield attached
* LinkIt 7697 HDK
created 13 July 2010
by dlf (Metodo2 srl)
modified 21 Junn 2012
by Tom Igoe and Jaymes Dec
modified 23 May 2017
by MediaTek Labs
*/

#include <LWiFi.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* WiFi shield attached
* LinkIt 7697 HDK
* LED attached to pin 9
created 25 Nov 2012
by Tom Igoe
modified 23 May 2017
by MediaTek Labs
*/
#include <LWiFi.h>

Expand All @@ -31,18 +33,7 @@ WiFiServer server(80);

void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(9, OUTPUT); // set the LED pin mode

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

String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}
pinMode(LED_BUILTIN, OUTPUT); // set the LED pin mode

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Expand All @@ -51,8 +42,6 @@ void setup() {

// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you're connected now, so print out the status
Expand Down Expand Up @@ -81,8 +70,8 @@ void loop() {
client.println();

// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on<br>");
client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off<br>");
client.print("Click <a href=\"/H\">here</a> turn the LED on pin 7 on<br>");
client.print("Click <a href=\"/L\">here</a> turn the LED on pin 7 off<br>");

// The HTTP response ends with another blank line:
client.println();
Expand All @@ -96,11 +85,11 @@ void loop() {
}

// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(9, HIGH); // GET /H turns the LED on
if (currentLine.startsWith("GET /H")) {
digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(9, LOW); // GET /L turns the LED off
if (currentLine.startsWith("GET /L")) {
digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
Web client
TLS client
This sketch connects to a website (http://download.labs.mediatek.com)
using LinkIt 7697
This sketch connects to a website
using LinkIt 7697 over TLS (HTTPS)
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Expand Down Expand Up @@ -69,8 +69,6 @@ void setup() {
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 2 seconds for connection:
delay(2000);
}
Serial.println("Connected to wifi");
printWifiStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
Circuit:
* WiFi shield attached
* LinkIt 7697 HDK
created 18 Dec 2009
by David A. Mellis
modified 31 May 2012
by Tom Igoe
modified Jan 2017
by MediaTek Labs
*/

#include <LWiFi.h>
Expand All @@ -39,27 +40,12 @@ void setup() {
; // wait for serial port to connect. Needed for native USB port only
}

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

String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

// start the server:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
by Michael Margolis
modified 9 Apr 2012
by Tom Igoe
modified Jan 2017
by MediaTek Labs
This code is in the public domain.
*/

Expand All @@ -25,7 +27,7 @@ int keyIndex = 0; // your network key Index number (needed only for W
unsigned int localPort = 2390; // local port to listen for UDP packets

IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
char *NTP_server = "time-a.nist.gov";
const char *NTP_server = "time-a.nist.gov";

const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message

Expand All @@ -41,27 +43,12 @@ void setup() {
; // wait for serial port to connect. Needed for native USB port only
}

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

String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

Serial.println("Connected to wifi");
Expand Down Expand Up @@ -122,7 +109,7 @@ void loop() {
}

// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(char* host) {
unsigned long sendNTPpacket(const char* host) {
//Serial.println("1");
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
When a packet is received an Acknowledge packet is sent to the client on port remotePort
Circuit:
* WiFi shield attached
* LinkIt 7697 HDK
created 30 December 2012
by dlf (Metodo2 srl)
modified Jan 2017
by MediaTek Labs
*/

#include <LWiFi.h>
Expand All @@ -33,27 +35,12 @@ void setup() {
; // wait for serial port to connect. Needed for native USB port only
}

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

String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
Expand Down

0 comments on commit 3f08204

Please sign in to comment.