Al final lo he desarrollado, os paso aquí el código por si le hace falta a alguien.
El INPUT es un código que se digita en un teclado 4x4, que va conectado a un NodeMCU Lua V3 ESP8266.
Este código se busca en una base de datos usando un GET a un fichero PHP, que realiza la query y si lo encuentra en la base de datos devuelve un "1".
Una vez leído el 1, el NodeMCU va conectado por el pin VU a un relé, que acciona la puerta
// 0 ES EL CARACTER DE RESET. TODAS LAS PASSWORD EMPIEZAN POR 0 Y NINGUNA CONTIENE 0
//LO DEL RELE ES EL CONTRARIO, LOW LO ACTIVA
//EL NODEMCU COGE LOS 5V DEL VU
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <Keypad.h>
int Rele = 15;
///WIFI///////
const char *ssid = "XXXXXXXX"; //ENTER YOUR WIFI SETTINGS
const char *password = "XXXXXXX";
const char *host = "miweb.com";
const int httpsPort = 443; //HTTPS= 443 and HTTP = 80
//certificado de miweb.com ya que es un https
const char fingerprint[] PROGMEM = "XX XX XX XX XX etc";
/////TECLADO
char codigo[6]; //Cadna donde se guardaran los caracteres de las teclas presionadas
int cont=0; //variable que se incrementara al presionar las teclas
const byte ROWS = 4; //Numero de filas del teclado que se esta usando
const byte COLS = 4; //Numero de columnas del teclado que se esta usando
char hexaKeys[ROWS][COLS] = //Aquí pondremos la disposición de los caracteres tal cual están en nuestro teclado
{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {D4, D5, D6, D7};
byte colPins[COLS] = {D0, D1, D2, D3};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //inicializa el teclado
void setup() {
pinMode(Rele, OUTPUT);
digitalWrite(Rele, HIGH);
Serial.begin(9600); //inicializar puerto serie
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
delay(1000);
Serial.begin(115200);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
}
//=======================================================================
// Main Program Loop
//=======================================================================
void loop() {
char customKey = customKeypad.getKey(); //se guarda en la variable customKey el caracter de la tecla presionada
if (customKey != NO_KEY) //se evalúa si se presionó una tecla
{
codigo[cont]=customKey; //se guarda caracter por caracter en el arreglo codigo[]
Serial.println(codigo[cont]); //se imprime en el puerto serie la tecla presionada
cont=cont+1;
if(customKey=='0')
{
cont=0;
}
if(cont==6) //si ya fueron presionadas 6 teclas se evalúa si la contraseña es correcta
{
WiFiClientSecure httpsClient;
//Serial.println(host);
//Serial.printf("Using fingerprint '%s'\n", fingerprint);
httpsClient.setFingerprint(fingerprint);
httpsClient.setTimeout(15000); // 15 Seconds
//delay(1000);
//Serial.print("HTTPS Connecting");
int r=0; //retry counter
while((!httpsClient.connect(host, httpsPort)) && (r < 30)){
delay(100);
Serial.print(".");
r++;
}
if(r==30) {
//Serial.println("Connection failed");
}
else {
//Serial.println("Connected to web");
}
String code, Link;
int i;
for (i = 0; i < 6; i++) {
code = code + codigo[i];
}
Link = "/miscript.php?aid=" + code;
//Serial.print("requesting URL: ");
//Serial.println(host+Link);
httpsClient.print(String("GET ") + Link + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
//Serial.println("request sent");
while (httpsClient.connected()) {
String line = httpsClient.readStringUntil('\n');
if (line == "\r") {
//Serial.println("headers received");
break;
}
}
//Serial.println("reply was:");
String line;
while(httpsClient.available()){
line = httpsClient.readStringUntil('\n'); //Read Line by Line
//Serial.println(line); //Print response
}
if (line =="1") {
Serial.println("enciendo");
digitalWrite(Rele, LOW);
delay(2000);
digitalWrite(Rele, HIGH);
}
else{
Serial.println("nada");
}
cont=0; //resetear a 0 la variable cont
}
}
}
Como lo veis? Le veis alguna pega?
Lo estoy probando y de momento funciona, aunque desconecto y vuelvo a conectar el router vuelve a conectarse sin problemas.