First commit

This commit is contained in:
Sébastien Vallée 2017-12-17 01:06:06 +01:00
parent ef6c833e3c
commit 76e0e54318

169
LibreMetric.ino Normal file
View File

@ -0,0 +1,169 @@
//################# LIBRARIES ##########################
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <DNSServer.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include <time.h>
//################# 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 = 2;
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);
void setup() {
Serial.begin(115200); // initialize serial communications
WiFiManager wifiManager;
wifiManager.setTimeout(180);
//fetches ssid and password and tries to connect, if connections succeeds it starts an access point with the name called "LibreMetric" and waits in a blocking loop for configuration
if(!wifiManager.autoConnect("LibreMetric")) {
Serial.println(F("failed to connect and timeout occurred"));
delay(6000);
ESP.reset(); //reset and try again
delay(180000);
}
// 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());
configTime(timezone * 3600, dstOffset * 3600, ntpServer); // First sync of time with (S)NTP
//----------------------------------------------------------------------
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);
}
server.on("/", GetMessage);
message = "Bienvenue !";
}
void loop() {
server.handleClient();
matrix.fillScreen(LOW);
time_t now = time(nullptr);
String time = String(ctime(&now));
time.trim();
time.substring(11,19).toCharArray(time_value, 10);
matrix.drawChar(2,0, time_value[0], HIGH,LOW,1); // H
matrix.drawChar(8,0, time_value[1], HIGH,LOW,1); // HH
matrix.drawChar(14,0,time_value[2], HIGH,LOW,1); // HH:
matrix.drawChar(20,0,time_value[3], HIGH,LOW,1); // HH:M
matrix.drawChar(26,0,time_value[4], HIGH,LOW,1); // HH:MM
matrix.write(); // Send bitmap to display
if (message != "") {
Serial.print("On envoi \"");
Serial.print(message);
Serial.println("\" à LibreMetric");
display_message(message); // Display the message
Serial.print("Message envoyé à LibreMetric : ");
Serial.println(message);
message = "";
}
if (brightness <= 15) {
Serial.print("Nouvelle luminosité : ");
Serial.println(brightness);
matrix.setIntensity(brightness);
brightness = 16;
}
}
void display_message(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() {
webpage = "";
append_page_header();
String IPaddress = WiFi.localIP().toString();
webpage += "<form action=\"http://"+IPaddress+"\" method='post'>";
webpage += "<P>Message à envoyer : <INPUT type='text' size ='60' name='message' placeholder='Votre message'><input type='submit', value='Envoyer !'></form></P><br>";
webpage += "<form action=\"http://"+IPaddress+"\" method='post'>";
webpage += "<P>Luminosité : <INPUT type='text' size ='60' name='brightness' placeholder='Luminosité de 0 à 15'><input type='submit', value='Envoyer !'></form></P><br>";
append_page_footer();
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") message = client_response;
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
}
void append_page_header() {
webpage = F("<!DOCTYPE HTML><html lang='fr'><head>");
webpage += "<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=utf-8'>";
webpage += F("<title>Envoi de messages - LibreMetric</title><style>");
webpage += F("body {width:");
webpage += SITE_WIDTH;
webpage += F("px;margin:0 auto;font-family:arial;font-size:14px;text-align:center;color:#776688;background-color:#F7F2Fd;}");
webpage += F("ul{list-style-type:circle; margin:0;padding:0;overflow:hidden;background-color:#d8d8d8;font-size:14px;}");
webpage += F("li{float:left;border-right:1px solid #bbb;}last-child {border-right:none;}");
webpage += F("li a{display: block;padding:2px 12px;text-decoration:none;}");
webpage += F("li a:hover{background-color:#FFFFFF;}");
webpage += F("section {font-size:16px;}");
webpage += F("p {background-color:#D1DBE3;font-size:16px;}");
webpage += F("div.header,div.footer{padding:0.5em;color:white;background-color:gray;clear:left;}");
webpage += F("h1{background-color:#d8d8d8;font-size:26px;}");
webpage += F("h2{color:#9370DB;font-size:22px;line-height:65%;}");
webpage += F("h3{color:#9370DB;font-size:16px;line-height:55%;}");
webpage += F("</style></head><body><h1>LibreMetric ");
webpage += version+"</h1>";
}
void append_page_footer(){ // Saves repeating many lines of code for HTML page footers
webpage += F("<ul><li><a href='/'>Recharger la page</a></li></ul>");
webpage += F("</body></html>");
}