Bonjour à tous ,je ne suis toujours pas programmeur mais je sais a peu prêt copier coller ! J'ai un programme de station météo sous arduino méga et un tft 2.4" . Tout fonctionne merveilleusement sauf que entre le moment ou j'appui sur l'écran et la réponse de celui ci le temps ,la température niveau etc ont eu le temps de changer!!! donc pas très efficace ! Je cherche une personne charitable pouvant m'expliquer à quel endroit j'ai merdé ou alors le matériel est trop limité pour ce que je demande?
Merci d'avance.
Le code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
//https://www.aranacorp.com/fr/utilisation-dun-shield-tft-lcd-avec-arduino/
//Je met un condensateur chimique de 1000µF et un  céramique de 100nF. Filtrage classique
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
#include <FreeDefaultFonts.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h> 
#include <math.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include "SPI.h"
#include <SFE_BMP180.h>
SFE_BMP180 pressure;
#define ALTITUDE 110.0 // Altitude of Robojax Headquarter (Ajax, Ontario, Canada)
 
#define DHTPIN 37    // what digital pin we're connected to
#define ONE_WIRE_BUS 35
SoftwareSerial nodemcu(18,19);//RX D19 et TX D18
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
#define MINPRESSURE 200
#define MAXPRESSURE 1000//
 
// Pin für Reed-Kontakt, Digital-2 für Interrupt 0
#define REEDPIN 19
// Hardware-Interrupt für den Reed-Pin
#define REEDINTERRUPT 0 // potard 
#define ledalarm 29              // LED ALARM sortie négatif (-)
#define resetmaxpin 33            // reset max Pin (button)
#define resetalarmpin 31          // reset Alarm Pin (button)
// Umfang in mm wurde im Windkanal mit dem Volkswindmesser ermittelt als Referenz
#define RADUMFANG 2200
 
 
//Capteur PH
const int analogInPin = A8; 
int sensorValue = 0; 
unsigned long int avgValue; 
//float b;
int buf[10],temp1=0;
String data;
String b;
  // On définit les variables
  float tmin,tmax,tnow,tlast;
  float rh; // relative humidity in %
  float t; //temperature in celsius
  float td; //dew point in celsius
  float sdata1 = 0; // temperature bassin
  float sdata2 = 0; // temperature estérieur
  int sdata3 = 0; // humiditée extérieur
  int sdata6 = 0; //humidité sol
String cdata; // complete data
char str[10];
  float pHVol=(float)avgValue*5.0/1024/4.3;
  float phValue = -5.70 * pHVol + 42.8;
 
// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
// ALL Touch panels and wiring is DIFFERENT
DHT dht(DHTPIN, DHTTYPE);
uint32_t runTime = -99999;       // time for next update
int reading1 = 0; // Value to be displayed
int d = 0; // Variable used for the sinewave test waveform
boolean alert = 0;
int8_t ramp = 1;
int tesmod =0;
float tempC = 0;
char TempCelciusFahrenheit[6];
 
 
 
double dewPoint(double celsius, double humidity){
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
double VP = pow(10, RHS - 3) * humidity;
double T = log(VP / 0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
//Ultrasonic Sensor
 
const int trigPin = 16;
const int echoPin = 17;
 
//Define Variables
long duration = 0;
long distance = 0;
long distanceOld = 0;
// copy-paste results from TouchScreen_Calibr_native.ino
//3.5 Calibration
const int XP = 8, XM = A2, YP = A3, YM = 9; //320x480 ID=0x9486
const int TS_LEFT = 151, TS_RT = 880, TS_TOP = 946, TS_BOT = 161;
 
//2.8 Calbiration
//const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x9341
//const int TS_LEFT=907,TS_RT=120,TS_TOP=74,TS_BOT=913;
 
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint p;
Adafruit_GFX_Button on_btn, off_btn, page1_btn, page2_btn, page3_btn, page4_btn;
Adafruit_GFX_Button ok_btn, cncl_btn, plus_btn, minus_btn;
Adafruit_GFX_Button menu_btn, info_btn, back_btn;
int pixel_x, pixel_y;     //Touch_getXY() updates global vars
// Button calibration
int margin = 5;
int btnWidth = 100;
int btnHeight = 40;
int btnY = 200;
// Software variable
bool enable_nuit = false;
int parameter = 50, old_parameter = 50;
 
#define BLACK   0x0000
#define GREY    0x5555
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define DARKGREEN   0x05C0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define GOLD    0xDDC0
#define WHITE   0xFFFF
// Color definitions
#define ILI9341_BLACK       0x0000      /*   0,   0,   0 */
#define ILI9341_NAVY        0x000F      /*   0,   0, 128 */
#define ILI9341_DARKGREEN   0x03E0      /*   0, 128,   0 */
#define ILI9341_DARKCYAN    0x03EF      /*   0, 128, 128 */
#define ILI9341_MAROON      0x7800      /* 128,   0,   0 */
#define ILI9341_PURPLE      0x780F      /* 128,   0, 128 */
#define ILI9341_OLIVE       0x7BE0      /* 128, 128,   0 */
#define ILI9341_LIGHTGREY   0xC618      /* 192, 192, 192 */
#define ILI9341_DARKGREY    0x7BEF      /* 128, 128, 128 */
#define ILI9341_BLUE        0x001F      /*   0,   0, 255 */
#define ILI9341_GREEN       0x07E0      /*   0, 255,   0 */
#define ILI9341_CYAN        0x07FF      /*   0, 255, 255 */
#define ILI9341_RED         0xF800      /* 255,   0,   0 */
#define ILI9341_MAGENTA     0xF81F      /* 255,   0, 255 */
#define ILI9341_YELLOW      0xFFE0      /* 255, 255,   0 */
#define ILI9341_WHITE       0xFFFF      /* 255, 255, 255 */
#define ILI9341_ORANGE      0xFD20      /* 255, 165,   0 */
#define ILI9341_GREENYELLOW 0xAFE5      /* 173, 255,  47 */
#define ILI9341_PINK        0xF81F
enum pageId {
 MENU,
 TEMPERATURES,
 ANEMOMETRE,
 PRESSION,
 PISCINE,
 INFO
};
unsigned int currentPage = MENU, oldPage = -1;
 
void setup(void)
{
 Serial.begin(9600);
   nodemcu.begin(9600);  
    if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.
 
    Serial.println("BMP180 init fail");
 
 
    while(1); // Pause forever.
  }
  pinMode(trigPin, OUTPUT); //sets the trigPin as an Output
  pinMode(echoPin, INPUT);  //sets the echoPin as an Input
     dht.begin();
  // on initialise la librairie Dallas
    sensors.begin();
  // on initialise la température max et min du DS18B20.
  tmin = 125;
  tmax = -55;
  tlast = 0;
 
  //******************* capteur effet hall***************************
pinMode(REEDPIN, INPUT_PULLUP); // Contact Reed connecté directement et sans résistance 
  attachInterrupt(digitalPinToInterrupt(REEDPIN), reedISR, FALLING);
  pinMode(resetmaxpin, INPUT_PULLUP); // Touche Resetmax connectée directement et 
  //sans résistance (contre ground)
  pinMode(resetalarmpin, INPUT_PULLUP); // Touche de réinitialisation directement et
  //sans résistance (contre grand)
 //******************* Fin capteur effet hall***************************
 
 
 //init TFTTouch
 uint16_t ID = tft.readID();
 Serial.print("TFT ID = 0x");
 Serial.println(ID, HEX);
 Serial.println(F("Calibrate for your Touch Panel"));
 if (ID == 0xD3D3) ID = 0x9488;  //for 3.5" TFT LCD Shield , 0x9341 for 2.8" TFT LCD Shield
 tft.begin(ID);
 tft.setRotation(1); //0-PORTRAIT 1-PAYSAGE 2-REVERSE PORTRAIT 3-REVERSE PAYSAGE
 //tft.setFont(&FreeSmallFont);
 
 currentPage = MENU; // Indicates that we are at Home Screen
 Serial.println("Home Page");
}
//*********************ANEMOMETRE***************************
 
float ms2; //globale Variable Speed in m/s
float msmax = 0;  //Maximalwert
unsigned long umin; // U/min
 
int alarm; // Limite d’alarme en m/s (m/s)
 
 
volatile byte reedCountSum;
volatile long reedMillisSum;
 
unsigned long lastReedMillis;
 
void reedISR()
{
  if (millis()-lastReedMillis>=5)   // 25 ms correspond au maximum à 40 tours par seconde (pour les anémomètres populaires, le temps de rebondissement est suffisant)
  {
    reedCountSum++;                 // eine Radumdrehung zählen
    reedMillisSum+=millis()-lastReedMillis;   // Zeit addieren
    lastReedMillis=millis();       // Zeit merken
  }
}
 
unsigned long gesamtUmdrehungen = 0; //4545300;
 
void tachoAnzeige()
{
  byte umdrehungen;
  unsigned long zeit;
  float kph, kilometer, ms;
  char buffer[10];
  noInterrupts();            // Interrupts sperren
    umdrehungen=reedCountSum;// Zählvariable umkopieren
    reedCountSum=0;          // Zählvariable auf 0 zurücksetzen
    zeit=reedMillisSum;      // Zeitzähler umkopieren
    reedMillisSum=0;         // Zeitzähler auf 0 zurücksetzen
  interrupts();              // Interrupts wieder zulassen
    gesamtUmdrehungen+= umdrehungen; // Aufsummieren aller Radumdrehungen
    kilometer=(float)gesamtUmdrehungen*(float)RADUMFANG/1000000.0; // Kilometerzähler 
 
  if (umdrehungen>0){
    kph=float(RADUMFANG)*(float)umdrehungen/(float)zeit*3.6;  
    ms=float(RADUMFANG)*(float)umdrehungen/(float)zeit;  
    umin=umdrehungen*60000/zeit;  }
  else {
    kph=0.0;      //Définir zéro si aucun tour n’a été trouvé
    umin = 0; }   //Définir zéro si aucun tour n’a été trouvé
 
 
    // ms = kph/3.6; //aus km/h mach m/s - kmh/3.6
    ms2 = ms; //variable ms in ms2 global kopieren
 
    //tft Ausgabe -- Kilometerzähler hier deaktiviert
    //tft.setCursor(0, 2);
    //dtostrf(kilometer,8,3,buffer);
    //tft.print(buffer);
    //Serial.println(buffer);
    //tft.print(" km MAX:");
    //dtostrf(msmax,4,1,buffer);
    //tft.print(buffer);
    // Serial.println(buffer);
    //maximalwert speichern
    if (ms2 > msmax) {
    msmax = ms2;   }
   // Serial.println(msmax);
 
    //umin 0 setzen wenn ms2 0 ist.
   // if (ms2 == 0.0) {
   // umin = 0;  }
 
   //Serial.println(gesamtUmdrehungen);
 
 
  }
 
unsigned long interval;           // update tft/LED interval (milliseconds)
unsigned long previousMillis = 0; 
unsigned long intervalalarm = 200;           // update Alarm Anzeige interval (milliseconds)
unsigned long previousMillisalarm = 0; 
 
unsigned long currentMillis;
 
int potpin = 10;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
bool down ;
 
void loop(void) {
   char status;
  double T,P,p0,a;
 
  // Loop here getting pressure readings every 10 seconds.
 
  // If you want sea-level-compensated pressure, as used in weather reports,
  // you will need to know the altitude at which your measurements are taken.
  // We're using a constant called ALTITUDE in this sketch:
 
  Serial.println();
  Serial.print("provided altitude: ");
  Serial.print(ALTITUDE,0);
  Serial.print(" meters, ");
  Serial.print(ALTITUDE*3.28084,0);
  Serial.println(" feet");
 
  // If you want to measure altitude, and not pressure, you will instead need
  // to provide a known baseline pressure. This is shown at the end of the sketch.
 
  // You must first get a temperature measurement to perform a pressure reading.
 
  // Start a temperature measurement:
  // If request is successful, the number of ms to wait is returned.
  // If request is unsuccessful, 0 is returned.
 
  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);
 
    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Function returns 1 if successful, 0 if failure.
 
    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Print out the measurement:
      Serial.print("temperature: ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      Serial.print((9.0/5.0)*T+32.0,2);
      Serial.println(" deg F");
 
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.
 
      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);
 
        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.
 
        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          // Print out the measurement:
          Serial.print("absolute pressure: ");
          Serial.print(P,2);
          Serial.print(" mb, ");
          Serial.print(P*0.0295333727,2);
          Serial.println(" inHg");
 
          // The pressure sensor returns abolute pressure, which varies with altitude.
          // To remove the effects of altitude, use the sealevel function and your current altitude.
          // This number is commonly used in weather reports.
          // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
          // Result: p0 = sea-level compensated pressure in mb
 
          p0 = pressure.sealevel(P,ALTITUDE); // we're at 90 meters (Boulder, CO)
          Serial.print("relative (sea-level) pressure: ");
          Serial.print(p0,2);
          Serial.print(" mb, ");
          Serial.print(p0*0.0295333727,2);
          Serial.println(" inHg");
 
          // On the other hand, if you want to determine your altitude from the pressure reading,
          // use the altitude function along with a baseline pressure (sea-level or other).
          // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb.
          // Result: a = altitude in m.
 
          a = pressure.altitude(P,p0);
          Serial.print("computed altitude: ");
          Serial.print(a,0);
          Serial.print(" meters, ");
          Serial.print(a*3.28084,0);
          Serial.println(" feet");
        }
        else Serial.println("error retrieving pressure measurement");
      }
      else Serial.println("error starting pressure measurement");
    }
    else Serial.println("error retrieving temperature measurement");
  }
  else Serial.println("error starting temperature measurement");
  delay(5000);  // Pause for 5 seconds.
 
 
  //******************* ANEMOMETRE***************************
  // Anzeige wird nach dem eingestellten inteval aktualisiert
  currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    tachoAnzeige();
    previousMillis = currentMillis;
  }
 
  // Alaramzeige wird nach dem eingestellten interval aktualisiert
  if (currentMillis - previousMillisalarm >= intervalalarm)
    //************* affichage de l'alarme*********************
  {
    val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
    alarm = map(val, 0, 1023, 0, 200);     // 200Km/h max min changer en foction des besoings
 
 
//************* Fin affichage de l'alarme*********************
   previousMillisalarm = currentMillis;
  }
 
 
 
    //Suppression des 10 premières rotations ou réinitialisation du bouton pour rétablir la valeur maximale
    if (gesamtUmdrehungen < 10 || digitalRead(resetmaxpin) == LOW ) {  
    msmax = 0;  }
 
    //Réinitialiser les rotations globales avant que le présentoir ne déborde
    if (gesamtUmdrehungen > 4545430) {
    gesamtUmdrehungen = 0;  }
//*************** Fin affichage remise a zero *************************************  
//******************* Fin ANEMOMETRE***************************
 
    //Capteur PH
for(int i=0;i<10;i++) 
 { 
  buf[i]=analogRead(analogInPin);
  delay(10);
 }
 for(int i=0;i<9;i++)
 {
  for(int j=i+1;j<10;j++)
  {
   if(buf[i]>buf[j])
   {
    t=buf[i];
    buf[i]=buf[j];
    buf[j]=t;
   }
  }
 }
 avgValue=0;
 for(int i=2;i<8;i++)
 avgValue+=buf[i];
 
    digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034/2;
 
  if(distance!=distanceOld){
    getDistance(distanceOld, BLACK);
    getDistance(distance, WHITE);
  }
 
  distanceOld = distance;
  delay(1000);
   float h = dht.readHumidity();
  float t = dht.readTemperature();
  rh = dht.readHumidity();
  t = dht.readTemperature();
  sdata2 = t ;
  sdata3 = h ;
  sensors.requestTemperatures();
//  float temp = sensors.getTempCByIndex(0);
 // tempC = sensors.getTempCByIndex(0);
//  sdata1 = temp;
 
  tnow = sensors.getTempCByIndex(0);
  if(tnow!=tlast) {
    tlast=tnow;
    if(tnow<tmin)
      tmin=tnow;
    if(tnow>tmax)
      tmax=tnow;} 
 switch (currentPage) {
   case MENU: //Menu page
     if (currentPage != oldPage)   drawMenuScreen();
     page1_btn.press(down && page1_btn.contains(pixel_x, pixel_y));
     page2_btn.press(down && page2_btn.contains(pixel_x, pixel_y));
     page3_btn.press(down && page3_btn.contains(pixel_x, pixel_y));
     page4_btn.press(down && page4_btn.contains(pixel_x, pixel_y));
 
     if (page1_btn.justReleased())
       page1_btn.drawButton();
     if (page2_btn.justReleased())
       page2_btn.drawButton();
     if (page3_btn.justReleased())
       page3_btn.drawButton();
     if (page4_btn.justReleased())
       page4_btn.drawButton(); 
 
     if (page1_btn.justPressed()) {
       page1_btn.drawButton(true);
       currentPage = TEMPERATURES;
 
     }
     if (page2_btn.justPressed()) {
       page2_btn.drawButton(true);
       currentPage = ANEMOMETRE;
     }
     if (page3_btn.justPressed()) {
       page3_btn.drawButton(true);
       currentPage = PRESSION;
     }
 
     if (page4_btn.justPressed()) {
       page4_btn.drawButton(true);
       currentPage = PISCINE;
     }
     break;
   case TEMPERATURES:// Menu TEMPERATURES
     if (currentPage != oldPage)   drawSensorScreen();
    tft.setCursor (0,20);
    tft.setTextSize (3);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("PISCINE ");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (7,80);
    tft.setTextSize (4);
     tft.setTextColor (YELLOW,BLACK);
    tft.print (tnow);
    tft.print ("C");
 
    tft.setCursor (150,20);
    tft.setTextSize (3);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("EXTERIEUR ");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (170,80);
    tft.setTextSize (4);
    tft.print (t);
    tft.print ("C");
 
    tft.setCursor (0,140);
    tft.setTextSize (3);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("HUMIDITE ");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (7,180);
    tft.setTextSize (4);
    tft.setTextColor (CYAN,BLACK);
    tft.print (h);
    tft.print ("%");
 
    tft.setCursor (150,140);
    tft.setTextSize (3);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("  VENT");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (170,180);
    tft.setTextSize (3);
    tft.setTextColor (CYAN,BLACK);
    //tft.print (h);
    tft.print ("km/h");
 
     if(tnow>28.5){
  tft.setCursor(7, 80);
  tft.setTextSize(4);
  tft.setTextColor(RED,BLACK);
  tft.print(tnow);tft.print ("C");}
  if(tnow<28.5){
 
  }
       if(t>32){
  tft.setCursor(170,80);
  tft.setTextSize(4);
  tft.setTextColor(RED,BLACK);
  tft.print(t);tft.print ("C");}
  if(t<32){
 
  }
 
     updateTemp();
     menu_btn.press(down && menu_btn.contains(pixel_x, pixel_y));
     info_btn.press(down && info_btn.contains(pixel_x, pixel_y));
     if (menu_btn.justReleased())
       menu_btn.drawButton();
     if (info_btn.justReleased())
       info_btn.drawButton();
     if (menu_btn.justPressed()) {
       menu_btn.drawButton(true);
       currentPage = MENU;
     }
       if (info_btn.justPressed()) {
       info_btn.drawButton(true);
       currentPage = INFO;
     }
     break;
   case ANEMOMETRE:// Menu ANEMOMETRE
      if (currentPage != oldPage)   drawSensorScreen();
    tft.setCursor (10,20);
    tft.setTextSize (2);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("ALARME VENT ");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (7,50);
    tft.setTextSize (4);
    tft.print (alarm);
    tft.print ("km/h");
    tft.setCursor(200, 20);
    /*if(alarm < 10) tft.print("0"); 
    tft.print(alarm);*/
 
     //Alarm LED einschalten wenn Wind > Alarmwert
    if (ms2 > alarm) {
    digitalWrite(ledalarm, HIGH);
    delay (1000);
    digitalWrite (ledalarm,LOW);
    while (ms2 < alarm);
    tft.setTextSize (4);
    tft.setTextColor(RED);
    tft.setCursor(285, 10);
    tft.print("A");
    tft.setCursor(285, 50);
    tft.print("L "); 
    tft.setCursor(285, 90);
    tft.print("A"); 
    tft.setCursor(285, 130);
    tft.print("R"); 
    tft.setCursor(285, 170);
    tft.print("M");    
    }
 
    //***************affichage remise a zero *************************************
 
    // Réinitialiser l’alarme LED si le vent est > 5 ms ou si le bouton de réinitialisation est enfoncé
    // if (ms2 < 2 && digitalRead(ledalarm) == HIGH || digitalRead(resetmaxpin) == LOW)
    tft.setTextColor(CYAN);
    if (digitalRead(resetalarmpin) == LOW) {
    digitalWrite(ledalarm, LOW);
    tft.setCursor(155, 20);
    tft.setTextSize(2);
    tft.print ("remise a 0 ");
   /* tft.setCursor(200, 60);
    tft.setTextSize(3);
    tft.print (" a 0");*/
    }
    float kph, kilometer, ms;
    tft.setCursor (10,95);
    tft.setTextSize (2);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("VENT MAX ");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (7,130);
    tft.setTextSize (4);
    tft.print (msmax);
    tft.print ("km/h");
 
    tft.setCursor (10,175);
    tft.setTextSize (2);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("VENT ACTUEL ");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (7,200);
    tft.setTextSize (4);
    tft.print (ms2);
    tft.print ("km/h");
 
     menu_btn.press(down && menu_btn.contains(pixel_x, pixel_y));
     info_btn.press(down && info_btn.contains(pixel_x, pixel_y));
     if (menu_btn.justReleased())
       menu_btn.drawButton();
     if (info_btn.justReleased())
       info_btn.drawButton();
     if (menu_btn.justPressed()) {
       menu_btn.drawButton(true);
       currentPage = MENU;
     }
 
     break;
   case PRESSION: // Menu PRESSION
          if (currentPage != oldPage)   drawSensorScreen();
 
    tft.setCursor (10,20);
    tft.setTextSize (2);
    tft.setTextColor (WHITE,BLACK);
    tft.print (" PRESSION ATMOPH");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (7,80);
    tft.setTextSize (4);
    tft.print (p0,2);
    tft.print ("hpa");
 
    tft.setCursor (10,175);
    tft.setTextSize (2);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("ALTITUDE");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (7,200);
    tft.setTextSize (4);
    tft.print (a,0);
    tft.print ("metres");
 
     menu_btn.press(down && menu_btn.contains(pixel_x, pixel_y));
     info_btn.press(down && info_btn.contains(pixel_x, pixel_y));
     if (menu_btn.justReleased())
       menu_btn.drawButton();
     if (info_btn.justReleased())
       info_btn.drawButton();
     if (menu_btn.justPressed()) {
       menu_btn.drawButton(true);
       currentPage = MENU;
     }
 
     break;
     case PISCINE:// Menu PISCINE
     if (currentPage != oldPage)   drawSensorScreen();
    tft.setCursor (10,20);
    tft.setTextSize (2);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("  NIVEAU ");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (7,80);
    tft.setTextSize (4);
    tft.print (distance);
    tft.print ("Cm");
 
    tft.setCursor (180,20);
    tft.setTextSize (2);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("  PH ");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (170,80);
    tft.setTextSize (4);
    tft.print (phValue);
    tft.print ("");
 
    tft.setCursor (10,140);
    tft.setTextSize (2);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("  DST ");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (7,180);
    tft.setTextSize (4);
    tft.print (h);
    tft.print ("ppm");
 
    tft.setCursor (200,140);
    tft.setTextSize (2);
    tft.setTextColor (WHITE,BLACK);
    tft.print ("  EC ");
    tft.setTextColor (GREEN,BLACK);
    tft.setCursor (185,160);
    tft.setTextSize (4);
    tft.print (h);
    tft.print ("");
 
     menu_btn.press(down && menu_btn.contains(pixel_x, pixel_y));
     info_btn.press(down && info_btn.contains(pixel_x, pixel_y));
     if (menu_btn.justReleased())
       menu_btn.drawButton();
     if (info_btn.justReleased())
       info_btn.drawButton();
     if (menu_btn.justPressed()) {
       menu_btn.drawButton(true);
       currentPage = MENU;
     }
 
     break;
 }
 if (oldPage == currentPage){
   down = Touch_getXY();
 }else{
   down=false;
 }
}
void getDistance(long distance, int color){
 
float pHVol=(float)avgValue*5.0/1024/4.3;
 float phValue = -5.70 * pHVol + 42.8;
 phValue=14.2-phValue;
 
  int x;
  if(distance < 10 ) x=41;
  else if((distance < 100) || (distance >= 1000)) x=28;
  else x=19;
/*
  tft.setCursor (210,184);
  tft.setTextColor(CYAN,BLACK);
  tft.setTextSize(3);
  if(distance < 1000) 
  {
  tft.print(distance);
  tft.print("cm");
   
  }
  else {
  tft.print(" ");
  }*/
    /*tft.setCursor (210,215);
  tft.setTextColor(CYAN,BLACK);
  tft.setTextSize(3);
  tft.print(phValue);*/
  Serial.println(nodemcu.available());
 
if(nodemcu.available() == 0 )
{ 
 
  cdata = cdata + sdata1 + "," + sdata2 + "," + sdata3  + "," + distance + "," + phValue  ; 
 
Serial.println("On affiche cdata: ");
   Serial.println(cdata); 
   nodemcu.println(cdata);
   delay(1000); // 100 milli seconds
   cdata = ""; 
}
if ( nodemcu.available() > 0 ) 
{
  data = nodemcu.parseInt();
  delay(100); 
  Serial.println(data); 
 
}
// Délai de 2 secondes entre chaque mesure. La lecture prend 250 millisecondes
  delay(2000);
 
 }
/************************************************************************************
    SCREENS DEFINTION
 ************************************************************************************/
//Menu d'accueil
 
void drawMenuScreen() {
 tft.fillScreen(BLACK);
 tft.setTextSize(2);
 // Title
 tft.setTextColor(WHITE, BLACK);
 tft.setCursor(60, 10);
 tft.print("GESTION MAISON"); // Prints the string on the screen
 tft.drawLine(60, 32, tft.width() * 0.7, 32, CYAN); // Draws the red line
 tft.setTextColor(WHITE, BLACK);//((255, 255, 255), (0,0,0));
 tft.setCursor(0, 80);
 tft.setTextColor(GREEN, BLACK);//((255, 255, 255), (0,0,0));
 // Button
 page1_btn.initButton(&tft, tft.width() / 2., tft.height() / 2. - (1.*btnHeight + margin), 2.5 * btnWidth, btnHeight, WHITE, GREEN, BLACK, "TEMPERATURES", 2);
 page2_btn.initButton(&tft, tft.width() / 2., tft.height() / 2., 2.5 * btnWidth, btnHeight, WHITE, GREEN, BLACK, "ANEMOMETRE", 2);
 page3_btn.initButton(&tft, tft.width() / 2., tft.height() / 2. + (1.*btnHeight + margin), 2.5 * btnWidth, btnHeight, WHITE, GREEN, BLACK, "PRESSION", 2);
 page4_btn.initButton(&tft, tft.width() / 2., tft.height() / 2. + (2.1*btnHeight + margin), 2.5 * btnWidth, btnHeight, WHITE, GREEN, BLACK, "PISCINE", 2);
 
 page1_btn.drawButton(false);
 page2_btn.drawButton(false);
 page3_btn.drawButton(false);
 page4_btn.drawButton(false);
 //Button frame
 tft.drawRoundRect(tft.width() / 2. - 1.5 * btnWidth, tft.height() / 2. - (1.5 * btnHeight + 2 * margin), 2 * btnWidth + btnWidth, 4.2 * btnHeight + 4 * margin, 10, GREEN);
 oldPage = currentPage;
}
/************************************************ Affichage temperatures *******************************************************/
//Menu bouton "menu"
void drawSensorScreen() {
 tft.fillScreen(BLACK);
 //tft.drawLine(tft.width() / 2., 0, tft.width() / 2., tft.height(), WHITE);//ligne blanche verticale
 //tft.drawLine(0, tft.height() / 2., tft.width(), tft.height() / 2., WHITE);// ligne blanche horizontale
 //updateTemp();
 tft.setTextSize(1);
 // bouton centré menu et info X,Y
 menu_btn.initButton(&tft,  tft.width() / 2. - btnWidth - margin , tft.height() - btnHeight / 2., btnWidth, btnHeight, BLACK, BLACK, BLACK, "", 2);
 info_btn.initButton(&tft, tft.width() / 2. + btnWidth + margin, tft.height() - btnHeight / 2., btnWidth, btnHeight, RED, GREY, BLACK, "MENU", 2);// bouton menu
 menu_btn.drawButton(false);
 info_btn.drawButton(false);
 oldPage = currentPage;
}
void updateTemp() {
 //temp1
 
 /*drawTextInRect(3 * tft.width() / 4., tft.height() / 4., temp1, 5, RED, 255);
 drawTextInRect(tft.width() / 4., 3 * tft.height() / 4., temp2, 5, RED, 255);
 drawTextInRect(3 * tft.width() / 4., 3 * tft.height() / 4., temp3, 5, RED, 255);*/
}
/************************************************ FIN Affichage temperatures *******************************************************/
 
/************************************************  Affichage Anemometre *******************************************************/
void drawTemperaturesScreen() {
 tft.setRotation(1);            
 tft.setTextSize(1);
 tft.fillScreen(BLACK);
 //back_btn.initButton(&tft,  200 , 20, btnWidth, btnHeight, BLACK, BLACK, WHITE, "<- Back", 2);
 
 tft.drawLine(tft.width() / 2., 0, tft.width() / 2., tft.height(), WHITE);//ligne blanche verticale
 tft.drawLine(0, tft.height() / 2., tft.width(), tft.height() / 2., WHITE);// ligne blanche horizontale
 
 
 oldPage = currentPage; 
 
}
/************************************************ FIN Affichage Anemometre *******************************************************/
void drawPiscineScreen() {
 tft.setRotation(1);            
 tft.setTextSize(1);
 tft.fillScreen(BLACK);
// back_btn.initButton(&tft,  200 , 20, btnWidth, btnHeight, BLACK, BLACK, WHITE, "<- Back", 2);
 
 //tft.drawLine(tft.width() / 2., 0, tft.width() / 2., tft.height(), WHITE);//ligne blanche verticale
 //tft.drawLine(0, tft.height() / 2., tft.width(), tft.height() / 2., WHITE);// ligne blanche horizontale
 
 
 oldPage = currentPage; 
 
}
 
 
/************************************************************************************
    UTILITY FUNCTION
*************************************************************************************/
void drawTextInRect(int x, int y, long temp, int tsize, unsigned int fColor, unsigned int bColor) {
 int marg = 10;
 char buf[12];
 int nbChar = strlen(itoa(temp, buf, 10)) + 2;
 if (bColor != 255) tft.fillRect(x - nbChar * 3 * tsize - marg, y - nbChar * 1 * tsize - marg, nbChar * 6 * tsize + 2 * marg, nbChar * 2 * tsize + 2 * marg, bColor);
 tft.setTextSize(tsize);
 tft.setTextColor(fColor, BLACK);
 //tft.setCursor(x-strlen(*text)*3*tsize+marg, y+rheight/2.+marg);
 tft.setCursor(x - nbChar * 3 * tsize, y - nbChar * 1 * tsize);
 tft.print(temp);
 //while(*text) tft.print(*text++);
 tft.write(0xF7);
 tft.print("C");
}
bool Touch_getXY(void)
{
 p = ts.getPoint();
 pinMode(YP, OUTPUT);      //restore shared pins
 pinMode(XM, OUTPUT);
 digitalWrite(YP, HIGH);
 digitalWrite(XM, HIGH);
 bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
 if (pressed) {
   if (tft.width() <= tft.height()) { //Portrait
     pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
     pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
   } else {
     pixel_x = map(p.y, TS_TOP, TS_BOT, 0, tft.width());
     pixel_y = map(p.x, TS_RT, TS_LEFT, 0, tft.height());
   }
 }
 return pressed;
}