Many improvments

This commit is contained in:
Sébastien Vallée 2017-12-22 14:39:31 +01:00
parent bf253506f2
commit e97d9b98b5

View File

@ -11,6 +11,8 @@
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include <time.h>
#include <ArduinoOTA.h>
#include "OTAsetup.h"
#include "d_helper.h"
@ -32,7 +34,7 @@ String SITE_WIDTH = "1000";
int timezone = 1;
int dstOffset = 1;
const char* ntpServer = "fr.pool.ntp.org";
int brightness = 2;
int brightness = 16;
int pinCS = D4;
int numberOfHorizontalDisplays = 4;
int numberOfVerticalDisplays = 1;
@ -43,20 +45,47 @@ String message, webpage;
ESP8266WebServer server(PORT);
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
void configModeCallback (WiFiManager *myWiFiManager) { // AP config mode
display_message("Setup");
}
void setup() {
Serial.begin(115200); // initialize serial communications
DEBUG_PRINT(F("\n\r"));
matrix.fillScreen(LOW); //clr screen
initOTA();
for (int i=0; i<numberOfHorizontalDisplays; i++){
DEBUG_PRINT(F("Changing orientation of display #"));
DEBUG_PRINTLN(i);
matrix.setRotation(i, 1);
}
display_message("Wi-Fi");
WiFiManager wifiManager;
// reset saved settings
// wifiManager.resetSettings();
#ifndef DEBUG_WifiM
wifiManager.setDebugOutput(false); // no debug
#endif
wifiManager.setTimeout(180);
wifiManager.setAPCallback(configModeCallback);
if(!wifiManager.autoConnect("LibreMetric")) {
Serial.println(F("failed to connect and timeout occurred"));
DEBUG_PRINTLN(F("failed to connect and timeout occurred"));
display_message("Failed to connect");
delay(6000);
ESP.reset(); //reset and try again
delay(180000);
}
matrix.fillScreen(LOW);
// At this stage the WiFi manager will have successfully connected to a network, or if not will try again in 180-seconds
Serial.println(F("WiFi connected..."));
Serial.println(WiFi.localIP());
DEBUG_PRINTLN(F("WiFi connected..."));
display_message("OK");
delay(10*wait);
DEBUG_PRINTLN(WiFi.localIP());
configTime(timezone * 3600, dstOffset * 3600, ntpServer); // First sync of time with (S)NTP
#ifdef DEBUG_TELNET
@ -67,20 +96,14 @@ void setup() {
//----------------------------------------------------------------------
server.begin();
Serial.println(F("Webserver started..."));
matrix.setIntensity(brightness);
brightness=16;
Serial.print(numberOfHorizontalDisplays);
Serial.println(" displays configurated...");
for (int i=0; i<numberOfHorizontalDisplays; i++){
Serial.print("Changing orientation of display #");
Serial.println(i);
matrix.setRotation(i, 1);
}
DEBUG_PRINTLN(F("Webserver started..."));
DEBUG_PRINT(numberOfHorizontalDisplays);
DEBUG_PRINTLN(F(" displays configurated..."));
server.on("/", GetMessage);
display_message(WiFi.localIP().toString());
message = "Bienvenue !";
}
void loop() {
@ -92,31 +115,46 @@ void loop() {
matrix.fillScreen(LOW);
time_t now = time(nullptr);
String time = String(ctime(&now));
//Serial.println(time);
time.trim();
time.substring(11,19).toCharArray(time_value, 10);
int seconds = time.substring(17,19).toInt();
//Serial.println(seconds);
for (int i = 0; i <= 4; i++) {
if (i != 2 || seconds % 2 == 0) matrix.drawChar(i*6+2,0, time_value[i], HIGH,LOW,1);
if (i != 2 || seconds % 2 == 0) matrix.drawChar(i*6+2,0, time_value[i], HIGH,LOW,1); // H
}
matrix.write(); // Send bitmap to display
if (message != "") {
Serial.print(time);
Serial.print(" : On envoi \"");
Serial.print(message);
Serial.println("\"");
display_message(message); // Display the message
DEBUG_PRINT(F("Message envoyé à LibreMetric : "));
DEBUG_PRINTLN(message);
message = "";
}
if (brightness <= 15) {
Serial.print("Nouvelle luminosité : ");
Serial.println(brightness);
DEBUG_PRINT(F("Nouvelle luminosité : "));
DEBUG_PRINTLN(brightness);
matrix.setIntensity(brightness);
brightness = 16;
}
yield();
ArduinoOTA.handle();
yield();
}
void display_message(String message){
if (width * message.length() - spacer > matrix.width()) {
display_message_long(message);
}
else {
matrix.fillScreen(LOW);
int x = (matrix.width()-(width * message.length() - spacer))/2;
int y = (matrix.height() - 8) / 2; // center the text vertically
for ( int i =0; i < message.length(); i++ ){
matrix.drawChar(x + (i * width), y, message[i], HIGH, LOW, 1); // HIGH LOW means foreground ON, background OFF, reverse these to invert the display!
}
matrix.write(); // Send bitmap to display
delay(4000);
}
}
void display_message_long(String message){
for ( int i = 0 ; i < width * message.length() + matrix.width() - spacer; i++ ) {
matrix.fillScreen(LOW);
int letter = i / width;
@ -148,7 +186,7 @@ void GetMessage() {
String Argument_Name = server.argName(i);
String client_response = server.arg(i);
if (Argument_Name == "message") message = client_response;
else if (Argument_Name == "brightness") brightness = client_response.toInt();
if (Argument_Name == "brightness") brightness = client_response.toInt();
}
}
server.send(200, "text/html", webpage); // Send a response to the client to enter their inputs, if needed, Enter=defaults