Skip to content

Commit 9dfdfd5

Browse files
committed
update examples
1 parent 19a0a0b commit 9dfdfd5

File tree

21 files changed

+268
-113
lines changed

21 files changed

+268
-113
lines changed

Diff for: libraries/ESP8266WiFi/examples/BearSSL_CertStore/BearSSL_CertStore.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@
3737
#include <CertStoreBearSSL.h>
3838
#include <time.h>
3939

40-
const char *ssid = "....";
41-
const char *pass = "....";
40+
#ifndef SSID
41+
#define SSID "your-ssid"
42+
#define PSK "your-password"
43+
#endif
44+
45+
const char *ssid = SSID;
46+
const char *pass = PSK;
4247

4348
// A single, global CertStore which can be used by all
4449
// connections. Needs to stay live the entire time any of

Diff for: libraries/ESP8266WiFi/examples/BearSSL_MaxFragmentLength/BearSSL_MaxFragmentLength.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66

77
#include <ESP8266WiFi.h>
88

9-
const char *ssid = "....";
10-
const char *pass = "....";
9+
#ifndef SSID
10+
#define SSID "your-ssid"
11+
#define PSK "your-password"
12+
#endif
13+
14+
const char *ssid = SSID;
15+
const char *pass = PSK;
1116

1217
void fetch(BearSSL::WiFiClientSecure *client) {
1318
client->write("GET / HTTP/1.0\r\nHost: tls.mbed.org\r\nUser-Agent: ESP8266\r\n\r\n");

Diff for: libraries/ESP8266WiFi/examples/BearSSL_Server/BearSSL_Server.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@
3737
#include <ESP8266WiFi.h>
3838
#include <time.h>
3939

40-
const char *ssid = "....";
41-
const char *pass = "....";
40+
#ifndef SSID
41+
#define SSID "your-ssid"
42+
#define PSK "your-password"
43+
#endif
44+
45+
const char *ssid = SSID;
46+
const char *pass = PSK;
4247

4348
// The HTTPS server
4449
BearSSL::WiFiServerSecure server(443);

Diff for: libraries/ESP8266WiFi/examples/BearSSL_ServerClientCert/BearSSL_ServerClientCert.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@
6565
#include <ESP8266WiFi.h>
6666
#include <time.h>
6767

68-
const char *ssid = "....";
69-
const char *pass = "....";
68+
#ifndef SSID
69+
#define SSID "your-ssid"
70+
#define PSK "your-password"
71+
#endif
72+
73+
const char *ssid = SSID;
74+
const char *pass = PSK;
7075

7176
// The server which will require a client cert signed by the trusted CA
7277
BearSSL::WiFiServerSecure server(443);

Diff for: libraries/ESP8266WiFi/examples/BearSSL_Sessions/BearSSL_Sessions.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
#include <ESP8266WiFi.h>
77
#include <time.h>
88

9-
const char *ssid = "....";
10-
const char *pass = "....";
9+
#ifndef SSID
10+
#define SSID "your-ssid"
11+
#define PSK "your-password"
12+
#endif
13+
14+
const char *ssid = SSID;
15+
const char *pass = PSK;
1116

1217
const char * host = "api.github.com";
1318
const uint16_t port = 443;

Diff for: libraries/ESP8266WiFi/examples/BearSSL_Validation/BearSSL_Validation.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
#include <ESP8266WiFi.h>
88
#include <time.h>
99

10-
const char *ssid = "....";
11-
const char *pass = "....";
10+
#ifndef SSID
11+
#define SSID "your-ssid"
12+
#define PSK "your-password"
13+
#endif
14+
15+
const char *ssid = SSID;
16+
const char *pass = PSK;
1217

1318
const char * host = "api.github.com";
1419
const uint16_t port = 443;

Diff for: libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino

+10-8
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@
1919
#include <ESP8266WiFi.h>
2020
#include <WiFiClientSecure.h>
2121

22-
const char* ssid = "........";
23-
const char* password = "........";
22+
#ifndef SSID
23+
#define SSID "your-ssid"
24+
#define PSK "your-password"
25+
#endif
26+
27+
const char* ssid = SSID;
28+
const char* password = PSK;
2429

2530
const char* host = "api.github.com";
2631
const int httpsPort = 443;
2732

2833
// Use web browser to view and copy
2934
// SHA1 fingerprint of the certificate
30-
const char* fingerprint = "5F F1 60 31 09 04 3E F2 90 D2 B0 8A 50 38 04 E8 37 9F BC 76";
35+
const char fingerprint[] PROGMEM = "5F F1 60 31 09 04 3E F2 90 D2 B0 8A 50 38 04 E8 37 9F BC 76";
3136

3237
void setup() {
3338
Serial.begin(115200);
@@ -54,11 +59,8 @@ void setup() {
5459
return;
5560
}
5661

57-
if (client.verify(fingerprint, host)) {
58-
Serial.println("certificate matches");
59-
} else {
60-
Serial.println("certificate doesn't match");
61-
}
62+
Serial.printf("Using fingerprint '%s'\n", fingerprint);
63+
client.setFingerprint(fingerprint);
6264

6365
String url = "/repos/esp8266/Arduino/commits/master/status";
6466
Serial.print("requesting URL: ");

Diff for: libraries/ESP8266WiFi/examples/HTTPSRequestCACert/HTTPSRequestCACert.ino

+14-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@
1717

1818
#include <time.h>
1919
#include <ESP8266WiFi.h>
20-
#include <WiFiClientSecure.h>
2120

22-
const char* ssid = "........";
23-
const char* password = "........";
21+
// force use of AxTLS (BearSSL is now default)
22+
#include <WiFiClientSecureAxTLS.h>
23+
using namespace AxTLS;
24+
25+
// uncomment the line below to run the sketch
26+
#error Keeping this example for history, watch BearSSL_Validation example instead
27+
28+
#ifndef SSID
29+
#define SSID "your-ssid"
30+
#define PSK "your-password"
31+
#endif
32+
33+
const char* ssid = SSID;
34+
const char* password = PSK;
2435

2536
const char* host = "api.github.com";
2637
const int httpsPort = 443;

Diff for: libraries/ESP8266WiFi/examples/NTPClient/NTPClient.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@
2121
#include <ESP8266WiFi.h>
2222
#include <WiFiUdp.h>
2323

24-
char ssid[] = "*************"; // your network SSID (name)
25-
char pass[] = "********"; // your network password
24+
#ifndef SSID
25+
#define SSID "your-ssid"
26+
#define PSK "your-password"
27+
#endif
28+
29+
char ssid[] = SSID; // your network SSID (name)
30+
char pass[] = PSK; // your network password
2631

2732

2833
unsigned int localPort = 2390; // local port to listen for UDP packets

Diff for: libraries/ESP8266WiFi/examples/WiFiAccessPoint/WiFiAccessPoint.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@
3434
#include <WiFiClient.h>
3535
#include <ESP8266WebServer.h>
3636

37+
#ifndef APSSID
38+
#define APSSID "ESPap"
39+
#define APPSK "thereisnospoon"
40+
#endif
41+
3742
/* Set these to your desired credentials. */
38-
const char *ssid = "ESPap";
39-
const char *password = "thereisnospoon";
43+
const char *ssid = APSSID;
44+
const char *password = APPSK;
4045

4146
ESP8266WebServer server(80);
4247

Diff for: libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino

+12-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55

66
#include <ESP8266WiFi.h>
77

8-
const char* ssid = "your-ssid";
9-
const char* password = "your-password";
8+
#ifndef SSID
9+
#define SSID "your-ssid"
10+
#define PSK "your-password"
11+
#endif
12+
13+
const char* ssid = SSID;
14+
const char* password = PSK;
1015

1116
const char* host = "djxmmx.net";
1217
const uint16_t port = 17;
@@ -54,7 +59,10 @@ void loop() {
5459

5560
// This will send a string to the server
5661
Serial.println("sending data to server");
57-
client.println("hello from ESP8266");
62+
if (client.connected())
63+
client.println("hello from ESP8266");
64+
65+
// wait for data to be available
5866
unsigned long timeout = millis();
5967
while (client.available() == 0) {
6068
if (millis() - timeout > 5000) {
@@ -67,6 +75,7 @@ void loop() {
6775

6876
// Read all the lines of the reply from server and print them to Serial
6977
Serial.println("receiving from remote server");
78+
// not testing 'client.connected()' since we do not need to send data here
7079
while (client.available()) {
7180
char ch = static_cast<char>(client.read());
7281
Serial.print(ch);

Diff for: libraries/ESP8266WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
#include <ESP8266WiFi.h>
88
#include <ESP8266WiFiMulti.h>
99

10-
const char* ssid = "your-ssid";
11-
const char* password = "your-password";
10+
#ifndef SSID
11+
#define SSID "your-ssid"
12+
#define PSK "your-password"
13+
#endif
14+
15+
const char* ssid = SSID;
16+
const char* password = PSK;
1217

1318
const char* host = "192.168.1.1";
1419
const uint16_t port = 3000;

Diff for: libraries/ESP8266WiFi/examples/WiFiEvents/WiFiEvents.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616
#include <ESP8266WiFi.h>
1717
#include <stdio.h>
1818

19-
const char* ssid = "ap-ssid";
20-
const char* password = "ap-password";
19+
#ifndef APSSID
20+
#define APSSID "esp8266"
21+
#define APPSK "esp8266"
22+
#endif
23+
24+
const char* ssid = APSSID;
25+
const char* password = APPSK;
2126

2227
WiFiEventHandler stationConnectedHandler;
2328
WiFiEventHandler stationDisconnectedHandler;

Diff for: libraries/ESP8266WiFi/examples/WiFiHTTPSServer/WiFiHTTPSServer.ino

+7-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@
4343

4444
#include <ESP8266WiFi.h>
4545

46-
const char* ssid = "your-ssid";
47-
const char* password = "your-password";
46+
#ifndef SSID
47+
#define SSID "your-ssid"
48+
#define PSK "your-password"
49+
#endif
50+
51+
const char* ssid = SSID;
52+
const char* password = PSK;
4853

4954
// The certificate is stored in PMEM
5055
static const uint8_t x509[] PROGMEM = {

Diff for: libraries/ESP8266WiFi/examples/WiFiWebServer/WiFiWebServer.ino renamed to libraries/ESP8266WiFi/examples/WiFiManualWebServer/WiFiManualWebServer.ino

+31-26
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99

1010
#include <ESP8266WiFi.h>
1111

12-
const char* ssid = "your-ssid";
13-
const char* password = "your-password";
12+
#ifndef SSID
13+
#define SSID "your-ssid"
14+
#define PSK "your-password"
15+
#endif
16+
17+
const char* ssid = SSID;
18+
const char* password = PSK;
1419

1520
// Create an instance of the server
1621
// specify the port to listen on as an argument
@@ -19,9 +24,9 @@ WiFiServer server(80);
1924
void setup() {
2025
Serial.begin(115200);
2126

22-
// prepare GPIO2
23-
pinMode(2, OUTPUT);
24-
digitalWrite(2, 0);
27+
// prepare LED
28+
pinMode(BUILTIN_LED, OUTPUT);
29+
digitalWrite(BUILTIN_LED, 0);
2530

2631
// Connect to WiFi network
2732
Serial.println();
@@ -53,17 +58,14 @@ void loop() {
5358
if (!client) {
5459
return;
5560
}
56-
57-
// Wait until the client sends some data
5861
Serial.println("new client");
59-
while (!client.available()) {
60-
delay(1);
61-
}
62+
63+
client.setTimeout(5000); // default is 1000
6264

6365
// Read the first line of the request
6466
String req = client.readStringUntil('\r');
67+
Serial.println("request: ");
6568
Serial.println(req);
66-
client.flush();
6769

6870
// Match the request
6971
int val;
@@ -77,22 +79,25 @@ void loop() {
7779
return;
7880
}
7981

80-
// Set GPIO2 according to the request
81-
digitalWrite(2, val);
82+
// Set LED according to the request
83+
digitalWrite(BUILTIN_LED, val);
8284

83-
client.flush();
84-
85-
// Prepare the response
86-
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
87-
s += (val) ? "high" : "low";
88-
s += "</html>\n";
85+
// read/ignore the rest of the request
86+
// do not client.flush(): it is for output only, see below
87+
while (client.available())
88+
// byte by byte is not very efficient
89+
client.read();
8990

9091
// Send the response to the client
91-
client.print(s);
92-
delay(1);
93-
Serial.println("Client disonnected");
94-
95-
// The client will actually be disconnected
96-
// when the function returns and 'client' object is detroyed
92+
// it is OK for multiple small client.print/write,
93+
// because nagle algorithm will group them into one single packet
94+
// ::print*_P(F("string")) is always usable and saves RAM
95+
client.print_P(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now "));
96+
client.print_P((val) ? F("high" ): F("low"));
97+
client.print_P(F("</html>\n"));
98+
99+
// The client will actually be *flushed* then disconnected
100+
// when the function returns and 'client' object is destroyed (out-of-scope)
101+
// flush = ensure written data are received by the other side
102+
Serial.println("Disconnecting from client");
97103
}
98-

0 commit comments

Comments
 (0)