1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-27 18:02:17 +03:00

examples: format all .ino files

This formats all the example source files using Arduino style rules.
This commit is contained in:
Ivan Grokhotkov
2018-02-19 18:30:59 +03:00
committed by Ivan Grokhotkov
parent e226251b27
commit 61cd8d8385
88 changed files with 2730 additions and 2801 deletions

View File

@ -1,20 +1,20 @@
/*
* HTTP over TLS (HTTPS) example sketch
*
* This example demonstrates how to use
* WiFiClientSecure class to access HTTPS API.
* We fetch and display the status of
* esp8266/Arduino project continuous integration
* build.
*
* Limitations:
* only RSA certificates
* no support of Perfect Forward Secrecy (PFS)
* TLSv1.2 is supported since version 2.4.0-rc1
*
* Created by Ivan Grokhotkov, 2015.
* This example is in public domain.
*/
HTTP over TLS (HTTPS) example sketch
This example demonstrates how to use
WiFiClientSecure class to access HTTPS API.
We fetch and display the status of
esp8266/Arduino project continuous integration
build.
Limitations:
only RSA certificates
no support of Perfect Forward Secrecy (PFS)
TLSv1.2 is supported since version 2.4.0-rc1
Created by Ivan Grokhotkov, 2015.
This example is in public domain.
*/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

View File

@ -1,22 +1,22 @@
/*
Udp NTP Client
Udp NTP Client
Get the time from a Network Time Protocol (NTP) time server
Demonstrates use of UDP sendPacket and ReceivePacket
For more on NTP time servers and the messages needed to communicate with them,
see http://en.wikipedia.org/wiki/Network_Time_Protocol
Get the time from a Network Time Protocol (NTP) time server
Demonstrates use of UDP sendPacket and ReceivePacket
For more on NTP time servers and the messages needed to communicate with them,
see http://en.wikipedia.org/wiki/Network_Time_Protocol
created 4 Sep 2010
by Michael Margolis
modified 9 Apr 2012
by Tom Igoe
updated for the ESP8266 12 Apr 2015
by Ivan Grokhotkov
created 4 Sep 2010
by Michael Margolis
modified 9 Apr 2012
by Tom Igoe
updated for the ESP8266 12 Apr 2015
by Ivan Grokhotkov
This code is in the public domain.
This code is in the public domain.
*/
*/
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
@ -28,7 +28,7 @@ char pass[] = "********"; // your network password
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 */
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
const char* ntpServerName = "time.nist.gov";
@ -40,8 +40,7 @@ byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing pack
// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;
void setup()
{
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
@ -51,13 +50,13 @@ void setup()
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
@ -68,20 +67,18 @@ void setup()
Serial.println(udp.localPort());
}
void loop()
{
void loop() {
//get a random server from the pool
WiFi.hostByName(ntpServerName, timeServerIP);
WiFi.hostByName(ntpServerName, timeServerIP);
sendNTPpacket(timeServerIP); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
int cb = udp.parsePacket();
if (!cb) {
Serial.println("no packet yet");
}
else {
} else {
Serial.print("packet received, length=");
Serial.println(cb);
// We've received a packet, read the data from it
@ -95,7 +92,7 @@ void loop()
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = " );
Serial.print("Seconds since Jan 1 1900 = ");
Serial.println(secsSince1900);
// now convert NTP time into everyday time:
@ -112,13 +109,13 @@ void loop()
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(':');
if ( ((epoch % 3600) / 60) < 10 ) {
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(':');
if ( (epoch % 60) < 10 ) {
if ((epoch % 60) < 10) {
// In the first 10 seconds of each minute, we'll want a leading '0'
Serial.print('0');
}
@ -129,8 +126,7 @@ void loop()
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress& address)
{
void sendNTPpacket(IPAddress& address) {
Serial.println("sending NTP packet...");
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);

View File

@ -1,37 +1,37 @@
/*
* Copyright (c) 2015, Majenko Technologies
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
Copyright (c) 2015, Majenko Technologies
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* * Neither the name of Majenko Technologies nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Create a WiFi access point and provide a web server on it. */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
/* Set these to your desired credentials. */
@ -41,28 +41,28 @@ const char *password = "thereisnospoon";
ESP8266WebServer server(80);
/* Just a little test message. Go to http://192.168.4.1 in a web browser
* connected to this access point to see it.
*/
connected to this access point to see it.
*/
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1>");
server.send(200, "text/html", "<h1>You are connected</h1>");
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
server.handleClient();
}

View File

@ -1,10 +1,10 @@
/*
* This sketch sends data via HTTP GET requests to data.sparkfun.com service.
*
* You need to get streamId and privateKey at data.sparkfun.com and paste them
* below. Or just customize this script to talk to other HTTP servers.
*
*/
This sketch sends data via HTTP GET requests to data.sparkfun.com service.
You need to get streamId and privateKey at data.sparkfun.com and paste them
below. Or just customize this script to talk to other HTTP servers.
*/
#include <ESP8266WiFi.h>
@ -25,20 +25,20 @@ void setup() {
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
@ -51,7 +51,7 @@ void loop() {
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
@ -59,7 +59,7 @@ void loop() {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/input/";
url += streamId;
@ -67,13 +67,13 @@ void loop() {
url += privateKey;
url += "&value=";
url += value;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
@ -83,13 +83,13 @@ void loop() {
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}

View File

@ -1,7 +1,7 @@
/*
* This sketch sends a message to a TCP server
*
*/
This sketch sends a message to a TCP server
*/
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
@ -9,61 +9,61 @@
ESP8266WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
delay(10);
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "passpasspass");
// We start by connecting to a WiFi network
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "passpasspass");
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
void loop() {
const uint16_t port = 80;
const char * host = "192.168.1.1"; // ip or dns
const uint16_t port = 80;
const char * host = "192.168.1.1"; // ip or dns
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
return;
}
Serial.print("connecting to ");
Serial.println(host);
// This will send the request to the server
client.println("Send this data to server");
// Use WiFiClient class to create TCP connections
WiFiClient client;
//read back one line from server
String line = client.readStringUntil('\r');
Serial.println(line);
Serial.println("closing connection");
client.stop();
if (!client.connect(host, port)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
return;
}
// This will send the request to the server
client.println("Send this data to server");
//read back one line from server
String line = client.readStringUntil('\r');
Serial.println(line);
Serial.println("closing connection");
client.stop();
Serial.println("wait 5 sec...");
delay(5000);
}

View File

@ -1,99 +1,99 @@
/*
This sketch shows how to use WiFi event handlers.
In this example, ESP8266 works in AP mode.
Three event handlers are demonstrated:
- station connects to the ESP8266 AP
- station disconnects from the ESP8266 AP
- ESP8266 AP receives a probe request from a station
Written by Markus Sattler, 2015-12-29.
Updated for new event handlers by Ivan Grokhotkov, 2017-02-02.
This example is released into public domain,
or, at your option, CC0 licensed.
*/
#include <ESP8266WiFi.h>
#include <stdio.h>
const char* ssid = "ap-ssid";
const char* password = "ap-password";
WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler;
WiFiEventHandler probeRequestPrintHandler;
WiFiEventHandler probeRequestBlinkHandler;
bool blinkFlag;
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// Don't save WiFi configuration in flash - optional
WiFi.persistent(false);
// Set up an access point
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
// Register event handlers.
// Callback functions will be called as long as these handler objects exist.
// Call "onStationConnected" each time a station connects
stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);
// Call "onStationDisconnected" each time a station disconnects
stationDisconnectedHandler = WiFi.onSoftAPModeStationDisconnected(&onStationDisconnected);
// Call "onProbeRequestPrint" and "onProbeRequestBlink" each time
// a probe request is received.
// Former will print MAC address of the station and RSSI to Serial,
// latter will blink an LED.
probeRequestPrintHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestPrint);
probeRequestBlinkHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestBlink);
}
void onStationConnected(const WiFiEventSoftAPModeStationConnected& evt) {
Serial.print("Station connected: ");
Serial.println(macToString(evt.mac));
}
void onStationDisconnected(const WiFiEventSoftAPModeStationDisconnected& evt) {
Serial.print("Station disconnected: ");
Serial.println(macToString(evt.mac));
}
void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived& evt) {
Serial.print("Probe request from: ");
Serial.print(macToString(evt.mac));
Serial.print(" RSSI: ");
Serial.println(evt.rssi);
}
void onProbeRequestBlink(const WiFiEventSoftAPModeProbeRequestReceived&) {
// We can't use "delay" or other blocking functions in the event handler.
// Therefore we set a flag here and then check it inside "loop" function.
blinkFlag = true;
}
void loop() {
if (millis() > 10000 && probeRequestPrintHandler) {
// After 10 seconds, disable the probe request event handler which prints.
// Other three event handlers remain active.
Serial.println("Not printing probe requests any more (LED should still blink)");
probeRequestPrintHandler = WiFiEventHandler();
}
if (blinkFlag) {
blinkFlag = false;
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
}
delay(10);
}
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]);
return String(buf);
}
/*
This sketch shows how to use WiFi event handlers.
In this example, ESP8266 works in AP mode.
Three event handlers are demonstrated:
- station connects to the ESP8266 AP
- station disconnects from the ESP8266 AP
- ESP8266 AP receives a probe request from a station
Written by Markus Sattler, 2015-12-29.
Updated for new event handlers by Ivan Grokhotkov, 2017-02-02.
This example is released into public domain,
or, at your option, CC0 licensed.
*/
#include <ESP8266WiFi.h>
#include <stdio.h>
const char* ssid = "ap-ssid";
const char* password = "ap-password";
WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler;
WiFiEventHandler probeRequestPrintHandler;
WiFiEventHandler probeRequestBlinkHandler;
bool blinkFlag;
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// Don't save WiFi configuration in flash - optional
WiFi.persistent(false);
// Set up an access point
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
// Register event handlers.
// Callback functions will be called as long as these handler objects exist.
// Call "onStationConnected" each time a station connects
stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);
// Call "onStationDisconnected" each time a station disconnects
stationDisconnectedHandler = WiFi.onSoftAPModeStationDisconnected(&onStationDisconnected);
// Call "onProbeRequestPrint" and "onProbeRequestBlink" each time
// a probe request is received.
// Former will print MAC address of the station and RSSI to Serial,
// latter will blink an LED.
probeRequestPrintHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestPrint);
probeRequestBlinkHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestBlink);
}
void onStationConnected(const WiFiEventSoftAPModeStationConnected& evt) {
Serial.print("Station connected: ");
Serial.println(macToString(evt.mac));
}
void onStationDisconnected(const WiFiEventSoftAPModeStationDisconnected& evt) {
Serial.print("Station disconnected: ");
Serial.println(macToString(evt.mac));
}
void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived& evt) {
Serial.print("Probe request from: ");
Serial.print(macToString(evt.mac));
Serial.print(" RSSI: ");
Serial.println(evt.rssi);
}
void onProbeRequestBlink(const WiFiEventSoftAPModeProbeRequestReceived&) {
// We can't use "delay" or other blocking functions in the event handler.
// Therefore we set a flag here and then check it inside "loop" function.
blinkFlag = true;
}
void loop() {
if (millis() > 10000 && probeRequestPrintHandler) {
// After 10 seconds, disable the probe request event handler which prints.
// Other three event handlers remain active.
Serial.println("Not printing probe requests any more (LED should still blink)");
probeRequestPrintHandler = WiFiEventHandler();
}
if (blinkFlag) {
blinkFlag = false;
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
}
delay(10);
}
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]);
return String(buf);
}

View File

@ -46,7 +46,7 @@
const char* ssid = "your-ssid";
const char* password = "your-password";
// The certificate is stored in PMEM
// The certificate is stored in PMEM
static const uint8_t x509[] PROGMEM = {
0x30, 0x82, 0x01, 0x3d, 0x30, 0x81, 0xe8, 0x02, 0x09, 0x00, 0xfe, 0x56,
0x46, 0xf2, 0x78, 0xc6, 0x51, 0x17, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
@ -107,7 +107,7 @@ static const uint8_t rsakey[] PROGMEM = {
0x2c, 0x3a, 0xcd, 0x0a, 0x9a, 0x4d, 0x7c, 0xad, 0x29, 0xd6, 0x36, 0x57,
0xd5, 0xdf, 0x34, 0xeb, 0x26, 0x03
};
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServerSecure server(443);
@ -119,15 +119,15 @@ void setup() {
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
@ -135,7 +135,7 @@ void setup() {
Serial.println("");
Serial.println("WiFi connected");
// Set the certificates from PMEM (if using DRAM remove the _P from the call)
// Set the certificates from PMEM (if using DRAM remove the _P from the call)
server.setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
// Start the server
@ -152,11 +152,11 @@ void loop() {
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
unsigned long timeout = millis() + 3000;
while(!client.available() && millis() < timeout){
while (!client.available() && millis() < timeout) {
delay(1);
}
if (millis() > timeout) {
@ -170,14 +170,14 @@ void loop() {
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
if (req.indexOf("/gpio/0") != -1)
if (req.indexOf("/gpio/0") != -1) {
val = 0;
else if (req.indexOf("/gpio/1") != -1)
} else if (req.indexOf("/gpio/1") != -1) {
val = 1;
else {
} else {
Serial.println("invalid request");
client.print("HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html><body>Not found</body></html>");
return;
@ -185,12 +185,12 @@ void loop() {
// Set GPIO2 according to the request
digitalWrite(2, val);
client.flush();
// Prepare the response
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 ";
s += (val)?"high":"low";
s += (val) ? "high" : "low";
s += "</html>\n";
// Send the response to the client
@ -198,7 +198,7 @@ void loop() {
delay(1);
Serial.println("Client disconnected");
// The client will actually be disconnected
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}

View File

@ -1,7 +1,7 @@
/*
* This sketch trys to Connect to the best AP based on a given list
*
*/
This sketch trys to Connect to the best AP based on a given list
*/
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
@ -9,26 +9,26 @@
ESP8266WiFiMulti wifiMulti;
void setup() {
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_STA);
wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
Serial.begin(115200);
delay(10);
Serial.println("Connecting Wifi...");
if(wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
WiFi.mode(WIFI_STA);
wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
Serial.println("Connecting Wifi...");
if (wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
}
void loop() {
if(wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi not connected!");
delay(1000);
}
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi not connected!");
delay(1000);
}
}

View File

@ -1,8 +1,8 @@
/*
* This sketch demonstrates how to scan WiFi networks.
* The API is almost the same as with the WiFi Shield library,
* the most obvious difference being the different file you need to include:
*/
This sketch demonstrates how to scan WiFi networks.
The API is almost the same as with the WiFi Shield library,
the most obvious difference being the different file you need to include:
*/
#include "ESP8266WiFi.h"
void setup() {
@ -22,14 +22,12 @@ void loop() {
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
if (n == 0) {
Serial.println("no networks found");
else
{
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
@ -37,7 +35,7 @@ void loop() {
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
delay(10);
}
}

View File

@ -1,9 +1,9 @@
/*
/*
WiFiTelnetToSerial - Example Transparent UART to Telnet Server for esp8266
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the ESP8266WiFi library for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
@ -34,16 +34,20 @@ void setup() {
WiFi.begin(ssid, password);
Serial1.print("\nConnecting to "); Serial1.println(ssid);
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500);
if(i == 21){
while (WiFi.status() != WL_CONNECTED && i++ < 20) {
delay(500);
}
if (i == 21) {
Serial1.print("Could not connect to"); Serial1.println(ssid);
while(1) delay(500);
while (1) {
delay(500);
}
}
//start UART and the server
Serial.begin(115200);
server.begin();
server.setNoDelay(true);
Serial1.print("Ready! Use 'telnet ");
Serial1.print(WiFi.localIP());
Serial1.println(" 23' to connect");
@ -52,40 +56,44 @@ void setup() {
void loop() {
uint8_t i;
//check if there are any new clients
if (server.hasClient()){
for(i = 0; i < MAX_SRV_CLIENTS; i++){
if (server.hasClient()) {
for (i = 0; i < MAX_SRV_CLIENTS; i++) {
//find free/disconnected spot
if (!serverClients[i] || !serverClients[i].connected()){
if(serverClients[i]) serverClients[i].stop();
if (!serverClients[i] || !serverClients[i].connected()) {
if (serverClients[i]) {
serverClients[i].stop();
}
serverClients[i] = server.available();
Serial1.print("New client: "); Serial1.print(i);
break;
}
}
//no free/disconnected spot so reject
if ( i == MAX_SRV_CLIENTS) {
WiFiClient serverClient = server.available();
serverClient.stop();
Serial1.println("Connection rejected ");
if (i == MAX_SRV_CLIENTS) {
WiFiClient serverClient = server.available();
serverClient.stop();
Serial1.println("Connection rejected ");
}
}
//check clients for data
for(i = 0; i < MAX_SRV_CLIENTS; i++){
if (serverClients[i] && serverClients[i].connected()){
if(serverClients[i].available()){
for (i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
if (serverClients[i].available()) {
//get data from the telnet client and push it to the UART
while(serverClients[i].available()) Serial.write(serverClients[i].read());
while (serverClients[i].available()) {
Serial.write(serverClients[i].read());
}
}
}
}
//check UART for data
if(Serial.available()){
if (Serial.available()) {
size_t len = Serial.available();
uint8_t sbuf[len];
Serial.readBytes(sbuf, len);
//push UART data to all connected telnet clients
for(i = 0; i < MAX_SRV_CLIENTS; i++){
if (serverClients[i] && serverClients[i].connected()){
for (i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
serverClients[i].write(sbuf, len);
delay(1);
}

View File

@ -1,11 +1,11 @@
/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://server_ip/gpio/0 will set the GPIO2 low,
* http://server_ip/gpio/1 will set the GPIO2 high
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
This sketch demonstrates how to set up a simple HTTP-like server.
The server will set a GPIO pin depending on the request
http://server_ip/gpio/0 will set the GPIO2 low,
http://server_ip/gpio/1 will set the GPIO2 high
server_ip is the IP address of the ESP8266 module, will be
printed to Serial when the module is connected.
*/
#include <ESP8266WiFi.h>
@ -23,23 +23,23 @@ void setup() {
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
@ -54,25 +54,25 @@ void loop() {
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
if (req.indexOf("/gpio/0") != -1)
if (req.indexOf("/gpio/0") != -1) {
val = 0;
else if (req.indexOf("/gpio/1") != -1)
} else if (req.indexOf("/gpio/1") != -1) {
val = 1;
else {
} else {
Serial.println("invalid request");
client.stop();
return;
@ -80,12 +80,12 @@ void loop() {
// Set GPIO2 according to the request
digitalWrite(2, val);
client.flush();
// Prepare the response
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 ";
s += (val)?"high":"low";
s += (val) ? "high" : "low";
s += "</html>\n";
// Send the response to the client
@ -93,7 +93,7 @@ void loop() {
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}