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:
committed by
Ivan Grokhotkov
parent
e226251b27
commit
61cd8d8385
@ -1,16 +1,16 @@
|
||||
/**
|
||||
* @file OTA-mDNS-SPIFFS.ino
|
||||
*
|
||||
* @author Pascal Gollor (http://www.pgollor.de/cms/)
|
||||
* @date 2015-09-18
|
||||
*
|
||||
* changelog:
|
||||
* 2015-10-22:
|
||||
* - Use new ArduinoOTA library.
|
||||
* - loadConfig function can handle different line endings
|
||||
* - remove mDNS studd. ArduinoOTA handle it.
|
||||
*
|
||||
*/
|
||||
@file OTA-mDNS-SPIFFS.ino
|
||||
|
||||
@author Pascal Gollor (http://www.pgollor.de/cms/)
|
||||
@date 2015-09-18
|
||||
|
||||
changelog:
|
||||
2015-10-22:
|
||||
- Use new ArduinoOTA library.
|
||||
- loadConfig function can handle different line endings
|
||||
- remove mDNS studd. ArduinoOTA handle it.
|
||||
|
||||
*/
|
||||
|
||||
// includes
|
||||
#include <ESP8266WiFi.h>
|
||||
@ -21,16 +21,16 @@
|
||||
|
||||
|
||||
/**
|
||||
* @brief mDNS and OTA Constants
|
||||
* @{
|
||||
*/
|
||||
@brief mDNS and OTA Constants
|
||||
@{
|
||||
*/
|
||||
#define HOSTNAME "ESP8266-OTA-" ///< Hostename. The setup function adds the Chip ID at the end.
|
||||
/// @}
|
||||
|
||||
/**
|
||||
* @brief Default WiFi connection information.
|
||||
* @{
|
||||
*/
|
||||
@brief Default WiFi connection information.
|
||||
@{
|
||||
*/
|
||||
const char* ap_default_ssid = "esp8266"; ///< Default SSID.
|
||||
const char* ap_default_psk = "esp8266esp8266"; ///< Default PSK.
|
||||
/// @}
|
||||
@ -39,21 +39,19 @@ const char* ap_default_psk = "esp8266esp8266"; ///< Default PSK.
|
||||
//#define SERIAL_VERBOSE
|
||||
|
||||
/**
|
||||
* @brief Read WiFi connection information from file system.
|
||||
* @param ssid String pointer for storing SSID.
|
||||
* @param pass String pointer for storing PSK.
|
||||
* @return True or False.
|
||||
*
|
||||
* The config file have to containt the WiFi SSID in the first line
|
||||
* and the WiFi PSK in the second line.
|
||||
* Line seperator can be \r\n (CR LF) \r or \n.
|
||||
*/
|
||||
bool loadConfig(String *ssid, String *pass)
|
||||
{
|
||||
@brief Read WiFi connection information from file system.
|
||||
@param ssid String pointer for storing SSID.
|
||||
@param pass String pointer for storing PSK.
|
||||
@return True or False.
|
||||
|
||||
The config file have to containt the WiFi SSID in the first line
|
||||
and the WiFi PSK in the second line.
|
||||
Line seperator can be \r\n (CR LF) \r or \n.
|
||||
*/
|
||||
bool loadConfig(String *ssid, String *pass) {
|
||||
// open file for reading.
|
||||
File configFile = SPIFFS.open("/cl_conf.txt", "r");
|
||||
if (!configFile)
|
||||
{
|
||||
if (!configFile) {
|
||||
Serial.println("Failed to open cl_conf.txt.");
|
||||
|
||||
return false;
|
||||
@ -62,26 +60,23 @@ bool loadConfig(String *ssid, String *pass)
|
||||
// Read content from config file.
|
||||
String content = configFile.readString();
|
||||
configFile.close();
|
||||
|
||||
|
||||
content.trim();
|
||||
|
||||
// Check if ther is a second line available.
|
||||
int8_t pos = content.indexOf("\r\n");
|
||||
uint8_t le = 2;
|
||||
// check for linux and mac line ending.
|
||||
if (pos == -1)
|
||||
{
|
||||
if (pos == -1) {
|
||||
le = 1;
|
||||
pos = content.indexOf("\n");
|
||||
if (pos == -1)
|
||||
{
|
||||
if (pos == -1) {
|
||||
pos = content.indexOf("\r");
|
||||
}
|
||||
}
|
||||
|
||||
// If there is no second line: Some information is missing.
|
||||
if (pos == -1)
|
||||
{
|
||||
if (pos == -1) {
|
||||
Serial.println("Infvalid content.");
|
||||
Serial.println(content);
|
||||
|
||||
@ -95,30 +90,28 @@ bool loadConfig(String *ssid, String *pass)
|
||||
ssid->trim();
|
||||
pass->trim();
|
||||
|
||||
#ifdef SERIAL_VERBOSE
|
||||
#ifdef SERIAL_VERBOSE
|
||||
Serial.println("----- file content -----");
|
||||
Serial.println(content);
|
||||
Serial.println("----- file content -----");
|
||||
Serial.println("ssid: " + *ssid);
|
||||
Serial.println("psk: " + *pass);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
return true;
|
||||
} // loadConfig
|
||||
|
||||
|
||||
/**
|
||||
* @brief Save WiFi SSID and PSK to configuration file.
|
||||
* @param ssid SSID as string pointer.
|
||||
* @param pass PSK as string pointer,
|
||||
* @return True or False.
|
||||
*/
|
||||
bool saveConfig(String *ssid, String *pass)
|
||||
{
|
||||
@brief Save WiFi SSID and PSK to configuration file.
|
||||
@param ssid SSID as string pointer.
|
||||
@param pass PSK as string pointer,
|
||||
@return True or False.
|
||||
*/
|
||||
bool saveConfig(String *ssid, String *pass) {
|
||||
// Open config file for writing.
|
||||
File configFile = SPIFFS.open("/cl_conf.txt", "w");
|
||||
if (!configFile)
|
||||
{
|
||||
if (!configFile) {
|
||||
Serial.println("Failed to open cl_conf.txt for writing");
|
||||
|
||||
return false;
|
||||
@ -129,21 +122,20 @@ bool saveConfig(String *ssid, String *pass)
|
||||
configFile.println(*pass);
|
||||
|
||||
configFile.close();
|
||||
|
||||
|
||||
return true;
|
||||
} // saveConfig
|
||||
|
||||
|
||||
/**
|
||||
* @brief Arduino setup function.
|
||||
*/
|
||||
void setup()
|
||||
{
|
||||
@brief Arduino setup function.
|
||||
*/
|
||||
void setup() {
|
||||
String station_ssid = "";
|
||||
String station_psk = "";
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
|
||||
delay(100);
|
||||
|
||||
Serial.println("\r\n");
|
||||
@ -161,15 +153,13 @@ void setup()
|
||||
|
||||
|
||||
// Initialize file system.
|
||||
if (!SPIFFS.begin())
|
||||
{
|
||||
if (!SPIFFS.begin()) {
|
||||
Serial.println("Failed to mount file system");
|
||||
return;
|
||||
}
|
||||
|
||||
// Load wifi connection information.
|
||||
if (! loadConfig(&station_ssid, &station_psk))
|
||||
{
|
||||
if (! loadConfig(&station_ssid, &station_psk)) {
|
||||
station_ssid = "";
|
||||
station_psk = "";
|
||||
|
||||
@ -178,15 +168,13 @@ void setup()
|
||||
|
||||
// Check WiFi connection
|
||||
// ... check mode
|
||||
if (WiFi.getMode() != WIFI_STA)
|
||||
{
|
||||
if (WiFi.getMode() != WIFI_STA) {
|
||||
WiFi.mode(WIFI_STA);
|
||||
delay(10);
|
||||
}
|
||||
|
||||
// ... Compare file config with sdk config.
|
||||
if (WiFi.SSID() != station_ssid || WiFi.psk() != station_psk)
|
||||
{
|
||||
if (WiFi.SSID() != station_ssid || WiFi.psk() != station_psk) {
|
||||
Serial.println("WiFi config changed.");
|
||||
|
||||
// ... Try to connect to WiFi station.
|
||||
@ -198,9 +186,7 @@ void setup()
|
||||
|
||||
// ... Uncomment this for debugging output.
|
||||
//WiFi.printDiag(Serial);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// ... Begin with sdk config.
|
||||
WiFi.begin();
|
||||
}
|
||||
@ -209,8 +195,7 @@ void setup()
|
||||
|
||||
// ... Give ESP 10 seconds to connect to station.
|
||||
unsigned long startTime = millis();
|
||||
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000)
|
||||
{
|
||||
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) {
|
||||
Serial.write('.');
|
||||
//Serial.print(WiFi.status());
|
||||
delay(500);
|
||||
@ -218,16 +203,13 @@ void setup()
|
||||
Serial.println();
|
||||
|
||||
// Check connection
|
||||
if(WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
// ... print IP Address
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Serial.println("Can not connect to WiFi station. Go into AP mode.");
|
||||
|
||||
|
||||
// Go into software AP mode.
|
||||
WiFi.mode(WIFI_AP);
|
||||
|
||||
@ -246,10 +228,9 @@ void setup()
|
||||
|
||||
|
||||
/**
|
||||
* @brief Arduino loop function.
|
||||
*/
|
||||
void loop()
|
||||
{
|
||||
@brief Arduino loop function.
|
||||
*/
|
||||
void loop() {
|
||||
// Handle OTA server.
|
||||
ArduinoOTA.handle();
|
||||
yield();
|
||||
|
@ -2,12 +2,12 @@
|
||||
ESP8266 mDNS-SD responder and query sample
|
||||
|
||||
This is an example of announcing and finding services.
|
||||
|
||||
|
||||
Instructions:
|
||||
- Update WiFi SSID and password as necessary.
|
||||
- Flash the sketch to two ESP8266 boards
|
||||
- The last one powered on should now find the other.
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
@ -38,7 +38,7 @@ void setup() {
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
if (!MDNS.begin(hostString)) {
|
||||
if (!MDNS.begin(hostString)) {
|
||||
Serial.println("Error setting up MDNS responder!");
|
||||
}
|
||||
Serial.println("mDNS responder started");
|
||||
@ -49,8 +49,7 @@ void setup() {
|
||||
Serial.println("mDNS query done");
|
||||
if (n == 0) {
|
||||
Serial.println("no services found");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
Serial.print(n);
|
||||
Serial.println(" service(s) found");
|
||||
for (int i = 0; i < n; ++i) {
|
||||
@ -66,7 +65,7 @@ void setup() {
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
|
||||
Serial.println("loop() next");
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@
|
||||
- For Mac OSX and iOS support is built in through Bonjour already.
|
||||
- Point your browser to http://esp8266.local, you should see a response.
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
@ -26,15 +26,14 @@ const char* password = "..............";
|
||||
// TCP server at port 80 will respond to HTTP requests
|
||||
WiFiServer server(80);
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
void setup(void) {
|
||||
Serial.begin(115200);
|
||||
|
||||
|
||||
// Connect to WiFi network
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
Serial.println("");
|
||||
|
||||
Serial.println("");
|
||||
|
||||
// Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
@ -53,22 +52,21 @@ void setup(void)
|
||||
// we send our IP address on the WiFi network
|
||||
if (!MDNS.begin("esp8266")) {
|
||||
Serial.println("Error setting up MDNS responder!");
|
||||
while(1) {
|
||||
while (1) {
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
Serial.println("mDNS responder started");
|
||||
|
||||
|
||||
// Start TCP (HTTP) server
|
||||
server.begin();
|
||||
Serial.println("TCP server started");
|
||||
|
||||
|
||||
// Add service to MDNS-SD
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
void loop(void) {
|
||||
// Check if a client has connected
|
||||
WiFiClient client = server.available();
|
||||
if (!client) {
|
||||
@ -78,13 +76,13 @@ void loop(void)
|
||||
Serial.println("New client");
|
||||
|
||||
// Wait for data from client to become available
|
||||
while(client.connected() && !client.available()){
|
||||
while (client.connected() && !client.available()) {
|
||||
delay(1);
|
||||
}
|
||||
|
||||
|
||||
// Read the first line of HTTP request
|
||||
String req = client.readStringUntil('\r');
|
||||
|
||||
|
||||
// First line of HTTP request looks like "GET /path HTTP/1.1"
|
||||
// Retrieve the "/path" part by finding the spaces
|
||||
int addr_start = req.indexOf(' ');
|
||||
@ -98,24 +96,21 @@ void loop(void)
|
||||
Serial.print("Request: ");
|
||||
Serial.println(req);
|
||||
client.flush();
|
||||
|
||||
|
||||
String s;
|
||||
if (req == "/")
|
||||
{
|
||||
if (req == "/") {
|
||||
IPAddress ip = WiFi.localIP();
|
||||
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
|
||||
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at ";
|
||||
s += ipStr;
|
||||
s += "</html>\r\n\r\n";
|
||||
Serial.println("Sending 200");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
s = "HTTP/1.1 404 Not Found\r\n\r\n";
|
||||
Serial.println("Sending 404");
|
||||
}
|
||||
client.print(s);
|
||||
|
||||
|
||||
Serial.println("Done with client");
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user