//COMPONENTS LIBRARIES
#include <Keypad.h>
#include <ESP32Servo.h>
//API LIBRERIES
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
//DATA TO BE SENT
String nombre = "";
String codigo = "";
//WIFI DATA
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//SERVER DATA
String host = "http://18.190.40.130/";
//COMPONENTS PINS
int led_rojo = 12;
int led_verde = 14;
int led_morado = 27;
//SERVO
int servoPin = 13;
Servo servo;
//KEYPAD VARIABLES
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
//LAYOUT DEFINITION
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
//KEYPAD KEYS(COLS AND ROWS)
uint8_t colPins[COLS] = { 16, 4, 2, 15 };
uint8_t rowPins[ROWS] = { 19, 18, 5, 17 };
//INSTANCE OF Keypad CLASS
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
//SETUP BEGIN
Serial.begin(9600);
//SETUP COMPONENTS
pinMode(led_rojo, OUTPUT);
pinMode(led_verde, OUTPUT);
pinMode(led_morado, OUTPUT);
servo.attach(servoPin);
servo.write(180);
//ESTABLISH WIFI CONNECTION
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
digitalWrite(led_rojo, HIGH);
delay(500);
Serial.print(".");
}
//OUTPUTS AFTER WIFI SUCCESS
Serial.print("Conectado al wifi con la ip local: ");
Serial.println(WiFi.localIP());
digitalWrite(led_rojo, LOW);
digitalWrite(led_verde, HIGH);
delay(500);
Serial.println("Escriba el codigo que desea usar, entonces la opcion correspondiente ");
Serial.println("'A': Registrar");
Serial.println("'B': Acceder");
Serial.println("'C': Editar");
Serial.println("'A': Borrar codigo");
}
void loop() {
//GET KEYPAD KEY PRESSED
char key = keypad.getKey();
nombre = "esp32prueba";
//CALL TO KEYPADHANDLER
keypadhandler(key);
//httpGETRequest(host);
}
void keypadhandler(char key){
if (key) {
switch(key){
case 'A':
if(codigo.length() == 5){
registrar(host,nombre,codigo);
}else{
Serial.println("Codigo debe ser de 5");
Serial.println("Presiona D para eliminar el codigo");
}
break;
case 'B':
if(codigo.length() == 5){
acceder(host,nombre,codigo);
}else{
Serial.println("Codigo debe ser de 5");
Serial.println("Presiona D para eliminar el codigo");
}
break;
case 'D':
codigo = "";
Serial.println("Codigo borrado. Escribalo de nuevo: ");
break;
default:
codigo+=key;
Serial.print(key);
break;
}
}
}
bool registrar(String serverName, String nombre, String codigo) {
bool resultado = false;
WiFiClient client;
HTTPClient http;
serverName+="?p=deviceReg";
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "nombre="+nombre+"&codigo="+codigo;
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
Serial.println("");
Serial.print("Enviando datos a ");
Serial.print(serverName);
if (httpResponseCode>0) {
int error = HIGH;
int exito = LOW;
auto payload = http.getString(); // JSON content here
StaticJsonDocument<1024> result;
deserializeJson(result, payload);
auto message = result["message"].as<String>();
auto code = result["code"].as<int>();
if(code == 2){
error = LOW;
exito = HIGH;
}
digitalWrite(led_rojo,error);
digitalWrite(led_verde,exito);
Serial.println(message);
return true;
}
else {
Serial.print("Error code: ");
Serial.print(httpResponseCode);
digitalWrite(led_morado, HIGH);
}
// Free resources
http.end();
return false;
}
bool acceder(String serverName, String nombre, String codigo) {
bool resultado = false;
WiFiClient client;
HTTPClient http;
serverName+="?p=deviceLog";
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "nombre="+nombre+"&codigo="+codigo;
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
Serial.println("");
Serial.print("Enviando datos a ");
Serial.println(serverName);
if (httpResponseCode>0) {
int pos = 180;
int error = HIGH;
int exito = LOW;
auto payload = http.getString(); // JSON content here
StaticJsonDocument<1024> result;
deserializeJson(result, payload);
auto message = result["message"].as<String>();
auto code = result["code"].as<int>();
if(code == 2){
error = LOW;
exito = HIGH;
pos = 90;
resultado = true;
}
servo.write(pos);
digitalWrite(led_rojo,error);
digitalWrite(led_verde,exito);
Serial.println(message);
}
else {
Serial.print("Error code: ");
Serial.print(httpResponseCode);
digitalWrite(led_morado, HIGH);
}
// Free resources
http.end();
return resultado;
}