Et voilà le résultat. Voici l'utilisation :
- Récupération des valeurs température et consommation sur le Jeedom pour affichage des informations.
- Arrêt/Démarrage écran en fonction de la présence ou bouton
- Intégration d'une sonde BMP280 pour remonter la valeur dans Jeedom
- MAJ via OTA
- Affichage de l'heure via NTPClient
Je reste bien sûr ouvert à toutes remarques/amélioration, surtout avec le niveau de novice en code.
//Déclaration des librairies
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
//******* OTA ********
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
//******* NTP ********
#include <NTPClient.h>
//Configuration MAX7219
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CS_PIN D6
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
//-------BMP----------
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10
Adafruit_BMP280 bmp;
// ----NTP-----
WiFiClient espClient;
WiFiUDP ntpUDP;
int16_t utc = +2;
NTPClient timeClient(ntpUDP, "ntp.univ-lyon1.fr", utc*3600, 600000);
// ----WIFI-----
const char* ssid = "Wifi";
const char* password = "xxxxxxxx";
//Déclaration variables
HTTPClient http;
String jeedom_url = "http://192.168.1.xxx/core/api/jeeApi.php?apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&type=cmd&id=";
String jeedom_url_post = "http://192.168.1.xxx/core/api/jeeApi.php?plugin=virtual&apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&type=virtual&id=";
String id_temp = "322";
String id_conso = "1278";
String id_journuit = "406";
String id_prox = "1117";
String prox = "2";
String journuit = "1";
String temp = "";
String conso = "";
String unit_conso = "kW";
#define BUF_SIZE 75
char temperature[BUF_SIZE] = "%%%%%";
char consomm[BUF_SIZE] = "%%%%%";
char temps[BUF_SIZE] = "1";
uint8_t degC[] = { 6, 3, 3, 56, 68, 68, 68 }; // Deg C
uint8_t pts[] = { 5, 64, 0, 64, 0, 64 }; // 3 points
char szMesg[20] = "";
//BMP280
float pressure;
float hpascals;
//Gestion du temps
unsigned long previousMillisSensor = 0;
unsigned long previousMillisGeneral = 0;
const unsigned long TIME_SENSOR = 30000; //30 secondes
const unsigned long TIME_GENERAL = 300000; // 5 minutes
//Button
const int button = D3;
int buttonState;
//################# SETUP ########################################
void setup()
{
Serial.begin(115200);
ArduinoOTA.setHostname("MAX7219");
P.begin();
P.setIntensity(5);
//Wifi
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(500);
WiFi.begin(ssid, password);
WiFi.hostname("ESP_Salon");
while (WiFi.status() != WL_CONNECTED) {
Serial.println();
Serial.println("Connecting..");
P.print("Start.");
delay(4000);
}
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
P.print("Wifi ok");
delay(2500);
timeClient.begin();
//Display
P.displayText(szMesg, PA_CENTER, 25, 5000, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
P.addChar('
, degC);
P.addChar('%', pts);
delay(2500);
P.displayClear();
// OTA
ArduinoOTA.onStart( {
Serial.println("Start");
});
ArduinoOTA.onEnd( {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
bmp.begin();
// Déclaration bouton
pinMode(button, INPUT_PULLUP);
//MAJ NTP pour première
timeClient.update();
String TM=timeClient.getFormattedTime().substring(0,5);
TM.toCharArray(temps,50);
}
//#####################LOOP######################################
void loop()
{
buttonState = digitalRead(button);
unsigned long currentMillis = millis();
//----------------- GET PARAMETERS ---------------------
if (currentMillis - previousMillisGeneral >= TIME_GENERAL) {
previousMillisGeneral = currentMillis;
http.begin(jeedom_url+id_prox);
int httpCode = http.GET();
if (httpCode > 0) {
prox = http.getString();
http.end();
delay(50);
Serial.print("Promixité : ");
Serial.println(prox);
}
http.begin(jeedom_url+id_journuit);
httpCode = http.GET();
if (httpCode > 0) {
journuit = http.getString();
http.end();
Serial.print("Jour / Nuit : ");
Serial.println(journuit);
}
//Read data
pressure = bmp.readPressure();
hpascals = (pressure/100)+34;
Serial.println(hpascals);
if (isnan(pressure)) {
Serial.println("BMP280 inaccessible!");
}
else{
String urlp = jeedom_url_post+"1194&value="+hpascals;
http.begin(urlp); //Virtuel Pression
httpCode = http.GET();
http.end();
Serial.println(urlp);
}
}
static uint8_t display = 0;
if (journuit=="1"){
P.setIntensity(3);
}
else {
P.setIntensity(0);
}
if (prox=="0" || buttonState == LOW){
P.displayClear();
Serial.print("Pause");
delay(60000);
}
else
{
// ------------ GET INFO CAPTEUR -------------------------------
if (currentMillis - previousMillisSensor >= TIME_SENSOR) {
previousMillisSensor = currentMillis;
http.begin(jeedom_url+id_temp);
int httpCode = http.GET();
if (httpCode > 0) {
String temp = http.getString()+"$";
temp.toCharArray(temperature,50);
http.end();
Serial.print("Température : ");
Serial.println(temperature);
delay(50);
}
http.begin(jeedom_url+id_conso);
httpCode = http.GET();
if (httpCode > 0) {
String conso = http.getString()+unit_conso;
conso.toCharArray(consomm,50);
http.end();
Serial.print("Consommation : ");
Serial.println(consomm);
}
timeClient.update();
String TM=timeClient.getFormattedTime().substring(0,5);
TM.toCharArray(temps,50);
Serial.print("Horloge : ");
Serial.println(temps);
}
if (P.displayAnimate())
{
switch(display)
{
case 0: //Temp
P.setTextEffect(0, PA_OPENING, PA_CLOSING);
P.setPause(10000);
display++;
strcpy(szMesg,temperature);
break;
case 1: //Conso
P.setTextEffect(0, PA_GROW_UP, PA_GROW_DOWN);
P.setPause(5000);
display++;
strcpy(szMesg,consomm);
break;
case 2: //Temp
P.setTextEffect(0, PA_OPENING, PA_CLOSING);
P.setPause(10000);
display++;
strcpy(szMesg,temperature);
break;
default:
P.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
P.setPause(5000);
display = 0;
strcpy(szMesg,temps);
break;
}
P.displayReset();
}
} //prox
ArduinoOTA.handle();
}