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,21 +1,25 @@
/*
* ESP8266 + FastLED + IR Remote: https://github.com/jasoncoon/esp8266-fastled-webserver
* Copyright (C) 2015-2016 Jason Coon
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
ESP8266 FastLED WebServer: https://github.com/jasoncoon/esp8266-fastled-webserver
Copyright (C) 2015-2018 Jason Coon
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
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>
FASTLED_USING_NAMESPACE
@ -27,7 +31,7 @@ extern "C" {
//#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <WebSocketsServer.h>
//#include <WebSocketsServer.h>
#include <FS.h>
#include <EEPROM.h>
//#include <IRremoteESP8266.h>
@ -42,24 +46,33 @@ extern "C" {
//#include "Commands.h"
const bool apMode = false;
ESP8266WebServer webServer(80);
WebSocketsServer webSocketsServer = WebSocketsServer(81);
//WebSocketsServer webSocketsServer = WebSocketsServer(81);
ESP8266HTTPUpdateServer httpUpdateServer;
#include "WiFi.h"
#include "FSBrowser.h"
#define DATA_PIN D4
#define DATA_PIN D5
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 24
#define COLOR_ORDER RGB
#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";
CRGB leds[NUM_LEDS];
const uint8_t brightnessCount = 5;
@ -203,6 +216,8 @@ const String paletteNames[paletteCount] = {
#include "Fields.h"
void setup() {
WiFi.setSleepMode(WIFI_NONE_SLEEP);
Serial.begin(115200);
delay(100);
Serial.setDebugOutput(true);
@ -236,6 +251,8 @@ void setup() {
SPIFFS.begin();
{
Serial.println("SPIFFS contents:");
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
String fileName = dir.fileName();
@ -279,19 +296,8 @@ void setup() {
if (String(WiFi.SSID()) != String(ssid)) {
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);
webServer.on("/all", HTTP_GET, []() {
@ -428,9 +434,9 @@ void setup() {
webServer.begin();
Serial.println("HTTP web server started");
webSocketsServer.begin();
webSocketsServer.onEvent(webSocketEvent);
Serial.println("Web socket server started");
// webSocketsServer.begin();
// webSocketsServer.onEvent(webSocketEvent);
// Serial.println("Web socket server started");
autoPlayTimeout = millis() + (autoplayDuration * 1000);
}
@ -448,25 +454,21 @@ void sendString(String value)
void broadcastInt(String name, uint8_t value)
{
String json = "{\"name\":\"" + name + "\",\"value\":" + String(value) + "}";
webSocketsServer.broadcastTXT(json);
// webSocketsServer.broadcastTXT(json);
}
void broadcastString(String name, String value)
{
String json = "{\"name\":\"" + name + "\",\"value\":\"" + String(value) + "\"}";
webSocketsServer.broadcastTXT(json);
// webSocketsServer.broadcastTXT(json);
}
void loop() {
// Add entropy to random number generator; we use a lot of it.
random16_add_entropy(random(65535));
EVERY_N_SECONDS(10) {
checkWiFi();
}
// dnsServer.processNextRequest();
webSocketsServer.loop();
// webSocketsServer.loop();
webServer.handleClient();
// handleIrInput();
@ -478,6 +480,20 @@ void loop() {
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) {
// Serial.print( F("Heap: ") ); Serial.println(system_get_free_heap_size());
// }
@ -505,45 +521,45 @@ void loop() {
FastLED.show();
// 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) {
switch (type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED:
{
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);
// send message to client
// webSocketsServer.sendTXT(num, "Connected");
}
break;
case WStype_TEXT:
Serial.printf("[%u] get Text: %s\n", num, payload);
// send message to client
// webSocketsServer.sendTXT(num, "message here");
// send data to all connected clients
// webSocketsServer.broadcastTXT("message here");
break;
case WStype_BIN:
Serial.printf("[%u] get binary length: %u\n", num, length);
hexdump(payload, length);
// send message to client
// webSocketsServer.sendBIN(num, payload, lenght);
break;
}
}
//void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
//
// switch (type) {
// case WStype_DISCONNECTED:
// Serial.printf("[%u] Disconnected!\n", num);
// break;
//
// case WStype_CONNECTED:
// {
// 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);
//
// // send message to client
// // webSocketsServer.sendTXT(num, "Connected");
// }
// break;
//
// case WStype_TEXT:
// Serial.printf("[%u] get Text: %s\n", num, payload);
//
// // send message to client
// // webSocketsServer.sendTXT(num, "message here");
//
// // send data to all connected clients
// // webSocketsServer.broadcastTXT("message here");
// break;
//
// case WStype_BIN:
// Serial.printf("[%u] get binary length: %u\n", num, length);
// hexdump(payload, length);
//
// // send message to client
// // webSocketsServer.sendBIN(num, payload, lenght);
// break;
// }
//}
//void handleIrInput()
//{
@ -1105,7 +1121,7 @@ void pride()
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, i + gHue, 255, LINEARBLEND);
}