Ciao a tutti, ho un problema di visualizzazione sul secondo oled (0x3D "display2").
Quando si attiva la funzione "if (digitalRead(scossa) == HIGH && allarme == true)"
o la funzione " if (digitalRead(sensore) == HIGH && allarme == true)",
il display2 rimanda il print in sincronia con l'intervallo del high/low dei led.
Vorrei che il print del display2 fosse fisso e non in blink .
essendo dentro alla funzione if, non ho la più pallida idea di come renderlo indipendente dallo stato di essa per avere un messaggio nel display2 fisso.
Come al solito le mie richieste sono spiegate da caprone, sorry.
Denkiu
modifico il codice eliminando i delay sulle 2 funzioni if
//========================================================
//ESP32 Web Server: Reading Potentiometer Value using AJAX
//========================================================
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include "webpage.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSerif9pt7b.h>
//-------------------------------------------OLED----------------
const unsigned char cri[1024] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xfc, 0x00, 0x00, 0x7f, 0x80, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0xff,
0xfc, 0x00, 0x00, 0x7f, 0x80, 0x00, 0xff, 0xff, 0xfb, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0xff,
0xfc, 0x00, 0x00, 0xff, 0xc0, 0x01, 0xff, 0xff, 0xf3, 0xff, 0xff, 0x80, 0x00, 0x00, 0x01, 0xff,
0xff, 0x80, 0x03, 0xff, 0xf0, 0x07, 0xff, 0xff, 0xf3, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x01, 0xff,
0xff, 0xe0, 0x07, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xe1, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0xc1, 0xff,
0xff, 0xf0, 0x07, 0xff, 0xf8, 0x3f, 0xff, 0xff, 0xe1, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0xe1, 0xff,
0xff, 0xf8, 0x07, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xe1, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0xf1, 0xff,
0xff, 0xfc, 0x07, 0xff, 0xf0, 0x7f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0xf9, 0xff,
0xff, 0xfc, 0x03, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0xf9, 0xff,
0xff, 0xfe, 0x01, 0xff, 0xe1, 0xff, 0xff, 0xff, 0xc0, 0x7f, 0xff, 0xf8, 0x0f, 0xff, 0xf8, 0xff,
0xff, 0xff, 0x01, 0xff, 0xe3, 0xff, 0xff, 0xff, 0x80, 0x7f, 0xff, 0xf8, 0x0f, 0xff, 0xfc, 0xff,
0xff, 0xff, 0x01, 0xff, 0xc3, 0xff, 0xff, 0xff, 0x80, 0x7f, 0xff, 0xf8, 0x0f, 0xff, 0xfc, 0xff,
0xff, 0xff, 0x80, 0xff, 0xc7, 0xff, 0xff, 0xff, 0x80, 0x7f, 0xff, 0xf8, 0x0f, 0xff, 0xff, 0xff,
0xff, 0xff, 0x80, 0xff, 0x87, 0xff, 0xff, 0xff, 0x00, 0x3f, 0xff, 0xf8, 0x0f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xc0, 0x7f, 0x0f, 0xff, 0xff, 0xff, 0x00, 0x3f, 0xff, 0xf8, 0x0f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xc0, 0x7f, 0x0f, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0xff, 0xf8, 0x0f, 0xff, 0xef, 0xff,
0xff, 0xff, 0xe0, 0x3e, 0x1f, 0xff, 0xff, 0xfe, 0x30, 0x1f, 0xff, 0xf8, 0x0f, 0xff, 0xef, 0xff,
0xff, 0xff, 0xe0, 0x1c, 0x3f, 0xff, 0xff, 0xfe, 0x30, 0x1f, 0xff, 0xf8, 0x0f, 0xff, 0xcf, 0xff,
0xff, 0xff, 0xf0, 0x1c, 0x3f, 0xff, 0xff, 0xfc, 0x38, 0x1f, 0xff, 0xf8, 0x0f, 0xff, 0xcf, 0xff,
0xff, 0xff, 0xf0, 0x08, 0x7f, 0xff, 0xff, 0xfc, 0x78, 0x0f, 0xff, 0xf8, 0x0f, 0xff, 0xcf, 0xff,
0xff, 0xff, 0xf8, 0x00, 0x7f, 0xff, 0xff, 0xfc, 0x78, 0x0f, 0xff, 0xf8, 0x0f, 0xff, 0xcf, 0xff,
0xff, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xf8, 0x7c, 0x0f, 0xff, 0xf8, 0x0f, 0xff, 0x8f, 0xff,
0xff, 0xff, 0xfc, 0x01, 0xff, 0xff, 0xff, 0xf8, 0xfc, 0x07, 0xff, 0xf8, 0x0f, 0xff, 0x0f, 0xff,
0xff, 0xff, 0xfc, 0x01, 0xff, 0xff, 0xff, 0xf8, 0xfc, 0x07, 0xff, 0xf8, 0x00, 0x00, 0x0f, 0xff,
0xff, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xf0, 0xfe, 0x07, 0xff, 0xf8, 0x00, 0x00, 0x0f, 0xff,
0xff, 0xff, 0xfe, 0x01, 0xff, 0xff, 0xff, 0xf1, 0xfe, 0x03, 0xff, 0xf8, 0x00, 0x00, 0x0f, 0xff,
0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0xf1, 0xfe, 0x03, 0xff, 0xf8, 0x00, 0x00, 0x0f, 0xff,
0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xe1, 0xfe, 0x03, 0xff, 0xf8, 0x0f, 0xfe, 0x0f, 0xff,
0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xe3, 0xff, 0x01, 0xff, 0xf8, 0x0f, 0xff, 0x0f, 0xff,
0xff, 0xff, 0xfe, 0x00, 0x7f, 0xff, 0xff, 0xc3, 0xff, 0x01, 0xff, 0xf8, 0x0f, 0xff, 0x8f, 0xff,
0xff, 0xff, 0xfc, 0x00, 0x7f, 0xff, 0xff, 0xc7, 0xff, 0x01, 0xff, 0xf8, 0x0f, 0xff, 0xcf, 0xff,
0xff, 0xff, 0xfc, 0x00, 0x3f, 0xff, 0xff, 0xc7, 0xff, 0x81, 0xff, 0xf8, 0x0f, 0xff, 0xcf, 0xff,
0xff, 0xff, 0xf8, 0x60, 0x3f, 0xff, 0xff, 0x87, 0xff, 0x80, 0xff, 0xf8, 0x0f, 0xff, 0xcf, 0xff,
0xff, 0xff, 0xf8, 0x70, 0x1f, 0xff, 0xff, 0x8f, 0xff, 0x80, 0xff, 0xf8, 0x0f, 0xff, 0xcf, 0xff,
0xff, 0xff, 0xf0, 0xf0, 0x1f, 0xff, 0xff, 0x8f, 0xff, 0xc0, 0x7f, 0xf8, 0x0f, 0xff, 0xcf, 0xff,
0xff, 0xff, 0xf1, 0xf8, 0x0f, 0xff, 0xff, 0x0f, 0xff, 0xc0, 0x7f, 0xf8, 0x0f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xe1, 0xf8, 0x0f, 0xff, 0xff, 0x1f, 0xff, 0xc0, 0x7f, 0xf8, 0x0f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xc3, 0xfc, 0x07, 0xff, 0xff, 0x1f, 0xff, 0xe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0, 0xff, 0xff, 0xe0, 0x0f, 0xe1, 0xff, 0xff, 0xfe, 0x07, 0xf8, 0x07, 0xff, 0xc0, 0x7f,
0xff, 0x00, 0x7f, 0xff, 0xc0, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x00, 0x00, 0x00, 0xff,
0xfc, 0x00, 0x1f, 0xff, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0xff,
0xf8, 0x00, 0x0f, 0xfe, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff,
0xfc, 0x00, 0x1f, 0xfe, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_ADDR1 0x3C
#define OLED_ADDR2 0x3D
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_ADDR1);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_ADDR2);
//---------------------------------------------------
WebServer server(80);
const char* ssid = "ssid";
const char* password = "pws";
//---------------------------------------------------
IPAddress ip(192, 168, 192, 251); // Imposta l'indirizzo IP statico desiderato
IPAddress gateway(192, 168, 192, 1);
IPAddress subnet(255, 255, 255, 0);
//-----ip statico fine
#include "handleFunctions.h"
#define ledv 17
#define led 18
#define ledr 16
#define ledv 17
#define ledb 19
#define attiny 4
#define buzzer 5
int scossa = 15;
int sensore = 23;
bool allarme = false;
int statusPir = LOW;
int statusscossa = LOW;
int lampeggi = 5;
unsigned long previousMillis = 0; // variabile per memorizzare il tempo dell'ultimo cambiamento di stato
const long interval = 50; // intervallo desiderato tra i cambi di stato del LED (in millisecondi)
//----------------------------------------------------------
void (*Riavvia)(void) = 0; //reset ARDUINO
void lampu2() {
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(500);
tone(buzzer, 13, 200);
}
//===================================================
void setup() {
Serial.begin(115200);
//-----------OLED--------------
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
if (!display2.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR2)) {
Serial.println(F("Display 2 non rilevato"));
while (true)
;
}
display.clearDisplay();
display2.clearDisplay();
display2.drawBitmap(0, 0, cri, 128, 64, WHITE);
display2.display();
delay(1000);
//-----------fine OLED------------
pinMode(ledv, OUTPUT);
pinMode(led, OUTPUT);
pinMode(ledr, OUTPUT);
pinMode(ledv, OUTPUT);
pinMode(ledb, OUTPUT);
pinMode(attiny, OUTPUT);
pinMode(buzzer, OUTPUT);
//-------------------------------------------------
// WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
if (WiFi.config(ip, gateway, subnet)) {
Serial.println("Configurazione IP statico avvenuta con successo.");
} else {
Serial.println("Errore durante la configurazione IP statico.");
}
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connessione al WiFi in corso...");
}
Serial.println("Connesso al WiFi!");
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
///-------------------------------------------------
server.on("/", handleRoot);
server.on("/readPOT", handlePOT);
server.on("/readPOT2", handlePOT2);
server.on("/readPOT3", handlePOT3);
server.begin();
Serial.println("HTTP server started");
server.on("/", handleRoot);
server.on("/readPOT", handlePOT);
server.on("/readPOT2", handlePOT2);
server.on("/readPOT3", handlePOT3);
server.on("/string", handleString);
server.begin();
digitalWrite(ledr, HIGH);
digitalWrite(ledv, HIGH);
digitalWrite(led, HIGH);
digitalWrite(ledb, HIGH);
tone(buzzer, 4186, 300);
delay(200);
digitalWrite(ledr, LOW);
digitalWrite(ledv, LOW);
digitalWrite(ledb, LOW);
}
//===================================================
void loop() {
unsigned long currentMillis = millis(); // ottenere il tempo attuale
server.handleClient();
if (digitalRead(scossa) == HIGH && allarme == true) //rilevo movimento con allarme in on
{
tone(buzzer, 4186, 300);
digitalWrite(attiny, HIGH);
if (currentMillis - previousMillis >= interval * 6) {
previousMillis = currentMillis; // memorizzare il tempo dell'ultimo cambiamento di stato
for (int i = 0; i < 6; i++) {
digitalWrite(ledr, HIGH);
unsigned long startMillis = millis();
while (millis() - startMillis < 50) {
// Attendere fino a quando non sono passati 50 millisecondi
}
digitalWrite(ledr, LOW);
startMillis = millis();
while (millis() - startMillis < 50) {
// Attendere fino a quando non sono passati 50 millisecondi
}
}
Serial.println(" ");
Serial.println("Rilevato MOVIMENTO sensore 1 !");
display2.clearDisplay();
display2.setCursor(0, 15);
display2.setTextSize(2);
display2.setTextColor(WHITE);
display2.println(" RILEVATO");
display2.println(" SCOSSA!");
display2.display();
}
}
if (digitalRead(sensore) == HIGH && allarme == true) //rilevo movimento con allarme in on
{
tone(buzzer, 4186, 300);
digitalWrite(attiny, HIGH);
if (currentMillis - previousMillis >= interval * 6) {
previousMillis = currentMillis; // memorizzare il tempo dell'ultimo cambiamento di stato
for (int i = 0; i < 6; i++) {
digitalWrite(ledr, HIGH);
unsigned long startMillis = millis();
while (millis() - startMillis < 50) {
// Attendere fino a quando non sono passati 50 millisecondi
}
digitalWrite(ledr, LOW);
startMillis = millis();
while (millis() - startMillis < 50) {
// Attendere fino a quando non sono passati 50 millisecondi
}
}
display2.clearDisplay();
display2.setCursor(0, 15);
display2.setTextSize(2);
display2.setTextColor(WHITE);
display2.println(" RILEVATO");
display2.println("MOVIMENTO!");
display2.display();
Serial.println(" ");
Serial.println("Rilevato MOVIMENTO sensore PIR !");
}
}
if (digitalRead(ledb) == HIGH) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(" WebAllarmino by Cri ");
display.setTextSize(2);
display.setCursor(0, 15);
display.println(" ALLARME");
display.setTextColor(BLACK, WHITE);
display.println(" ON ");
display.display();
}
if (digitalRead(ledb) == LOW) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
// Display static text
display.println(" WebAllarmino by Cri ");
display.setTextSize(2);
display.setCursor(0, 15);
// Display static text
display.println(" ALLARME");
display.println(" OFF");
display.display();
} else {
digitalWrite(ledr, LOW);
digitalWrite(attiny, LOW);
noTone(buzzer);
}
if (digitalRead(attiny) == LOW) {
display2.clearDisplay();
display2.display();
}
}
void handleString() {
// Controllo che il browser abbia inviato il parametro che mi aspetto
if (server.hasArg("val")) {
// Recupero il valore del parametro
String valore = server.arg("val");
// Ci faccio quello che voglio
if (valore == "PR#6") {
Serial.println("Attivazione ALLARME");
for (byte i = 0; i < lampeggi; i++) {
// WebSerial.print(".");
Serial.print(".");
// tone(buzzer, 13, 1700);
// WebSerial.println(" ");
Serial.println(" ");
lampu2();
}
digitalWrite(led, LOW);
digitalWrite(ledv, LOW);
digitalWrite(ledb, HIGH);
Serial.println(" ");
Serial.println("ALLARME ATTIVATO");
Serial.println(" ");
allarme = true;
String risposta1 = "Allarme attivo";
// risposta += valore;
// risposta += "' eseguito";
Serial.println(risposta1);
// Mando la risposta al browser
server.send(200, "text/html", risposta1);
}
if (valore == "PR#7") {
allarme = false;
//tone(buzzer, 13, 1700);
digitalWrite(ledr, LOW);
digitalWrite(buzzer, LOW);
digitalWrite(ledv, HIGH);
digitalWrite(ledb, LOW);
Serial.println(" ");
Serial.println("ALLARME DISATTIVATO");
Serial.println(" ");
String risposta2 = "Allarme disattivato";
// risposta += valore;
// risposta += "' eseguito";
Serial.println(risposta2);
// Mando la risposta al browser
server.send(200, "text/html", risposta2);
}
if (valore == "RESET") {
//tone(buzzer, 13, 1700);
digitalWrite(ledr, LOW);
digitalWrite(buzzer, LOW);
digitalWrite(ledv, HIGH);
digitalWrite(ledb, LOW);
Serial.println(" ");
Serial.println("RESET ESEGUITO");
Serial.println(" ");
Riavvia();
}
String risposta = "Comando eseguito";
// risposta += valore;
// risposta += "' eseguito";
Serial.println(risposta);
// Mando la risposta al browser
server.send(200, "text/html", risposta);
} else
server.send(500, "text/html", "Richiesta GET non corretta");
}