Experimenting with Wi-Fi auto configuration.

This commit is contained in:
Jason Coon 2017-11-26 10:20:09 -06:00
parent 1d4f7e74de
commit a402019e4d
8 changed files with 109 additions and 132 deletions

153
WiFi.h
View File

@ -4,66 +4,35 @@
//const byte DNS_PORT = 53; //const byte DNS_PORT = 53;
// bool apMode = false;
// AP mode password // AP mode password
const char WiFiAPPSK[] = ""; const char WiFiAPPSK[] = "";
// Wi-Fi network to connect to (if not in AP mode) // Wi-Fi network to connect to (leave blank to connect to saved network, or to start in AP mode)
char* ssid = ""; const char* ssid = "";
char* password = ""; const char* password = "";
#define HOSTNAME "ESP8266-" ///< Hostname. The initializeWiFi function adds the Chip ID at the end. #define HOSTNAME "ESP8266-" ///< Hostname. The initializeWiFi function adds the Chip ID at the end.
#define DEBUG_WIFI 1 #define DEBUG_WIFI 1
unsigned long futureTimeout = 0; WiFiMode mode = WIFI_STA; // connect to existing Wi-Fi network
uint16_t connectionTimeout = 20000; //WiFiMode mode = WIFI_AP; // act as an Access Point, creating a new Wi-Fi network
//WiFiMode mode = WIFI_AP_STA; // act as both a client and Access Point (mesh mode)
template <typename Generic>
void debugPrint(Generic text) {
if (DEBUG_WIFI) {
Serial.print(text);
}
}
template <typename Generic> template <typename Generic>
void debugPrintln(Generic text) { void debugPrintln(Generic text) {
if (DEBUG_WIFI) { if (DEBUG_WIFI) {
Serial.print("*WiFi: ");
Serial.println(text); Serial.println(text);
} }
} }
void startAp() {
// WiFi.disconnect();
// apMode = true;
// WiFi.mode(WIFI_AP_STA);
// debugPrintln("SET AP STA");
String AP_NameString = "ESP8266-";
AP_NameString += String(ESP.getChipId(), HEX);
char AP_NameChar[AP_NameString.length() + 1];
memset(AP_NameChar, 0, AP_NameString.length() + 1);
for (int i = 0; i < AP_NameString.length(); i++)
AP_NameChar[i] = AP_NameString.charAt(i);
debugPrintln("Starting soft AP");
if (WiFiAPPSK != NULL) {
debugPrintln(WiFi.softAP(AP_NameChar, WiFiAPPSK) ? "ready" : "failed");
} else {
debugPrintln(WiFi.softAP(AP_NameChar) ? "ready" : "failed");
}
debugPrintln("Connect to Wi-Fi access point: ");
debugPrintln(AP_NameChar);
delay(500); // Without delay I've seen the IP address blank
debugPrintln("AP IP address: ");
debugPrintln(WiFi.softAPIP());
// dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
// dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
}
String getWiFiJson() { String getWiFiJson() {
String hostname = String(HOSTNAME); String hostname = String(HOSTNAME);
hostname += String(ESP.getChipId(), HEX); hostname += String(ESP.getChipId(), HEX);
@ -77,16 +46,16 @@ String getWiFiJson() {
json += ",\"ssid\":\"" + WiFi.SSID() + "\""; json += ",\"ssid\":\"" + WiFi.SSID() + "\"";
json += ",\"rssi\":\"" + String(WiFi.RSSI()) + "\""; json += ",\"rssi\":\"" + String(WiFi.RSSI()) + "\"";
// json += ",\"networks\":["; json += ",\"networks\":[";
// byte ssidCount = WiFi.scanNetworks(); byte ssidCount = WiFi.scanNetworks();
// for (byte i = 0; i < ssidCount; i++) { for (byte i = 0; i < ssidCount; i++) {
// if (i > 0) if (i > 0)
// json += ","; json += ",";
//
// json += "{\"name\":\"" + WiFi.SSID(i) + "\",\"rssi\":\"" + String(WiFi.RSSI(i)) + "\"}"; json += "{\"name\":\"" + WiFi.SSID(i) + "\",\"rssi\":\"" + String(WiFi.RSSI(i)) + "\"}";
// } }
//
// json += "]"; json += "]";
json += "}"; json += "}";
@ -94,8 +63,6 @@ String getWiFiJson() {
} }
void initializeWiFi() { void initializeWiFi() {
WiFi.mode(WIFI_AP_STA);
// Set Hostname. // Set Hostname.
String hostname = String(HOSTNAME); String hostname = String(HOSTNAME);
hostname += String(ESP.getChipId(), HEX); hostname += String(ESP.getChipId(), HEX);
@ -119,20 +86,53 @@ void initializeWiFi() {
// WiFi.mode(WIFI_STA); // WiFi.mode(WIFI_STA);
String stored_ssid = WiFi.SSID(); String stored_ssid = WiFi.SSID();
if (stored_ssid != NULL && stored_ssid != "") {
debugPrintln("Connecting to stored SSID:"); if (ssid != NULL && password != NULL &&
ssid != "" && password != "") {
debugPrint("WiFi mode: ");
debugPrintln(WIFI_STA);
WiFi.mode(WIFI_STA);
debugPrint("Connecting to hard-coded SSID: ");
debugPrintln(stored_ssid);
WiFi.begin(ssid, password);
}
else if (stored_ssid != NULL && stored_ssid != "") {
debugPrint("WiFi mode: ");
debugPrintln(WIFI_STA);
WiFi.mode(WIFI_STA);
debugPrint("Connecting to stored SSID: ");
debugPrintln(stored_ssid); debugPrintln(stored_ssid);
WiFi.begin(); WiFi.begin();
} else { }
else {
debugPrintln("No stored SSID"); debugPrintln("No stored SSID");
debugPrint("Starting soft AP: ");
debugPrintln(hostnameChar);
WiFi.mode(WIFI_AP);
if (WiFiAPPSK != NULL) {
debugPrint(WiFi.softAP(hostnameChar, WiFiAPPSK) ? "ready" : "failed");
} else {
debugPrint(WiFi.softAP(hostnameChar) ? "ready" : "failed");
} }
startAp(); debugPrint("Connect to Wi-Fi access point: ");
debugPrintln(hostnameChar);
delay(500); // Without delay I've seen the IP address blank
debugPrint("AP IP address: ");
debugPrintln(WiFi.softAPIP());
// dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
// dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
}
webServer.on("/wifi", HTTP_POST, []() { webServer.on("/wifi", HTTP_POST, []() {
String ssid = webServer.arg("ssid"); String ssid = webServer.arg("ssid");
String password = webServer.arg("password"); String password = webServer.arg("password");
// String mode = webServer.arg("mode");
char ssidChars[50]; char ssidChars[50];
ssid.toCharArray(ssidChars, 50); ssid.toCharArray(ssidChars, 50);
@ -140,16 +140,10 @@ void initializeWiFi() {
char passwordChars[50]; char passwordChars[50];
password.toCharArray(passwordChars, 50); password.toCharArray(passwordChars, 50);
debugPrintln("Connecting to new SSID:"); debugPrint("Connecting to new SSID: ");
debugPrintln(ssid); debugPrintln(ssid);
// dnsServer.stop();
// WiFi.softAPdisconnect(true);
// apMode = false;
// WiFi.mode(WIFI_STA);
WiFi.begin(ssidChars, passwordChars); WiFi.begin(ssidChars, passwordChars);
// futureTimeout = millis() + connectionTimeout;
webServer.sendHeader("Location", "/wifi.htm"); webServer.sendHeader("Location", "/wifi.htm");
webServer.send(303); webServer.send(303);
@ -161,26 +155,3 @@ void initializeWiFi() {
}); });
} }
void checkWiFi() {
// if (WiFi.status() == WL_CONNECTED) {
// debugPrintln("connected");
// futureTimeout = millis() + connectionTimeout;
// return;
// }
//
// if (apMode) {
// debugPrintln("Already running in AP mode.");
// return;
// }
//
// // time to give up on the stored network and switch to ap mode?
// if (futureTimeout != 0 && millis() < futureTimeout) {
// return;
// }
//
// debugPrintln("Switching to AP mode, timeout elapsed: ");
// debugPrintln(connectionTimeout);
//
// startApMode();
}

View File

@ -8,12 +8,12 @@
<title>ESP8266 + FastLED by Evil Genius Labs</title> <title>ESP8266 + FastLED by Evil Genius Labs</title>
<!-- request CSS from internet CDN --> <!-- request CSS from internet CDN -->
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-minicolors/2.2.4/jquery.minicolors.min.css" integrity="sha256-4wnSkPYU5B4yngAlx/rEb8LdfMah4teUth4AfhGEuaY=" crossorigin="anonymous" /> --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-minicolors/2.2.4/jquery.minicolors.min.css" integrity="sha256-4wnSkPYU5B4yngAlx/rEb8LdfMah4teUth4AfhGEuaY=" crossorigin="anonymous" />
<!-- request CSS from the ESP8266 web server --> <!-- request CSS from the ESP8266 web server -->
<link rel="stylesheet" href="css/bootstrap.min.css"> <!-- <link rel="stylesheet" href="css/bootstrap.min.css"> -->
<link rel="stylesheet" href="css/minicolors.min.css"> <!-- <link rel="stylesheet" href="css/minicolors.min.css"> -->
<link rel="stylesheet" href="css/styles.css"> <link rel="stylesheet" href="css/styles.css">
@ -216,16 +216,16 @@
</nav> </nav>
<!-- request js from internet CDN --> <!-- request js from internet CDN -->
<!-- <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script> --> <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script> -->
<!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-minicolors/2.2.4/jquery.minicolors.min.js" integrity="sha256-XAFQ9dZ6hy8p/GRhU8h/8pMvM1etymiJLZW1CiHV3bQ=" crossorigin="anonymous"></script> --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-minicolors/2.2.4/jquery.minicolors.min.js" integrity="sha256-XAFQ9dZ6hy8p/GRhU8h/8pMvM1etymiJLZW1CiHV3bQ=" crossorigin="anonymous"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/reconnecting-websocket/1.0.0/reconnecting-websocket.min.js" integrity="sha256-A4JwlcDvqO4JXpvEtvWY1RH8JAEMu5W21wP8GUXLUNs=" crossorigin="anonymous"></script> --> <script src="https://cdnjs.cloudflare.com/ajax/libs/reconnecting-websocket/1.0.0/reconnecting-websocket.min.js" integrity="sha256-A4JwlcDvqO4JXpvEtvWY1RH8JAEMu5W21wP8GUXLUNs=" crossorigin="anonymous"></script>
<!-- request js from the ESP8266 web server --> <!-- request js from the ESP8266 web server -->
<script src="js/jquery-3.1.1.min.js"></script> <!-- <script src="js/jquery-3.1.1.min.js"></script> -->
<script src="js/bootstrap.min.js"></script> <!-- <script src="js/bootstrap.min.js"></script> -->
<script src="js/minicolors.min.js"></script> <!-- <script src="js/minicolors.min.js"></script> -->
<script src="js/r-websocket.min.js"></script> <!-- <script src="js/r-websocket.min.js"></script> -->
<script src="js/app.js"></script> <script src="js/app.js"></script>

View File

@ -1,3 +1,8 @@
// require ./jquery-3.1.1.min.js
// require ./bootstrap.min.js
// require ./minicolors.min.js
// require ./r-websocket.min.js
// used when hosting the site on the ESP8266 // used when hosting the site on the ESP8266
var address = location.hostname; var address = location.hostname;
var urlBase = ""; var urlBase = "";

View File

@ -1,3 +1,5 @@
// require ./jquery-3.1.1.min.js
/*! /*!
* Bootstrap v3.3.7 (http://getbootstrap.com) * Bootstrap v3.3.7 (http://getbootstrap.com)
* Copyright 2011-2016 Twitter, Inc. * Copyright 2011-2016 Twitter, Inc.

View File

@ -1,3 +1,5 @@
// require ./jquery-3.1.1.min.js
/* /*
* jQuery MiniColors: A tiny color picker built on jQuery * jQuery MiniColors: A tiny color picker built on jQuery
* *

View File

@ -8,10 +8,10 @@
<title>ESP8266 + FastLED by Evil Genius Labs</title> <title>ESP8266 + FastLED by Evil Genius Labs</title>
<!-- request CSS from internet CDN --> <!-- request CSS from internet CDN -->
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- request CSS from the ESP8266 web server --> <!-- request CSS from the ESP8266 web server -->
<link rel="stylesheet" href="css/bootstrap.min.css"> <!-- <link rel="stylesheet" href="css/bootstrap.min.css"> -->
<link rel="stylesheet" href="css/simple.css"> <link rel="stylesheet" href="css/simple.css">
@ -42,14 +42,14 @@
</div> </div>
<!-- request js from internet CDN --> <!-- request js from internet CDN -->
<!-- <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script> --> <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- <script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js"></script> --> <script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/reconnecting-websocket/1.0.0/reconnecting-websocket.min.js" integrity="sha256-A4JwlcDvqO4JXpvEtvWY1RH8JAEMu5W21wP8GUXLUNs=" crossorigin="anonymous"></script> --> <script src="https://cdnjs.cloudflare.com/ajax/libs/reconnecting-websocket/1.0.0/reconnecting-websocket.min.js" integrity="sha256-A4JwlcDvqO4JXpvEtvWY1RH8JAEMu5W21wP8GUXLUNs=" crossorigin="anonymous"></script>
<!-- request js from the ESP8266 web server --> <!-- request js from the ESP8266 web server -->
<script src="js/jquery-3.1.1.min.js"></script> <!-- <script src="js/jquery-3.1.1.min.js"></script> -->
<script src="js/bootstrap.min.js"></script> <!-- <script src="js/bootstrap.min.js"></script> -->
<script src="js/simple.js"></script> <script src="js/simple.js"></script>

View File

@ -8,10 +8,10 @@
<title>ESP8266 + FastLED by Evil Genius Labs</title> <title>ESP8266 + FastLED by Evil Genius Labs</title>
<!-- request CSS from internet CDN --> <!-- request CSS from internet CDN -->
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- request CSS from the ESP8266 web server --> <!-- request CSS from the ESP8266 web server -->
<link rel="stylesheet" href="css/bootstrap.min.css"> <!-- <link rel="stylesheet" href="css/bootstrap.min.css"> -->
<link rel="stylesheet" href="css/styles.css"> <link rel="stylesheet" href="css/styles.css">
@ -81,14 +81,14 @@
</div> </div>
<!-- request js from internet CDN --> <!-- request js from internet CDN -->
<!-- <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script> --> <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- <script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js"></script> --> <!-- <script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js"></script> -->
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/reconnecting-websocket/1.0.0/reconnecting-websocket.min.js" integrity="sha256-A4JwlcDvqO4JXpvEtvWY1RH8JAEMu5W21wP8GUXLUNs=" crossorigin="anonymous"></script> --> <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/reconnecting-websocket/1.0.0/reconnecting-websocket.min.js" integrity="sha256-A4JwlcDvqO4JXpvEtvWY1RH8JAEMu5W21wP8GUXLUNs=" crossorigin="anonymous"></script> -->
<!-- request js from the ESP8266 web server --> <!-- request js from the ESP8266 web server -->
<script src="js/jquery-3.1.1.min.js"></script> <!-- <script src="js/jquery-3.1.1.min.js"></script> -->
<script src="js/bootstrap.min.js"></script> <!-- <script src="js/bootstrap.min.js"></script> -->
</body> </body>

View File

@ -16,6 +16,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define FASTLED_INTERRUPT_RETRY_COUNT 1
//#define FASTLED_ALLOW_INTERRUPTS 0
#include <FastLED.h> #include <FastLED.h>
FASTLED_USING_NAMESPACE FASTLED_USING_NAMESPACE
@ -50,12 +53,12 @@ ESP8266HTTPUpdateServer httpUpdateServer;
#include "FSBrowser.h" #include "FSBrowser.h"
#define DATA_PIN D8 #define DATA_PIN D5
#define LED_TYPE WS2811 #define LED_TYPE WS2812
#define COLOR_ORDER GRB #define COLOR_ORDER RGB
#define NUM_LEDS 24 #define NUM_LEDS 50
#define MILLI_AMPS 2000 // IMPORTANT: set the max milli-Amps of your power supply (4A = 4000mA) #define MILLI_AMPS 3000 // IMPORTANT: set the max milli-Amps of your power supply (4A = 4000mA)
#define FRAMES_PER_SECOND 120 // here you can control the speed. With the Access Point / Web Server the animations run a bit slower. #define FRAMES_PER_SECOND 120 // here you can control the speed. With the Access Point / Web Server the animations run a bit slower.
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
@ -245,8 +248,6 @@ void setup() {
initializeWiFi(); initializeWiFi();
checkWiFi();
httpUpdateServer.setup(&webServer); httpUpdateServer.setup(&webServer);
webServer.on("/all", HTTP_GET, []() { webServer.on("/all", HTTP_GET, []() {
@ -416,10 +417,6 @@ void loop() {
// Add entropy to random number generator; we use a lot of it. // Add entropy to random number generator; we use a lot of it.
random16_add_entropy(random(65535)); random16_add_entropy(random(65535));
EVERY_N_SECONDS(10) {
checkWiFi();
}
// dnsServer.processNextRequest(); // dnsServer.processNextRequest();
webSocketsServer.loop(); webSocketsServer.loop();
webServer.handleClient(); webServer.handleClient();
@ -949,7 +946,7 @@ void sinelon()
{ {
// a colored dot sweeping back and forth, with fading trails // a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20); fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16(speed, 0, NUM_LEDS); int pos = beatsin16(speed, 0, NUM_LEDS - 1);
static int prevpos = 0; static int prevpos = 0;
CRGB color = ColorFromPalette(palettes[currentPaletteIndex], gHue, 255); CRGB color = ColorFromPalette(palettes[currentPaletteIndex], gHue, 255);
if( pos < prevpos ) { if( pos < prevpos ) {