Created
October 2, 2019 00:38
-
-
Save KEVINALBO13/41028db25b2f6b19d0dd8688fddbc061 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 <MQTT.h> | |
const int Trigger = 12; | |
const int Echo = 13; | |
int PersonaX = 0; | |
int PersonaXa = 0; | |
const char ssid1[] = "ALSW"; | |
const char pass1[] = "25264897"; | |
const char ssid2[] = "ALSW2"; | |
const char pass2[] = "7210-3607"; | |
const char ssid3[] = "ssid"; | |
const char pass3[] = "pass"; | |
WiFiClient net; | |
MQTTClient client; | |
unsigned long lastMillis = 0; | |
void Conectar() { | |
Serial.print("Conectandoo Al Wifi..."); | |
while (wifiMulti.run() != WL_CONNECTED) { | |
Serial.print("."); | |
digitalWrite(LED_BUILTIN , 1); | |
delay(2000); | |
digitalWrite(LED_BUILTIN , 0); | |
delay(2000); | |
} | |
Serial.print("\nConectado con Wifi\n Buscando MQTT"); | |
while (!client.connect("SensorUltrasonico", "Pulseraxd", "12345678")) { | |
Serial.print("*"); | |
digitalWrite(LED_BUILTIN , 1); | |
delay(500); | |
digitalWrite(LED_BUILTIN , 0); | |
delay(500); | |
} | |
Serial.println("\nSistema Conectado"); | |
digitalWrite(LED_BUILTIN , 0); | |
client.subscribe("/Pulsera/Puerta/Principal"); | |
// client.unsubscribe("/hello"); | |
} | |
void MensajeRecibido(String &topic, String &payload) { | |
Serial.println("Mensaje: " + topic + " - " + payload); | |
} | |
void setup() { | |
pinMode(Trigger, OUTPUT); | |
pinMode(Echo, INPUT); | |
digitalWrite(Trigger, LOW); | |
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(); | |
} | |
long t; //timepo que demora en llegar el eco | |
long d; //distancia en centimetros | |
digitalWrite(Trigger, HIGH); | |
delayMicroseconds(10); //Enviamos un pulso de 10us | |
digitalWrite(Trigger, LOW); | |
t = pulseIn(Echo, HIGH); //obtenemos el ancho del pulso | |
d = t / 59; //escalamos el tiempo a una distancia en cm | |
if (d < 50) { | |
PersonaX = 1; | |
} | |
else { | |
PersonaX = 0; | |
} | |
if (PersonaX != PersonaXa) { | |
PersonaXa = PersonaX; | |
String Mensaje = String(PersonaX); | |
client.publish("/Pulsera/Puerta/Principal", Mensaje); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment