Lab_interaccio/2021/mireia/mireia.ino

83 lines
2.3 KiB
Arduino
Raw Permalink Normal View History

2025-02-25 21:29:42 +01:00
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <HTTPUpdateServer.h>
#define TIME_SORTIDA 8000 //ms
#define TIME_ESPERA 40000 //ms
// Set these to your desired credentials.
const char *ssid = "Som_pols";
const char *password = "saladrigues";
const char* host = "esp32-webupdate";
WebServer httpServer(80);
HTTPUpdateServer httpUpdater;
#define R1 13
#define R2 27
#define SENSOR A0
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Serial.begin(115200);
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(SENSOR, INPUT_PULLUP);
digitalWrite(R1, LOW);
digitalWrite(R2, LOW);
Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
httpUpdater.setup(&httpServer);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", host);
}
unsigned long time_stop = millis();
boolean sensor_active = false;
void loop() {
httpServer.handleClient();
if ((digitalRead(SENSOR))&&(!sensor_active))
{
sensor_active = true;
Serial.println("Entrada activada");
}
else if ((sensor_active)&&((millis()-time_stop)<TIME_SORTIDA))
{
digitalWrite(R1, HIGH);
digitalWrite(R2, HIGH);
Serial.println("Salida activada");
}
else if ((sensor_active)&&((millis()-time_stop)<(TIME_ESPERA + TIME_SORTIDA)))
{
digitalWrite(R1, LOW);
Serial.println("Salida desactivada");
}
else
{
digitalWrite(R2, LOW);
time_stop = millis();
sensor_active = false;
}
// if ((millis()-time_stop)<60000) digitalWrite(R1, LOW);
// else if ((millis()-time_stop)<(60000 + 3000)) digitalWrite(R1, HIGH);
// else time_stop = millis();
// digitalWrite(R1, LOW); // wait for a second
// for(int i=0; i<60; i++) delay(1000);
// digitalWrite(R1, HIGH); // turn the LED on (HIGH is the voltage level)
// delay(3000); // wait for a second
}