#include #include #include "FS.h" #include "SD.h" #include "SPI.h" #include "RTClib.h" #include #include "driver/pcnt.h" #include #include #include "main.h" //#include ///////////////////////////// /* DÉFINITIONS DU COMPTEUR */ ///////////////////////////// #define PCNT_TEST_UNIT PCNT_UNIT_0 // Il existe 8 compteurs de 0 à 7 #define PCNT_TEST_CHANNEL PCNT_CHANNEL_0 #define PCNT_INPUT_SIG_IO GPIO_NUM_15 // Pulse Input GPIO #define PCNT_INPUT_CTRL_IO GPIO_NUM_2 // Control GPIO HIGH=count up, LOW=count down bool sem_compteur; // sémaphore pour le compteur int16_t count = 0; // variable de compteur int vRotReel; // vitesse réelle mesurée du moteur unsigned long compteur_minuteur = 0; // minuteur pour le déclenchement du compteur int refresh_compte_tour = 500; // durée entre deux mesures du compte-tour en ms ///////////////////////////////// /*NUMÉRO DE SÉRIE DE L'APPAREIL*/ ///////////////////////////////// char numero_capteur[] = "capteur n°0001\n"; /////////////////////////// /* DÉFINITIONS DE LA RTC */ /////////////////////////// RTC_DS3231 rtc; //déclaration de la rtc DateTime now{rtc.now()}; /////////////////////////// /* DÉFINITIONS DE LA LED */ /////////////////////////// #define LED 13 //////////////////////////////// /* DÉFINITIONS DE LA CARTE SD */ //////////////////////////////// unsigned long freq_ecriture = 60000; // durée entre deux écritures sur la carte SD en micro secondes char horodatage[12]; //création du tableau pour contenir l'horodatage char fichier[] = "/"; //tableau pour le nom de fichier ////////////////////////////////// /* DÉFINITIONS DU DRIVER MOTEUR */ ////////////////////////////////// int vRotVis = 2400; //vitesse de rotation recherchée en tr/min Servo moteur; //création de l'objet moteur int minUs = 1000; // intervalle pwm mini int maxUs = 2000; // intervalle pwm maxi int pos = 135; // vitesse de départ, 135 donne environ 40Hz int moteurPin = 4; //pin de contrôle du BEC de l'ESC du moteur ESP32PWM pwm; // Activation du pwm //////////////////////////////// /* DÉFINITIONS DU BOUTON WIFI */ //////////////////////////////// int bouton_wifi = 2; // pin du bouton wifi bool sem_wifi; //sémaphore du minuteur du bouton wifi unsigned long wifi_minuteur = 0; // minuteur pour l'appui long sur le bouton wifi bool etat_bouton_wifi; int tempo_bouton_wifi = 5000; //temps d'appui en millisecondes pour arrêter le capteur et passer en mode wifi /////////////////////////////////////// /* DÉFINITIONS DU POINT D'ACCES WIFI */ /////////////////////////////////////// const char *ssid = "IFV-Sporix"; // SSID const char *password = "12345678"; // PASSWORD, 8 caractères minimum WiFiServer server(80); // port du serveur //////////////////////////// /// FONCTION CODE ERREUR /// //////////////////////////// void errorCode(int codeNumber) { while(1) { delay(3700); for( int i=1 ; i<=codeNumber ; ++i ){ Serial.println("balise errCode Blink"); delay(500); digitalWrite(LED,HIGH); delay(500); digitalWrite(LED,LOW); delay(500); } } } ///////////////////////// /* FONCTIONS DU MOTEUR */ ///////////////////////// void start_moteur() { ESP32PWM::allocateTimer(2); moteur.setPeriodHertz(50); moteur.attach(moteurPin, minUs, maxUs); delay(15); moteur.write(0); // envoi du 0 à l'ESC pour initialisation delay(1500); // délai permettant au variateur de détecter le zero moteur.write(pos); // vitesse initiale } void gaz_moteur() { if (vRotVis > vRotReel) { pos ++; } if (vRotReel > vRotVis) { pos --; } moteur.write(pos); } void stop_moteur() { moteur.write(0); delay(1000); moteur.detach(); } /////////////////////////// /* FONCTIONS DU COMPTEUR */ /////////////////////////// void start_compteur() { Serial.println("Initialisation du compteur"); pcnt_config_t pcnt_config = { PCNT_INPUT_SIG_IO, // Pulse input gpio_num, if you want to use gpio16, pulse_gpio_num = 16, a negative value will be ignored PCNT_PIN_NOT_USED, // Control signal input gpio_num, a negative value will be ignored PCNT_MODE_KEEP, // PCNT low control mode PCNT_MODE_KEEP, // PCNT high control mode PCNT_COUNT_INC, // PCNT positive edge count mode PCNT_COUNT_DIS, // PCNT negative edge count mode //PCNT_H_LIM_VAL, // Maximum counter value //PCNT_L_LIM_VAL, // Minimum counter value PCNT_TEST_UNIT, // PCNT unit number PCNT_TEST_CHANNEL, // the PCNT channel }; if(pcnt_unit_config(&pcnt_config) == ESP_OK) //init unit Serial.println("Config Unit_0 = ESP_OK"); pcnt_set_filter_value(PCNT_TEST_UNIT, 100); /*Configure input filter value*/ pcnt_filter_enable(PCNT_TEST_UNIT); /*Enable input filter*/ pcnt_counter_pause(PCNT_TEST_UNIT); pcnt_counter_clear(PCNT_TEST_UNIT); /*Reset counter value*/ pcnt_counter_resume(PCNT_TEST_UNIT); /*Resume counting*/ } void stop_compteur() { /*Pause counter*/ pcnt_counter_pause(PCNT_TEST_UNIT); /*Reset counter value*/ pcnt_counter_clear(PCNT_TEST_UNIT); // peut-être libérer la mémoire compteur? } void compte_tour() { int offset = (compteur_minuteur - millis()); if (offset >= refresh_compte_tour ){ vRotReel = (pcnt_get_counter_value(PCNT_TEST_UNIT, &count)/offset * 60000); //vitesse en tours par min gaz_moteur(); // ajustement des gaz en fonction de la vitesse mesurée Serial.println("vRotReel = "); Serial.println(vRotReel); } } ///////////////////////// /// FONCTIONS SD CARD /// ///////////////////////// void readFile(fs::FS &fs, const char * path){ Serial.printf("Reading file: %s\n", path); File file = fs.open(path); if(!file){ Serial.println("Failed to open file for reading"); return; } Serial.print("Read from file: "); while(file.available()){ Serial.write(file.read()); } file.close(); } void writeFile(fs::FS &fs, const char * path, const char * message){ Serial.printf("Writing file: %s\n", path); File file = fs.open(path, FILE_WRITE); if(!file){ Serial.println("Failed to open file for writing"); errorCode(2); return; } if(file.print(message)){ Serial.println("File written"); } else { Serial.println("Write failed"); } file.close(); } void appendFile(fs::FS &fs, const char * path, const char * message){ Serial.printf("Appending to file: %s\n", path); File file = fs.open(path, FILE_APPEND); if(!file){ Serial.println("Failed to open file for appending"); return; } if(file.print(message)){ Serial.println("Message appended"); } else { Serial.println("Append failed"); } file.close(); } void deleteFile(fs::FS &fs, const char * path){ Serial.printf("Deleting file: %s\n", path); if(fs.remove(path)){ Serial.println("File deleted"); } else { Serial.println("Delete failed"); } } void testFileIO(fs::FS &fs, const char * path){ File file = fs.open(path); static uint8_t buf[512]; size_t len = 0; uint32_t start = millis(); uint32_t end = start; if(file){ len = file.size(); size_t flen = len; start = millis(); while(len){ size_t toRead = len; if(toRead > 512){ toRead = 512; } file.read(buf, toRead); len -= toRead; } end = millis() - start; Serial.printf("%u bytes read for %u ms\n", flen, end); file.close(); } else { Serial.println("Failed to open file for reading"); } file = fs.open(path, FILE_WRITE); if(!file){ Serial.println("Failed to open file for writing"); return; } size_t i; start = millis(); for(i=0; i<2048; i++){ file.write(buf, 512); } end = millis() - start; Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end); file.close(); } void sd_init() { if(!SD.begin()){ Serial.println("Card Mount Failed"); errorCode(3); return; } uint8_t cardType = SD.cardType(); if(cardType == CARD_NONE){ Serial.println("No SD card attached"); errorCode(4); return; } //DateTime now{rtc.now()};//DateTime now = rtc.now(); char date_format[] = "MM-DD-hh-mm"; char *date = now.toString(date_format); strcat(fichier,date); char entete[] = "# YY-MM-DD:hh:mm:ss Vrot en tr/min \n"; writeFile(SD, fichier, numero_capteur); appendFile(SD, fichier, entete); } void scribe_sd (){ if ( sem_compteur == false ) { compteur_minuteur = millis(); sem_compteur = true; } if ( sem_compteur == true && compteur_minuteur - millis() >= freq_ecriture ){ char timestamp[] = "YY-MM-DD-hh:mm:ss"; char *horodatage = now.toString(timestamp); char buffer[64]; snprintf(buffer, sizeof buffer, "%d", vRotReel); appendFile(SD, fichier, horodatage); appendFile(SD, fichier, " " ); appendFile(SD, fichier, buffer); appendFile(SD, fichier, "\n" ); } } ///////////////////// /// FONCTIONS RTC /// ///////////////////// void rtc_init() { if (! rtc.begin()) { Serial.println("RTC introuvable !"); // ligne de debug à commenter en prod delay(2000); errorCode(1); // ESP.restart(); //while (1); } if (rtc.lostPower()) { Serial.println("Veuillez régler l'heure et vérifier la pile du module RTC!"); // ligne de debug à commenter en prod errorCode(5); } else { //DateTime now = rtc.now(); // Serial.println("rtc OK"); } } ////////////////////// /// FONCTIONS WIFI /// ////////////////////// void start_wifiAP() { pinMode(LED_BUILTIN, OUTPUT); Serial.println(); Serial.println("Configuring access point..."); //WiFi.softAP(ssid, password); // You can remove the password parameter if you want the AP to be open. IPAddress myIP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(myIP); server.begin(); Serial.println("Server started"); } void wifi_AP() { // ajouter menu vitesse et fréquence d'écriture WiFiClient client = server.available(); // listen for incoming clients 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 the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: 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(); // the content of the HTTP response follows the header: client.print("Click here to turn ON the LED.
"); client.print("Click here to turn OFF the LED.
"); // 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(LED_BUILTIN, HIGH); // GET /H turns the LED on } if (currentLine.endsWith("GET /L")) { digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off } } } // close the connection: client.stop(); Serial.println("Client Disconnected."); } ESP.restart(); } /////////////// /* PAGE HTML */ /////////////// // Peut-être externaliser la page html dans un fichier txt String html =" \ \ \

ESP32 IFV Soft access point

\

Web Server

\ \ "; /////FONCTIONS COMMUNES////// void vigie_Wifi () { etat_bouton_wifi = digitalRead(bouton_wifi); if ( etat_bouton_wifi == HIGH && sem_wifi == false ) { sem_wifi = true; wifi_minuteur = millis(); } if ( etat_bouton_wifi == HIGH && sem_wifi == true && millis() - wifi_minuteur > tempo_bouton_wifi ) { stop_moteur(); stop_compteur(); start_wifiAP(); wifi_AP(); } } void setup() { Serial.begin(9600); // serial just for feedback delay(3000); pinMode(LED,OUTPUT); // LED bouton wifi et erreurs pinMode(bouton_wifi, INPUT); // bouton wifi Serial.println("balise 0"); rtc_init(); // RTC sd_init (); // initialisation de la carte SD sem_wifi = false; // initialisation du sémaphore /* compteur de pulsations */ sem_compteur = false; // initialisation du sémaphore //pinMode(pulsePin,INPUT_PULLUP); start_compteur(); start_moteur(); // moteur } void loop (){ vigie_Wifi(); compte_tour(); scribe_sd(); }