Hi all
So an interesting project.
My kids sleep with the AC on, so by morning the room is an ice box as well costing me higher electricity usage.
So I built a small thermostat using an ESP8266, I would set the AC on temperature to around 26 degrees and the off temperature to around 19 degrees, the unit would wake up by every 15 mins and carry out the appropriate action by sending the correct IR code to control the AC.
It worked OK but every time time I wanted to change temperatures I had to re-program which was annoying and was not reliable enough, so I then thought about a system where I could set the on off temperatures controlled through my phone.
I found a project online which has the 2 set points but switches a relay on and off at the set points.
My question is can someone with more experience check the code, to see how easy it would be to implement IR code sending at the trip points rather than the relay on off.
Alm help is greatfully appreciated.
Cheers Alan
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <FS.h>
#include <ESP8266mDNS.h>
#include <user_interface.h>
#include <OneWire.h>
//gpio pin //nodeMCU pin
//ds18b20 5 //d1
const int relay = 4; //d2
const int button_toggle = 14; //d5
const int button_manual = 12; //d6
const int led_manual = 13; //d7
const int led_connected = 15; //d8
#define TRIGGER_LOW (0)
#define TRIGGER_HIGH (1)
#define TRIGGER_INSIDE (2)
#define TRIGGER_OUTSIDE (3)
#define TRIGGER_HIGH_LOW (4)
#define TRIGGER_LOW_HIGH (5)
#define TRIGGER_CUSTOM1 (6)
#define TRIGGER_CUSTOM2 (7)
#define TRIGGER_CUSTOM3 (8)
typedef struct
{
float temperature;
float trigger_high;
float trigger_low;
uint8_t trigger_type;
bool fahrenheit;
bool manual;
bool on_off;
bool unprotected;
}
settings_t;
settings_t settings = { 99, 30, 20, TRIGGER_LOW, false, false, false, false };
const int temperature_sample_period = 1000; // in milliseconds.
uint32_t temperature_sample_timer = 0;
const int led_connected_blink_timer_period = 1000; // in milliseconds.
uint32_t led_connected_blink_timer = 0;
bool button_manual_being_pressed = false;
bool button_toggle_being_pressed = false;
bool send_settings_to_webpage = true;
char ssid[51];
char pass[51];
char APName[] = "Wi-Fi Thermostat";
String beginPageText;
unsigned long lastSettingsChange = 0;
byte wifiState = 0;
bool ap_connected = false;
#define connected 1
#define ssidIncorrect 2
#define passIncorrect 3
ESP8266WebServer server(80);
OneWire ds(5); // on pin 5 (a 4.7k resistor is necessary)
void settings_print(void)
{
// Wi-Fi settings.
Serial.println("ssid: " + String(ssid));
Serial.println("pass: " + String(pass));
Serial.println("unprotected: " + String(settings.unprotected));
// Thermostat settings.
Serial.println("fahrenheit: " + String(settings.fahrenheit));
Serial.println("high trigger: " + String(settings.trigger_high));
Serial.println("low trigger: " + String(settings.trigger_low));
Serial.println("trigger type: " + String(settings.trigger_type));
Serial.println("manual: " + String(settings.manual));
}
bool button_pressed(uint8_t button)
{
if (digitalRead(button)==0)
{
// Crude debounce.
delay(25);
if (digitalRead(button)==0) return true;
}
return false;
}
void setup(void)
{
pinMode(relay,OUTPUT);
pinMode(button_toggle,INPUT_PULLUP);
pinMode(button_manual,INPUT_PULLUP);
pinMode(led_manual,OUTPUT);
pinMode(led_connected,OUTPUT);
Serial.begin(9600);
EEPROM.begin(512);
// Uncomment to erase EEPROM, then recomment for normal operation.
//eeprom_clear();
eeprom_read();
settings_print();
ds1820_start_conversion();
startWiFi();
SPIFFS.begin();
setupWebserver();
send_settings_to_webpage = true;
Serial.println("HTTP server started");
}
void loop(void)
{
// Wi-Fi connected led. Connected: LED on, not connected: LED blinks.
if (wifiState==connected || ap_connected==true) digitalWrite(led_connected,HIGH);
else if (led_connected_blink_timer<=millis())
{
led_connected_blink_timer = millis() + led_connected_blink_timer_period;
digitalWrite(led_connected,!digitalRead(led_connected));
}
// Read temperature.
if (temperature_sample_timer<=millis())
{
temperature_sample_timer = millis() + temperature_sample_period;
settings.temperature = ds1820_get_temperature();
ds1820_start_conversion();
//Serial.print(settings.temperature);
//Serial.print(char(176)); // Print degree symbol.
//Serial.println(settings.fahrenheit==true?"F":"C");
}
server.handleClient();
// Do the thermostat thing.
if (settings.manual==false)
{
// Automatic mode.
if (settings.trigger_type==TRIGGER_CUSTOM1) // Custom 1
{
// Add your custom 1 code here.
}
else if (settings.trigger_type==TRIGGER_CUSTOM2) // Custom 2
{
// Add your custom 2 code here.
}
if (settings.trigger_type==TRIGGER_CUSTOM3) // Custom 3
{
// Add your custom 3 code here.
}
else if (settings.trigger_type==TRIGGER_HIGH_LOW) // Switch with hysteresis
{
// Switch on when T>T_high, switch off when T<T_low
if (settings.temperature>settings.trigger_high) settings.on_off = true; // Output on.
else if (settings.temperature<settings.trigger_low) settings.on_off = false; // Output off.
}
else if (settings.trigger_type==TRIGGER_LOW_HIGH) // Switch with hysteresis
{
// Switch on when T<T_low, switch off when T>T_high
if (settings.temperature<settings.trigger_low) settings.on_off = true; // Output on.
else if (settings.temperature>settings.trigger_high) settings.on_off = false; // Output off.
}
else if ((settings.trigger_type==TRIGGER_LOW && settings.temperature<settings.trigger_low) || // Switch on when T<T_low, switch off when T>T_low
(settings.trigger_type==TRIGGER_HIGH && settings.temperature>settings.trigger_high) || // Switch on when T>T_high, switch off when T<T_high
(settings.trigger_type==TRIGGER_INSIDE && (settings.temperature>settings.trigger_low && settings.temperature<settings.trigger_high)) || // Switch on when T_low<T<T_high
(settings.trigger_type==TRIGGER_OUTSIDE && (settings.temperature<settings.trigger_low || settings.temperature>settings.trigger_high))) // Switch on when T<T_low or T>T_high
{
settings.on_off = true; // Output on.
}
else settings.on_off = false; // Output off.
}
else
{
// Manual mode.
if (button_pressed(button_toggle)==true)
{
// Button pressed and was not being pressed?
if (button_toggle_being_pressed==false)
{
settings.on_off = !settings.on_off;
button_toggle_being_pressed = true;
send_settings_to_webpage = true;
}
}
else button_toggle_being_pressed = false; // Button released.
}
// Read button Manual
if (button_pressed(button_manual)==true)
{
// Button pressed and was not being pressed?
if (button_manual_being_pressed==false)
{
settings.manual = !settings.manual;
button_manual_being_pressed = true;
send_settings_to_webpage = true;
}
}
else button_manual_being_pressed = false; // Button released.
digitalWrite(relay,settings.on_off); // Update output.
digitalWrite(led_manual,settings.manual); // Update Manual LED.
}