Created
October 2, 2019 00:40
-
-
Save KEVINALBO13/19a2950e43d9d395e236963dcd826de8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef ARDUINO_ARCH_ESP32 | |
#include <WiFi.h> | |
#include <WiFiMulti.h> | |
WiFiMulti wifiMulti; | |
#else | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
ESP8266WiFiMulti wifiMulti; | |
#endif | |
#include "PCD8544.h" | |
#include "driver/gpio.h" | |
#include <MQTT.h> | |
#include <Wire.h> | |
static PCD8544 lcd = PCD8544(14, 13, 27, 26, 15); | |
int PersonaX = 0; | |
int PersonaXa = 0; | |
const char ssid1[] = "ALSWl"; | |
const char pass1[] = "25264897"; | |
const char ssid2[] = "ALSW2"; | |
const char pass2[] = "7210-3607"; | |
const char ssid3[] = "ssid"; | |
const char pass3[] = "pass"; | |
boolean estadoCuna = false; | |
boolean estadoPuerta = false; | |
WiFiClient net; | |
MQTTClient client; | |
unsigned long lastMillis = 0; | |
void Conectar() { | |
Serial.print("Conectandoo Al Wifi..."); | |
lcd.clear(); | |
lcd.setCursor(0, 0); | |
lcd.print("Conectando"); | |
lcd.setCursor(0, 1); | |
lcd.print("Wifi"); | |
while (wifiMulti.run() != WL_CONNECTED) { | |
Serial.print("."); | |
lcd.setCursor(30, 1); | |
lcd.print("|"); | |
delay(500); | |
lcd.setCursor(30, 1); | |
lcd.print("-"); | |
delay(500); | |
} | |
Serial.print("\nConectado con Wifi\n Buscando MQTT"); | |
lcd.clear(); | |
lcd.setCursor(0, 0); | |
lcd.print("Conectando"); | |
lcd.setCursor(0, 1); | |
lcd.print("MQTT"); | |
delay (500); | |
while (!client.connect("Pantalla", "Pulseraxd", "12345678")) { | |
Serial.print("*"); | |
lcd.setCursor(30, 1); | |
lcd.print("|"); | |
digitalWrite(LED_BUILTIN , 1); | |
delay(500); | |
lcd.setCursor(30, 1); | |
lcd.print("-"); | |
digitalWrite(LED_BUILTIN , 0); | |
delay(500); | |
} | |
Serial.println("\nSistema Conectado"); | |
digitalWrite(LED_BUILTIN , 1); | |
client.subscribe("/Pulsera/Puerta/Principal"); | |
client.subscribe("/Pulsera/Cuna"); | |
// client.unsubscribe("/hello"); | |
} | |
void MensajeRecibido(String &topic, String &payload) { | |
Serial.println("Mensaje: " + topic + " - " + payload); | |
if (topic == "/Pulsera/Cuna") { | |
if (payload == "1") { | |
estadoCuna = true; | |
} else { | |
estadoCuna = false; | |
} | |
} | |
if (topic == "/Pulsera/Puerta/Principal") { | |
if (payload == "1") { | |
estadoPuerta = true; | |
} else { | |
estadoPuerta = false; | |
} | |
} | |
} | |
void setup() { | |
gpio_set_direction( GPIO_NUM_23, GPIO_MODE_OUTPUT); | |
gpio_set_level( GPIO_NUM_23, 1); | |
lcd.begin(84, 48); | |
lcd.clear(); | |
lcd.setCursor(0, 0); | |
lcd.print("Iniciando programa"); | |
Serial.begin(115200); | |
WiFi.mode(WIFI_STA);//Cambiar modo del Wi-Fi | |
delay(100); | |
wifiMulti.addAP(ssid1, pass1); | |
wifiMulti.addAP(ssid2, pass2); | |
wifiMulti.addAP(ssid3, pass3); | |
pinMode(LED_BUILTIN, OUTPUT); | |
client.begin("broker.shiftr.io", net); | |
client.onMessage(MensajeRecibido); | |
Conectar(); | |
} | |
void loop() { | |
client.loop(); | |
delay(10); | |
if (!client.connected()) { | |
Conectar(); | |
} | |
if (millis() - lastMillis > 500) { | |
lastMillis = millis(); | |
lcd.clear(); | |
lcd.setCursor (0, 1); | |
lcd.print ("Puerta Princ."); | |
lcd.setCursor (0, 4); | |
lcd.print("Proyecto final"); | |
lcd.setCursor (10, 2); | |
if (estadoCuna) { | |
lcd.print("Actividad"); | |
} else { | |
lcd.print("Inactividad"); | |
} | |
lcd.setCursor (10, 5); | |
if (estadoPuerta) { | |
lcd.print("Actividad"); | |
delay(1000); | |
} else { | |
lcd.print("Inactividad"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment