Bonjour et merci. En fait je suis un petit bidouilleur du dimanche c'est très sale car reprise d'un de mes anciens scripts avec capteurs, et j'ai des grosses lacunes.
C'est pour un ESP32, une mesure de niveau de décharge d'une batterie solaire sur balcon parisien, et je me suis risqué essayer le multithreading, sans doute à tort vu mon niveau. Dans le passage qui me pose problème je veux simplement faire une moyenne sur plusieurs mesures, rien de plus banal, mais ma variable 'tour' ne s'incrémentait pas, j'ai alors compris que le test ==3 répondait toujours TRUE...
Je poste l'ensemble pour ceux qui auraient le courage. Je m'excuse pour le fouillis, j'ai essayé de développer en 'modulaire' mais manifestement cela ne me réussit pas 
/***********************************************************************************
ESP32 serveur wifi ethernet et DHT22 pour tester l'humidité et la température
Surtout un différentiel intérieur/extérieur pour gestion de canicule
================================================================================
22 09 16 REPRISE pour CAN > ici récepteur >
1 = supprimer ESPNOW
**********************************************************************************/
#include "B_DECLARATIONS.h"
#include "C_SETUP_A.h" // pinmodes, etc.
#include "D_SETUP_SERVER.h"
#include "E_SETUP_MULTITHREAD.h"
#include "functions.h"
int tour=4; // moyenne de mesure sur n tours:
int battery_can_sum;
int CanAverageGlobal;
int ALARM_LEVEL = 3300;
float approxMapR=0;
float MapR;
// =========== Task1code COEUR 0 : SERVEUR < ============================ <
void Task1code( void * pvParameters ) {
for (;;) {
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
WiFiClient client = server.available(); // listen for incoming clients
// SERVEUR
//---------- SERVEUR ECOUTE ----------------------------
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.println("<head>");
client.println("<meta http-equiv=\"refresh\" content=\"20\">");
client.println("<title>""</title>");
client.println("<style>");
client.println("body {");
client.println("background-color: #ebcc76;");
client.println("}");
client.println("</style>");
client.println("</head>");
// the content of the HTTP response follows the header:
client.print("<font size=");
client.print("7");
client.print("<br>");
client.print("Carte ");
client.println(carte);
client.print(" ");
client.print("<a href=\"http://82.64.37.196:1902\">22</a>");
client.print(" ");
client.println("<a href=\"http://82.64.37.196:1900\">20</a>");
client.print("<br>");
client.print("<br>");
client.print(NOW); // (= bufferA)
client.print("<br>");
client.print("<br>");
}
// =======>
client.print("<p style=\"color:black;font-size:28px;\">");
client.print("Alarm level: ");
client.println(ALARM_LEVEL);
client.print("</p>");
client.print("CAN battery: ");
//client.print(" ");
//client.println(battery_can);
client.print(CanAverageGlobal);
client.print(" ");
client.println(" /4095");
client.print("<br>");
client.println(approxMapR); // XXXXXXXXXXXXXX
client.print("<br>");
client.print("MAP : ");
//client.print(" ");
client.println(MapR); // XXXXXXXXXXXXXX
client.println(" V");
alarmIf( CanAverageGlobal, ALARM_LEVEL);
client.print("<br><br>");
//client.print(" ");
client.print("SPIFFS essai - random float: ");
client.println(myVariable);
client.print("<br>");
client.print("<br>");
client.print("<a href=\"/H\">5 ON</a>");
client.print(" ");
client.print("<a href=\"/L\">5 OFF</a><br>");
client.print("<br>");
client.print("<a href=\"/3100\">3100</a>");
client.print(" ");
client.print("<a href=\"/3300\">3300</a><br>");
client.print("<br>");
//client.print(" ");
client.print("RSSI: ");
rssi = WiFi.RSSI();
client.println(rssi);
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(5, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(5, LOW); // GET /L turns the LED off
}
if (currentLine.endsWith("GET /3100")) {
Serial.println("");
Serial.println ("3100 demandé"); // setup niveau d'alarme
ALARM_LEVEL = 3100;
//delay(2000);
}
if (currentLine.endsWith("GET /3300")) {
Serial.println("");
Serial.println ("3300 demandé");
ALARM_LEVEL = 3300;
//delay(2000);
}
} // client avaible end >
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
delay(SERVER_SLEEP);
}
}
//======== task1 - SERVEUR END ==== >
// ====================================== TASK 2 COEUR 1 =========================================== <
void Task2code( void * pvParameters ) {
// pour moyenne des mesures CAN volts
// int tour=1; // moyenne de mesure sur n tours:
// int battery_can_sum=0;
// int average_can=0; // made global sister 'int Average' for sum to print
for (;;) {
Serial.println("===================================================================== ");
Serial.print("Task2 running on core ");
Serial.println(xPortGetCoreID());
Serial.println("===================================================================== ");
CanAverageGlobal=BatteryAlarm();
// approxMapR=approxMap(CanAverageGlobal);
Serial.println ("MAAAAAAAAAAAAAAAAAAP XXXXXXXXXXXXX XXXXXXXXXX XXXXXXXXX");
// XXXX ESSAI MAP
MapR=CanMap(CanAverageGlobal);
Serial.println(MapR);
delay (TASK2_SLEEP);
}
}
void loop () { // NTP TIME ================<
time_t timestamp = time( NULL );
char buffer[MAX_SIZE];
struct tm *pTime = localtime(×tamp );
strftime(buffer, MAX_SIZE, "%d/%m/%Y %H:%M:%S", pTime);
Serial.println(buffer);
bufferA=buffer; //CH fait n'importe comment
NOW=bufferA; // je garde bufferA pour me rappeler de voir ce que c'est le 'buffer'
// supprimer après cette variable sans significationclaire dans le programme
delay(1000);
// ========>
}
/***********************************************************************************
ESP32 serveur wifi ethernet et DHT22 pour tester l'humidité et la température
Surtout un différentiel intérieur/extérieur pour gestion de canicule
================================================================================
**********************************************************************************/
#include "functions.h"
const int BATTERY_MINI = 3000; // voir
const int BATTERY_MINI_20 = 3100;
const int SERVER_SLEEP = 3000; // sur coeur 0
const int TASK2_SLEEP = 3000; // sur coeur 1
//============= NTP TIME==============
#define MAX_SIZE 80
const char* ntpServer = "pool.ntp.org";
char* bufferA; //CH fait n'importe comment
char* NOW = "";
//====================================
// ETALONNAGES /CONVERSIONS selon carte ========================
int carte = 0; //CH repérer quelle carte pour éventuel étalonnage (caduque >> adresse mac)
String mac= "";
float myVariable; // XXX
//------
int rssi = 0;
// CAN BATTERIE =====
int battery_can=0; // mesure batterie quand alim autonome
//------->
//------------ WIFI & ESPNOW ----------- <
#include <WiFi.h>
//ESP XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <esp_now.h>
uint8_t broadcastAddress[] = {0xEC,0x62,0x60,0x84,0xF9,0xA0}; //adresse mac receiver
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
float a;
float b;
float c;
float d;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.println("TEST_String: ########################### ");
Serial.print(" ####################################### ");
Serial.println(myData.a);
Serial.print("FLOAT B FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF: ");
Serial.println(myData.b);
Serial.print("Float: ");
Serial.println(myData.c);
Serial.print("Bool: ");
Serial.println(myData.d);
Serial.println();
}
//multitâche-------------
TaskHandle_t Task1;
TaskHandle_t Task2;
// DHT:------------------
//#include <SimpleDHT.h>
//int pinDHT22 = 4;
//SimpleDHT22 dht22(pinDHT22);
//--------
const char* ssid = "xxxx";
const char* password = "xxxxxx";
WiFiServer server(80);
void setup() { // ============================================================ SETUP <
Serial.begin(115200);
spiffs_setup();
Serial.println(__FILE__); // get sketch name and print it
// pinMode(5, OUTPUT); //led rouge (différentiel>+0.5 °C)
// pinMode(2, OUTPUT); //led jaune (différentiel>-0.5 °C)
pinMode(16,OUTPUT); // buzzer
// pinMode(17, OUTPUT); //led verte(différentiel>-4 °C)
pinMode(34, INPUT);//CAN batterie
digitalWrite(17, HIGH);
delay(2000);
digitalWrite(17, LOW);
digitalWrite(2, LOW);
digitalWrite(5, LOW);
digitalWrite(16, LOW);
delay(10);
Serial.println("");
buzzA(100);
//======= SERVEUR SETUP
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("ESP Board MAC Address: ");
mac = WiFi.macAddress();
// ============== NTP TIME ==================
configTzTime("CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", ntpServer);
// ==========================================
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ESP-NOW XXXXXXXXXXXXXXXXXXXXXXXXX
WiFi.mode(WIFI_MODE_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
// Adresse IP:
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//===================================================================== >
rssi = WiFi.RSSI();
Serial.print("RRSI: ");
Serial.println(rssi);
server.begin();
// SERVEUR SETUP end ======== >
//MULTITHREAD SETUP ========================================================
//create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
void Task1code (void * pvParameters);
void Task2code (void * pvParameters);
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
3, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task2code, /* Task function. */
"Task2", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
3, /* XXXXXXXXXXXXXXX
priority of the task */
&Task2, /* Task handle to keep track of created task */
1); /* pin task to core 1 */
delay(500);
}// ======= SETUP END >
#include "functions.h"
#include "SPIFFS.h"
//------ CAN BATTERY -----
extern int tour;
extern int battery_can_sum;
int average_can=0;
// ---->
void spiffs_setup(){
// SPIFFS setup : ==================<
Serial.println();
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
//=========>
}
}
/*-----------------------------------------------------------------------
* float spiffs_read_write(float myVariable)
*
* essais Ecriture/lecture dans la mémoire flash de la carte d'une variable changée à
* chaque tour
-----------------------------------------------------------------------*/
bool spiffs_read_write(float Mydata){
//--------- Append to file --------------
File fileToAppend = SPIFFS.open("/testAppend.txt", "a");
if(!fileToAppend){
Serial.println("There was an error opening the file for appending");
return(1);
}
if(fileToAppend.println(Mydata)){
Serial.println("File content was appended");
} else {
Serial.println("File append failed");
}
fileToAppend.close();
//---------- Read from file fileWrToRead --------------
File fileWrToReadApp = SPIFFS.open("/testAppend.txt", "r");
if(!fileWrToReadApp){
Serial.println("Failed to open testWr for reading");
return(1);
}
Serial.println("TTTTTTTTT testAppend.txt Content TTTTTTTTTTTTTTTTTTTTTT TTT :");
Serial.println("TEMPERATURE EXTERIEURE :");
while(fileWrToReadApp.available()){
Serial.write(fileWrToReadApp.read());
}
fileWrToReadApp.close();
return(0);
}
//------------------------->
/*-----------------------------------------------------------------------
* bool spiffs_read_writeB(const char* FilePath, float Mydata)
*
écriture/lecture de température extérieur/intérieur (amélioré)
(essais de passage d'objet file à une fonction)
===================================
!!!! const char* FilePath : A VOIR
===================================
-----------------------------------------------------------------------*/
bool spiffs_read_writeB(const char* FilePath, float Mydata){
//--------- Append to file --------------
File fileToAppend = SPIFFS.open(FilePath, "a");
if(!fileToAppend){
Serial.println("There was an error opening the fileB for appending");
return(1);
}
String MydataStr = " "+String (Mydata); // XXXXXXXXXXXX //concaten pour espace
//Serial.println("VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV");
//Serial.println(MydataStr);
//Serial.println("VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV");
//delay(50000);
if(fileToAppend.println(MydataStr)){
Serial.println("FileB content was appended");
} else {
Serial.println("FileB append failed");
}
fileToAppend.close();
//---------- Read from file fileWrToRead --------------
File fileWrToReadApp = SPIFFS.open(FilePath, "r");
if(!fileWrToReadApp){
Serial.println("Failed to open testWrB for reading");
return(1);
}
Serial.println("UUUUUUUUUUUUUU testAppend.txt Content UUUUUUUUUUUUUUUUUUUUUUU UUU :");
Serial.println("TEMPERATURE INTERIEURE :");
// Serial.print(" ");
while(fileWrToReadApp.available()){
Serial.write(fileWrToReadApp.read());
}
fileWrToReadApp.close();
return(0);
}
// XXXXXXXXXXXXX A FAIRE: if 0.00 comme ds serveur html
//----------->
/*-----------------------------------------------------------------------
* void buzzA()
*
* Petit buz discret à installer sur pin 16
-----------------------------------------------------------------------*/
void buzzA(int aTime){
Serial.print ("Buzzer >>>>>>>>>>>>>>>>>>");
{
digitalWrite(16,HIGH);
delay(aTime);
digitalWrite(16,LOW);
}
}
/*-----------------------------------------------------------------------
* void BatteryAlarm(int CanAverageGlobal, int battery_can,bool usbMode,bool justOneTime,char tour)
*
* Tests batterie
-----------------------------------------------------------------------*/
int BatteryAlarm() {
Serial.print ("tour IN: "); // XXX
Serial.println (tour);
Serial.print ("battery_can_sum IN: ");
Serial.println (battery_can_sum);
int battery_can=0;
Serial.println("< -------------- TEST BATTERIE ----------");
Serial.print("Battery_can: ");
Serial.println(battery_can);
battery_can=analogRead(34);
Serial.print ("CAN ACCU :");
Serial.println (battery_can);
// moyenne de mesure sur trois tours:
battery_can_sum=battery_can_sum+battery_can;
Serial.print ("battery_can_sum: ");
Serial.println (battery_can_sum);
tour+=1;
Serial.println("QQQQQQQQQQQQQQQQQQQQQQQQQQQQ");
Serial.print ("tour: ");
Serial.println (tour);
if (tour == 3){ // remise à zéro de la boucle 3
Serial.println ("tour == 3"); // XXX
average_can = battery_can_sum/3;
Serial.print ("average_can: ");
Serial.println (average_can);
tour=0;
average_can =0;
battery_can_sum=0;
Serial.println("QQQQQQQQQQQQQQQQQQQQQQQQQQQQ");
return (battery_can);
}
// delay(2000);
// ========>
}
void alarmIf(int sample,int level){
if (sample<level){
Serial.println("ALAAAAAAAAAAAAAAAARM XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
buzzA(200);
delay(100);
buzzA(300);
}
}
float CanMap(int can){
int fromLow=3350;
int fromHigh=4095;
int toLow=120;
int toHigh=140;
int result;
float resultF;
result = map(can, fromLow, fromHigh, toLow, toHigh);
Serial.println("OOOOOOOOOOO MAP OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
Serial.println(result);
resultF = float(result)/10;
Serial.println("OOOOOOOOOOO MAP FFFF OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
Serial.println(resultF);
return (resultF);
}
//#include "B_DECLARATIONS.h"
bool spiffs_read_write(float);
void buzzA(int aTime);
void spiffs_setup(void);
// (GGG passage d'objet File à une fonction):
bool spiffs_read_writeB(const char* FilePath, float Myadata);
int BatteryAlarm();
void alarmIf(int sample,int level);
float approxMap(int can); // sorte de conversion très approximative du CAN en volts à partir de quelques correspondances
float CanMap(int can);