1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
// EMETTEUR uPesy ESP32 Wroom DevKit //
#include <SPIFFS.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <XPT2046_Touchscreen.h> // https : //github. com/PaulStoffregen/XPT2046_Touchscreen
#include <esp_now.h>
#include <WiFi.h>
TFT_eSPI tft = TFT_eSPI();
// Touchscreen pins
#define XPT2046_IRQ 36 // T_IRQ
#define XPT2046_MOSI 32 // T_DIN
#define XPT2046_MISO 39 // T_OUT
#define XPT2046_CLK 25 // T_CLK
#define XPT2046_CS 33 // T_CS
SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define FONT_SIZE 2
// definition des boutons
#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 60
#define BUTTON_MARGIN 10
// Structure pour représenter un bouton
struct Button {
int x, y;
bool state;
};
Button buttons[9]; // 9 boutons
// Coordonnées de l'écran tactile : (x, y) et pression (z)
int x, y, z;
// Imprimer les informations de l'écran tactile concernant X, Y et la pression (Z) sur le moniteur série
void printTouchToSerial(int touchX, int touchY, int touchZ) {
Serial.print("X = ");
Serial.print(touchX);
Serial.print(" | Y = ");
Serial.print(touchY);
Serial.print(" | Pressure = ");
Serial.print(touchZ);
Serial.println();
}
/******************************************************/
// REPLACE WITH YOUR RECEIVER MAC Address
//uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// *** Structure Fichier Config.txt ********************************
const size_t tailleMaxNom = 16; // y compris le caractère nul final
struct ClientPC {
char nom[tailleMaxNom]; // le nom du client PC
uint8_t adresse[6]; // les 6 octets de son adresse
};
const size_t nbMaxClients = 20; // nombre maximum de clients
ClientPC clients[nbMaxClients];
size_t nbClientsPC = 0; // le nombre de clients lu dans le fichier de config
//*******************************************************************
// ***** Structure du récepteur *************************************
typedef struct struct_message {
char a[32]; // ex PC1
bool b; // Etat = 0 ou 1
} struct_message;
// Créer une struct_message appelée myData
struct_message myData;
//........... Etat enoi ********************************************
esp_now_peer_info_t peerInfo;
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nÉtat de l'envoi du dernier paquet:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Succès" : "Échec");
}
//*********************************************************************
// Init ESP Now with fallback
void InitESPNow() {
WiFi.disconnect();
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
else {
Serial.println("ESPNow Init Failed");
// Retry InitESPNow, add a counte and then restart?
// InitESPNow();
// or Simply Restart
ESP.restart();
}
}
int touchX, touchY, touchZ , i;
//************************************************
// SETUP
//************************************************
void setup()
{
Serial.begin(115200);
if(!SPIFFS.begin(true)){
Serial.println("Une erreur s'est produite lors du montage de SPIFFS");
return;
}
File fichierConfig = SPIFFS.open("/config.txt", FILE_READ);
if (!fichierConfig) {
Serial.println("Impossible d'ouvrir le fichier en lecture");
return;
}
char bufferLigne[100]; // Buffer pour stocker chaque ligne
char format[64]; // pour bâtir dynamiquement le format en tenant compte de la longueur max du nom (reco de Kernighan et Pike dans "The Practice of Programming")
snprintf(format, sizeof format, "%%%ds %%hhx %%hhx %%hhx %%hhx %%hhx %%hhx", tailleMaxNom - 1); // lire une chaine de taille max tailleMaxNom - 1 suivie de 6 octets en hexa
nbClientsPC = 0;
while (fichierConfig.available() && nbClientsPC < nbMaxClients) { // tant qu'on peut lire quelque chose et qu'on a de la place pour stocker
memset(bufferLigne, '\0', sizeof bufferLigne); // Effacer le buffer
fichierConfig.readBytesUntil('\n', bufferLigne, sizeof bufferLigne); // Lire la ligne dans le buffer
memset(clients[nbClientsPC].nom, '\0', sizeof clients[nbClientsPC].nom); // on efface le nom pour être tranquille
int nbChampsLus = sscanf(bufferLigne, format,
clients[nbClientsPC].nom,
&clients[nbClientsPC].adresse[0], &clients[nbClientsPC].adresse[1],
&clients[nbClientsPC].adresse[2], &clients[nbClientsPC].adresse[3],
&clients[nbClientsPC].adresse[4], &clients[nbClientsPC].adresse[5]);
if (nbChampsLus == 7) {
// la lecture des 7 champs (le nom et 8 octets sous forme hexadécimale) s'est bien passée
nbClientsPC++;
} else {
// on arrête de lire là
break;
}
}
fichierConfig.close();
// Affichage des adresses lues depuis le fichier
for (size_t i = 0; i < nbClientsPC; i++) {
Serial.printf("%3zu %-*s : ", i+1, tailleMaxNom - 1, clients[i].nom); // l'index sur 3 caractères, le nom sur tailleMaxNom - 1 cadrée à gauche
for (int j = 0; j < 6; j++) Serial.printf("0x%02X ", clients[i].adresse[j]);
Serial.println();
}
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
InitESPNow();
if (esp_now_init() != ESP_OK) {
Serial.println("Erreur d'initialisation de l'ESP-NOW");
return;
}
// Une fois que ESPNow est Init avec succès, nous nous inscrirons pour l'envoi du CB .
// obtenir l'état du paquet transmis.
esp_now_register_send_cb(OnDataSent);
// Enregistrer un pair
//memcpy(peerInfo.peer_addr, broadcastAddress, 1);
//memcpy(peerInfo.peer_addr, broadcastAddress , 6);
//memcpy(peerInfo.peer_addr, clients[].adresse , 6);
/*
for (size_t i = 0; i < nbClientsPC; i++) {
memcpy(peerInfo.peer_addr,clients[i].adresse, 6);
}
*/
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Ajouter un pair
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Échec de l'ajout d'un pair");
return;
}
// Start the SPI for the touchscreen and init the touchscreen
touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
touchscreen.begin(touchscreenSPI);
// Set the Touchscreen rotation in landscape mode
// Note: in some displays, the touchscreen might be upside down, so you might need to set the rotation to 3: touchscreen.setRotation(3);
touchscreen.setRotation(3);
// Start the tft display
tft.init();
// Set the TFT display rotation in landscape mode
tft.setRotation(1);
// Clear the screen before writing to it
tft.fillScreen(TFT_BLACK);
// Initialisation des positions des boutons
int buttonX = 20;
int buttonY = 20;
for (int i = 0; i < 9; ++i)
{
buttons[i].x = buttonX;
buttons[i].y = buttonY;
buttons[i].state = false;
// Affichage des boutons
drawButton(buttonX, buttonY, buttons[i].state);
buttonX += BUTTON_WIDTH + BUTTON_MARGIN;
if (buttonX + BUTTON_WIDTH > tft.width()) {
buttonX = 20;
buttonY += BUTTON_HEIGHT + BUTTON_MARGIN;
}
}
}
//************************************************
// LOOP
//************************************************
void loop()
{
// Checks if Touchscreen was touched, and prints X, Y and Pressure (Z) info on the TFT display and Serial Monitor
if (touchscreen.tirqTouched() && touchscreen.touched())
{
// Get Touchscreen points
TS_Point p = touchscreen.getPoint();
// Calibrate Touchscreen points with map function to the correct width and height
touchX = map(p.x, 200, 3700, 1, SCREEN_WIDTH);
touchY = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
touchZ = p.z;
//printTouchToSerial(touchX, touchY, touchZ);
for (int i = 0; i < 9; ++i)
{
if (touchX >= buttons[i].x && touchX <= buttons[i].x + BUTTON_WIDTH &&
touchY >= buttons[i].y && touchY <= buttons[i].y + BUTTON_HEIGHT)
{
// Si un bouton est touché, inverse son état et met à jour l'affichage
buttons[i].state = !buttons[i].state;
drawButton(buttons[i].x, buttons[i].y, buttons[i].state);
if (buttons[i].state == 1){
Serial.print("Btn "); Serial.print(i+1); Serial.println(" enfoncé");
snprintf(myData.a, sizeof myData.a, "PC%02d=> ON", i+1);
myData.b = true;
memcpy(peerInfo.peer_addr,clients[i].adresse, 6);
esp_err_t result = esp_now_send(peerInfo.peer_addr,(uint8_t *)&myData, sizeof(myData));
if (result == ESP_OK) {
Serial.print("PC");Serial.print(i+1); Serial.println(" Envoyé avec succès");
}else {
Serial.print("Erreur d'envoi des données PC");Serial.print(i+1);
}
}
if (buttons[i].state == 0)
{
Serial.print("Btn "); Serial.print(i+1); Serial.println(" relaché");
snprintf(myData.a, sizeof myData.a, "PC%02d=> OFF", i+1);
myData.b = false;
memcpy(peerInfo.peer_addr,clients[i].adresse, 6);
esp_err_t result = esp_now_send(peerInfo.peer_addr,(uint8_t *)&myData, sizeof(myData));
if (result == ESP_OK) {
Serial.print("PC");Serial.print(i+1); Serial.println(" Envoyé avec succès");
}else {
Serial.print("Erreur d'envoi des données PC");Serial.print(i+1);
}
}
}
//break;
delay(200);
}
}
}
// Dessine un bouton à une position donnée avec un état donné
void drawButton(int x, int y, bool state) {
uint16_t color = state ? TFT_RED : TFT_GREEN;
tft.fillRect(x, y, BUTTON_WIDTH, BUTTON_HEIGHT, color);
tft.drawRect(x, y, BUTTON_WIDTH, BUTTON_HEIGHT, TFT_WHITE);
} |
Partager