#include "driver/rtc_io.h"
#include "BluetoothSerial.h"
#include <EEPROM.h>
hw_timer_t *timer = NULL;
hw_timer_t *timer1 = NULL;
hw_timer_t *timer2 = NULL;
unsigned long tempo_innesco = 20000000; //20 secondi
unsigned long tempo_consenso = 3000000; // 3 secondi
unsigned long tempo_dormi = 10000000; // 10 secondi
unsigned long tempo_sirena = 60000000; // 60 secondi
unsigned long prendi_tempo, prendi_tempo_cl;
byte pwd [] = {42,76,77}; //lettere *LM
bool cons_pressed, sirena;
#define avviaPin 33
#define ReleRossoPin 21
#define consensoPin 19
#define relePin 16
#define triangoloPin 17
#define clacsonPin 18
#define wakeupPin 34
byte comando = 0;
BluetoothSerial SerialBT;
void ARDUINO_ISR_ATTR timerallarme() {
// Increment the counter and set the time of ISR
Serial.println("SIRENA");
timerStop(timer);
timerWrite(timer, 0);
sirena = true;
}
void ARDUINO_ISR_ATTR timerconsenso() {
// Increment the counter and set the time of ISR
Serial.println("CONSENSO");
cons_pressed = true;
timerStop(timer1);
timerWrite(timer1, 0);
detachInterrupt(consensoPin);
}
void ARDUINO_ISR_ATTR timerdormi() {
// Increment the counter and set the time of ISR
Serial.println("DORMO");
esp_deep_sleep_start();
}
void IRAM_ATTR avvia() {
Serial.println("Hai avviato l'auto");
if (!cons_pressed){
Serial.println("SIRENA");
timerStop(timer);
timerWrite(timer, 0);
sirena = true;
}else{
Serial.println("ed hai dato precedentemente consenso");
}
}
void IRAM_ATTR consenso() { //se tieni premuto presse
if (digitalRead(consensoPin) == HIGH){
timerStart(timer1);
}else{
timerStop(timer1);
timerWrite(timer1, 0);
}
}
void IRAM_ATTR dormi() { //se manca positivo sotto chiave
Serial.print("Pin 34 a ");
Serial.println(digitalRead(wakeupPin));
if (digitalRead(wakeupPin) == LOW){
Serial.println("start");
timerStart(timer2);
}else{
Serial.println("stop");
timerStop(timer2);
timerWrite(timer2, 0);
}
}
void setup() {
Serial.begin(115200);
SerialBT.begin("CESP"); // MIO Bluetooth device name
EEPROM.begin(160); // Quanti byte si intende usare
rtc_gpio_pullup_dis(GPIO_NUM_34);
rtc_gpio_pulldown_dis(GPIO_NUM_34);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_34, 1);
timer = timerBegin(1000000);
timerAttachInterrupt(timer, &timerallarme);
timerAlarm(timer, tempo_innesco, false, 0);
timer1 = timerBegin(1000000);
timerAttachInterrupt(timer1, &timerconsenso);
timerAlarm(timer1, tempo_consenso, false, 0);
timer2 = timerBegin(1000000);
timerAttachInterrupt(timer2, &timerdormi);
timerAlarm(timer2, tempo_dormi, false, 0);
timerStop(timer1);
timerWrite(timer1, 0);
timerStop(timer2);
timerWrite(timer2, 0);
pinMode(avviaPin, INPUT);
pinMode(consensoPin, INPUT);
pinMode(wakeupPin, INPUT);
pinMode(relePin, OUTPUT);
pinMode(clacsonPin, OUTPUT);
pinMode(triangoloPin, OUTPUT);
pinMode(ReleRossoPin, OUTPUT);
digitalWrite(relePin, LOW);
digitalWrite(ReleRossoPin, HIGH);
attachInterrupt(avviaPin, avvia, RISING);
attachInterrupt(consensoPin, consenso, CHANGE);
attachInterrupt(wakeupPin, dormi, CHANGE);
cons_pressed = false;
if (digitalRead(wakeupPin) == LOW){
Serial.println("Inizio e subito dormo");
esp_deep_sleep_start();
}
}
void loop() {
if (cons_pressed){
digitalWrite(relePin, HIGH);
digitalWrite(ReleRossoPin, LOW);
timerStop(timer);
timerWrite(timer, 0);
Blue();
}
if (sirena) {
while (digitalRead(consensoPin) == HIGH){}
Serial.println("PIIIPOOO");
detachInterrupt(wakeupPin);
attachInterrupt(consensoPin, consenso, CHANGE);
for (int x=0; x<3; x++){
digitalWrite(triangoloPin, HIGH);
delay(50);
digitalWrite(triangoloPin, LOW);
prendi_tempo = millis();
prendi_tempo_cl = millis();
while (millis()- prendi_tempo <= tempo_sirena){
if (cons_pressed){
digitalWrite(triangoloPin, HIGH);
delay(50);
digitalWrite(triangoloPin, LOW);
digitalWrite(clacsonPin, LOW);
digitalWrite(relePin, HIGH);
digitalWrite(ReleRossoPin, LOW);
Blue();
}
if (millis()-prendi_tempo_cl >=500){
digitalWrite(clacsonPin, !digitalRead(clacsonPin));
prendi_tempo_cl = millis();
}
}
digitalWrite(triangoloPin, HIGH);
delay(50);
digitalWrite(triangoloPin, LOW);
digitalWrite(clacsonPin, LOW);
prendi_tempo = millis();
while (millis()- prendi_tempo <= tempo_sirena){
if (cons_pressed){
digitalWrite(triangoloPin, HIGH);
delay(50);
digitalWrite(triangoloPin, LOW);
digitalWrite(clacsonPin, LOW);
digitalWrite(relePin, HIGH);
digitalWrite(ReleRossoPin, LOW);
Blue();
}
}
}
detachInterrupt(consensoPin);
attachInterrupt(wakeupPin, dormi, CHANGE);
Blue();
}
}
void Blue() {
while(true){
if (SerialBT.available()) { //SerialBT.available()
for (int x=0; x<3; x++){
comando= SerialBT.read();
if (comando != pwd[x]) x = 6;
}
if ( x != 6){ //se la pwd è corretta
comando= SerialBT.read();
delay(40);
if (comando >= 0 && comando <=7){ //indirizzo congruo
if (comando == 0) { // manda lista completa
}else{ // assegna valore ad indirizzo
x = comando; //mantengo l'indirizzo in x
comando= SerialBT.read(); /legge valore da modificare
EEPROM.write(x-1, comando);
EEPROM.commit();
}
}
}
}
}
}Positivo sotto chiave