Ok, ecco il codice, spero si capisca qualcosa...
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <ESP32Servo.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Stepper.h>
Servo servo;
//WIFI
#ifndef STASSID
#define STASSID "*****"
#define STAPSK "***"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
String statoSlot;
String statohtml;
String selfClose;
int statoRFID;
String ritiraSuccesso;
WebServer server(80);
//RFID
#define SS_PIN 21
#define RST_PIN 22
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
// LED
#define PinBianco 14
#define PinRosso 27
#define PinVerde 26
// Stepper
#define stepPin 17
#define dirPin 16
#define enPin 32
void setup(void) {
WiFiClient client;
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
servo.attach(2);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
pinMode (33, OUTPUT);
digitalWrite(enPin, LOW);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("XX")) {
Serial.println("MDNS responder started");
}
if (digitalRead(5) == HIGH && mfrc522.PICC_IsNewCardPresent()){
statoSlot = (F("<button class= 'errore' onclick= 'ritiraSlot1();'> ERRORE - Slot pieno </a></button>"));
Serial.println(statoSlot);
}
else if (digitalRead(5) == HIGH && ! mfrc522.PICC_IsNewCardPresent()){
statoSlot = (F("<button class= 'ritira' onclick= 'ritiraSlot1();'> Slot 1 vuoto </a></button>"));
Serial.println(statoSlot);
}
else if (digitalRead(5) == LOW && mfrc522.PICC_IsNewCardPresent())
{ statoSlot = (F("<button class= 'consegna' onclick= 'consegnaSlot1();'> Slot 1 pieno </a></button>"));
Serial.println(statoSlot);
}
else if (digitalRead(5) == LOW && ! mfrc522.PICC_IsNewCardPresent())
{ statoSlot = (F("<button class= 'errore' onclick= 'consegnaSlot1();'> ERRORE - Slot vuoto </a></button>"));
Serial.println(statoSlot);
}
server.on("/stato",HTTP_GET, []() {
server.sendHeader("Access-Control-Max-Age", "10000");
server.sendHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
server.sendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
server.sendHeader("Access-Control-Allow-Origin","*");
server.send(200, "text/html", statoSlot );
});
server.on("/acquisto", acquisto);
server.on("/ritira", ritira);
server.on("/exit", uscita);
server.on("/pausa", pausa);
server.begin();
Serial.println("HTTP server started");
}
void acquisto() {
WiFiClient client;
servo.attach(2);
pinMode(5, INPUT);
pinMode (PinBianco, OUTPUT);
pinMode (PinRosso, OUTPUT);
pinMode (PinVerde, OUTPUT);
digitalWrite(33, HIGH);
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init();
int j = 0;
/*******************************************************
* CHIUDE LA PAGINA DOPO 5 SECONDI
******************************************************/
server.sendHeader("connection", "keep-alive");
server.sendHeader("Access-Control-Allow-Origin","*");
server.send(200, "text/html", "<html>\
<head>\
<script>\
setTimeout('self.close()', 5000);\
</script>\
<body onload = setTimeout(self.close(), 5000)>\
</body>\
");
// ritiraSuccesso = (F(""));
if (digitalRead(5) == LOW and mfrc522.PICC_IsNewCardPresent() and mfrc522.PICC_ReadCardSerial()) //Legge RFID e sensore. se ok procede
{ digitalWrite(PinBianco, LOW);
digitalWrite(PinVerde, HIGH);
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : "");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
delay(10);
delay(300);
servo.write(0);
delay(3000);
digitalWrite(dirPin, HIGH); //STEPPER
for (int x = 0; x < 800; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800);
}
delay(1000); // One second delay
digitalWrite(dirPin, LOW);
for (int x = 0; x < 800; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800); //STEPPER
}
int detect = digitalRead(5);
bool USCITO = true;
/*******************************************************
* aspetto che il dispositivo esca
******************************************************/
while (USCITO) {
if (digitalRead(5) == HIGH) {
USCITO = false;
delay(2000);
servo.write(90);
digitalWrite(PinBianco, HIGH);
digitalWrite(PinVerde, LOW);
}
}
statoSlot = (F("<button class= 'ritira' onclick= 'ritiraSlot1();'> Slot 1 vuoto </a></button>"));
client.println(statoSlot);
Serial.println(statoSlot);
}
else { //se RFID no e sensore no allora non disp
Serial.println ("NON DISPONIBILE");
statoSlot = (F("<button class= 'errore' onclick= 'ritiraSlot1();'> ERRORE </a></button>"));
client.println(statoSlot);
Serial.println(statoSlot);
statohtml = (F("<div class='problema'> ATTENZIONE! Riprovare </div>\
<div class=bottoni>\
<form action='http://XXXXX/XX.html' method='get' target='_self'>\
<button class='esci' href='http://XXXXX/XX.html'> RIPROVA </button>\
</form>\
</div>\
"));
delay(2000);
}
while (digitalRead(5) == LOW)
{j++;
delay(1000);
Serial.println(j);
if (j==15 && ! mfrc522.PICC_IsNewCardPresent() ) {
Serial.println("ritiraRE DISPOSITIVO");
}
if (j==30 && ! mfrc522.PICC_IsNewCardPresent() ) {
statoSlot = (F("<button> Errore ritira </a></button>"));
Serial.println(statoSlot);
servo.write(90);
delay (1000);
return;
}
}
}
void ritira() {
WiFiClient client;
digitalWrite(2, LOW);
servo.attach(12);
pinMode(5, INPUT);
pinMode (PinBianco, OUTPUT);
pinMode (PinRosso, OUTPUT);
pinMode (PinVerde, OUTPUT);
digitalWrite(33, HIGH);
#define VIN 34;
statoRFID = 0;
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init();
int j = 0;
/*******************************************************
* CHIUDE LA PAGINA DOPO 5 SECONDI
******************************************************/
server.sendHeader("connection", "keep-alive");
server.sendHeader("Access-Control-Allow-Origin","*");
server.send(200, "text/html", "<html>\
<head>\
<script>\
setTimeout('self.close()', 5000);\
</script>\
<body onload = setTimeout(self.close(), 5000)>\
</body>\
");
// ritiraSuccesso = (F(""));
if //controllo
(mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
(statoRFID == 0);
}
else {
(statoRFID == 1);
}
if (digitalRead(5) == LOW)
{
statoSlot = (F("<button class= 'consegna' onclick= 'consegnaSlot1();'> Slot 1 pieno </a></button>"));
client.println(statoSlot);
Serial.println(statoSlot);
Serial.println ("NON DISPONIBILE");
statohtml = (F("<div class='problema'> ATTENZIONE! Riprovare </div>\
<div class=bottoni>\
<form action='http://XXXXX/XX.html' method='get' target='_self'>\
<button class='esci' href='http://XXXXX/XX.html'> RIPROVA </button>\
</form>\
</div>\
"));
delay(2000);
}
else { //apre il vetro, accende i led e legge la carica
digitalWrite(PinVerde, HIGH);
digitalWrite(PinBianco, LOW);
float average = 0;
for (int i = 0; i < 1000; i++) {
average = average + (.0264 * analogRead(34) - 13.51) / 1000;
delay(0);
}
delay(300);
Serial.println(average);
servo.write(0);
/*******************************************************
* HA 30 SECONDI PER INSERIRE IL DISPOSITIVO
******************************************************/
while (digitalRead(5) == HIGH)
{j++;
delay(1000);
Serial.println(j);
if (j==15 && ! mfrc522.PICC_IsNewCardPresent() ) {
Serial.println("INSERIRE DISPOSITIVO");
}
if (j==30 && ! mfrc522.PICC_IsNewCardPresent() ) {
statoSlot = (F("<button> NON è STATO INSERITO IL DISPOSITIVO </a></button>"));
Serial.println(statoSlot);
servo.write(90);
statoSlot = (F("<button class= 'ritira' onclick= 'ritiraSlot1();'> Slot 1 vuoto </a></button>"));
delay (1000);
return;
}
else if (j==30 && mfrc522.PICC_IsNewCardPresent()) {
statoSlot = (F("<button> ERRORE PROSSIMITà </a></button>"));
Serial.println(statoSlot);
servo.write(90);
delay (1000);
return;
}
}
int j=0;
/*******************************************************
* LETTURA CORRENTE
******************************************************/
delay(1000);
float average1 = 0;
for (int i = 0; i < 1000; i++) {
average1 = average1 + (.0264 * analogRead(34) - 13.51) / 1000;
delay(0);
}
delay(300);
Serial.println(average1);
delay(300);
/*******************************************************
* LETTURA DI TUTTI I VALORI
******************************************************/
if
(mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() && average1 > average && digitalRead(5) == LOW ) {
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : "");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println("");
delay (2000);
servo.write(90);
delay (1000);
return pausa();
}
/*******************************************************
* SE CORRENTE è INFERIORE
******************************************************/
else if
(mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() && average1 < average && digitalRead(5) == LOW ) {
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : "");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println("");
delay (2000);
servo.write(90);
statoSlot = (F("<button> Errore Corrente </a></button>"));
Serial.println(statoSlot);
delay (1000);
return;
}
/*********************************************************************************
* SE IL DISPOSITIVO è STATO INSERITO AL CONTRARIO, FA USCIRE IL DISPOSITIVO
**********************************************************************************/
else if ( ! mfrc522.PICC_IsNewCardPresent() && average1 > average && digitalRead(5) == LOW ) {
Serial.println("INSERIRE DISPOSITIVO CORRETTAMENTE");
digitalWrite(PinVerde, LOW);
digitalWrite(PinRosso, HIGH);
delay(3000);
digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < 800; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800);
}
delay(1000);
Serial.println("INSERIRE NUOVAMENTE");
digitalWrite(dirPin, LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for (int x = 0; x < 800; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800);
}
}
while (digitalRead(5) == HIGH)
{j++;
delay(1000);
Serial.println(j);
if (j==15 && ! mfrc522.PICC_IsNewCardPresent() ) {
Serial.println("INSERIRE DISPOSITIVO");
}
if (j==30 && ! mfrc522.PICC_IsNewCardPresent() ) {
statoSlot = (F("<button> Non è stato inserito il XX </a></button>"));
Serial.println(statoSlot);
servo.write(90);
delay (1000);
return;
}
else if (digitalRead(5) == LOW && ! mfrc522.PICC_IsNewCardPresent()) {
delay (2000);
statoSlot = (F("<button> XX inserito al contrario </a></button>"));
Serial.println(statoSlot);
servo.write(90);
delay (1000);
return;
}
}
if
(mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() && average1 > average && digitalRead(5) == LOW ) {
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : "");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println("");
delay (2000);
servo.write(90);
// ritiraSuccesso = (F(""));
delay (1000);
return pausa();
}
}
}
void pausa(){
WiFiClient client;
digitalWrite(2, LOW);
servo.attach(12);
pinMode(5, INPUT);
pinMode (PinBianco, OUTPUT);
pinMode (PinRosso, OUTPUT);
pinMode (PinVerde, OUTPUT);
digitalWrite(33, HIGH);
#define VIN 34;
statoRFID = 0;
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init();
digitalWrite(PinRosso, LOW);
digitalWrite(PinVerde, LOW);
digitalWrite(PinBianco, HIGH);
statoSlot = (F("<button> DISPOSITIVO in carica </a></button>"));
client.println(statoSlot);
Serial.println(statoSlot);
delay (20000);
statoSlot = (F("<button class= 'consegna' onclick= 'consegnaSlot1();'> Slot 1 pieno </a></button>"));
Serial.println(statoSlot);
client.println(statoSlot);
Serial.println();
return;
}
void uscita() {
WiFiClient client;
servo.attach(2);
pinMode(5, INPUT);
pinMode (PinBianco, OUTPUT);
pinMode (PinRosso, OUTPUT);
pinMode (PinVerde, OUTPUT);
digitalWrite(33, HIGH);
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init();
server.sendHeader("connection", "keep-alive");
server.sendHeader("Access-Control-Allow-Origin","*");
server.send(200, "text/html");
servo.write(0);
delay(3000);
digitalWrite(dirPin, HIGH); //STEPPER
for (int x = 0; x < 800; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800);
}
delay(1000); // One second delay
digitalWrite(dirPin, LOW);
for (int x = 0; x < 800; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800); //STEPPER
}
int detect = digitalRead(5);
bool USCITO = true;
/*******************************************************
* aspetto che il dispositivo esca
******************************************************/
while (USCITO) {
if (digitalRead(5) == HIGH) {
USCITO = false;
delay(2000);
servo.write(90);
}
}
statoSlot = (F("<button class= 'ritira' onclick= 'ritiraSlot1();'> Slot 1 vuoto </a></button>"));
client.println(statoSlot);
Serial.println(statoSlot);
}
void loop(void) {
server.handleClient();
delay(1);
}
String RFID(){
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : "");
Serial.print(mfrc522.uid.uidByte[i], HEX);
Serial.print("");
}
Serial.println();
}