Skip to content

Instantly share code, notes, and snippets.

@pedrogk
Last active July 2, 2016 18:00
Show Gist options
  • Save pedrogk/9e96d0983dc03c352b12f860a42ba7b6 to your computer and use it in GitHub Desktop.
Save pedrogk/9e96d0983dc03c352b12f860a42ba7b6 to your computer and use it in GitHub Desktop.
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
const char* ssid = "Tu Wifi";
const char* password = "tupassword";
const char* mqtt_server = "tuservidor";
WiFiClient espClient;
PubSubClient client(espClient);
const byte ledPin = 0; // pin con el LED
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Mensaje recibido [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
char receivedChar = (char)payload[i];
Serial.print(receivedChar);
if (receivedChar == '0')
// En el ESP8266 Huzzah el led está invertido
digitalWrite(ledPin, HIGH);
if (receivedChar == '1')
digitalWrite(ledPin, LOW);
}
Serial.println();
}
void reconnect() {
while (!client.connected()) {
Serial.print("Intentando conectarse...");
if (client.connect("ESP8266 Client")) {
Serial.println("conectado");
client.subscribe("ledStatus");
} else {
Serial.print("falla, rc=");
Serial.print(client.state());
Serial.println(" reintentaremos en 5 segundos");
delay(5000);
}
}
}
void setup() {
Serial.begin(9600);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment