// global config #include "config.h" //################# LIBRARIES ########################## #include #include #include #include // https://github.com/tzapu/WiFiManager #include #include #include #include #include #include #include "OTAsetup.h" #include "d_helper.h" #include "FS.h" #define HOSTNAME "LibreMetric-" //################# DISPLAY CONNECTIONS ################ // LED Matrix Pin -> ESP8266 Pin // Vcc -> 3v (3V on NodeMCU 3V3 on WEMOS) // Gnd -> Gnd (G on NodeMCU) // DIN -> D7 (Same Pin for WEMOS) // CS -> D4 (Same Pin for WEMOS) // CLK -> D5 (Same Pin for WEMOS) //################ PROGRAM SETTINGS #################### String version = "0.1b"; int PORT = 80; int wait = 50; int spacer = 1; int width = 5 + spacer; String SITE_WIDTH = "1000"; int timezone = 1; int dstOffset = 1; const char* ntpServer = "fr.pool.ntp.org"; int brightness = 16; int pinCS = D4; int numberOfHorizontalDisplays = 4; int numberOfVerticalDisplays = 1; //################## INITIALIZING ###################### char time_value[20]; String message, webpage; ESP8266WebServer server(PORT); Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays); // in a terminal: telnet esp IP #ifdef DEBUG_TELNET WiFiServer telnetServer(23); WiFiClient telnetClient; void handleTelnet(void) { if (telnetServer.hasClient()) { if (!telnetClient || !telnetClient.connected()) { if (telnetClient) { telnetClient.stop(); } telnetClient = telnetServer.available(); } else { telnetServer.available().stop(); } } } #endif void configModeCallback (WiFiManager *myWiFiManager) { // AP config mode display_message("Setup"); } void setup() { // Initialisation Serial.begin(115200); DEBUG_PRINT(F("\n\r")); matrix.fillScreen(LOW); // clear screen initOTA(); SPIFFS.begin(); for (int i=0; i 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(20*wait); } } // Function for displaying only long messages (longer than LED matrix length) // This function is called only by the display_message function 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; int x = (matrix.width() - 1) - i % width; int y = (matrix.height() - 8) / 2; // center the text vertically while ( x + width - spacer >= 0 && letter >= 0 ) { if ( letter < message.length() ) { matrix.drawChar(x, y, message[letter], HIGH, LOW, 1); // HIGH LOW means foreground ON, background OFF, reverse these to invert the display! } letter--; x -= width; } matrix.write(); // Send bitmap to display delay(wait); } } void GetMessage() { if (server.args() > 0 ) { // Arguments were received for ( uint8_t i = 0; i <= server.args(); i++ ) { String Argument_Name = server.argName(i); String client_response = server.arg(i); if (Argument_Name == "message" && client_response != "") message = client_response; if (Argument_Name == "brightness" && client_response != "") brightness = client_response.toInt(); } } String webpage = fileRead("/index.html"); String ipaddress = WiFi.localIP().toString(); webpage.replace("ipaddress", ipaddress); server.send(200, "text/html", webpage); // Send a response to the client to enter their inputs, if needed, Enter=defaults } String fileRead(String name){ //read file from SPIFFS and store it as a String variable String contents; File file = SPIFFS.open(name.c_str(), "r"); if (!file) { String errorMessage = "Can't open '" + name + "' !\r\n"; DEBUG_PRINTLN(errorMessage); return "FILE ERROR"; } else { // this is going to get the number of bytes in the file and give us the value in an integer int fileSize = file.size(); int chunkSize=1024; //This is a character array to store a chunk of the file. //We'll store 1024 characters at a time char buf[chunkSize]; int numberOfChunks=(fileSize/chunkSize)+1; int count=0; int remainingChunks=fileSize; for (int i=1; i <= numberOfChunks; i++){ if (remainingChunks-chunkSize < 0){ chunkSize=remainingChunks; } file.read((uint8_t *)buf, chunkSize-1); remainingChunks=remainingChunks-chunkSize; contents+=String(buf); } file.close(); return contents; } }