I found that I needed to increase the declarations to 4 and 3 to permit higher temperatures and 100% humidity. Here’s the current working code:
// Reads two DHT22 sensors and connects to wifi
// sends correct information (state of Fan 1, Louver status, and indoor
// and outdoor temperature and humidity to the Openhab broker via MQTT
// August 18, 2018
//
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include “DHT.h”
#include <PubSubClient.h>
/************ WIFI and MQTT INFORMATION (CHANGE THESE FOR YOUR SETUP) ******************/
#define wifi_ssid “[SSID]” //type your WIFI information inside the quotes
#define wifi_password “[PSWD]”
#define mqtt_server “192.168.50.153” // this is the ip address for the openhab Pi server
#define mqtt_user “[user name]”
#define mqtt_password “[user password]”
#define mqtt_port 1883
/****************** Manually set MQTT packet size **************/
#define MQTT_MAX_PACKET_SIZE 512
/*************** Define Sensor types and data pins ************/
DHT dht(D7,DHT22);
DHT dht1(D6,DHT22);
/**************** needed for WiFi and MQTT???*****************/
WiFiClient espClient;
PubSubClient client(espClient);
/******************* Declare variables **********************/
float PoolIndoorTemp;
float PoolIndoorHumidity;
float OutdoorTemp;
float OutdoorHumidity;
int IndoorTempAdjustment = 0; // Adjustments are used to calibrate the temperature and
int IndoorHumidityAdjustment = 0; // humidity readings for accuracy and comparability
int OutdoorTempAdjustment = 1;
int OutdoorHumidityAdjustment = 2;
int PoolIndoorHumiditySetPoint = 34;
int PoolIndoorHumidityBuffer = 2; // buffer is used to separate on and off by the number
int OutdoorTempSetPoint = 45; // degrees indicated. The idea is to prevent short cycling
int OutdoorTempBuffer = 2;
int Fan1Status;
int LouverStatus;
char message_buff[100];
void setup()
{
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
boolean rt = client.connect(mqtt_server, mqtt_user, mqtt_password);
client.setCallback(callback);
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print(“Message arrived [”);
Serial.print(topic);
Serial.print("] ");
int i=0;
for (i=0;i<length;i++) {
Serial.print((char)payload[i]);
message_buff[i] = payload[i];
}
message_buff[i] = ‘\0’;
String msgString = String(message_buff);
if (msgString.equals(“OFF”))
{
client.publish(“openhab/himitsu/command”,“acknowedging OFF”);
}
else if(msgString.equals(“ON”))
{
client.publish(“openhab/himitsu/command”,“acknowedging ON”);
}
Serial.println();
}
/*************************** START SETUP WIFI*****************/
void setup_wifi()
{
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“connecting to wifi”);
}
Serial.println("");
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
// Serial.println(WiFi.localIP());
}
void loop()
{
/************* check for sensor reading errors and print if found ***/
/*************Read Sensors and adjust for accuracy (manually set adjustment above) **************/
PoolIndoorTemp = (dht.readTemperature(true)*5/9+32 + IndoorTempAdjustment); // read and convert to Fahrenheit
PoolIndoorHumidity = (dht.readHumidity()+ IndoorHumidityAdjustment);
OutdoorTemp = (dht1.readTemperature(true)*5/9+32 + OutdoorTempAdjustment); // read and convert to Fahrenheit
OutdoorHumidity = (dht1.readHumidity()+ OutdoorHumidityAdjustment);
/************* Print Indoor Sensor readings - use for display **************/
Serial.print (“Indoor Temperature: “); // prints the words
Serial.print(PoolIndoorTemp,0); //reads temp
Serial.print(”\xC2\xB0”); // this adds the degree symbol. I don’t know how, but it does.
Serial.println (“F”); // adds the F after the degree symbol
Serial.print(“Indoor - Relative Humidity: “); // prints RH: with a space but no carriage return
Serial.print(PoolIndoorHumidity,0);
Serial.println(”%”); // adds the % symbol;
/*************Read and Print outdoor Sensor - use for display **************/
Serial.print (“Outdoor Temperature: “); // prints the words
Serial.print((OutdoorTemp),0); //converts to Fahrenheit
Serial.print(”\xC2\xB0”); // this adds the degree symbol. I don’t know how, but it does.
Serial.println (“F”);
Serial.print(“Outdoor - Relative Humidity: “); // prints RH: with a space but no carriage return
Serial.print(OutdoorHumidity,0);
Serial.println(”%”); // adds the % symbol
Serial.println();
/************** Determine whether change Fan and Louver Status ****************/
// If Fan is off, set Fan variable to “on” if humidity is too high.
if ((Fan1Status == 0) and (PoolIndoorHumidity > PoolIndoorHumiditySetPoint))
{
Fan1Status = 1;
delay (100);
Serial.println (“Turning on Fan 1.”);
// If fan is on but louver is closed, change louver status to open if not too cold out
// this is a nested loop. It should only run if the first "if" is satisfied
if ((LouverStatus == 0) and (OutdoorTemp > OutdoorTempSetPoint))
{
LouverStatus = 1;
delay (100);
Serial.println ("Opening Louvers.");
}
}
// If fan is on and louver is open but it gets too cold out, close louver, leave fan on.
if (((Fan1Status == 1) and (LouverStatus == 1)) and (OutdoorTemp < (OutdoorTempSetPoint - OutdoorTempBuffer)))
{
LouverStatus = 0;
delay (100);
Serial.println (“Too cold outside; closing louvers”);
}
// If fan is on and indoor humidity drops below set point munus the buffer, then turn off and close.
if ((Fan1Status == 1) and (PoolIndoorHumidity < (PoolIndoorHumiditySetPoint - PoolIndoorHumidityBuffer)))
{
delay (100);
Fan1Status = 0;
LouverStatus = 0;
Serial.println (“Humidity is low enough, turning off Fan 1 and closing the Louvers.”);
}
/**************** Confirm MQTT is connected **********/
/**************** Publish data over MQTT *************/
char fan1_state_topic; // defines topic as char type
char louver_state_topic; // defines topic as char type
char indoor_temp_topic; // defines topic as char type
char indoor_humidity_topic; // defines topic as char type
char outside_temp_topic;
char outside_humidity_topic;
// The following reads the state of Fan1Status and LouverStatus and publishes it over MQTT
if (Fan1Status == 0)
{
client.publish(“fan1_state_topic”, “OFF”);
delay (100);
}
if (Fan1Status == 1)
{
client.publish(“fan1_state_topic”, “ON”);
delay (100);
}
if (LouverStatus == 0)
{
client.publish(“louver_state_topic”, “OFF”);
delay (100);
}
if (LouverStatus == 1)
{
client.publish(“louver_state_topic”, “ON”);
delay (100);
}
char OutTemp [4];
dtostrf (OutdoorTemp,3,0,OutTemp);
char OutHumidity [4];
dtostrf (OutdoorHumidity,3,0,OutHumidity);
char IndoorTemp [4]; // defines IndoorTemp as character type
dtostrf (PoolIndoorTemp,3,0,IndoorTemp); // This converts digital to string
char IndoorHumidity [4]; // Defines IndoorHumidity as character type
dtostrf (PoolIndoorHumidity,3,0,IndoorHumidity); // This converts digital to string
client.publish(“indoor_temp_topic”, IndoorTemp);
client.publish(“indoor_humidity_topic”, IndoorHumidity);
client.publish(“outside_temp_topic”, OutTemp);
client.publish(“outside_humidity_topic”, OutHumidity);
delay(10000); // The delay between the repeat of everything below void loop()
}
My next steps:
1.name the esp 8266 a unique name
2. insert the correct mqtt reconnect loop to assure reconnection if connection is lost
3. Add OTA updating
4. add the ability to change the set points remotely
5. log the temperature and humidity data for graphing
6. add error detection