Conexion Esp
Conexion Esp
h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h >
#include <WiFiManager.h>
#include <Ticker.h>
Ticker ticker;
byte pinLed = 1;
byte pin = 2;
void parpadeoLed(){
// Cambiar de estado el LED
byte estado = digitalRead(pinLed);
digitalWrite(pinLed, !estado);
}
//************termina conexionwifi inteligente*********************
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login
details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME,
AIO_KEY);
void setup() {
Serial.begin(115200);
delay(10);
// Eliminamos el temporizador
ticker.detach();
// Apagamos el LED
digitalWrite(pinLed, HIGH);
//**** termina conexion wifi inteligente****************
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
String dato=(char *)onoffbutton.lastread;
if(dato=="1")
{
digitalWrite(pinLed,LOW);
digitalWrite(pin, HIGH);
}
if(dato=="0")
{
digitalWrite(pinLed,HIGH);
digitalWrite(pin, LOW);
}
}
}
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}