Async server
This commit is contained in:
parent
cde03edb44
commit
5056994b96
|
|
@ -1,3 +1,6 @@
|
|||
# Clock made out of 7 segment displays
|
||||
This is a clock made out of many 7 segment displays driven by a MAX7219 driver IC.
|
||||
Some parts of the 7 segment displays are driven like pixels.
|
||||
Some parts of the 7 segment displays are driven like pixels.
|
||||
|
||||
## Setup / Preparations
|
||||
In Platformio, use "Project Tasks -> Platform -> Upload Filesystem Image" to upload the content of the data folder to the flash of the esp.
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<!DOCTYPE html> <html>
|
||||
<head><meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
||||
<title>TooManySevenSegmentsClock</title>
|
||||
<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}
|
||||
body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}
|
||||
p {font-size: 24px;color: #444444;margin-bottom: 10px;}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="webpage">
|
||||
<h1>TooManySevenSegmentsClock</h1>
|
||||
|
||||
<p>Time:
|
||||
</p>
|
||||
|
||||
<form action="/set_time" method="post">
|
||||
<input type="number" min="0" max="23" id="hours" value="%HOURS%" name="hours"><br><br>
|
||||
<input type="number" min="0" max="59" id="minutes" value="%MINUTES%" name="minutes"><br><br>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -14,11 +14,10 @@ board = d1_mini
|
|||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
upload_port = /dev/ttyUSB0
|
||||
|
||||
board_build.filesystem = littlefs
|
||||
lib_deps =
|
||||
wayoda/LedControl@^1.0.6
|
||||
adafruit/RTClib@^1.13.0
|
||||
tzapu/WiFiManager@^0.16.0
|
||||
adafruit/Adafruit SSD1306@^2.4.6
|
||||
adafruit/Adafruit GFX Library@^1.10.10
|
||||
adafruit/Adafruit BusIO@^1.8.2
|
||||
me-no-dev/ESPAsyncTCP@^1.2.2
|
||||
me-no-dev/ESP Async WebServer@^1.2.3
|
||||
alanswx/ESPAsyncWiFiManager@^0.30
|
||||
|
|
|
|||
249
src/main.cpp
249
src/main.cpp
|
|
@ -2,14 +2,16 @@
|
|||
#include "LedControl.h"
|
||||
#include "RTClib.h" // RTC library
|
||||
|
||||
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
||||
#include <ESP8266WebServer.h>
|
||||
#include "WiFiManager.h" //https://github.com/tzapu/WiFiManager
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncWiFiManager.h>
|
||||
|
||||
#include <ESPAsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <FS.h> // Use the SPIFFS library for the filesystem
|
||||
#include <LittleFS.h>
|
||||
|
||||
#include "disp.h"
|
||||
|
||||
|
|
@ -54,22 +56,59 @@ unsigned long delaytime = 500;
|
|||
|
||||
char str[24];
|
||||
|
||||
void configModeCallback(WiFiManager *myWiFiManager);
|
||||
ESP8266WebServer server(80);
|
||||
void configModeCallback(AsyncWiFiManager *myWiFiManager);
|
||||
AsyncWebServer server(80);
|
||||
DNSServer dns;
|
||||
|
||||
void handleRoot(); // function prototypes for HTTP handlers
|
||||
void handleNotFound();
|
||||
void handleBody();
|
||||
void reconnect();
|
||||
void setTime(char* timeStr);
|
||||
void setTime(char *timeStr);
|
||||
String SendHTML();
|
||||
String processor(const String &var);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
delay(100);
|
||||
Serial.println("Starting...");
|
||||
|
||||
if (!rtc.begin())
|
||||
{
|
||||
Serial.println("Couldn't find RTC");
|
||||
delay(200);
|
||||
ESP.reset();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("RTC ready.");
|
||||
}
|
||||
|
||||
/* The MAX72XX is in power-saving mode on startup, we have to do a wakeup call */
|
||||
for (int i = 0; i < lc.getDeviceCount(); i++)
|
||||
{
|
||||
lc.shutdown(i, false);
|
||||
lc.setIntensity(i, 8);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
lc.setDigit(1, i, 8, true);
|
||||
}
|
||||
Serial.println("Display initialized.");
|
||||
|
||||
// Initialize SPIFFS
|
||||
if (!LittleFS.begin())
|
||||
{
|
||||
Serial.println("An Error has occurred while mounting LittleFS");
|
||||
return;
|
||||
}
|
||||
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wifiManager;
|
||||
AsyncWiFiManager wifiManager(&server, &dns);
|
||||
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
|
||||
wifiManager.setAPCallback(configModeCallback);
|
||||
|
||||
|
|
@ -79,51 +118,58 @@ void setup()
|
|||
//and goes into a blocking loop awaiting configuration
|
||||
if (!wifiManager.autoConnect())
|
||||
{
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
Serial.println("failed to connect Wifi and hit timeout");
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
ESP.reset();
|
||||
delay(1000);
|
||||
//ESP.reset();
|
||||
//delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("Wifi connected.");
|
||||
}
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
|
||||
server.on("/", handleRoot);
|
||||
server.on("/set_time", handleBody);
|
||||
server.onNotFound(handleNotFound);
|
||||
//server.on("/", handleRoot);
|
||||
|
||||
// Route for root / web page
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(LittleFS, "/index.html", String(), false, processor); });
|
||||
|
||||
server.on("/set_time", HTTP_POST, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
//List all parameters
|
||||
int params = request->params();
|
||||
uint8_t hours = rtc.now().hour();
|
||||
uint8_t minutes = rtc.now().minute();
|
||||
|
||||
for (int i = 0; i < params; i++)
|
||||
{
|
||||
AsyncWebParameter *p = request->getParam(i);
|
||||
if (p->isPost())
|
||||
{
|
||||
Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
||||
if (p->name() == "hours")
|
||||
{
|
||||
hours = atoi(p->value().c_str());
|
||||
}
|
||||
else if (p->name() == "minutes")
|
||||
{
|
||||
minutes = atoi(p->value().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
DateTime now = rtc.now();
|
||||
rtc.adjust(DateTime(2021, 9, 17, hours, minutes, 0));
|
||||
request->redirect("/");
|
||||
});
|
||||
|
||||
server.begin();
|
||||
Serial.println("HTTP server started");
|
||||
|
||||
|
||||
/* The MAX72XX is in power-saving mode on startup, we have to do a wakeup call */
|
||||
for (int i=0;i<lc.getDeviceCount();i++) {
|
||||
lc.shutdown(i,false);
|
||||
lc.setIntensity(i,8);
|
||||
}
|
||||
|
||||
for (int i=0;i<8;i++) {
|
||||
lc.setDigit(1,i,8,true);
|
||||
}
|
||||
|
||||
/* and clear the display */
|
||||
|
||||
Serial.println("starting");
|
||||
|
||||
delay(3000); // wait for console opening
|
||||
|
||||
//rtc.begin();
|
||||
|
||||
if (!rtc.begin())
|
||||
{
|
||||
Serial.println("Couldn't find RTC");
|
||||
//while (1);
|
||||
return;
|
||||
}
|
||||
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
|
||||
Serial.println("Setup complete.");
|
||||
}
|
||||
|
||||
void configModeCallback(WiFiManager *myWiFiManager)
|
||||
void configModeCallback(AsyncWiFiManager *myWiFiManager)
|
||||
{
|
||||
Serial.println("Entered config mode");
|
||||
Serial.println(WiFi.softAPIP());
|
||||
|
|
@ -131,116 +177,27 @@ void configModeCallback(WiFiManager *myWiFiManager)
|
|||
Serial.println(myWiFiManager->getConfigPortalSSID());
|
||||
}
|
||||
|
||||
void printNumber(int v)
|
||||
{
|
||||
int ones;
|
||||
int tens;
|
||||
int hundreds;
|
||||
boolean negative;
|
||||
|
||||
if (v < -999 || v > 999)
|
||||
return;
|
||||
if (v < 0)
|
||||
{
|
||||
negative = true;
|
||||
v = v * -1;
|
||||
}
|
||||
ones = v % 10;
|
||||
v = v / 10;
|
||||
tens = v % 10;
|
||||
v = v / 10;
|
||||
hundreds = v;
|
||||
if (negative)
|
||||
{
|
||||
//print character '-' in the leftmost column
|
||||
lc.setChar(0, 3, '-', false);
|
||||
}
|
||||
else
|
||||
{
|
||||
//print a blank in the sign column
|
||||
lc.setChar(0, 3, ' ', false);
|
||||
}
|
||||
//Now print the number digit by digit
|
||||
lc.setDigit(0, 2, (byte)hundreds, false);
|
||||
lc.setDigit(0, 1, (byte)tens, false);
|
||||
lc.setDigit(0, 0, (byte)ones, false);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
server.handleClient();
|
||||
setTime(str);
|
||||
printString(str, lc);
|
||||
}
|
||||
|
||||
void setTime(char* timeStr) {
|
||||
void setTime(char *timeStr)
|
||||
{
|
||||
DateTime now = rtc.now();
|
||||
sprintf(timeStr, "%02d:%02d", now.hour(), now.minute());
|
||||
}
|
||||
|
||||
|
||||
void handleRoot()
|
||||
// Replaces placeholder with minutes and hours
|
||||
String processor(const String &var)
|
||||
{
|
||||
server.send(200, "text/html", SendHTML());
|
||||
if (var == "HOURS")
|
||||
{
|
||||
return String((int)rtc.now().hour());
|
||||
}
|
||||
else if (var == "MINUTES")
|
||||
{
|
||||
return String((int)rtc.now().minute());
|
||||
}
|
||||
}
|
||||
|
||||
void handleBody() { //Handler for the body path
|
||||
|
||||
if (server.hasArg("hours")== false || server.hasArg("minutes")==false){ //Check if body received
|
||||
|
||||
server.send(200, "text/plain", "Body not received");
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
uint8_t hours = atoi(server.arg("hours").c_str());
|
||||
uint8_t minutes = atoi(server.arg("minutes").c_str());
|
||||
|
||||
DateTime now = rtc.now();
|
||||
rtc.adjust(DateTime(2021, 9, 17, hours, minutes, 0));
|
||||
|
||||
server.send(200, "text/html", SendHTML());
|
||||
|
||||
|
||||
}
|
||||
|
||||
void handleNotFound()
|
||||
{
|
||||
server.send(404, "text/plain", "Not found");
|
||||
}
|
||||
|
||||
String SendHTML()
|
||||
{
|
||||
setTime(str);
|
||||
String ptr = "<!DOCTYPE html> <html>\n";
|
||||
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
|
||||
ptr += "<title>TooManySevenSegmentsClock</title>\n";
|
||||
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
|
||||
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
|
||||
ptr += "p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
|
||||
ptr += "</style>\n";
|
||||
ptr += "</head>\n";
|
||||
ptr += "<body>\n";
|
||||
ptr += "<div id=\"webpage\">\n";
|
||||
ptr += "<h1>TooManySevenSegmentsClock</h1>\n";
|
||||
|
||||
ptr += "<p>Time: ";
|
||||
ptr += "</p>";
|
||||
|
||||
ptr += "<form action=\"/set_time\" method=\"post\">";
|
||||
ptr += " <input type=\"number\" min=\"0\" max=\"23\" id=\"hours\" value=\"";
|
||||
ptr += (int)rtc.now().hour();
|
||||
ptr += "\" name=\"hours\"><br><br>";
|
||||
ptr += " <input type=\"number\" min=\"0\" max=\"59\" id=\"minutes\" value=\"";
|
||||
ptr += (int)rtc.now().minute();
|
||||
ptr += "\" name=\"minutes\"><br><br>";
|
||||
ptr += " <input type=\"submit\" value=\"Submit\">";
|
||||
ptr += "</form> ";
|
||||
|
||||
|
||||
ptr += "</div>\n";
|
||||
ptr += "</body>\n";
|
||||
ptr += "</html>\n";
|
||||
return ptr;
|
||||
}
|
||||
Loading…
Reference in New Issue