Bonjour
je n'avais pas mis le sketch complet, seulement la partie qui me pose problème. Voici le sketch presque complet( moins de 9000 car sur le forum). Pas de soucis particuliers, excepté ce que j'ai mis plus haut, c'est à dire que la partie qui va me servir à changer la couleur du fond ne fonctionne pas.
cordialement Olivier
/*
Projet d'apprentissage d'un objet connecté (IoT) pour réaliser une sonde de température
ESP8266 + DHT22 + LED + MQTT + Home-Assistant
Projets DIY (https://www.projetsdiy.fr) - Mai 2016
Licence : MIT
*/
#include <WiFi.h>
#include <PubSubClient.h> // librairie mqtt
#include "DHT.h" // Librairie des capteurs DHT
#include <TFT_eSPI.h> // ecran
#include <SPI.h>
#include <Wire.h>
#include <OneButton.h> // gestion bouttons sur la carte
#include "bmp.h" // image de demarrage
#include "PMS.h" //capteur poussiere
#include "HardwareSerial.h" // pas de softwareserial sur les esp32
#include "MHZ19.h"
// pin propre a la ttgo-t-display
#ifndef TFT_DISPOFF
#define TFT_DISPOFF 0x28
#endif
#ifndef TFT_SLPIN
#define TFT_SLPIN 0x10
#endif
#define TFT_MOSI 19
#define TFT_SCLK 18
#define TFT_CS 5
#define TFT_DC 16
#define TFT_RST 23
#define TFT_BL 4 // Display backlight control pin
#define ADC_EN 14
#define ADC_PIN 34
//pin pour le pms
#define TX1_pin 13
#define RX1_pin 12
#define setPms 15
//pin pour le mh z19
#define RX_PIN 17 // Rx pin which the MHZ19 Tx pin is attached to
#define TX_PIN 2
#define BAUDRATE 9600
// pin pour le dht
#define DHTPIN 27 // Pin sur lequel est branché le DHT
// Dé-commentez la ligne qui correspond à votre capteur
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//box et serveur mosquitto
#define mqtt_server "192.168.1.72"
#define mqtt_user "xxxxx" //s'il a été configuré sur Mosquitto
#define mqtt_password "xxxxxxxxxxxxx" //idem
#define domoticz_topic "domoticz/in"
#define ssid "Rxxxxxxx"
#define password "xxxxxxxxxxxxxxx"
//const int setPms = 15;
char buff[512];
const byte buttonPin = 0;
//Buffer qui permet de décoder les messages MQTT reçus
char message_buff[100];
String information_str;
char information_char[40];
String PMS1;
char PMS1_char[40];
String PMS25;
char PMS25_char[40];
String PMS10;
char PMS10_char[40];
String CO2;
char CO2_char[40];
long lastMsg = 0; //Horodatage du dernier message publié sur MQTT
long lastMsg1 = 0;
unsigned long lastMsg2 = 0;
bool debug = true; //Affiche sur la console si True
float h = 0;
float t = 0;
float particules = 0;
int mesure_CO2;
char couleurFond[] = {0x0000, 0xFFFF, 0xF800, 0x07E0, 0x001F, 0x07FF, 0xF81F, 0xFFE0}; //black blanc, rouge, vert,bleu,cyan magenta,jaune
//Création des objets
HardwareSerial serial1(1); // RX, TX
PMS pms(serial1);
PMS::DATA data;
TFT_eSPI tft = TFT_eSPI(135, 240); // Invoke custom library
HardwareSerial serial2(2); // RX, TX
MHZ19 myMHZ19;
DHT dht(DHTPIN, DHTTYPE);
WiFiClient espClient;
PubSubClient client(espClient);
OneButton button(buttonPin, true); // true pour le mettre en INPUT_pullup
void setup() {
Serial.begin(9600); //Facultatif pour le debug
serial1.begin(9600, SERIAL_8N1, RX1_pin, TX1_pin); // pin 13 et 12 sur la ttgo Development Board pour le capteur pms
serial2.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN); // ESP32
tft.init();
tft.setRotation(1);
tft.fillScreen(couleurFond[0]);
tft.setTextSize(2);
tft.setTextColor(couleurFond[0]);
tft.setCursor(0, 0);
tft.setTextDatum(MC_DATUM);
tft.setTextSize(2);
button.attachClick(simpleclick); // On attache la fonction simpleClick() comme callBack en cas de simple click
button.attachDoubleClick(doubleclick);
button.attachLongPressStop(longPressStop); // On attache
pinMode(setPms, OUTPUT);
setup_wifi(); //On se connecte au réseau wifi
client.setServer(mqtt_server, 1883); //Configuration de la connexion au serveur MQTT
//client.setCallback(callback); //La fonction de callback qui est executée à chaque réception de message
dht.begin();
myMHZ19.begin(serial2); // *Important, Pass your Stream reference
myMHZ19.autoCalibration();
digitalWrite(setPms, LOW); //pms.passiveMode(); tft.setCursor(130, 50); tft.print("PMS sleep ") ;
if (TFT_BL > 0) { // TFT_BL has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
pinMode(TFT_BL, OUTPUT); // Set backlight pin to output mode
digitalWrite(TFT_BL, TFT_BACKLIGHT_ON); // Turn backlight on. TFT_BACKLIGHT_ON has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
}
for (int count = 0; count < 7; count++) {
tft.fillScreen(couleurFond[count]);
Serial.println(couleurFond[count]);
delay(1000);
}
tft.setSwapBytes(true);
tft.pushImage(0, 0, 240, 135, ttgo);
delay(2000);
tft.fillScreen(0xFFFF);
}
//Connexion au réseau WiFi
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connexion a ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connexion WiFi etablie ");
Serial.print("=> Addresse IP : ");
Serial.print(WiFi.localIP());
}
//Reconnexion
void reconnect() {
//Boucle jusqu'à obtenur une reconnexion
while (!client.connected()) {
Serial.print("Connexion au serveur MQTT...");
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
Serial.println("OK");
} else {
Serial.print("KO, erreur : ");
Serial.print(client.state());
Serial.println(" On attend 5 secondes avant de recommencer");
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
button.tick();
long now = millis();
//Envoi d'un message par 20 sec
if (now - lastMsg > 1000 * 20) {
lastMsg = now;
//Lecture de l'humidité ambiante
float h = dht.readHumidity();
// Lecture de la température en Celcius
float t = dht.readTemperature();
//Inutile d'aller plus loin si le capteur ne renvoi rien
if ( isnan(t) || isnan(h)) {
Serial.println("Echec de lecture ! Verifiez votre capteur DHT");
return;
}
if ( debug ) {
Serial.print(millis() / 1000);
Serial.print("Temperature : ");
Serial.print(t);
Serial.print(" | Humidite : ");
Serial.println(h);
String information_str = String ("{\"idx\":2,\"svalue\":\"") + String(t).c_str() + ";" + String(h).c_str() + ";3\"}";
information_str.toCharArray(information_char, information_str.length() + 1);
client.publish(domoticz_topic, information_char); // on envoie l'info au serveur mqtt format domoticz
tft.fillRect(60 , 12, 100, 40, couleurFond[1]);
tft.setTextColor(couleurFond[0]);
tft.setCursor(0, 0);
tft.println(WiFi.localIP());
tft.print("temp:"); tft.println(String(t).c_str()); tft.print("hum: "); tft.println(h);
}
}
}
}