#include <Wire.h>
//#define BUTTON_PIN_BITMASK 128 //pin7 0x1014 // 2^2 + 2^4 + 2^12 in hex
#include <IRremote.h> // DIYables_IRcontroller library
#include <Adafruit_BMP280.h>
#include <Adafruit_BMP085.h>
//#include <AHT10.h>
Adafruit_BMP085 bmp;
Adafruit_BMP280 bmp2;
//AHT10 myAHT20(AHT10_ADDRESS_0X38, AHT20_SENSOR);
//display 5110
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
/* Declare LCD object for SPI
Adafruit_PCD8544(CLK,DIN,D/C,CE,RST); (36, 35, 37, 38, 39);*/
Adafruit_PCD8544 display = Adafruit_PCD8544(38, 36, 34, 39 , 21);
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// Wifi network station credentials
#define WIFI_SSID "Ospiti-TIM-23835029"
#define WIFI_PASSWORD "guest@000000"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "6479842572:AAHpDviarZP705T5om_-aWy3fJawMgqw99I"
#define uS_TO_S_FACTOR 2000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 60 /* Time ESP32 will go to sleep (in seconds) */
#include <HardwareSerial.h>
HardwareSerial Serial2(1);
#define RXD1 12
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
//RTC_DATA_ATTR int bootCount = 0;
unsigned long BOT_MTBS = 0; // mean time between scan messages
unsigned long bot_lasttime = 0; // last time messages' scan has been done
const int pinRxIr = 7;
const int led_ir = 3;
const int ret2 = 40;
//const int led6 = 6; //LIBERO
const int Rele3 = 1;
const int Rele4 = 5;
const int led15 = 15;
bool togle; // bit togle tx ir
int tac; //tempo apertura cancello
String chat_id;
int ledStatus;
int x = 1;
int x_ir = 0;//attesa ricevitore ir
const int x_ir_imp = 10000000; //tempo di attesa comandi IR
int TipoRichiesta = 0;
int cont = 0;
int rx_n = 0; //numero dato ricevuto
int rx_d; //dato ricevuto
int c_dl = 0; //ciclo dati lenti
int temp; // temporaneo
int vcm; // temperatura vano camino
int rrm; // temperatura ritorno riscaldamento Michele
int bat; // temperatura vano batterie
int comSolT;
int mod_luce;
float whs1; float whs2; float whs3; float wh220; float whcs; float whs; float whc; float whb;
float ws1; float ws2; float ws3; float w220; float wcs; float ws; float wc; float wb;
int is1; int is2; int is3; int i220; int ics; int is; int ic; int ib;
float vbat; int ind_dl; float ahbat;
int cod_tx;
char b[12];
String msg;
RTC_DATA_ATTR int n_bot;//conto i risvegli e alternativamente cambio la porta i2c e leggo i sensori 1 per risveglio
RTC_DATA_ATTR int m280_alt;
RTC_DATA_ATTR int m280_pre;
RTC_DATA_ATTR float m280_tem;
RTC_DATA_ATTR float m180_alt;
RTC_DATA_ATTR int m180_pre;
RTC_DATA_ATTR float m180_tem;
RTC_DATA_ATTR int aht20_umi;
RTC_DATA_ATTR float aht20_tem;
void Prta() { //stampa temperature a
rx_d = rx_d - 29;
itoa(rx_d, b, 10);
msg += (b);
msg += "°C ";
}
void Prtb() { //stampa temperature b
rx_d = rx_d - 29;
itoa(rx_d, b, 10);
msg += (b);
msg += "°C";
bot.sendMessage(chat_id, msg);
}
void Prt1() { // stampa solare termico
itoa(temp, b, 10);
msg += " Pomp. ";
msg += (b);
msg += "%";
bot.sendMessage(chat_id, msg);
}
void Prt3() { //stampa fotovoltaico
itoa(temp, b, 10);
msg += (b);
bot.sendMessage(chat_id, msg);
}
void tx_ir() {
bitWrite(cod_tx, 10, 1); // primo bit sempre a 1
for (int r = 4; r > 0; r--) {
noInterrupts();
for (int e = 11; e >= 0; e--) {
if (bitRead(cod_tx, e) == 1)
{
delayMicroseconds(7200);
} else {
delayMicroseconds(4700);
}
for (int i = 12; i > 0; i--) {
digitalWrite(led_ir, HIGH);
delayMicroseconds(6);
digitalWrite(led_ir, LOW);
delayMicroseconds(13);
}
}
interrupts();
delay (51);
}
}
void AllTrama() {
for (int tout = 0; tout < 4100; tout++) {
if (digitalRead(RXD1)) {
cont++;
digitalWrite(led15, HIGH);
} else {
cont = 0;
digitalWrite(led15, LOW);
}
if (cont == 100) {
Serial2.end();
rx_n = 0; rx_d = 0;
}
if (cont == 150) {
//Serial2.begin(701, SERIAL_8N1, RXD1, TXD1);
Serial2.begin(701, SERIAL_8N1, RXD1 );//701 meglio di 702 703 peggio
break;
}
if (tout == 4000) {
digitalWrite(led15, LOW);
TipoRichiesta = 0;
bot.sendMessage(chat_id, "No Dati", "");
}
delay(1);
}
}
void NewMes(int numNewMessages)
{
String from_name = bot.messages[numNewMessages - 1].from_name;
chat_id = bot.messages[numNewMessages - 1].chat_id;
String text = bot.messages[numNewMessages - 1].text;
bot.sendMessage(chat_id, (from_name), "");
if (text == "/pedo")
{
digitalWrite(Rele3, HIGH); // Pigio tasto cancelletto e porta
bot.sendMessage(chat_id, "Apro Pedonale", "");
}
if (text == "/apri")
{
tac = 9;
digitalWrite(Rele4, HIGH); // Pigio tasto cancello
bot.sendMessage(chat_id, "Apro Cancello", "");
}
if (text == "/start")
{
msg = "/tx000 : TRASMETTE CODICE 000 IR\n"; bot.sendMessage(chat_id, msg);
msg = "/pedo : APRE PEDONALE\n"; bot.sendMessage(chat_id, msg);
msg = "/apri : APRI CANCELLO\n"; bot.sendMessage(chat_id, msg);
msg = "/sens : LETTURA SENSORI\n"; bot.sendMessage(chat_id, msg);
msg = "/SolT : SOLARE TERMICO\n"; bot.sendMessage(chat_id, msg);
msg = "/Foto : FOTOVOLTAICO\n"; bot.sendMessage(chat_id, msg);
msg = "/Inso : NEW INSOL\n"; bot.sendMessage(chat_id, msg);
msg = "/Bat : BATTERIA\n"; bot.sendMessage(chat_id, msg);
msg = "/Tb : Tb e Text\n"; bot.sendMessage(chat_id, msg);
}
//eseguo varie concatenazioni per l'output
if (text == "/sens") {
msg = "TEMPERATURA a ";
dtostrf(aht20_tem, 3, 1, b);
msg += (b);
msg += " C°";
bot.sendMessage(chat_id, msg);
msg = "UMIDITA' ";
itoa(aht20_umi, b, 10);
msg += (b);
msg += "%";
bot.sendMessage(chat_id, msg);
msg = "PRESSIONE ";
itoa(m180_pre, b, 10);
msg += (b);
msg += " Pa";
bot.sendMessage(chat_id, msg);
msg = "PRESSIONE ";
itoa(m280_pre, b, 10);
msg += (b);
msg += " Pa";
bot.sendMessage(chat_id, msg);
}
if (text == "/SolT") {
TipoRichiesta = 1;
AllTrama();
}
if (text == "/Foto") {
TipoRichiesta = 2;
AllTrama();
}
if (text == "/Inso") {
TipoRichiesta = 3;
AllTrama();
}
if (text == "/Bat") {
TipoRichiesta = 4;
AllTrama();
}
if (text == "/Tb") {
TipoRichiesta = 5;
AllTrama();
}
int cen = (text[3] - 48) * 100; // estraggo il codice
int dec = (text[4] - 48) * 10;
int uni = text[5] - 48;
text[3] = '0';
text[4] = '0';
text[5] = '0'; // Sostituisco le ultime tre cifre in 000
if (text == "/tx000") {
cod_tx = cen + dec + uni;
itoa(cod_tx, b, 10); msg = (b);
tx_ir();
bot.sendMessage(chat_id, msg);
}
}
void setup()
{
Serial.begin(9600, SERIAL_8N1, 37,35 );//701 meglio di 702 703 peggio
Serial2.begin(701, SERIAL_8N1, RXD1 );//701 meglio di 702 703 peggio
/* Initialize the Display*/
display.begin();
/* Change the contrast using the following API*/
//display.setContrast(contrastValue);
display.clearDisplay();
//display.display();
//delay(10);
/* Now let us display some text */
display.setTextColor(BLACK, WHITE);
//display.setCursor(0, 1);
//display.setTextSize(2);
//display.println("|ESP32|");
display.setTextSize(1);
//display.setTextColor(WHITE, BLACK);
pinMode(led_ir, OUTPUT);
pinMode(RXD1, INPUT); // initialize digital ledPin as an output.
pinMode(Rele3, OUTPUT);
pinMode(Rele4, OUTPUT);
//pinMode(led6, OUTPUT); // initialize digital ledPin as an output.
pinMode(ret2, OUTPUT); // initialize digital ledPin as an output.
pinMode(led15, OUTPUT); //
//delay(10);
digitalWrite(Rele3, LOW); // SPENGO PER SICUREZZA
digitalWrite(Rele4, LOW); // Pigio tasto cancello
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
n_bot++; //conto i risvegli
if (bitRead(n_bot, 0)) {
Wire.begin(13, 9, 100000);
if (bmp.begin()) {
m180_alt = bmp.readAltitude(1013.25);
m180_pre = bmp.readPressure();
m180_tem = bmp.readTemperature();
} else {
display.setCursor(42, 8);
display.print("No bmp180");
display.display();
}
} else {
Wire.begin(11, 9, 100000);
if (bmp2.begin()) {
bmp2.setSampling(Adafruit_BMP280::MODE_FORCED, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
m280_alt = bmp2.readAltitude(1013.25);
m280_pre = bmp2.readPressure();
m280_tem = bmp2.readTemperature();
//aht20_tem = myAHT20.readTemperature();
//aht20_umi = myAHT20.readHumidity();
} else {
display.setCursor(0, 8);
display.print("No bmp280");
display.display();
}
}
display.setCursor(2, 8);
display.print(aht20_tem);
display.println(" *c");
display.setCursor(2, 16);
display.print(aht20_umi);
display.println(" %");
display.setCursor(2, 24);
display.print(m280_pre);
display.println(" Pa");
display.display();
display.setCursor(2, 32);
display.print(m280_tem);
display.println(" *c");
display.setCursor(2, 40);
display.print(m180_tem);
display.println(" *c");
display.display();
//IR RX
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT0) {
x_ir = x_ir_imp; // ricevuto ir lascio acceso
}
int t = 0;
while (WiFi.status() != WL_CONNECTED)
{
if (t > 300) {
display.setCursor(2, 0);
display.println("No Wi-Fi");
display.display();
if (x_ir == 0) {
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR * 10);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_7, LOW);
esp_deep_sleep_start();
}
x = 0;
}
delay(10);
t++;
}
digitalWrite(ret2, 1); // Accendo retroilluminazione display
}
void loop() {
if (x_ir == x_ir_imp) {
IRrecv receiver(pinRxIr); //pin RX IR 1
decode_results results;
receiver.enableIRIn();
while (x_ir > 0) {
x_ir--;
if (receiver.decode(&results)) {
Serial.println(results.value, HEX);
receiver.resume();
x_ir = x_ir_imp; // ricevuto ir lascio acceso
if (results.value == 0x57D22308) {
digitalWrite(15, 1);
}
if (results.value == 0xB3E2CB06) {
digitalWrite(15, 0);
}
}
}
}
if (TipoRichiesta == 1) {
for (rx_n = 0; rx_n < 120; ) {
if (Serial2.available()) {
rx_n++; rx_d = int(Serial2.read());
switch (rx_n) {
case 5 : vcm = rx_d; break;
case 6 : rrm = rx_d; break;
case 7 : msg = "ASC "; Prta(); break;
case 8 : msg += "ASF "; Prtb (); break;
case 9 : msg = "BR5 "; Prta (); break;
case 10 : msg += "BR4 "; Prtb (); break;
case 11 : msg = "BR3 "; Prta (); break;
case 12 : msg += "BR2 "; Prtb (); break;
case 13 : msg = "BR1 "; Prta (); break;
case 14 : msg += "MSO "; Prtb (); break;
case 15 : msg = "PAN "; Prta (); break;
case 16 : msg += "EXT "; Prtb (); break;
case 21 : msg = "APP "; Prta (); break;
case 22 : msg += "APM "; Prtb (); break;
case 23 : bat = rx_d - 29; msg = "BAT "; Prta (); break;
case 24 : msg += "FIN "; Prtb (); break;
case 25 : msg = "RRP "; Prta (); rx_d = rrm; msg += "RRM "; Prtb (); break;
case 26 : msg = "RRX "; Prta (); break;
case 27 : msg += "MRI "; Prtb (); break;
case 28 : msg = "CAM "; Prta (); rx_d = vcm; msg += "VCM "; Prtb (); break;
case 37 : temp = rx_d; break; //potenza pompa solare
case 38 : msg = "Sol.Ter. ";
switch (rx_d) {
case 83 : msg += "STOP Tasto"; Prt1 (); break; //S
case 97 : msg += "Tmax B5"; Prt1 (); break; //a
case 98 : msg += "Tmax B1"; Prt1 (); break; //b
case 99 : msg += "Pan>100°c"; Prt1 (); break; //c
case 100 : msg += "Start"; Prt1 (); break; //d
case 101 : msg += "Stop"; Prt1 (); break; //e
case 102 : msg += "Att Reg"; Prt1 (); break; //f
case 103 : msg += "Com Car Bo"; Prt1 (); break; //g
case 104 : msg += "Acqua Stop"; Prt1 (); break; //h
case 105 : msg += "Surr Pan"; Prt1 (); break; //i
case 106 : msg += "Regolo"; Prt1 (); break; //j
} break;
case 39 : msg = "Risc. ";
switch (rx_d) {
case 0 : msg += "STOP"; break;
case 1 : msg += "Cam"; break;
case 2 : msg += "Car Bo Cam"; break;
case 4 : msg += "Car Bo Cal"; break;
case 8 : msg += "Svuota Cam"; break;
case 16 : msg += "Solare"; break;
case 32 : msg += "Cal"; break;
case 64 : msg += "Svu Cal Bo"; break;
case 128 : msg += "Svu Cal Ri"; break;
} break;
case 41 : itoa(rx_d, b, 10); msg += " da min "; msg += (b); bot.sendMessage(chat_id, msg); break; //da min "xx"
case 54 :
case 52 : temp = rx_d; break;
case 53 : itoa(temp, b, 10); msg = "Tv_Vc "; msg += (b); msg += " h "; //tempo ventola vano camino
itoa(rx_d, b, 10); msg += "Tps "; msg += (b); msg += "0 min"; //tempo funzionamento pompa solare
bot.sendMessage(chat_id, msg); break;
case 55 : itoa(temp, b, 10); msg = "Tpcal "; msg += (b); msg += " h "; //tempo pompa caldaia
itoa(rx_d, b, 10); msg += "Tpbo "; msg += (b); msg += " h"; //tempo pompa boiler
bot.sendMessage(chat_id, msg); break;
case 56 : itoa(rx_d, b, 10); msg = "Tpcam "; msg += (b); msg += " h"; //tempo pompa camino
bot.sendMessage(chat_id, msg); break;
case 51 : //posizione selettore riscaldamento
if (bitRead(rx_d, 4)) {
msg = "PS Abil. ";
} else {
msg = "PS Dis. ";
}
if (bitRead(rx_d, 3)) {
msg += "Risc. ON ";
} else {
msg += "Risc. OFF ";
}
bitWrite(rx_d, 3, 0); bitWrite(rx_d, 4, 0);
bitWrite(rx_d, 5, 0); bitWrite(rx_d, 6, 0); bitWrite(rx_d, 7, 0);
msg += "Sel. ";
switch (rx_d) {
case 0 : msg += "OFF"; break;
case 1 : msg += "San."; break;
case 2 : msg += "Auto"; break;
case 3 : msg += "ON-2"; break;
case 4 : msg += "ON 0"; break;
case 5 : msg += "ON+2"; break;
case 6 : msg += "ON+4"; break;
case 7 : msg += "Man. San. OFF"; break;
}
bot.sendMessage(chat_id, msg);
// itoa(rx_d, b, 10); msg = (b); bot.sendMessage(chat_id, msg); break;
case 120 : TipoRichiesta = 0;
}
}
}
}
if (TipoRichiesta == 2) {
for (rx_n = 0; rx_n < 120; ) {
if (Serial2.available()) {
rx_n++; rx_d = int(Serial2.read());
switch (rx_n) {
case 42 :
case 44 : temp = rx_d * 256; break; // rx_dH*256+rx_dL
case 43 : temp = rx_d + temp; msg = "≈< Wh +"; Prt3 (); break; //≈< Wh+ 2437
case 45 : temp = rx_d + temp; msg = "≈> Wh -"; Prt3 (); break; //≈> Wh- 2437
case 81 : case 69 :
case 57 : temp = rx_d; break; // registro rx_d
case 58 : temp = rx_d * 256 + temp; msg = "≈# Wh +"; Prt3 (); break; //≈# Wh 2437
case 70 : if (rx_d >= 128) {
temp = (rx_d - 128) * 256 + temp; msg = "+++ ≈ W +"; Prt3();
} else {
temp = rx_d * 256 + temp; msg = "--- ≈ W -"; Prt3();
} ; break;
case 82 : temp = rx_d * 256 + temp; msg = "≈#Fot W +"; Prt3(); break; //≈Wfot 2437
case 120 : TipoRichiesta = 0;
}
}
}
}
if (TipoRichiesta == 3) {
for (rx_n = 0; rx_n < 120; ) {
if (Serial2.available()) {
rx_n++; rx_d = int(Serial2.read());
switch (rx_n) {
case 23 : bat = rx_d - 29;
case 100 : case 98 : case 96 : case 94 : case 92 : case 79 : case 77 :
case 75 : case 73 : case 71 : case 67 : case 65 : case 63 : case 61 :
case 59 : temp = rx_d * 256; break; // rx_dH*256+rx_dL
case 60 : whs3 = (rx_d + temp) / 10.0; break;
case 62 : whs2 = (rx_d + temp) / 10.0; break;
case 64 : whs1 = (rx_d + temp) / 10.0; break;
case 66 : wh220 = (rx_d + temp) / 10.0; break;
case 68 : whcs = (rx_d + temp) / 10.0; break;
case 72 : ws3 = (rx_d + temp) / 100.0; break;
case 74 : ws2 = (rx_d + temp) / 100.0; break;
case 76 : ws1 = (rx_d + temp) / 100.0; break;
case 78 : w220 = (rx_d + temp) / 100.0; break;
case 80 : wcs = (rx_d + temp) / 100.0; break;
case 93 : is3 = (rx_d + temp) * 10; break;
case 95 : is2 = (rx_d + temp) * 10; break;
case 97 : is1 = (rx_d + temp) * 10; break;
case 99 : i220 = (rx_d + temp) * 10; break;
case 101 : ics = (rx_d + temp) * 10; break;
case 103 : ind_dl = rx_d ; break;
case 113 : vbat = rx_d / 10.0 ; break;
case 120 :
is = is1 + is2 + is3;
ic = ics + i220;
ib = ic - is;
ws = ws1 + ws2 + ws3;
wc = wcs + w220;
wb = wc - ws;
whs = whs1 + whs2 + whs3;
whc = whcs + wh220;
whb = whc - whs;
// fotovoltaico 12v
dtostrf(vbat, 4, 1, b); msg = (b); msg += "V Ib "; itoa(ib, b, 10); msg += (b); msg += "mA "; itoa(bat, b, 10); msg += (b); msg += "°C\n";
msg += "Is1 -"; itoa(is1, b, 10); msg += (b); msg += "mA Is2 -"; itoa(is2, b, 10); msg += (b); msg += "mA Is3 -"; itoa(is3, b, 10); msg += (b); msg += "mA\n";
msg += "Ics "; itoa(ics, b, 10); msg += (b); msg += "mA Icr "; itoa(i220, b, 10); msg += (b); msg += "mA";
bot.sendMessage(chat_id, msg);
dtostrf(wb, 3, 2, b); msg = (b); msg += "Wb "; dtostrf(wcs, 3, 2, b); msg += (b); msg += "Wcs "; dtostrf(w220, 3, 2, b); msg += (b); msg += "Wcr\n";
dtostrf(ws1, 3, 2, b); msg += (b); msg += "Ws1 -"; dtostrf(ws2, 3, 2, b); msg += (b); msg += "Ws2 -"; dtostrf(ws3, 3, 2, b); msg += (b); msg += "Ws3\n";
bot.sendMessage(chat_id, msg);
dtostrf(whb, 3, 1, b); msg = (b); msg += "Whb "; dtostrf(whcs, 3, 1, b); msg += (b); msg += "Whcs "; dtostrf(wh220, 3, 1, b); msg += (b); msg += "Whcr\n";
dtostrf(whs1, 3, 1, b); msg += (b); msg += "Whs1 -"; dtostrf(whs2, 3, 1, b); msg += (b); msg += "Whs2 -"; dtostrf(whs3, 3, 1, b); msg += (b); msg += "Whs3\n";
bot.sendMessage(chat_id, msg);
TipoRichiesta = 0;
}
}
}
}
if (TipoRichiesta == 4) {
for (rx_n = 0; rx_n < 120; ) {
if (Serial2.available()) {
rx_n++; rx_d = int(Serial2.read());
switch (rx_n) {
case 23 : bat = rx_d - 29;
case 51 : comSolT = rx_d; break;
case 100 : case 98 : case 96 : case 94 : case 92 : case 79 : case 77 :
case 75 : case 73 : case 71 : case 67 : case 65 : case 63 : case 61 :
case 59 : temp = rx_d * 256; break; // rx_dH*256+rx_dL
case 93 : is3 = (rx_d + temp) * 10; break;
case 95 : is2 = (rx_d + temp) * 10; break;
case 97 : is1 = (rx_d + temp) * 10; break;
case 99 : i220 = (rx_d + temp) * 10; break;
case 101 : ics = (rx_d + temp) * 10; break;
case 103 : ind_dl = rx_d ; break;
case 104 : (temp = rx_d); break; //DH
case 105 : //DL
switch (ind_dl) {
case 9 : ahbat = (temp * 256 + rx_d) / 100.0; dtostrf(ahbat, 4, 2, b);
msg = "Bat "; msg += (b); msg += "Ah "; itoa(bat, b, 10); ; msg += (b); msg += " °c ";
bot.sendMessage(chat_id, msg); break;
case 1 : itoa(rx_d, b, 10); msg = "Pk Ric min "; msg += (b);
bot.sendMessage(chat_id, msg);
is = is1 + is2 + is3;
ic = ics + i220;
ib = ic - is;
dtostrf(vbat, 4, 1, b); msg = (b); msg += "V "; itoa(ib, b, 10); msg += (b); msg += "mA";
bot.sendMessage(chat_id, msg);
} break;
case 113 : vbat = rx_d / 10.0 ; break;
case 120 : c_dl++;
if (c_dl == 16) {
TipoRichiesta = 0;
bot.sendMessage(chat_id, "Fine");
c_dl = 0;
}
}
}
}
}
if (TipoRichiesta == 5) {
for (rx_n = 0; rx_n < 120; ) {
if (Serial2.available()) {
rx_n++; rx_d = int(Serial2.read());
switch (rx_n) {
case 9 : msg = "BR5 "; Prta (); break;
case 16 : msg += "EXT "; Prtb (); break;
case 120 : TipoRichiesta = 0;
}
}
}
}
if (millis() - bot_lasttime > BOT_MTBS)
{
if (x != 0) //se nuovo messaggio rimango nel ciclo x volte senza spegnere wifi
{
x--;
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
if (numNewMessages != 0)
{
NewMes(numNewMessages);
digitalWrite(Rele3, LOW); // Rilascio tasto cancello sempre
digitalWrite(Rele4, LOW); // Rilascio tasto cancello sempre
x = 10;
BOT_MTBS = 4000;
}
if (tac > 0) //ogni tac impiega circa 2,5 sec
{
tac-- ;
if (tac == 7) {
bot.sendMessage(chat_id, "Aperto 55 Sec", "");
}
if (tac == 2) {
bot.sendMessage(chat_id, "10 Sec", "");
}
if (tac == 1)
{
bot.sendMessage(chat_id, " Chiudo Cancello", "");
}
}
}
bot_lasttime = millis();
if (x == 1) {
bot.sendMessage(chat_id, "Ciao");
}
if (x == 0)
{
TipoRichiesta = 0;
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_7, LOW);
esp_deep_sleep_start();
}
}
}Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1