322 lines
11 KiB
C++
322 lines
11 KiB
C++
#include <Arduino.h>
|
|
#include <ESP32Servo.h>
|
|
#include "FS.h"
|
|
#include "SD.h"
|
|
#include "SPI.h"
|
|
#include "RTClib.h"
|
|
#include <PID_v1.h>
|
|
#include <Wire.h>
|
|
// #include "pcnt.h" // https://github.com/espressif/arduino-esp32/blob/master/tools/sdk/include/driver/driver/pcnt.h
|
|
#include "driver/pcnt.h"
|
|
#include <WiFi.h>
|
|
#include <WiFiClient.h>
|
|
#include <WiFiAP.h>
|
|
|
|
//définition du compteur
|
|
#define PULSE_COMPTEUR
|
|
#define PCNT_TEST_UNIT PCNT_UNIT_0 // Il existe 8 compteurs de 0 à 7
|
|
//#define PCNT_H_LIM_VAL 10000 //valeur max du compteur_minuteur
|
|
#define PCNT_L_LIM_VAL 0 //valeur mini du compteur
|
|
//#define PCNT_THRESH1_VAL 5
|
|
//#define PCNT_THRESH0_VAL -5
|
|
#define PCNT_INPUT_SIG_IO 15 // Pulse Input GPIO
|
|
#define PCNT_INPUT_CTRL_IO 2 // Control GPIO HIGH=count up, LOW=count down
|
|
bool sem_compteur; // sémaphore pour le compteur
|
|
int16_t pulsations = 0; // variable de compteur
|
|
int vitesse = 2400; //vitesse de rotation en tr/min
|
|
// int16_t Pulses = 0; // variable de lecture de compteur
|
|
byte pulsePin = 13; //broche de l'encodeur
|
|
|
|
//définition du PID
|
|
#define PIN_INPUT 26 //entrée physique perso?
|
|
#define PIN_OUTPUT 3// sortie physique perso?
|
|
double Setpoint, Input, Output; //PID Define Variables we'll be connecting to
|
|
double Kp=2, Ki=5, Kd=1; //PID Specify the links and initial tuning parameters
|
|
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
|
|
|
|
//définition du numéro de série de l'appareil
|
|
int numero_capteur = 0001;
|
|
|
|
//définition du bouton Wifi
|
|
int bouton_wifi = 7; // 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
|
|
unsigned long compteur_minuteur = 0; // minuteur pour l'appui long sur le bouton wifi
|
|
char horodatage[25]; //création du tableau pour contenir l'horodatage
|
|
bool etat_bouton_wifi;
|
|
|
|
//définition de la RTC
|
|
RTC_DS3231 rtc; //déclaration de la rtc
|
|
|
|
//définition de la carteSD
|
|
const int broche_CS = 5; // broche de la carte SD
|
|
unsigned long freq_ecriture = 60000; // durée entre deux écritures sur la carte SD en micro secondes
|
|
|
|
//définition du driver moteur ESC
|
|
const byte pinMoteur = 4; // broche de contrôle du moteur
|
|
Servo moteur; //création de l'objet moteur
|
|
|
|
// définition du point d'accès Wifi
|
|
const char *ssid = "yourAP";
|
|
const char *password = "yourPassword";
|
|
WiFiServer server(80);
|
|
|
|
|
|
void start_compteur() {
|
|
|
|
esp_err_t error;
|
|
|
|
Serial.println("Initialisation du compteur");
|
|
|
|
pcnt_config_t pcnt_config = {
|
|
pulsePin, // 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_CHANNEL_0, // the PCNT channel
|
|
};
|
|
|
|
if(pcnt_unit_config(&pcnt_config) == ESP_OK) //init unit
|
|
Serial.println("Config Unit_0 = ESP_OK");
|
|
/*Configure input filter value*/
|
|
pcnt_set_filter_value(PCNT_TEST_UNIT, 100);
|
|
/*Enable input filter*/
|
|
pcnt_filter_enable(PCNT_TEST_UNIT);
|
|
/*Set value for watch point thresh1*/
|
|
//pcnt_set_event_value(PCNT_TEST_UNIT, PCNT_EVT_THRES_1, PCNT_THRESH1_VAL);
|
|
/*Enable watch point event of thresh1*/
|
|
//pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_THRES_1);
|
|
//pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_THRES_1);
|
|
/*Set value for watch point thresh0*/
|
|
//pcnt_set_event_value(PCNT_TEST_UNIT, PCNT_EVT_THRES_0, PCNT_THRESH0_VAL);
|
|
/*Enable watch point event of thresh0*/
|
|
//pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_THRES_0);
|
|
//pcnt_event_disable(PCNT_TEST_UNIT, PCNT_EVT_THRES_0);
|
|
/*Enable watch point event of h_lim*/
|
|
//pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_H_LIM);
|
|
//pcnt_event_disable(PCNT_TEST_UNIT, PCNT_EVT_H_LIM);
|
|
/*Enable watch point event of l_lim*/
|
|
//pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_L_LIM);
|
|
//pcnt_event_disable(PCNT_TEST_UNIT, PCNT_EVT_L_LIM);
|
|
/*Enable watch point event of zero*/
|
|
//pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_ZERO);
|
|
//pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_ZERO);
|
|
/*Pause counter*/
|
|
//pcnt_counter_pause(PCNT_TEST_UNIT);
|
|
/*Reset counter value*/
|
|
pcnt_counter_clear(PCNT_TEST_UNIT);
|
|
/*Register ISR handler - à activer par défaut? */
|
|
//pcnt_isr_register(pcnt_example_intr_handler, NULL, 0, NULL);
|
|
/*Enable interrupt for PCNT unit*/
|
|
//pcnt_intr_enable(PCNT_TEST_UNIT);
|
|
/*Resume counting*/
|
|
//pcnt_counter_resume(PCNT_TEST_UNIT);
|
|
pcnt_get_counter_value(PCNT_TEST_UNIT, &pulsations);
|
|
}
|
|
void pid () {
|
|
Input = analogRead(PIN_INPUT);
|
|
myPID.Compute();
|
|
analogWrite(PIN_OUTPUT, Output);
|
|
}
|
|
void pidsetup() {
|
|
//initialize the variables we're linked to
|
|
Input = analogRead(PIN_INPUT);
|
|
Setpoint = vitesse;
|
|
//turn the PID on
|
|
myPID.SetMode(AUTOMATIC);
|
|
}
|
|
|
|
void sd_init(){
|
|
if (!SD.begin(broche_CS)) {
|
|
Serial.println("Carte SD introuvable , reboot");
|
|
delay(1000);
|
|
ESP.restart();
|
|
}
|
|
else{
|
|
Serial.println("Carte SD détectée"); // ligne de debug à commenter en prod
|
|
//writeFile(SD, );// ajouter une fonction : "écrire fichier d'identité et créer fichier data sur la carte SD
|
|
}
|
|
}
|
|
|
|
void start_moteur() {
|
|
moteur.attach(pinMoteur);
|
|
moteur.writeMicroseconds(1500); // envoi d'un 'neutre' au variateur
|
|
delay(1000);
|
|
moteur.writeMicroseconds(2000); //valeur de départ pour atteindre 2400tr/min
|
|
}
|
|
|
|
void rtc_init() {
|
|
if (! rtc.begin()) {
|
|
Serial.println("RTC introuvable !"); // ligne de debug à commenter en prod
|
|
delay(2000);
|
|
// ajouter un message d'erreur par clignotement de LED
|
|
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
|
|
// ajouter un message d'erreur par clignotement de LED
|
|
}
|
|
else
|
|
{
|
|
DateTime now = rtc.now(); // mise à l'heure de l'esp, à partir de la rtc
|
|
Serial.println("mise à l'heure de l'esp, à partir de la rtc");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
void stop_compteur() {
|
|
/*Pause counter*/
|
|
pcnt_counter_pause(PCNT_TEST_UNIT);
|
|
/*Reset counter value*/
|
|
pcnt_counter_clear(PCNT_TEST_UNIT);
|
|
}
|
|
|
|
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() {
|
|
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 <a href=\"/H\">here</a> to turn ON the LED.<br>");
|
|
client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
|
|
|
|
// 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();
|
|
}
|
|
|
|
void stop_moteur() {
|
|
moteur.writeMicroseconds(1500);
|
|
delay(1000);
|
|
moteur.detach();
|
|
}
|
|
|
|
void compte_tour() {
|
|
if ( sem_compteur == false ) {
|
|
compteur_minuteur = millis();
|
|
sem_compteur = true;
|
|
}
|
|
if ( sem_compteur == true && compteur_minuteur - millis() > freq_ecriture ){
|
|
/// vitesse = readPcntCounter_0() / 2; //vitesse en tours par min
|
|
/// scribe_sd ();
|
|
/// pcnt_counter_clear();
|
|
compteur_minuteur = millis();
|
|
sem_compteur = false;
|
|
}
|
|
}
|
|
|
|
void scribe_sd (fs::FS &fs, const char * path, const char * horodatage) {
|
|
Serial.printf("Ecriture dans le fichier: %s\n", path);
|
|
|
|
File file = fs.open(path, FILE_APPEND);
|
|
if (!file) {
|
|
Serial.println("Echec d'ouverture du fichier");
|
|
return;
|
|
}
|
|
if (file.print(horodatage)) {
|
|
Serial.println("Fichier modifié avec succes.");
|
|
} else {
|
|
Serial.println("Echec de la modification du fichier.");
|
|
}
|
|
file.close();
|
|
}
|
|
|
|
void vigie () {
|
|
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 > 5000 ) {
|
|
stop_moteur();
|
|
stop_compteur();
|
|
start_wifiAP();
|
|
wifi_AP();
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
// serial just for feedback
|
|
Serial.begin(115200);
|
|
// bouton wifi
|
|
pinMode(bouton_wifi, INPUT);
|
|
sem_wifi = false; // initialisation du sémaphore
|
|
// moteur
|
|
moteur.attach(pinMoteur);
|
|
start_moteur();
|
|
// compteur de pulsations
|
|
sem_compteur = false; // initialisation du sémaphore
|
|
pinMode(pulsePin,INPUT_PULLUP);
|
|
start_compteur();
|
|
// lecteur de carte SD
|
|
sd_init ();
|
|
// RTC
|
|
rtc_init();
|
|
//PID
|
|
pidsetup();
|
|
}
|
|
|
|
void loop (){
|
|
vigie();
|
|
pid();
|
|
compte_tour();
|
|
delay(10000);
|
|
}
|