Para terminar, adjunto todo mi proyecto , que es el proyecto final de mi carrera de ingenieria en telecomunicaciones en Argentina. Conciste en controlar una puerta de acceso, las luces por rele y el aire acondicionado ademas de verificar temperatura y humedad cada 6 segundos.Esta conectado por MQTT a un servidor el cual escucha topicos y escribe por los mismos y nos permitio hacer la conexion con la aplicacion MQTTDASHBOARD disponible para android.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#define DHTPIN D7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Connect to the WiFi
const char* ssid =
const char* password =
//connect to the server MQTT
const char* mqtt_server =
const int mqttPort = 14218;
const char* mqttUser = "xknjnxuo";
const char* mqttPassword =
unsigned long interval=60000; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
//Connect to IR
const uint16_t kIrLed = 4; // ESP8266 GPIO pin to use. Recommended: 4 (D1).
IRsend irsend(kIrLed); // Set the GPIO to be used to sending the message.
uint16_t khz = 38; // 38kHz carrier frequency for the NEC protocol
uint16_t rawDataapagar[91] = {8800,4500, 550,600, 500,650, 550,1750, 550,600, 550,650, 500,600, 550,650, 500,650, 500,650, 550,1750, 550,600, 500,650, 500,1800, 500,650, 500,1750, 550,1750, 500,650, 500,1750, 550,650, 500,650, 500,1750, 550,1750, 500,650, 500,650, 550,1750, 500,650, 500,650, 550,600, 500,1800, 500,650, 500,650, 550,1750, 550,1700, 550,600, 500,650, 550,1750, 500,650, 550,650, 500,600, 550,650, 500,650, 500,1800, 500,650, 500,650, 500}; // AIWA_RC_T501 FFFFB37D
uint16_t rawDataprender[91] = {8800,4500, 500,650, 550,600, 500,1800, 500,650, 500,1750, 550,650, 500,650, 500,650, 500,650, 500,1800, 500,650, 550,600, 500,1750, 550,650, 500,1750, 550,1750, 500,650, 550,1700, 550,600, 550,1750, 500,650, 550,650, 500,1750, 550,1700, 550,650, 500,650, 500,1750, 550,650, 500,1750, 550,600, 500,1800, 500,650, 550,600, 550,1750, 500,1750, 550,600, 500,650, 550,1750, 500,650, 550,600, 550,1750, 500,650, 500,650, 550,650, 500}; // AIWA_RC_T501 FFFFACDB
uint16_t rawDatabajar[91] = {8800,4550, 500,650, 500,650, 550,1750, 500,650, 500,1750, 500,650, 550,650, 500,650, 500,650, 550,1700, 550,650, 500,1750, 550,650, 500,650, 500,1750, 550,1700, 550,650, 500,650, 500,650, 550,650, 500,600, 550,650, 500,650, 500,650, 550,650, 500,600, 550,650, 500,650, 500,650, 550,650, 500,600, 550,650, 500,650, 500,650, 550,650, 500,600, 550,650, 500,650, 500,650, 550,650, 500,1750, 500,1750, 500,650, 550,1750, 500}; // AIWA_RC_T501 FFFFFFF9
uint16_t rawDatasubir[91] = {8750,4550, 500,650, 500,650, 500,1800, 500,650, 500,1750, 550,600, 550,650, 500,650, 500,650, 500,1800, 500,600, 550,1750, 500,650, 550,1750, 500,650, 500,650, 550,600, 550,650, 500,650, 500,650, 500,650, 550,600, 550,650, 500,650, 500,650, 500,650, 550,600, 550,600, 550,650, 500,650, 500,650, 550,600, 500,650, 550,650, 500,650, 500,650, 550,600, 500,650, 550,650, 500,650, 500,1800, 500,1750, 550,600, 500,650, 550}; // AIWA_RC_T501 FFFFFFF9
WiFiClient espClient;
PubSubClient client(espClient);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]");
for (int i=0;i<length;i++) {
char receivedChar = (char)payload[i];
Serial.print(receivedChar);
if (strcmp(topic, "Luces") == 0){
if (receivedChar == '0' )
{ digitalWrite(D5, LOW); }
if (receivedChar == '1')
{ digitalWrite(D5, HIGH); }
}
if (strcmp(topic, "Puerta") == 0){
if (receivedChar == '0' )
{ digitalWrite(D4, LOW); }
if (receivedChar == '1')
{ digitalWrite(D4, HIGH); }
}
if (strcmp(topic, "Aire") == 0){
if (receivedChar == '0' )
{irsend.sendRaw(rawDataapagar, 91, khz);
Serial.print("Apagando Aire");
}
if (receivedChar == '1')
{irsend.sendRaw(rawDataprender, 91, khz);
Serial.print("Enciendo Aire");
}
if (receivedChar == '2' )
{irsend.sendRaw(rawDatasubir, 91, khz);
Serial.print("Subir 1 grado aire");
}
if (receivedChar == '3')
{irsend.sendRaw(rawDatabajar, 91, khz);
Serial.print("Bajar 1 grado aire");
}
}
Serial.println();
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP8266 Client",mqttUser,mqttPassword)) {
Serial.println("connected");
client.subscribe("Luces");
client.subscribe("Temperatura");
client.subscribe("Puerta");
client.subscribe("Aire");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup()
{
irsend.begin();
Serial.begin(9600);
client.setServer(mqtt_server, mqttPort);
client.setCallback(callback);
setup_wifi();
dht.begin();
pinMode(D5,OUTPUT);
pinMode(D4,OUTPUT);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{ unsigned long currentMillis = millis();
float h = dht.readHumidity();
float t = dht.readTemperature();
char result[8];
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
int sensorValue = 0;
int sensorPin = A0;
String Temp ="";
sensorValue = analogRead(sensorPin);
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.println(sensorValue, DEC);
Serial.print("Temperatura: ");
Serial.print(t);
Serial.print("Humedad: ");
Serial.print(h);
if (sensorValue >= 800){
client.publish("Luces","1");
}
if (sensorValue <= 800){
client.publish("Luces","0");
}
client.publish("Temperatura",dtostrf(t, 6, 2, result));
previousMillis = millis();
}
if (!client.connected()) {
reconnect();
}
client.loop();
}
Saludos y muchas gracias por la ayuda.