Hola. En primer lugar, pedir disculpas si no he posteado como se supone que debería haberlo hecho.
Mis conocimientos de programación son muy limitados, y básicamente me busco la vida haciendo corta-pega para ajustar los programas ya existentes a mis necesidades. Este es uno de esos casos. En un primer momento, tenía 2 placas de Arduino para realizar dos tareas distintas. Cada una por separado cumplían perfectamente su función. Pero ahora, para ahorrarme una placa, un adaptador de corriente, etc. quiero unificarlo todo en un mismo sketch. He visto y leído tutoriales en Internet y he conseguido hacerlo funcionar, salvo ese detalle que comentaba.
Adjunto la primera parte del código de programación (completo, con las aclaraciones que voy añadiendo en los programas, ocupa más caracteres de los permitidos para publicarlo).
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <RCSwitch.h>
LiquidCrystal_I2C lcd(0x3F,16,2);
RCSwitch mySwitch = RCSwitch();
//------------------------------------------------------------------------------
char* secretCode = "XXXX"; // PIN.
char* secretCode1 = "XXXX"; // PIN2 ver datos intrusion y alarmas.
char* secretCode2 = "XXXX"; // PIN3 borrar datos.
char* secretCode3 = "XXXX"; // PUK.
String keydata;
int position = 0;
const byte rows = 4;
const byte cols = 4;
char keys[rows][cols] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[rows] = {12, 11, 10, 9};
byte colPins[cols] = {8, 7, 6, 5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
byte switchs = A4;
byte pir = A3;
byte leds = A1;
byte sirena = A2;
byte buzzer = 13;
byte estado = 0;
byte cntPul = 0;
byte cntInt = 0;
char recibirDato;
long tiempo;
char key;
boolean claveBien = false;
boolean claveMal = false;
String versi = "09-01-2017 20:52";
int x = 0, mrest, luz = 0, secs, srest, timeluz = 10000, twatch = 10000;
unsigned long watchmillis, watchluz, rearmado = 60000;
int sube = 1, trig1 = 0, trig2 = 0; long value =0;
//Para activar y desactivar el sensor PIR y el relé de la sirena y leer la temperatura:
#include <SPI.h>
#include <Ethernet.h> //Declaración de la direcciones MAC e IP.
byte mac[]={0xDE,0xAD,0xBE,0xEF,0xFE,0xED}; //MAC
IPAddress ip(192, 168, X, XX); //IP
EthernetServer servidor(XX
); //Puerto
int PIN_LED=23; // Activa/Desactiva el relé de la sirena.
int PIN_PIR=22; // Activa/Desactiva el sensor de movimiento PIR.
String readString=String(30); //lee los caracteres de una secuencia en una cadena.
//Los strings se representan como arrays de caracteres (tipo char)
String state1=String(3);
String state2=String(3);
const int sensorTemp= A0;
//----------------------------------------------------------------------------------------
void setup()
{
{
Serial.begin(9600);
delay(200);
lcd.begin(16, 2);
Serial.println("Inicio:");
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin 2
delay(50);
pinMode(pir, INPUT);
pinMode(switchs, INPUT);
pinMode(leds, OUTPUT);
pinMode(sirena, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.print("Alarma aun desactivada. Pulsar PIN para conectarla.");
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("BIENVENIDO"); // Mensaje a despegar
delay(3000);
watchluz = millis();
}
{
Ethernet.begin(mac, ip);
servidor.begin();
pinMode(PIN_LED,OUTPUT);
digitalWrite(PIN_LED,LOW);
state1="OFF";
pinMode(PIN_PIR,OUTPUT);
digitalWrite(PIN_PIR,HIGH);
state2="ON";
}
}// end setup
//------------------------------------------------------------------------------
void loop(){
int value = analogRead(sensorTemp);
float millivolts = ((value / 1023.0) * 5000)-25;
float celsius = millivolts / 10;
Serial.print(celsius);
Serial.println("C");
delay(1000);
//EthernetClient Crea un cliente que se puede conectar a
//una dirección específica de Internet IP
EthernetClient cliente= servidor.available();
if(cliente) {
boolean lineaenblanco=true;
while(cliente.connected()) {
if(cliente.available()) {
char c=cliente.read();
if(readString.length()<30) {
readString.concat(c);
//Cliente conectado
//Leemos petición HTTP caracter a caracter
//Almacenar los caracteres en la variable readString
}
if(c=='\n' && lineaenblanco) //Si la petición HTTP ha finalizado
{
int LED = readString.indexOf("LED=");
if(readString.substring(LED,LED+5)=="LED=T") {
digitalWrite(PIN_LED,HIGH);
state1="ON"; }
else if (readString.substring(LED,LED+5)=="LED=F") {
digitalWrite(PIN_LED,LOW);
state1="OFF";
}
int PIR = readString.indexOf("PIR=");
if(readString.substring(PIR,PIR+5)=="PIR=T") {
digitalWrite(PIN_PIR,HIGH);
state2="ON"; }
else if (readString.substring(PIR,PIR+5)=="PIR=F") {
digitalWrite(PIN_PIR,LOW);
state2="OFF";
}
//Cabecera HTTP estándar
cliente.println("HTTP/1.1 200 OK");
cliente.println("Content-Type: text/html");
cliente.println(); //Página Web en HTML
cliente.println("<html>");
cliente.println("<head>");
cliente.println("<title>ALARMA ON/OFF</title>");
cliente.println("</head>");
cliente.println("<body width=100% height=100%>");
cliente.println("<center>");
cliente.println("<h1>ALARMA ON/OFF</h1>");
cliente.print("
");
cliente.print("Estado del sensor de movimiento: ");
cliente.print(state2);
cliente.print("
");
cliente.println("<input type=submit value=ON style=width:200px;height:75px onClick=location.href='./?PIR=T\'>");
cliente.println("<input type=submit value=OFF style=width:200px;height:75px onClick=location.href='./?PIR=F\'>");
cliente.print("
");
cliente.print("Estado de la sirena: ");
cliente.print(state1);
cliente.print("
");
cliente.println("<input type=submit value=ON style=width:200px;height:75px onClick=location.href='./?LED=T\'>");
cliente.println("<input type=submit value=OFF style=width:200px;height:75px onClick=location.href='./?LED=F\'>");
cliente.print("
");
cliente.print("Temperatura: ");
cliente.print(celsius);
cliente.print(" ºC");
cliente.println("</center>");
cliente.println("</body>");
cliente.println("</html>");
cliente.stop();
//Cierro conexión con el cliente
readString="";
}
}
}
}
/* ESTADO 0
while (estado == 0){
if (watchluz + timeluz <= millis() && luz == 1){
lcd.setBacklight(0);
luz = 0;
}
puertoSerie();
digitalWrite(leds, LOW);
digitalWrite(sirena, HIGH);
digitalWrite(buzzer, LOW);
lcd.setCursor(0, 0);
lcd.print("Alarma: O F F ");
lcd.setCursor(0, 1);
lcd.print("PIN para Activar");
clavePin2();
value = 0;
remote();
if (keydata == secretCode1 || value == 16776966){
lcd.clear();
beep();
lcd.setBacklight(1);
lcd.setCursor(0,0);
lcd.print(" Datos grabados ");
delay(1000);
lcd.setCursor(0,0);
lcd.print("Intrusiones: ");
lcd.setCursor(12,0);
lcd.print(trig1);
lcd.setCursor(0,1);
lcd.print(" Alarmas: ");
lcd.setCursor(12,1);
lcd.print(trig2);
delay(5000);
keydata = "";
value = 0;
watchluz = millis();
lcd.clear();
claveBien = false;
}
if (keydata == secretCode2){
lcd.clear();
beep();
lcd.setCursor(0,0);
lcd.print("Datos-> BORRADOS");
trig1 = 0;
trig2 = 0;
keydata = "";
value = 0;
delay(1000);
watchluz = millis();
lcd.clear();
claveBien = false;
}
if (value == 5592512){
lcd.clear();
beep();
estado = 4;
watchmillis = millis();
watchluz = millis();
lcd.clear();
luz = 1;
keydata = "";
value = 0;
trig2++;
claveBien = false;
Serial.print("4");
}
if (claveBien || value == 16776967){
keydata = "";
estado = 1;
beep();
lcd.clear();
value = 0;
lcd.setBacklight(1);
watchmillis = millis();
watchluz = millis();
Serial.print("Alarma conectada.");
claveBien = false;
}
else if (claveMal){
estado = 5;
beep();
lcd.clear();
lcd.setBacklight(1);
watchmillis = millis();
watchluz = millis();
luz = 1;
keydata = "";
Serial.print("5");
claveMal = false;
}
} // end while estado0
//----------------------------------------------
La parte que quisiera que se repitiera constantemente es la que comienza justo debajo del void loop hasta que empieza el "ESTADO 0" (lectura de temperatura y monitorización y control de sensores y relé a través de internet).
Gracias.