Misc changes

Simplified wi-fi, reverting back to explicit AP or STA modes.
Moved wifi ssid and password to Secrets.h file, which is intentionally omitted from git.
Commented out websockets while troubleshooting wi-fi and jitter issues.
Removed delay while wi-fi connecting, just show the LEDs already!
WIP jitter fixes/improvements.  This configuration all but eliminates jitter on my test setups.
Added different FastLED interrupt options (commented out).
Auto-formatted file.
This commit is contained in:
Jason Coon 2018-10-06 13:09:10 -05:00
parent 04103b80a3
commit cdb02c9bbf
2 changed files with 120 additions and 290 deletions

186
WiFi.h
View File

@ -1,186 +0,0 @@
//#include <DNSServer.h>
//DNSServer dnsServer;
//const byte DNS_PORT = 53;
// bool apMode = false;
// AP mode password
const char WiFiAPPSK[] = "";
// Wi-Fi network to connect to (if not in AP mode)
char* ssid = "";
char* password = "";
#define HOSTNAME "ESP8266-" ///< Hostname. The initializeWiFi function adds the Chip ID at the end.
#define DEBUG_WIFI 1
unsigned long futureTimeout = 0;
uint16_t connectionTimeout = 20000;
template <typename Generic>
void debugPrintln(Generic text) {
if (DEBUG_WIFI) {
Serial.print("*WiFi: ");
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 hostname = String(HOSTNAME);
hostname += String(ESP.getChipId(), HEX);
String json = "{";
json += "\"status\":\"" + String(WiFi.status()) + "\"";
json += ",\"localIP\":\"" + WiFi.localIP().toString() + "\"";
json += ",\"softAPIP\":\"" + WiFi.softAPIP().toString() + "\"";
json += ",\"hostname\":\"" + hostname + "\"";
json += ",\"ssid\":\"" + WiFi.SSID() + "\"";
json += ",\"rssi\":\"" + String(WiFi.RSSI()) + "\"";
// json += ",\"networks\":[";
// byte ssidCount = WiFi.scanNetworks();
// for (byte i = 0; i < ssidCount; i++) {
// if (i > 0)
// json += ",";
//
// json += "{\"name\":\"" + WiFi.SSID(i) + "\",\"rssi\":\"" + String(WiFi.RSSI(i)) + "\"}";
// }
//
// json += "]";
json += "}";
return json;
}
void initializeWiFi() {
WiFi.mode(WIFI_AP_STA);
// Set Hostname.
String hostname = String(HOSTNAME);
hostname += String(ESP.getChipId(), HEX);
WiFi.hostname(hostname);
// Print hostname.
Serial.println("Hostname: " + hostname);
char hostnameChar[hostname.length() + 1];
memset(hostnameChar, 0, hostname.length() + 1);
for (uint8_t i = 0; i < hostname.length(); i++)
hostnameChar[i] = hostname.charAt(i);
// MDNS.begin(hostnameChar);
// Add service to MDNS-SD
// MDNS.addService("http", "tcp", 80);
// attempt to connect; should it fail, fall back to AP mode
// WiFi.mode(WIFI_STA);
String stored_ssid = WiFi.SSID();
if (stored_ssid != NULL && stored_ssid != "") {
debugPrintln("Connecting to stored SSID:");
debugPrintln(stored_ssid);
WiFi.begin();
} else {
debugPrintln("No stored SSID");
}
startAp();
webServer.on("/wifi", HTTP_POST, []() {
String ssid = webServer.arg("ssid");
String password = webServer.arg("password");
// String mode = webServer.arg("mode");
char ssidChars[50];
ssid.toCharArray(ssidChars, 50);
char passwordChars[50];
password.toCharArray(passwordChars, 50);
debugPrintln("Connecting to new SSID:");
debugPrintln(ssid);
// dnsServer.stop();
// WiFi.softAPdisconnect(true);
// apMode = false;
// WiFi.mode(WIFI_STA);
WiFi.begin(ssidChars, passwordChars);
// futureTimeout = millis() + connectionTimeout;
webServer.sendHeader("Location", "/wifi.htm");
webServer.send(303);
});
webServer.on("/wifi", HTTP_GET, []() {
String json = getWiFiJson();
webServer.send(200, "application/json", json);
});
}
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

@ -1,20 +1,24 @@
/* /*
* ESP8266 + FastLED + IR Remote: https://github.com/jasoncoon/esp8266-fastled-webserver ESP8266 FastLED WebServer: https://github.com/jasoncoon/esp8266-fastled-webserver
* Copyright (C) 2015-2016 Jason Coon Copyright (C) 2015-2018 Jason Coon
*
* This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
* 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_ALLOW_INTERRUPTS 1
//#define INTERRUPT_THRESHOLD 1
#define FASTLED_INTERRUPT_RETRY_COUNT 0
#include <FastLED.h> #include <FastLED.h>
FASTLED_USING_NAMESPACE FASTLED_USING_NAMESPACE
@ -27,7 +31,7 @@ extern "C" {
//#include <ESP8266mDNS.h> //#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h> #include <ESP8266HTTPUpdateServer.h>
#include <WebSocketsServer.h> //#include <WebSocketsServer.h>
#include <FS.h> #include <FS.h>
#include <EEPROM.h> #include <EEPROM.h>
//#include <IRremoteESP8266.h> //#include <IRremoteESP8266.h>
@ -42,23 +46,32 @@ extern "C" {
//#include "Commands.h" //#include "Commands.h"
const bool apMode = false;
ESP8266WebServer webServer(80); ESP8266WebServer webServer(80);
WebSocketsServer webSocketsServer = WebSocketsServer(81); //WebSocketsServer webSocketsServer = WebSocketsServer(81);
ESP8266HTTPUpdateServer httpUpdateServer; ESP8266HTTPUpdateServer httpUpdateServer;
#include "WiFi.h"
#include "FSBrowser.h" #include "FSBrowser.h"
#define DATA_PIN D4 #define DATA_PIN D5
#define LED_TYPE WS2811 #define LED_TYPE WS2811
#define COLOR_ORDER GRB #define COLOR_ORDER RGB
#define NUM_LEDS 24 #define NUM_LEDS 200
#define MILLI_AMPS 2000 // 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.
const bool apMode = false;
#include "Secrets.h" // this file is intentionally not included in the sketch, so nobody accidentally commits their secret information.
// create a Secrets.h file with the following:
// AP mode password
// const char WiFiAPPSK[] = "your-password";
// Wi-Fi network to connect to (if not in AP mode)
// char* ssid = "your-ssid";
// char* password = "your-password";
#define MILLI_AMPS 2000 // 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.
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
@ -172,8 +185,8 @@ const uint8_t patternCount = ARRAY_SIZE(patterns);
typedef struct { typedef struct {
CRGBPalette16 palette; CRGBPalette16 palette;
String name; String name;
} PaletteAndName; } PaletteAndName;
typedef PaletteAndName PaletteAndNameList[]; typedef PaletteAndName PaletteAndNameList[];
const CRGBPalette16 palettes[] = { const CRGBPalette16 palettes[] = {
@ -195,14 +208,16 @@ const String paletteNames[paletteCount] = {
"Cloud", "Cloud",
"Lava", "Lava",
"Ocean", "Ocean",
"Forest", "Forest",
"Party", "Party",
"Heat", "Heat",
}; };
#include "Fields.h" #include "Fields.h"
void setup() { void setup() {
WiFi.setSleepMode(WIFI_NONE_SLEEP);
Serial.begin(115200); Serial.begin(115200);
delay(100); delay(100);
Serial.setDebugOutput(true); Serial.setDebugOutput(true);
@ -221,7 +236,7 @@ void setup() {
FastLED.setBrightness(brightness); FastLED.setBrightness(brightness);
// irReceiver.enableIRIn(); // Start the receiver // irReceiver.enableIRIn(); // Start the receiver
Serial.println(); Serial.println();
Serial.print( F("Heap: ") ); Serial.println(system_get_free_heap_size()); Serial.print( F("Heap: ") ); Serial.println(system_get_free_heap_size());
@ -236,6 +251,8 @@ void setup() {
SPIFFS.begin(); SPIFFS.begin();
{ {
Serial.println("SPIFFS contents:");
Dir dir = SPIFFS.openDir("/"); Dir dir = SPIFFS.openDir("/");
while (dir.next()) { while (dir.next()) {
String fileName = dir.fileName(); String fileName = dir.fileName();
@ -279,19 +296,8 @@ void setup() {
if (String(WiFi.SSID()) != String(ssid)) { if (String(WiFi.SSID()) != String(ssid)) {
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
} }
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected! Open http://");
Serial.print(WiFi.localIP());
Serial.println(" in your browser");
} }
checkWiFi();
httpUpdateServer.setup(&webServer); httpUpdateServer.setup(&webServer);
webServer.on("/all", HTTP_GET, []() { webServer.on("/all", HTTP_GET, []() {
@ -342,7 +348,7 @@ void setup() {
webServer.on("/twinkleSpeed", HTTP_POST, []() { webServer.on("/twinkleSpeed", HTTP_POST, []() {
String value = webServer.arg("value"); String value = webServer.arg("value");
twinkleSpeed = value.toInt(); twinkleSpeed = value.toInt();
if(twinkleSpeed < 0) twinkleSpeed = 0; if (twinkleSpeed < 0) twinkleSpeed = 0;
else if (twinkleSpeed > 8) twinkleSpeed = 8; else if (twinkleSpeed > 8) twinkleSpeed = 8;
broadcastInt("twinkleSpeed", twinkleSpeed); broadcastInt("twinkleSpeed", twinkleSpeed);
sendInt(twinkleSpeed); sendInt(twinkleSpeed);
@ -351,7 +357,7 @@ void setup() {
webServer.on("/twinkleDensity", HTTP_POST, []() { webServer.on("/twinkleDensity", HTTP_POST, []() {
String value = webServer.arg("value"); String value = webServer.arg("value");
twinkleDensity = value.toInt(); twinkleDensity = value.toInt();
if(twinkleDensity < 0) twinkleDensity = 0; if (twinkleDensity < 0) twinkleDensity = 0;
else if (twinkleDensity > 8) twinkleDensity = 8; else if (twinkleDensity > 8) twinkleDensity = 8;
broadcastInt("twinkleDensity", twinkleDensity); broadcastInt("twinkleDensity", twinkleDensity);
sendInt(twinkleDensity); sendInt(twinkleDensity);
@ -428,9 +434,9 @@ void setup() {
webServer.begin(); webServer.begin();
Serial.println("HTTP web server started"); Serial.println("HTTP web server started");
webSocketsServer.begin(); // webSocketsServer.begin();
webSocketsServer.onEvent(webSocketEvent); // webSocketsServer.onEvent(webSocketEvent);
Serial.println("Web socket server started"); // Serial.println("Web socket server started");
autoPlayTimeout = millis() + (autoplayDuration * 1000); autoPlayTimeout = millis() + (autoplayDuration * 1000);
} }
@ -448,28 +454,24 @@ void sendString(String value)
void broadcastInt(String name, uint8_t value) void broadcastInt(String name, uint8_t value)
{ {
String json = "{\"name\":\"" + name + "\",\"value\":" + String(value) + "}"; String json = "{\"name\":\"" + name + "\",\"value\":" + String(value) + "}";
webSocketsServer.broadcastTXT(json); // webSocketsServer.broadcastTXT(json);
} }
void broadcastString(String name, String value) void broadcastString(String name, String value)
{ {
String json = "{\"name\":\"" + name + "\",\"value\":\"" + String(value) + "\"}"; String json = "{\"name\":\"" + name + "\",\"value\":\"" + String(value) + "\"}";
webSocketsServer.broadcastTXT(json); // webSocketsServer.broadcastTXT(json);
} }
void loop() { 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) { // dnsServer.processNextRequest();
checkWiFi(); // webSocketsServer.loop();
}
// dnsServer.processNextRequest();
webSocketsServer.loop();
webServer.handleClient(); webServer.handleClient();
// handleIrInput(); // handleIrInput();
if (power == 0) { if (power == 0) {
fill_solid(leds, NUM_LEDS, CRGB::Black); fill_solid(leds, NUM_LEDS, CRGB::Black);
@ -478,6 +480,20 @@ void loop() {
return; return;
} }
static bool hasConnected = false;
EVERY_N_SECONDS(1) {
if (WiFi.status() != WL_CONNECTED) {
// Serial.printf("Connecting to %s\n", ssid);
hasConnected = false;
}
else if (!hasConnected) {
hasConnected = true;
Serial.print("Connected! Open http://");
Serial.print(WiFi.localIP());
Serial.println(" in your browser");
}
}
// EVERY_N_SECONDS(10) { // EVERY_N_SECONDS(10) {
// Serial.print( F("Heap: ") ); Serial.println(system_get_free_heap_size()); // Serial.print( F("Heap: ") ); Serial.println(system_get_free_heap_size());
// } // }
@ -505,45 +521,45 @@ void loop() {
FastLED.show(); FastLED.show();
// insert a delay to keep the framerate modest // insert a delay to keep the framerate modest
// FastLED.delay(1000 / FRAMES_PER_SECOND); FastLED.delay(1000 / FRAMES_PER_SECOND);
} }
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { //void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
//
switch (type) { // switch (type) {
case WStype_DISCONNECTED: // case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num); // Serial.printf("[%u] Disconnected!\n", num);
break; // break;
//
case WStype_CONNECTED: // case WStype_CONNECTED:
{ // {
IPAddress ip = webSocketsServer.remoteIP(num); // IPAddress ip = webSocketsServer.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); // Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
//
// send message to client // // send message to client
// webSocketsServer.sendTXT(num, "Connected"); // // webSocketsServer.sendTXT(num, "Connected");
} // }
break; // break;
//
case WStype_TEXT: // case WStype_TEXT:
Serial.printf("[%u] get Text: %s\n", num, payload); // Serial.printf("[%u] get Text: %s\n", num, payload);
//
// send message to client // // send message to client
// webSocketsServer.sendTXT(num, "message here"); // // webSocketsServer.sendTXT(num, "message here");
//
// send data to all connected clients // // send data to all connected clients
// webSocketsServer.broadcastTXT("message here"); // // webSocketsServer.broadcastTXT("message here");
break; // break;
//
case WStype_BIN: // case WStype_BIN:
Serial.printf("[%u] get binary length: %u\n", num, length); // Serial.printf("[%u] get binary length: %u\n", num, length);
hexdump(payload, length); // hexdump(payload, length);
//
// send message to client // // send message to client
// webSocketsServer.sendBIN(num, payload, lenght); // // webSocketsServer.sendBIN(num, payload, lenght);
break; // break;
} // }
} //}
//void handleIrInput() //void handleIrInput()
//{ //{
@ -797,7 +813,7 @@ void setPower(uint8_t value)
} }
void setAutoplay(uint8_t value) void setAutoplay(uint8_t value)
{ {
autoplay = value == 0 ? 0 : 1; autoplay = value == 0 ? 0 : 1;
EEPROM.write(6, autoplay); EEPROM.write(6, autoplay);
@ -876,8 +892,8 @@ void setPattern(uint8_t value)
void setPatternName(String name) void setPatternName(String name)
{ {
for(uint8_t i = 0; i < patternCount; i++) { for (uint8_t i = 0; i < patternCount; i++) {
if(patterns[i].name == name) { if (patterns[i].name == name) {
setPattern(i); setPattern(i);
break; break;
} }
@ -899,8 +915,8 @@ void setPalette(uint8_t value)
void setPaletteName(String name) void setPaletteName(String name)
{ {
for(uint8_t i = 0; i < paletteCount; i++) { for (uint8_t i = 0; i < paletteCount; i++) {
if(paletteNames[i] == name) { if (paletteNames[i] == name) {
setPalette(i); setPalette(i);
break; break;
} }
@ -997,10 +1013,10 @@ void sinelon()
int pos = beatsin16(speed, 0, NUM_LEDS); int pos = beatsin16(speed, 0, NUM_LEDS);
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 ) {
fill_solid( leds+pos, (prevpos-pos)+1, color); fill_solid( leds + pos, (prevpos - pos) + 1, color);
} else { } else {
fill_solid( leds+prevpos, (pos-prevpos)+1, color); fill_solid( leds + prevpos, (pos - prevpos) + 1, color);
} }
prevpos = pos; prevpos = pos;
} }
@ -1105,7 +1121,7 @@ void pride()
void radialPaletteShift() void radialPaletteShift()
{ {
for (uint8_t i = 0; i < NUM_LEDS; i++) { for (uint16_t i = 0; i < NUM_LEDS; i++) {
// leds[i] = ColorFromPalette( gCurrentPalette, gHue + sin8(i*16), brightness); // leds[i] = ColorFromPalette( gCurrentPalette, gHue + sin8(i*16), brightness);
leds[i] = ColorFromPalette(gCurrentPalette, i + gHue, 255, LINEARBLEND); leds[i] = ColorFromPalette(gCurrentPalette, i + gHue, 255, LINEARBLEND);
} }