DHT + Relay + MQTT Help
Posted: Thu Jan 28, 2016 6:59 am
ESP'ians ,
I have been messing around with a DHT script over from Adafruit's learn site where they use 2 ESP's (albeit Huzzah) to send DHT sensor information via one device and control a lamp via another device connected to their mqtt dashboard.
So I decided to use only one ESP device to do both by combining the 2 scripts as I decided it would be good to turn on/off the porch light too
My problem is it works as it should but! the DHT sampling delay time seems to give me a small window between updates to turn on or off the lamp .
Following is the code , if someone can spot the mistake I am making and help me correct it , would be appreciated .
I have been messing around with a DHT script over from Adafruit's learn site where they use 2 ESP's (albeit Huzzah) to send DHT sensor information via one device and control a lamp via another device connected to their mqtt dashboard.
So I decided to use only one ESP device to do both by combining the 2 scripts as I decided it would be good to turn on/off the porch light too
My problem is it works as it should but! the DHT sampling delay time seems to give me a small window between updates to turn on or off the lamp .
Following is the code , if someone can spot the mistake I am making and help me correct it , would be appreciated .
Code: Select all
/***************************************************
Adafruit ESP8266 Sensor Module
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board:
----> https://www.adafruit.com/product/2471
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
// Libraries
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"
// DHT 11 sensor
#define DHTPIN 13
#define DHTTYPE DHT21 //AM2301
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "your ssid"
#define WLAN_PASS "your password"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "your ID"
#define AIO_KEY "your key"
// DHT sensor
DHT dht(DHTPIN, DHTTYPE, 15);
// Lamp pin
const int lamp_pin = 2;
// Functions
void connect();
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Store the MQTT server, client ID, username, and password in flash memory.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM = AIO_KEY __DATE__ __TIME__;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);
/****************************** Feeds ***************************************/
// Setup feeds for temperature & humidity
const char TEMPERATURE_FEED[] PROGMEM = AIO_USERNAME "/feeds/temperature";
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, TEMPERATURE_FEED);
const char HUMIDITY_FEED[] PROGMEM = AIO_USERNAME "/feeds/humidity";
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, HUMIDITY_FEED);
const char LAMP_FEED[] PROGMEM = AIO_USERNAME "/feeds/lamp";
Adafruit_MQTT_Subscribe lamp = Adafruit_MQTT_Subscribe(&mqtt, LAMP_FEED);
/*************************** Sketch Code ************************************/
void setup() {
// Set lamp pin to output
pinMode(lamp_pin, OUTPUT);
// Init sensor
dht.begin();
Serial.begin(115200);
Serial.println(F("Adafruit IO Example"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
delay(10);
Serial.print(F("Connecting to "));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
// listen for events on the lamp feed
mqtt.subscribe(&lamp);
// connect to adafruit io
connect();
}
void loop() {
Adafruit_MQTT_Subscribe *subscription;
// ping adafruit io a few times to make sure we remain connected
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}
// this is our 'wait for incoming subscription packets' busy subloop
while (subscription = mqtt.readSubscription(1000)) {
// we only care about the lamp events
if (subscription == &lamp) {
// convert mqtt ascii payload to int
char *value = (char *)lamp.lastread;
Serial.print(F("Received: "));
Serial.println(value);
// Apply message to lamp
String message = String(value);
message.trim();
if (message == "ON") {digitalWrite(lamp_pin, HIGH);}
if (message == "OFF") {digitalWrite(lamp_pin, LOW);}
sensor();
}
}
}
void sensor() {
// Grab the current state of the sensor
int humidity_data = (int)dht.readHumidity();
int temperature_data = (int)dht.readTemperature();
// Publish data
if (! temperature.publish(temperature_data))
Serial.println(F("Failed to publish temperature"));
else
Serial.println(F("Temperature published!"));
if (! humidity.publish(humidity_data))
Serial.println(F("Failed to publish humidity"));
else
Serial.println(F("Humidity published!"));
}
// Repeat every 10 seconds
// delay(10000);
void relay(){
}
// connect to adafruit io via MQTT
void connect() {
Serial.print(F("Connecting to Adafruit IO... "));
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
switch (ret) {
case 1: Serial.println(F("Wrong protocol")); break;
case 2: Serial.println(F("ID rejected")); break;
case 3: Serial.println(F("Server unavail")); break;
case 4: Serial.println(F("Bad user/pass")); break;
case 5: Serial.println(F("Not authed")); break;
case 6: Serial.println(F("Failed to subscribe")); break;
default: Serial.println(F("Connection failed")); break;
}
if(ret >= 0)
mqtt.disconnect();
Serial.println(F("Retrying connection..."));
delay(5000);
}
Serial.println(F("Adafruit IO Connected!"));
}