Voici le code que j'utilise.
Je préfère prévenir: il est vraiment crado, c'est du bidouillage
Code : Tout sélectionner
/* ESP8266 plus MAX7219 LED Matrix that displays messages revecioved via a WiFi connection using a Web Server
Provides an automous display of messages
The MIT License (MIT) Copyright (c) 2017 by David Bird.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, but not sub-license and/or
to sell copies of the Software or to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
See more at http://dsbird.org.uk
*/
//################# LIBRARIES ##########################
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
//#include <WiFiClient.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>
//TEST
#include <TimeLib.h>
#include <NtpClientLib.h>
//TEST
int pinCS = D4; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI )
int numberOfHorizontalDisplays = 9;
int numberOfVerticalDisplays = 1;
char time_value[20];
String temperature_maison, webpage, consommation_maison, message, text, intro, heure;
//################# 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 = "v2.0"; // Version of this program
ESP8266WebServer server(80); // Start server on port 80 (default for a web-browser, change to your requirements, e.g. 8080 if your Router uses port 80
// To access server from the outside of a WiFi network e.g. ESP8266WebServer server(8266) add a rule on your Router that forwards a
// connection request to http://your_network_ip_address:8266 to port 8266 and view your ESP server from anywhere.
// Example http://yourhome.ip:8266 will be directed to http://192.168.0.40:8266 or whatever IP address your router gives to this server
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
int wait = 6000; // In milliseconds between scroll movements
int spacer = 1;
int width = 5 + spacer; // The font width is 5 pixels
int length;
int count=0;
String SITE_WIDTH = "1000";
unsigned long previousMillis = 0;
void setup() {
Serial.begin(115200); // initialize serial communications
WiFiManager wifiManager;
// New OOB ESP8266 has no Wi-Fi credentials so will connect and not need the next command to be uncommented and compiled in, a used one with incorrect credentials will
// so restart the ESP8266 and connect your PC to the wireless access point called 'ESP8266_AP' or whatever you call it below in ""
// wifiManager.resetSettings(); // Command to be included if needed, then connect to http://192.168.4.1/ and follow instructions to make the WiFi connection
// Set a timeout until configuration is turned off, useful to retry or go to sleep in n-seconds
wifiManager.setTimeout(180);
//fetches ssid and password and tries to connect, if connections succeeds it starts an access point with the name called "ESP8266_AP" and waits in a blocking loop for configuration
if(!wifiManager.autoConnect("ESP8266_AP")) {
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..."));
//----------------------------------------------------------------------
server.begin(); Serial.println(F("Webserver started...")); // Start the webserver configTime(0 * 3600, 0, "pool.ntp.org", "time.nist.gov");
matrix.setIntensity(2); // Use a value between 0 and 15 for brightness
matrix.setRotation(0, 0); // The first display is position upside down
matrix.setRotation(1, 0); // The first display is position upside down
matrix.setRotation(2, 0); // The first display is position upside down
matrix.setRotation(3, 0); // The first display is position upside down
matrix.setRotation(4, 0); // The first display is position upside down
matrix.setRotation(5, 1); // The first display is position upside down
matrix.setRotation(6, 1); // The first display is position upside down
matrix.setRotation(7, 1); // The first display is position upside down
matrix.setRotation(8, 1); // The first display is position upside down
server.on("/", GetMessage);
wait = 40;
intro = "Domoticz Infos";
display_scroll(intro); // Display the message
wait = 75;
//display_scroll(message); // Display the message
//message = "Waiting data...";
//wait = 100;
//TEST
NTP.begin("pool.ntp.org", 1, true);
NTP.setInterval(600);
//TEST
}
void loop() {
unsigned long currentMillis = millis();
server.handleClient();
if (currentMillis - previousMillis >= 1000) {
previousMillis = currentMillis;
count++; //Seconds
}
if (server.args() < 1 ) {
display_scroll("Waiting data...");
count=0;
//TEST
//Serial.print(hour());
// printDigits(minute());
// Serial.println();
//display_static(hour() + ":" + minute());
//delay(5000);
//TEST
}
else if (message.length() == 0 && count>=0 && count <1 && consommation_maison.length() > 0 ){
//display_static(" ");
display_scroll("Consomation"); // Display the message
Serial.println(count);
}
else if (message.length() == 0 && count>=1 && count<6 && consommation_maison.length() > 0){
display_static(consommation_maison);
Serial.println(count);
}
else if (message.length() == 0 && count>=6 && count <7 && temperature_maison.length() > 0){
//display_scroll(" ");
display_scroll("Temperature"); // Display the message
Serial.println(count);
}
else if (message.length() == 0 && count>=7 && count<12 && temperature_maison.length() > 0){
display_static(temperature_maison);
Serial.println(count);
}
else if (message.length() == 0 && count>=12 && count<13 && heure.length() > 0){
display_scroll(" ");
Serial.println(count);
}
else if (message.length() == 0 && count>=13 && count<23 && heure.length() > 0){
display_static(heure);
Serial.println(count);
}
else if (message.length() == 0 && count>=22 && temperature_maison.length() > 0){
count=0; //<----reset
Serial.println(count);
}
else if (message.length() >0){
display_scroll(message);
consommation_maison = "";
temperature_maison = "";
}
}
//TEST
//void printDigits(int digits)
//{
// utility for digital clock display: prints preceding colon and leading 0
// Serial.print(":");
// if (digits < 10)
// Serial.print('0');
// Serial.print(digits);
//}
//TEST
void display_scroll(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/2);
// length = message.length() * width;
// //fill with blank spaces the rest of the display area
// for (int i = 8; i>0; i--){
// message += " ";
// }
// matrix.setCursor((numberOfHorizontalDisplays*8-length)/2,0); // Center text
// matrix.fillScreen(LOW);
// matrix.print(message);
// matrix.write();
}
}
void display_static(String text){
length = text.length() * width;
//fill with blank spaces the rest of the display area
for (int i = 8; i>0; i--){
text += " ";
}
matrix.setCursor((numberOfHorizontalDisplays*8-length)/2,0); // Center text
matrix.fillScreen(LOW);
matrix.print(text);
matrix.write();
}
void GetMessage() {
webpage = ""; // don't delete this command, it ensures the server works reliably!
append_page_header();
String IPaddress = WiFi.localIP().toString();
webpage += "<form action=\"http://"+IPaddress+"\" method='post'>";
webpage += "<P>Enter message: <INPUT type='text' size ='60' name='message' value='Enter message'></P></form><br>";
//webpage += "<P>Enter consommation: <INPUT type='text' size ='60' name='consommation' value='Enter consommation'></P></form><br>";
append_page_footer();
server.send(200, "text/html", webpage); // Send a response to the client to enter their inputs, if needed, Enter=defaults
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 == "temperature_maison") temperature_maison = client_response;
if (Argument_Name == "consommation_maison") consommation_maison = client_response;
if (Argument_Name == "message") message = client_response;
if (Argument_Name == "heure") heure = client_response;
}
}
}
void append_page_header() {
webpage = F("<!DOCTYPE HTML><html lang='en'><head>"); // Change language (en) as required
webpage += "<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=utf-8'>";
webpage += F("<title>Message Board</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:#cc66ff;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:#E3D1E2;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>Message Display Board ");
webpage += version+"</h1>";
}
void append_page_footer(){ // Saves repeating many lines of code for HTML page footers
webpage += F("<ul><li><a href='/'>Enter Message</a></li></ul>");
webpage += "©"+String(char(byte(0x40>>1)))+String(char(byte(0x88>>1)))+String(char(byte(0x5c>>1)))+String(char(byte(0x98>>1)))+String(char(byte(0x5c>>1)));
webpage += String(char((0x84>>1)))+String(char(byte(0xd2>>1)))+String(char(0xe4>>1))+String(char(0xc8>>1))+String(char(byte(0x40>>1)));
webpage += String(char(byte(0x64/2)))+String(char(byte(0x60>>1)))+String(char(byte(0x62>>1)))+String(char(0x6e>>1));
webpage += F("</body></html>");
}
Pour afficher heure/conso/température j'utilise:
Code : Tout sélectionner
os.execute('curl -m 5 -X POST -F "temperature_maison=' .. tempmoymaison .. ' degres" -F "consommation_maison=' .. conso_maison .. ' watts" -F "heure=' .. heure_actuelle .. ':' .. minute_actuelle ..'" http://192.168.0.4')
Où
tempmoymaison,
conso_maison et
heure_actuelle sont des variables à définir via un script time.
Il est possible de n'afficher qu'un seul message qui défile à l'infini:
Code : Tout sélectionner
os.execute('curl -m 5 -X POST -F "temperature_maison=MON MESSAGE QUI DEFILE" http://192.168.0.4')
Pour revenir à l'affichage conso/température/heure il suffit de:
Code : Tout sélectionner
os.execute('curl -m 5 -X POST -F "temperature_maison=" http://192.168.0.4')
Si ça peut servir à quelqu'un
