mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-27 18:02:17 +03:00
Migrate from astyle to clang-format (#8464)
This commit is contained in:
committed by
Max Prokhorov
parent
46190b61f1
commit
19b7a29720
@ -41,7 +41,7 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char *ssid = STASSID;
|
||||
@ -72,9 +72,7 @@ void setClock() {
|
||||
|
||||
// Try and connect using a WiFiClientBearSSL to specified host:port and dump URL
|
||||
void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_t port, const char *path) {
|
||||
if (!path) {
|
||||
path = "/";
|
||||
}
|
||||
if (!path) { path = "/"; }
|
||||
|
||||
Serial.printf("Trying: %s:443...", host);
|
||||
client->connect(host, port);
|
||||
@ -94,11 +92,9 @@ void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_
|
||||
do {
|
||||
char tmp[32];
|
||||
memset(tmp, 0, 32);
|
||||
int rlen = client->read((uint8_t*)tmp, sizeof(tmp) - 1);
|
||||
int rlen = client->read((uint8_t *)tmp, sizeof(tmp) - 1);
|
||||
yield();
|
||||
if (rlen < 0) {
|
||||
break;
|
||||
}
|
||||
if (rlen < 0) { break; }
|
||||
// Only print out first line up to \r, then abort connection
|
||||
char *nl = strchr(tmp, '\r');
|
||||
if (nl) {
|
||||
@ -136,13 +132,13 @@ void setup() {
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
setClock(); // Required for X.509 validation
|
||||
setClock(); // Required for X.509 validation
|
||||
|
||||
int numCerts = certStore.initCertStore(LittleFS, PSTR("/certs.idx"), PSTR("/certs.ar"));
|
||||
Serial.printf("Number of CA certs read: %d\n", numCerts);
|
||||
if (numCerts == 0) {
|
||||
Serial.printf("No certs found. Did you run certs-from-mozilla.py and upload the LittleFS directory before running?\n");
|
||||
return; // Can't connect to anything w/o certs!
|
||||
return; // Can't connect to anything w/o certs!
|
||||
}
|
||||
|
||||
BearSSL::WiFiClientSecure *bear = new BearSSL::WiFiClientSecure();
|
||||
@ -156,9 +152,7 @@ void setup() {
|
||||
void loop() {
|
||||
Serial.printf("\nPlease enter a website address (www.blah.com) to connect to: ");
|
||||
String site;
|
||||
do {
|
||||
site = Serial.readString();
|
||||
} while (site == "");
|
||||
do { site = Serial.readString(); } while (site == "");
|
||||
// Strip newline if present
|
||||
site.replace(String("\r"), emptyString);
|
||||
site.replace(String("\n"), emptyString);
|
||||
@ -170,4 +164,3 @@ void loop() {
|
||||
fetchURL(bear, site.c_str(), 443, "/");
|
||||
delete bear;
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char *ssid = STASSID;
|
||||
@ -22,13 +22,11 @@ void fetch(BearSSL::WiFiClientSecure *client) {
|
||||
oneShot timeout(5000);
|
||||
do {
|
||||
char tmp[32];
|
||||
int rlen = client->read((uint8_t*)tmp, sizeof(tmp) - 1);
|
||||
int rlen = client->read((uint8_t *)tmp, sizeof(tmp) - 1);
|
||||
yield();
|
||||
if (rlen < 0) {
|
||||
break;
|
||||
}
|
||||
if (rlen < 0) { break; }
|
||||
if (rlen == 0) {
|
||||
delay(10); // Give background processes some time
|
||||
delay(10); // Give background processes some time
|
||||
continue;
|
||||
}
|
||||
tmp[rlen] = '\0';
|
||||
@ -82,9 +80,7 @@ int fetchMaxFragmentLength() {
|
||||
bool mfln = client.probeMaxFragmentLength("tls.mbed.org", 443, 512);
|
||||
Serial.printf("\nConnecting to https://tls.mbed.org\n");
|
||||
Serial.printf("MFLN supported: %s\n", mfln ? "yes" : "no");
|
||||
if (mfln) {
|
||||
client.setBufferSizes(512, 512);
|
||||
}
|
||||
if (mfln) { client.setBufferSizes(512, 512); }
|
||||
client.connect("tls.mbed.org", 443);
|
||||
if (client.connected()) {
|
||||
Serial.printf("MFLN status: %s\n", client.getMFLNStatus() ? "true" : "false");
|
||||
|
@ -39,7 +39,7 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char *ssid = STASSID;
|
||||
@ -138,11 +138,11 @@ GBEnkz4KpKv7TkHoW+j7F5EMcLcSrUIpyw==
|
||||
|
||||
#endif
|
||||
|
||||
#define CACHE_SIZE 5 // Number of sessions to cache.
|
||||
#define USE_CACHE // Enable SSL session caching.
|
||||
// Caching SSL sessions shortens the length of the SSL handshake.
|
||||
// You can see the performance improvement by looking at the
|
||||
// Network tab of the developer tools of your browser.
|
||||
#define CACHE_SIZE 5 // Number of sessions to cache.
|
||||
#define USE_CACHE // Enable SSL session caching.
|
||||
// Caching SSL sessions shortens the length of the SSL handshake.
|
||||
// You can see the performance improvement by looking at the
|
||||
// Network tab of the developer tools of your browser.
|
||||
//#define DYNAMIC_CACHE // Whether to dynamically allocate the cache.
|
||||
|
||||
#if defined(USE_CACHE) && defined(DYNAMIC_CACHE)
|
||||
@ -181,7 +181,7 @@ void setup() {
|
||||
#ifndef USE_EC
|
||||
server.setRSACert(serverCertList, serverPrivKey);
|
||||
#else
|
||||
server.setECCert(serverCertList, BR_KEYTYPE_KEYX|BR_KEYTYPE_SIGN, serverPrivKey);
|
||||
server.setECCert(serverCertList, BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN, serverPrivKey);
|
||||
#endif
|
||||
|
||||
// Set the server's cache
|
||||
@ -193,31 +193,28 @@ void setup() {
|
||||
server.begin();
|
||||
}
|
||||
|
||||
static const char *HTTP_RES =
|
||||
"HTTP/1.0 200 OK\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Content-Length: 62\r\n"
|
||||
"Content-Type: text/html; charset=iso-8859-1\r\n"
|
||||
"\r\n"
|
||||
"<html>\r\n"
|
||||
"<body>\r\n"
|
||||
"<p>Hello from ESP8266!</p>\r\n"
|
||||
"</body>\r\n"
|
||||
"</html>\r\n";
|
||||
static const char *HTTP_RES = "HTTP/1.0 200 OK\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Content-Length: 62\r\n"
|
||||
"Content-Type: text/html; charset=iso-8859-1\r\n"
|
||||
"\r\n"
|
||||
"<html>\r\n"
|
||||
"<body>\r\n"
|
||||
"<p>Hello from ESP8266!</p>\r\n"
|
||||
"</body>\r\n"
|
||||
"</html>\r\n";
|
||||
|
||||
void loop() {
|
||||
static int cnt;
|
||||
BearSSL::WiFiClientSecure incoming = server.accept();
|
||||
if (!incoming) {
|
||||
return;
|
||||
}
|
||||
Serial.printf("Incoming connection...%d\n",cnt++);
|
||||
|
||||
if (!incoming) { return; }
|
||||
Serial.printf("Incoming connection...%d\n", cnt++);
|
||||
|
||||
// Ugly way to wait for \r\n (i.e. end of HTTP request which we don't actually parse here)
|
||||
uint32_t timeout=millis() + 1000;
|
||||
uint32_t timeout = millis() + 1000;
|
||||
int lcwn = 0;
|
||||
for (;;) {
|
||||
unsigned char x=0;
|
||||
unsigned char x = 0;
|
||||
if ((millis() > timeout) || (incoming.available() && incoming.read(&x, 1) < 0)) {
|
||||
incoming.stop();
|
||||
Serial.printf("Connection error, closed\n");
|
||||
@ -228,14 +225,12 @@ void loop() {
|
||||
} else if (x == 0x0D) {
|
||||
continue;
|
||||
} else if (x == 0x0A) {
|
||||
if (lcwn) {
|
||||
break;
|
||||
}
|
||||
if (lcwn) { break; }
|
||||
lcwn = 1;
|
||||
} else
|
||||
lcwn = 0;
|
||||
}
|
||||
incoming.write((uint8_t*)HTTP_RES, strlen(HTTP_RES));
|
||||
incoming.write((uint8_t *)HTTP_RES, strlen(HTTP_RES));
|
||||
incoming.flush();
|
||||
incoming.stop();
|
||||
Serial.printf("Connection closed.\n");
|
||||
|
@ -67,7 +67,7 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char *ssid = STASSID;
|
||||
@ -160,8 +160,7 @@ seoK24dHmt6tWmn/sbxX7Aa6TL/4mVlFoOgcaTJyVaY/BrY=
|
||||
// head of the app.
|
||||
|
||||
// Set time via NTP, as required for x.509 validation
|
||||
void setClock()
|
||||
{
|
||||
void setClock() {
|
||||
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
|
||||
|
||||
Serial.print("Waiting for NTP time sync: ");
|
||||
@ -199,7 +198,7 @@ void setup() {
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
setClock(); // Required for X.509 validation
|
||||
setClock(); // Required for X.509 validation
|
||||
|
||||
// Attach the server private cert/key combo
|
||||
BearSSL::X509List *serverCertList = new BearSSL::X509List(server_cert);
|
||||
@ -214,30 +213,27 @@ void setup() {
|
||||
server.begin();
|
||||
}
|
||||
|
||||
static const char *HTTP_RES =
|
||||
"HTTP/1.0 200 OK\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Content-Length: 59\r\n"
|
||||
"Content-Type: text/html; charset=iso-8859-1\r\n"
|
||||
"\r\n"
|
||||
"<html>\r\n"
|
||||
"<body>\r\n"
|
||||
"<p>Hello my friend!</p>\r\n"
|
||||
"</body>\r\n"
|
||||
"</html>\r\n";
|
||||
static const char *HTTP_RES = "HTTP/1.0 200 OK\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Content-Length: 59\r\n"
|
||||
"Content-Type: text/html; charset=iso-8859-1\r\n"
|
||||
"\r\n"
|
||||
"<html>\r\n"
|
||||
"<body>\r\n"
|
||||
"<p>Hello my friend!</p>\r\n"
|
||||
"</body>\r\n"
|
||||
"</html>\r\n";
|
||||
|
||||
void loop() {
|
||||
BearSSL::WiFiClientSecure incoming = server.accept();
|
||||
if (!incoming) {
|
||||
return;
|
||||
}
|
||||
if (!incoming) { return; }
|
||||
Serial.println("Incoming connection...\n");
|
||||
|
||||
|
||||
// Ugly way to wait for \r\n (i.e. end of HTTP request which we don't actually parse here)
|
||||
uint32_t timeout=millis() + 1000;
|
||||
uint32_t timeout = millis() + 1000;
|
||||
int lcwn = 0;
|
||||
for (;;) {
|
||||
unsigned char x=0;
|
||||
unsigned char x = 0;
|
||||
if ((millis() > timeout) || (incoming.available() && incoming.read(&x, 1) < 0)) {
|
||||
incoming.stop();
|
||||
Serial.printf("Connection error, closed\n");
|
||||
@ -248,14 +244,12 @@ void loop() {
|
||||
} else if (x == 0x0D) {
|
||||
continue;
|
||||
} else if (x == 0x0A) {
|
||||
if (lcwn) {
|
||||
break;
|
||||
}
|
||||
if (lcwn) { break; }
|
||||
lcwn = 1;
|
||||
} else
|
||||
lcwn = 0;
|
||||
}
|
||||
incoming.write((uint8_t*)HTTP_RES, strlen(HTTP_RES));
|
||||
incoming.write((uint8_t *)HTTP_RES, strlen(HTTP_RES));
|
||||
incoming.flush();
|
||||
incoming.stop();
|
||||
Serial.printf("Connection closed.\n");
|
||||
|
@ -9,13 +9,13 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char *ssid = STASSID;
|
||||
const char *pass = STAPSK;
|
||||
|
||||
const char * path = "/";
|
||||
const char *path = "/";
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
@ -53,9 +53,7 @@ void setup() {
|
||||
|
||||
// Try and connect using a WiFiClientBearSSL to specified host:port and dump HTTP response
|
||||
void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_t port, const char *path) {
|
||||
if (!path) {
|
||||
path = "/";
|
||||
}
|
||||
if (!path) { path = "/"; }
|
||||
|
||||
Serial.printf("Trying: %s:443...", host);
|
||||
client->connect(host, port);
|
||||
@ -75,11 +73,9 @@ void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_
|
||||
do {
|
||||
char tmp[32];
|
||||
memset(tmp, 0, 32);
|
||||
int rlen = client->read((uint8_t*)tmp, sizeof(tmp) - 1);
|
||||
int rlen = client->read((uint8_t *)tmp, sizeof(tmp) - 1);
|
||||
yield();
|
||||
if (rlen < 0) {
|
||||
break;
|
||||
}
|
||||
if (rlen < 0) { break; }
|
||||
// Only print out first line up to \r, then abort connection
|
||||
char *nl = strchr(tmp, '\r');
|
||||
if (nl) {
|
||||
@ -130,6 +126,5 @@ void loop() {
|
||||
finish = millis();
|
||||
Serial.printf("Total time: %dms\n", finish - start);
|
||||
|
||||
delay(10000); // Avoid DDOSing github
|
||||
delay(10000); // Avoid DDOSing github
|
||||
}
|
||||
|
||||
|
@ -12,13 +12,13 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char *ssid = STASSID;
|
||||
const char *pass = STAPSK;
|
||||
|
||||
const char * path = "/";
|
||||
const char *path = "/";
|
||||
|
||||
// Set time via NTP, as required for x.509 validation
|
||||
void setClock() {
|
||||
@ -40,9 +40,7 @@ void setClock() {
|
||||
|
||||
// Try and connect using a WiFiClientBearSSL to specified host:port and dump HTTP response
|
||||
void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_t port, const char *path) {
|
||||
if (!path) {
|
||||
path = "/";
|
||||
}
|
||||
if (!path) { path = "/"; }
|
||||
|
||||
ESP.resetFreeContStack();
|
||||
uint32_t freeStackStart = ESP.getFreeContStack();
|
||||
@ -64,11 +62,9 @@ void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_
|
||||
do {
|
||||
char tmp[32];
|
||||
memset(tmp, 0, 32);
|
||||
int rlen = client->read((uint8_t*)tmp, sizeof(tmp) - 1);
|
||||
int rlen = client->read((uint8_t *)tmp, sizeof(tmp) - 1);
|
||||
yield();
|
||||
if (rlen < 0) {
|
||||
break;
|
||||
}
|
||||
if (rlen < 0) { break; }
|
||||
// Only print out first line up to \r, then abort connection
|
||||
char *nl = strchr(tmp, '\r');
|
||||
if (nl) {
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char* ssid = STASSID;
|
||||
@ -74,10 +74,7 @@ void setup() {
|
||||
Serial.print("Requesting URL: ");
|
||||
Serial.println(url);
|
||||
|
||||
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
|
||||
"Host: " + github_host + "\r\n" +
|
||||
"User-Agent: BuildFailureDetectorESP8266\r\n" +
|
||||
"Connection: close\r\n\r\n");
|
||||
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + github_host + "\r\n" + "User-Agent: BuildFailureDetectorESP8266\r\n" + "Connection: close\r\n\r\n");
|
||||
|
||||
Serial.println("Request sent");
|
||||
while (client.connected()) {
|
||||
@ -100,5 +97,4 @@ void setup() {
|
||||
Serial.println("Closing connection");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
void loop() {}
|
||||
|
@ -23,12 +23,12 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
#define FQDN F("www.google.com") // with both IPv4 & IPv6 addresses
|
||||
#define FQDN2 F("www.yahoo.com") // with both IPv4 & IPv6 addresses
|
||||
#define FQDN6 F("ipv6.google.com") // does not resolve in IPv4
|
||||
#define FQDN F("www.google.com") // with both IPv4 & IPv6 addresses
|
||||
#define FQDN2 F("www.yahoo.com") // with both IPv4 & IPv6 addresses
|
||||
#define FQDN6 F("ipv6.google.com") // does not resolve in IPv4
|
||||
#define STATUSDELAY_MS 10000
|
||||
#define TCP_PORT 23
|
||||
#define UDP_PORT 23
|
||||
@ -71,29 +71,17 @@ void status(Print& out) {
|
||||
|
||||
for (int i = 0; i < DNS_MAX_SERVERS; i++) {
|
||||
IPAddress dns = WiFi.dnsIP(i);
|
||||
if (dns.isSet()) {
|
||||
out.printf("dns%d: %s\n", i, dns.toString().c_str());
|
||||
}
|
||||
if (dns.isSet()) { out.printf("dns%d: %s\n", i, dns.toString().c_str()); }
|
||||
}
|
||||
|
||||
out.println(F("Try me at these addresses:"));
|
||||
out.println(F("(with 'telnet <addr> or 'nc -u <addr> 23')"));
|
||||
for (auto a : addrList) {
|
||||
out.printf("IF='%s' IPv6=%d local=%d hostname='%s' addr= %s",
|
||||
a.ifname().c_str(),
|
||||
a.isV6(),
|
||||
a.isLocal(),
|
||||
a.ifhostname(),
|
||||
a.toString().c_str());
|
||||
out.printf("IF='%s' IPv6=%d local=%d hostname='%s' addr= %s", a.ifname().c_str(), a.isV6(), a.isLocal(), a.ifhostname(), a.toString().c_str());
|
||||
|
||||
if (a.isLegacy()) {
|
||||
out.printf(" / mask:%s / gw:%s",
|
||||
a.netmask().toString().c_str(),
|
||||
a.gw().toString().c_str());
|
||||
}
|
||||
if (a.isLegacy()) { out.printf(" / mask:%s / gw:%s", a.netmask().toString().c_str(), a.gw().toString().c_str()); }
|
||||
|
||||
out.println();
|
||||
|
||||
}
|
||||
|
||||
// lwIP's dns client will ask for IPv4 first (by default)
|
||||
@ -101,8 +89,8 @@ void status(Print& out) {
|
||||
fqdn(out, FQDN);
|
||||
fqdn(out, FQDN6);
|
||||
#if LWIP_IPV4 && LWIP_IPV6
|
||||
fqdn_rt(out, FQDN, DNSResolveType::DNS_AddrType_IPv4_IPv6); // IPv4 before IPv6
|
||||
fqdn_rt(out, FQDN2, DNSResolveType::DNS_AddrType_IPv6_IPv4); // IPv6 before IPv4
|
||||
fqdn_rt(out, FQDN, DNSResolveType::DNS_AddrType_IPv4_IPv6); // IPv4 before IPv6
|
||||
fqdn_rt(out, FQDN2, DNSResolveType::DNS_AddrType_IPv6_IPv4); // IPv6 before IPv4
|
||||
#endif
|
||||
out.println(F("------------------------------"));
|
||||
}
|
||||
@ -125,7 +113,7 @@ void setup() {
|
||||
|
||||
status(Serial);
|
||||
|
||||
#if 0 // 0: legacy connecting loop - 1: wait for IPv6
|
||||
#if 0 // 0: legacy connecting loop - 1: wait for IPv6
|
||||
|
||||
// legacy loop (still valid with IPv4 only)
|
||||
|
||||
@ -146,11 +134,9 @@ void setup() {
|
||||
for (bool configured = false; !configured;) {
|
||||
for (auto addr : addrList)
|
||||
if ((configured = !addr.isLocal()
|
||||
// && addr.isV6() // uncomment when IPv6 is mandatory
|
||||
// && addr.ifnumber() == STATION_IF
|
||||
)) {
|
||||
break;
|
||||
}
|
||||
// && addr.isV6() // uncomment when IPv6 is mandatory
|
||||
// && addr.ifnumber() == STATION_IF
|
||||
)) { break; }
|
||||
Serial.print('.');
|
||||
delay(500);
|
||||
}
|
||||
@ -188,10 +174,8 @@ void loop() {
|
||||
udp.remoteIP().printTo(Serial);
|
||||
Serial.print(F(" :"));
|
||||
Serial.println(udp.remotePort());
|
||||
int c;
|
||||
while ((c = udp.read()) >= 0) {
|
||||
Serial.write(c);
|
||||
}
|
||||
int c;
|
||||
while ((c = udp.read()) >= 0) { Serial.write(c); }
|
||||
|
||||
// send a reply, to the IP address and port that sent us the packet we received
|
||||
udp.beginPacket(udp.remoteIP(), udp.remotePort());
|
||||
@ -200,8 +184,5 @@ void loop() {
|
||||
}
|
||||
|
||||
|
||||
if (showStatusOnSerialNow) {
|
||||
status(Serial);
|
||||
}
|
||||
|
||||
if (showStatusOnSerialNow) { status(Serial); }
|
||||
}
|
||||
|
@ -23,24 +23,24 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char * ssid = STASSID; // your network SSID (name)
|
||||
const char * pass = STAPSK; // your network password
|
||||
const char* ssid = STASSID; // your network SSID (name)
|
||||
const char* pass = STAPSK; // your network password
|
||||
|
||||
|
||||
unsigned int localPort = 2390; // local port to listen for UDP packets
|
||||
unsigned int localPort = 2390; // local port to listen for UDP packets
|
||||
|
||||
/* Don't hardwire the IP address or we won't get the benefits of the pool.
|
||||
Lookup the IP address for the host name instead */
|
||||
//IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
|
||||
IPAddress timeServerIP; // time.nist.gov NTP server address
|
||||
// IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
|
||||
IPAddress timeServerIP; // time.nist.gov NTP server address
|
||||
const char* ntpServerName = "time.nist.gov";
|
||||
|
||||
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
|
||||
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
|
||||
|
||||
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
|
||||
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
|
||||
|
||||
// A UDP instance to let us send and receive packets over UDP
|
||||
WiFiUDP udp;
|
||||
@ -73,10 +73,10 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//get a random server from the pool
|
||||
// get a random server from the pool
|
||||
WiFi.hostByName(ntpServerName, timeServerIP);
|
||||
|
||||
sendNTPpacket(timeServerIP); // send an NTP packet to a time server
|
||||
sendNTPpacket(timeServerIP); // send an NTP packet to a time server
|
||||
// wait to see if a reply is available
|
||||
delay(1000);
|
||||
|
||||
@ -87,10 +87,10 @@ void loop() {
|
||||
Serial.print("packet received, length=");
|
||||
Serial.println(cb);
|
||||
// We've received a packet, read the data from it
|
||||
udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
|
||||
udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
|
||||
|
||||
//the timestamp starts at byte 40 of the received packet and is four bytes,
|
||||
// or two words, long. First, esxtract the two words:
|
||||
// the timestamp starts at byte 40 of the received packet and is four bytes,
|
||||
// or two words, long. First, esxtract the two words:
|
||||
|
||||
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
|
||||
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
|
||||
@ -112,19 +112,19 @@ void loop() {
|
||||
|
||||
// print the hour, minute and second:
|
||||
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
|
||||
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
|
||||
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
|
||||
Serial.print(':');
|
||||
if (((epoch % 3600) / 60) < 10) {
|
||||
// In the first 10 minutes of each hour, we'll want a leading '0'
|
||||
Serial.print('0');
|
||||
}
|
||||
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
|
||||
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
|
||||
Serial.print(':');
|
||||
if ((epoch % 60) < 10) {
|
||||
// In the first 10 seconds of each minute, we'll want a leading '0'
|
||||
Serial.print('0');
|
||||
}
|
||||
Serial.println(epoch % 60); // print the second
|
||||
Serial.println(epoch % 60); // print the second
|
||||
}
|
||||
// wait ten seconds before asking for the time again
|
||||
delay(10000);
|
||||
@ -137,19 +137,19 @@ void sendNTPpacket(IPAddress& address) {
|
||||
memset(packetBuffer, 0, NTP_PACKET_SIZE);
|
||||
// Initialize values needed to form NTP request
|
||||
// (see URL above for details on the packets)
|
||||
packetBuffer[0] = 0b11100011; // LI, Version, Mode
|
||||
packetBuffer[1] = 0; // Stratum, or type of clock
|
||||
packetBuffer[2] = 6; // Polling Interval
|
||||
packetBuffer[3] = 0xEC; // Peer Clock Precision
|
||||
packetBuffer[0] = 0b11100011; // LI, Version, Mode
|
||||
packetBuffer[1] = 0; // Stratum, or type of clock
|
||||
packetBuffer[2] = 6; // Polling Interval
|
||||
packetBuffer[3] = 0xEC; // Peer Clock Precision
|
||||
// 8 bytes of zero for Root Delay & Root Dispersion
|
||||
packetBuffer[12] = 49;
|
||||
packetBuffer[13] = 0x4E;
|
||||
packetBuffer[14] = 49;
|
||||
packetBuffer[15] = 52;
|
||||
packetBuffer[12] = 49;
|
||||
packetBuffer[13] = 0x4E;
|
||||
packetBuffer[14] = 49;
|
||||
packetBuffer[15] = 52;
|
||||
|
||||
// all NTP fields have been given values, now
|
||||
// you can send a packet requesting a timestamp:
|
||||
udp.beginPacket(address, 123); //NTP requests are to port 123
|
||||
udp.beginPacket(address, 123); // NTP requests are to port 123
|
||||
udp.write(packetBuffer, NTP_PACKET_SIZE);
|
||||
udp.endPacket();
|
||||
}
|
||||
|
@ -25,10 +25,10 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char* ssid = STASSID;
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
|
||||
ArduinoWiFiServer server(2323);
|
||||
@ -60,13 +60,13 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
|
||||
WiFiClient client = server.available(); // returns first client which has data to read or a 'false' client
|
||||
if (client) { // client is true only if it is connected and has data to read
|
||||
String s = client.readStringUntil('\n'); // read the message incoming from one of the clients
|
||||
s.trim(); // trim eventual \r
|
||||
Serial.println(s); // print the message to Serial Monitor
|
||||
client.print("echo: "); // this is only for the sending client
|
||||
server.println(s); // send the message to all connected clients
|
||||
server.flush(); // flush the buffers
|
||||
WiFiClient client = server.available(); // returns first client which has data to read or a 'false' client
|
||||
if (client) { // client is true only if it is connected and has data to read
|
||||
String s = client.readStringUntil('\n'); // read the message incoming from one of the clients
|
||||
s.trim(); // trim eventual \r
|
||||
Serial.println(s); // print the message to Serial Monitor
|
||||
client.print("echo: "); // this is only for the sending client
|
||||
server.println(s); // send the message to all connected clients
|
||||
server.flush(); // flush the buffers
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "mynetwork"
|
||||
#define STAPSK "mynetworkpassword"
|
||||
#define STAPSK "mynetworkpassword"
|
||||
#endif
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
@ -30,7 +30,7 @@ void dump(int netif_idx, const char* data, size_t len, int out, int success) {
|
||||
// optional filter example: if (netDump_is_ARP(data))
|
||||
{
|
||||
netDump(Serial, data, len);
|
||||
//netDumpHex(Serial, data, len);
|
||||
// netDumpHex(Serial, data, len);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -51,19 +51,14 @@ void setup() {
|
||||
Serial.print('.');
|
||||
delay(500);
|
||||
}
|
||||
Serial.printf("\nSTA: %s (dns: %s / %s)\n",
|
||||
WiFi.localIP().toString().c_str(),
|
||||
WiFi.dnsIP(0).toString().c_str(),
|
||||
WiFi.dnsIP(1).toString().c_str());
|
||||
Serial.printf("\nSTA: %s (dns: %s / %s)\n", WiFi.localIP().toString().c_str(), WiFi.dnsIP(0).toString().c_str(), WiFi.dnsIP(1).toString().c_str());
|
||||
|
||||
// give DNS servers to AP side
|
||||
dhcpSoftAP.dhcps_set_dns(0, WiFi.dnsIP(0));
|
||||
dhcpSoftAP.dhcps_set_dns(1, WiFi.dnsIP(1));
|
||||
|
||||
WiFi.softAPConfig( // enable AP, with android-compatible google domain
|
||||
IPAddress(172, 217, 28, 254),
|
||||
IPAddress(172, 217, 28, 254),
|
||||
IPAddress(255, 255, 255, 0));
|
||||
IPAddress(172, 217, 28, 254), IPAddress(172, 217, 28, 254), IPAddress(255, 255, 255, 0));
|
||||
WiFi.softAP(STASSID "extender", STAPSK);
|
||||
Serial.printf("AP: %s\n", WiFi.softAPIP().toString().c_str());
|
||||
|
||||
@ -73,14 +68,10 @@ void setup() {
|
||||
if (ret == ERR_OK) {
|
||||
ret = ip_napt_enable_no(SOFTAP_IF, 1);
|
||||
Serial.printf("ip_napt_enable_no(SOFTAP_IF): ret=%d (OK=%d)\n", (int)ret, (int)ERR_OK);
|
||||
if (ret == ERR_OK) {
|
||||
Serial.printf("WiFi Network '%s' with same password is now NATed behind '%s'\n", STASSID "extender", STASSID);
|
||||
}
|
||||
if (ret == ERR_OK) { Serial.printf("WiFi Network '%s' with same password is now NATed behind '%s'\n", STASSID "extender", STASSID); }
|
||||
}
|
||||
Serial.printf("Heap after napt init: %d\n", ESP.getFreeHeap());
|
||||
if (ret != ERR_OK) {
|
||||
Serial.printf("NAPT initialization failed\n");
|
||||
}
|
||||
if (ret != ERR_OK) { Serial.printf("NAPT initialization failed\n"); }
|
||||
}
|
||||
|
||||
#else
|
||||
@ -92,6 +83,4 @@ void setup() {
|
||||
|
||||
#endif
|
||||
|
||||
void loop() {
|
||||
}
|
||||
|
||||
void loop() {}
|
||||
|
@ -20,14 +20,14 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
unsigned int localPort = 8888; // local port to listen on
|
||||
unsigned int localPort = 8888; // local port to listen on
|
||||
|
||||
// buffers for receiving and sending data
|
||||
char packetBuffer[UDP_TX_PACKET_MAX_SIZE + 1]; //buffer to hold incoming packet,
|
||||
char ReplyBuffer[] = "acknowledged\r\n"; // a string to send back
|
||||
char packetBuffer[UDP_TX_PACKET_MAX_SIZE + 1]; // buffer to hold incoming packet,
|
||||
char ReplyBuffer[] = "acknowledged\r\n"; // a string to send back
|
||||
|
||||
WiFiUDP Udp;
|
||||
|
||||
@ -49,11 +49,7 @@ void loop() {
|
||||
// if there's data available, read a packet
|
||||
int packetSize = Udp.parsePacket();
|
||||
if (packetSize) {
|
||||
Serial.printf("Received packet of size %d from %s:%d\n (to %s:%d, free heap = %d B)\n",
|
||||
packetSize,
|
||||
Udp.remoteIP().toString().c_str(), Udp.remotePort(),
|
||||
Udp.destinationIP().toString().c_str(), Udp.localPort(),
|
||||
ESP.getFreeHeap());
|
||||
Serial.printf("Received packet of size %d from %s:%d\n (to %s:%d, free heap = %d B)\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort(), Udp.destinationIP().toString().c_str(), Udp.localPort(), ESP.getFreeHeap());
|
||||
|
||||
// read the packet into packetBufffer
|
||||
int n = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
|
||||
@ -66,11 +62,10 @@ void loop() {
|
||||
Udp.write(ReplyBuffer);
|
||||
Udp.endPacket();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
test (shell/netcat):
|
||||
--------------------
|
||||
nc -u 192.168.esp.address 8888
|
||||
nc -u 192.168.esp.address 8888
|
||||
*/
|
||||
|
@ -36,7 +36,7 @@
|
||||
|
||||
#ifndef APSSID
|
||||
#define APSSID "ESPap"
|
||||
#define APPSK "thereisnospoon"
|
||||
#define APPSK "thereisnospoon"
|
||||
#endif
|
||||
|
||||
/* Set these to your desired credentials. */
|
||||
|
@ -7,10 +7,10 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char* ssid = STASSID;
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
|
||||
const char* host = "djxmmx.net";
|
||||
@ -61,9 +61,7 @@ void loop() {
|
||||
|
||||
// This will send a string to the server
|
||||
Serial.println("sending data to server");
|
||||
if (client.connected()) {
|
||||
client.println("hello from ESP8266");
|
||||
}
|
||||
if (client.connected()) { client.println("hello from ESP8266"); }
|
||||
|
||||
// wait for data to be available
|
||||
unsigned long timeout = millis();
|
||||
@ -90,7 +88,7 @@ void loop() {
|
||||
client.stop();
|
||||
|
||||
if (wait) {
|
||||
delay(300000); // execute once every 5 minutes, don't flood remote service
|
||||
delay(300000); // execute once every 5 minutes, don't flood remote service
|
||||
}
|
||||
wait = true;
|
||||
}
|
||||
|
@ -9,10 +9,10 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char* ssid = STASSID;
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
|
||||
const char* host = "192.168.1.1";
|
||||
@ -64,7 +64,7 @@ void loop() {
|
||||
// This will send the request to the server
|
||||
client.println("hello from ESP8266");
|
||||
|
||||
//read back one line from server
|
||||
// read back one line from server
|
||||
Serial.println("receiving from remote server");
|
||||
String line = client.readStringUntil('\r');
|
||||
Serial.println(line);
|
||||
@ -75,4 +75,3 @@ void loop() {
|
||||
Serial.println("wait 5 sec...");
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
|
@ -7,11 +7,11 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
#include <PolledTimeout.h>
|
||||
#include <algorithm> // std::min
|
||||
#include <algorithm> // std::min
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
constexpr int port = 23;
|
||||
@ -19,12 +19,12 @@ constexpr int port = 23;
|
||||
WiFiServer server(port);
|
||||
WiFiClient client;
|
||||
|
||||
constexpr size_t sizes [] = { 0, 512, 384, 256, 128, 64, 16, 8, 4 };
|
||||
constexpr size_t sizes[] = { 0, 512, 384, 256, 128, 64, 16, 8, 4 };
|
||||
constexpr uint32_t breathMs = 200;
|
||||
esp8266::polledTimeout::oneShotFastMs enoughMs(breathMs);
|
||||
esp8266::polledTimeout::periodicFastMs test(2000);
|
||||
int t = 1; // test (1, 2 or 3, see below)
|
||||
int s = 0; // sizes[] index
|
||||
int t = 1; // test (1, 2 or 3, see below)
|
||||
int s = 0; // sizes[] index
|
||||
|
||||
void setup() {
|
||||
|
||||
@ -63,19 +63,13 @@ void loop() {
|
||||
static uint32_t cnt = 0;
|
||||
if (test && cnt) {
|
||||
Serial.printf("measured-block-size=%u min-free-stack=%u", tot / cnt, ESP.getFreeContStack());
|
||||
if (t == 2 && sizes[s]) {
|
||||
Serial.printf(" (blocks: at most %d bytes)", sizes[s]);
|
||||
}
|
||||
if (t == 3 && sizes[s]) {
|
||||
Serial.printf(" (blocks: exactly %d bytes)", sizes[s]);
|
||||
}
|
||||
if (t == 3 && !sizes[s]) {
|
||||
Serial.printf(" (blocks: any size)");
|
||||
}
|
||||
if (t == 2 && sizes[s]) { Serial.printf(" (blocks: at most %d bytes)", sizes[s]); }
|
||||
if (t == 3 && sizes[s]) { Serial.printf(" (blocks: exactly %d bytes)", sizes[s]); }
|
||||
if (t == 3 && !sizes[s]) { Serial.printf(" (blocks: any size)"); }
|
||||
Serial.printf("\n");
|
||||
}
|
||||
|
||||
//check if there are any new clients
|
||||
// check if there are any new clients
|
||||
if (server.hasClient()) {
|
||||
client = server.accept();
|
||||
Serial.println("New client");
|
||||
@ -84,10 +78,25 @@ void loop() {
|
||||
if (Serial.available()) {
|
||||
s = (s + 1) % (sizeof(sizes) / sizeof(sizes[0]));
|
||||
switch (Serial.read()) {
|
||||
case '1': if (t != 1) s = 0; t = 1; Serial.println("byte-by-byte (watch then press 2, 3 or 4)"); break;
|
||||
case '2': if (t != 2) s = 1; t = 2; Serial.printf("through buffer (watch then press 2 again, or 1, 3 or 4)\n"); break;
|
||||
case '3': if (t != 3) s = 0; t = 3; Serial.printf("direct access (sendAvailable - watch then press 3 again, or 1, 2 or 4)\n"); break;
|
||||
case '4': t = 4; Serial.printf("direct access (sendAll - close peer to stop, then press 1, 2 or 3 before restarting peer)\n"); break;
|
||||
case '1':
|
||||
if (t != 1) s = 0;
|
||||
t = 1;
|
||||
Serial.println("byte-by-byte (watch then press 2, 3 or 4)");
|
||||
break;
|
||||
case '2':
|
||||
if (t != 2) s = 1;
|
||||
t = 2;
|
||||
Serial.printf("through buffer (watch then press 2 again, or 1, 3 or 4)\n");
|
||||
break;
|
||||
case '3':
|
||||
if (t != 3) s = 0;
|
||||
t = 3;
|
||||
Serial.printf("direct access (sendAvailable - watch then press 3 again, or 1, 2 or 4)\n");
|
||||
break;
|
||||
case '4':
|
||||
t = 4;
|
||||
Serial.printf("direct access (sendAll - close peer to stop, then press 1, 2 or 3 before restarting peer)\n");
|
||||
break;
|
||||
}
|
||||
tot = cnt = 0;
|
||||
ESP.resetFreeContStack();
|
||||
@ -113,9 +122,7 @@ void loop() {
|
||||
uint8_t buf[maxTo];
|
||||
size_t tcp_got = client.read(buf, maxTo);
|
||||
size_t tcp_sent = client.write(buf, tcp_got);
|
||||
if (tcp_sent != maxTo) {
|
||||
Serial.printf("len mismatch: available:%zd tcp-read:%zd serial-write:%zd\n", maxTo, tcp_got, tcp_sent);
|
||||
}
|
||||
if (tcp_sent != maxTo) { Serial.printf("len mismatch: available:%zd tcp-read:%zd serial-write:%zd\n", maxTo, tcp_got, tcp_sent); }
|
||||
tot += tcp_sent;
|
||||
cnt++;
|
||||
}
|
||||
@ -141,7 +148,7 @@ void loop() {
|
||||
|
||||
else if (t == 4) {
|
||||
// stream to print, possibly with only one copy
|
||||
tot += client.sendAll(&client); // this one might not exit until peer close
|
||||
tot += client.sendAll(&client); // this one might not exit until peer close
|
||||
cnt++;
|
||||
|
||||
switch (client.getLastSendReport()) {
|
||||
@ -152,5 +159,4 @@ void loop() {
|
||||
case Stream::Report::ShortOperation: Serial.println("Stream::send: short transfer"); break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,10 +18,10 @@
|
||||
|
||||
#ifndef APSSID
|
||||
#define APSSID "esp8266"
|
||||
#define APPSK "esp8266"
|
||||
#define APPSK "esp8266"
|
||||
#endif
|
||||
|
||||
const char* ssid = APSSID;
|
||||
const char* ssid = APSSID;
|
||||
const char* password = APPSK;
|
||||
|
||||
WiFiEventHandler stationConnectedHandler;
|
||||
@ -98,7 +98,6 @@ void loop() {
|
||||
|
||||
String macToString(const unsigned char* mac) {
|
||||
char buf[20];
|
||||
snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
return String(buf);
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char* ssid = STASSID;
|
||||
@ -55,12 +55,10 @@ void setup() {
|
||||
void loop() {
|
||||
// Check if a client has connected
|
||||
WiFiClient client = server.accept();
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
if (!client) { return; }
|
||||
Serial.println(F("new client"));
|
||||
|
||||
client.setTimeout(5000); // default is 1000
|
||||
client.setTimeout(5000); // default is 1000
|
||||
|
||||
// Read the first line of the request
|
||||
String req = client.readStringUntil('\r');
|
||||
|
@ -40,15 +40,7 @@ void loop() {
|
||||
for (int8_t i = 0; i < scanResult; i++) {
|
||||
WiFi.getNetworkInfo(i, ssid, encryptionType, rssi, bssid, channel, hidden);
|
||||
|
||||
Serial.printf(PSTR(" %02d: [CH %02d] [%02X:%02X:%02X:%02X:%02X:%02X] %ddBm %c %c %s\n"),
|
||||
i,
|
||||
channel,
|
||||
bssid[0], bssid[1], bssid[2],
|
||||
bssid[3], bssid[4], bssid[5],
|
||||
rssi,
|
||||
(encryptionType == ENC_TYPE_NONE) ? ' ' : '*',
|
||||
hidden ? 'H' : 'V',
|
||||
ssid.c_str());
|
||||
Serial.printf(PSTR(" %02d: [CH %02d] [%02X:%02X:%02X:%02X:%02X:%02X] %ddBm %c %c %s\n"), i, channel, bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], rssi, (encryptionType == ENC_TYPE_NONE) ? ' ' : '*', hidden ? 'H' : 'V', ssid.c_str());
|
||||
yield();
|
||||
}
|
||||
} else {
|
||||
|
@ -17,7 +17,7 @@
|
||||
#endif
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <include/WiFiState.h> // WiFiState structure details
|
||||
#include <include/WiFiState.h> // WiFiState structure details
|
||||
|
||||
WiFiState state;
|
||||
|
||||
@ -26,7 +26,7 @@ const char* password = STAPSK;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(74880);
|
||||
//Serial.setDebugOutput(true); // If you need debug output
|
||||
// Serial.setDebugOutput(true); // If you need debug output
|
||||
Serial.println("Trying to resume WiFi connection...");
|
||||
|
||||
// May be necessary after deepSleep. Otherwise you may get "error: pll_cal exceeds 2ms!!!" when trying to connect
|
||||
@ -36,17 +36,14 @@ void setup() {
|
||||
// Here you can do whatever you need to do that doesn't need a WiFi connection.
|
||||
// ---
|
||||
|
||||
ESP.rtcUserMemoryRead(RTC_USER_DATA_SLOT_WIFI_STATE, reinterpret_cast<uint32_t *>(&state), sizeof(state));
|
||||
ESP.rtcUserMemoryRead(RTC_USER_DATA_SLOT_WIFI_STATE, reinterpret_cast<uint32_t*>(&state), sizeof(state));
|
||||
unsigned long start = millis();
|
||||
|
||||
if (!WiFi.resumeFromShutdown(state)
|
||||
|| (WiFi.waitForConnectResult(10000) != WL_CONNECTED)) {
|
||||
if (!WiFi.resumeFromShutdown(state) || (WiFi.waitForConnectResult(10000) != WL_CONNECTED)) {
|
||||
Serial.println("Cannot resume WiFi connection, connecting via begin...");
|
||||
WiFi.persistent(false);
|
||||
|
||||
if (!WiFi.mode(WIFI_STA)
|
||||
|| !WiFi.begin(ssid, password)
|
||||
|| (WiFi.waitForConnectResult(10000) != WL_CONNECTED)) {
|
||||
if (!WiFi.mode(WIFI_STA) || !WiFi.begin(ssid, password) || (WiFi.waitForConnectResult(10000) != WL_CONNECTED)) {
|
||||
WiFi.mode(WIFI_OFF);
|
||||
Serial.println("Cannot connect!");
|
||||
Serial.flush();
|
||||
@ -64,7 +61,7 @@ void setup() {
|
||||
// ---
|
||||
|
||||
WiFi.shutdown(state);
|
||||
ESP.rtcUserMemoryWrite(RTC_USER_DATA_SLOT_WIFI_STATE, reinterpret_cast<uint32_t *>(&state), sizeof(state));
|
||||
ESP.rtcUserMemoryWrite(RTC_USER_DATA_SLOT_WIFI_STATE, reinterpret_cast<uint32_t*>(&state), sizeof(state));
|
||||
|
||||
// ---
|
||||
// Here you can do whatever you need to do that doesn't need a WiFi connection anymore.
|
||||
|
@ -20,11 +20,11 @@
|
||||
*/
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
#include <algorithm> // std::min
|
||||
#include <algorithm> // std::min
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
/*
|
||||
@ -64,9 +64,9 @@ SoftwareSerial* logger = nullptr;
|
||||
#define logger (&Serial1)
|
||||
#endif
|
||||
|
||||
#define STACK_PROTECTOR 512 // bytes
|
||||
#define STACK_PROTECTOR 512 // bytes
|
||||
|
||||
//how many clients should be able to telnet to this ESP8266
|
||||
// how many clients should be able to telnet to this ESP8266
|
||||
#define MAX_SRV_CLIENTS 2
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
@ -98,7 +98,7 @@ void setup() {
|
||||
logger->printf("Serial receive buffer size: %d bytes\n", RXBUFFERSIZE);
|
||||
|
||||
#if SERIAL_LOOPBACK
|
||||
USC0(0) |= (1 << UCLBE); // incomplete HardwareSerial API
|
||||
USC0(0) |= (1 << UCLBE); // incomplete HardwareSerial API
|
||||
logger->println("Serial Internal Loopback enabled");
|
||||
#endif
|
||||
|
||||
@ -114,7 +114,7 @@ void setup() {
|
||||
logger->print("connected, address=");
|
||||
logger->println(WiFi.localIP());
|
||||
|
||||
//start server
|
||||
// start server
|
||||
server.begin();
|
||||
server.setNoDelay(true);
|
||||
|
||||
@ -124,19 +124,19 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//check if there are any new clients
|
||||
// check if there are any new clients
|
||||
if (server.hasClient()) {
|
||||
//find free/disconnected spot
|
||||
// find free/disconnected spot
|
||||
int i;
|
||||
for (i = 0; i < MAX_SRV_CLIENTS; i++)
|
||||
if (!serverClients[i]) { // equivalent to !serverClients[i].connected()
|
||||
if (!serverClients[i]) { // equivalent to !serverClients[i].connected()
|
||||
serverClients[i] = server.accept();
|
||||
logger->print("New client: index ");
|
||||
logger->print(i);
|
||||
break;
|
||||
}
|
||||
|
||||
//no free/disconnected spot so reject
|
||||
// no free/disconnected spot so reject
|
||||
if (i == MAX_SRV_CLIENTS) {
|
||||
server.accept().println("busy");
|
||||
// hints: server.accept() is a WiFiClient with short-term scope
|
||||
@ -147,7 +147,7 @@ void loop() {
|
||||
}
|
||||
}
|
||||
|
||||
//check TCP clients for data
|
||||
// check TCP clients for data
|
||||
#if 1
|
||||
// Incredibly, this code is faster than the buffered one below - #4620 is needed
|
||||
// loopback/3000000baud average 348KB/s
|
||||
@ -165,9 +165,7 @@ void loop() {
|
||||
uint8_t buf[maxToSerial];
|
||||
size_t tcp_got = serverClients[i].read(buf, maxToSerial);
|
||||
size_t serial_sent = Serial.write(buf, tcp_got);
|
||||
if (serial_sent != maxToSerial) {
|
||||
logger->printf("len mismatch: available:%zd tcp-read:%zd serial-write:%zd\n", maxToSerial, tcp_got, serial_sent);
|
||||
}
|
||||
if (serial_sent != maxToSerial) { logger->printf("len mismatch: available:%zd tcp-read:%zd serial-write:%zd\n", maxToSerial, tcp_got, serial_sent); }
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -189,7 +187,7 @@ void loop() {
|
||||
}
|
||||
}
|
||||
|
||||
//check UART for data
|
||||
// check UART for data
|
||||
size_t len = std::min(Serial.available(), maxToTcp);
|
||||
len = std::min(len, (size_t)STACK_PROTECTOR);
|
||||
if (len) {
|
||||
@ -202,9 +200,7 @@ void loop() {
|
||||
// ensure write space is sufficient:
|
||||
if (serverClients[i].availableForWrite() >= serial_got) {
|
||||
size_t tcp_sent = serverClients[i].write(sbuf, serial_got);
|
||||
if (tcp_sent != len) {
|
||||
logger->printf("len mismatch: available:%zd serial-read:%zd tcp-write:%zd\n", len, serial_got, tcp_sent);
|
||||
}
|
||||
if (tcp_sent != len) { logger->printf("len mismatch: available:%zd serial-read:%zd tcp-write:%zd\n", len, serial_got, tcp_sent); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user