Using Arduino IDE to make a thermostat with the ESP8266 based NodeMCU board for WiFi connectivity in order to use a web server to control the thermostat.
Full sketch:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "Adafruit_ILI9341.h"
#include "Adafruit_GFX.h"
#include "SPI.h"
#include <dht.h>
#define button 12
#define relay 16
#define dhtPin 2
#define pot A0
#define DC 5
#define CS 4
const char* ssid = "BTHub5-72NX";
const char* password = "d6c6a43fe2";
String page;
int heatMode = 0;
int target;
int temp;
int dhtValue;
int potRef;
int potVal;
int i = 0;
int a = 0;
Adafruit_ILI9341 tft = Adafruit_ILI9341(CS, DC);
ESP8266WebServer server(6565);
dht DHT;
void setup() {
Serial.begin(115200);
Serial.println("[Serial] Initialized");
tft.begin();
tft.setRotation(3);
Serial.println("[TFT] Initialized");
pinMode(pot, INPUT);
pinMode(button, INPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay(100);
server.on("/", [](){
server.send(200, "text/html", page);
});
server.on("/thermostat/on", [](){
server.send(200, "text/html", page);
heatMode = 1;
delay(1000);
});
server.on("/thermostat/off", [](){
server.send(200, "text/html", page);
heatMode = 0;
delay(1000);
});
server.on("/thermostat/+", [](){
server.send(200, "text/html", page);
target = target + 1;
delay(1000);
});
server.on("/thermostat/-", [](){
server.send(200, "text/html", page);
target = target - 1;
delay(1000);
});
server.begin();
Serial.println("[WiFi] Web server started");
tft.fillScreen(ILI9341_BLACK);
delay(1000);
}
void loop() {
page = "<!DOCTYPE HTML>\n";
page += "<html>\n";
page += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head>\n";
page += "<body>\n";
page += "<h2>Current temperature: ";
page += String(temp);
page += "</h2>\n";
page += "<h2>Target temperature: ";
page += String(target);
page += "</h2>\n";
page += "<h2>Heating mode: ";
if (heatMode == 1) {
page += "On</h2>\n";
} else if (heatMode == 0) {
page += "Off</h2>\n";
}
page += "<a href='/thermostat/+'><button>+</button></a>\n";
page += "<a href='/thermostat/on'><button>On</button></a>\n";
page += "<a href='/thermostat/off'><button>Off</button></a>\n";
page += "<a href='/thermostat/-'><button>-</button></a>\n";
page += "
";
page += "<a href='/'><button>Press to reset after any button above is pressed!</button></a>\n";
page += "</body>\n";
page += "</html>\n";
server.handleClient();
if (a == 2000) {
a = 0;
dhtValue = DHT.read11(dhtPin);
temp = DHT.temperature;
if (heatMode == 1) {
if (int(temp) << target) {
Serial.println("Relay ON");
digitalWrite(relay, LOW);
} else if (target << int(temp) || target == int(temp)) {
Serial.println("Relay OFF");
digitalWrite(relay, HIGH);
}
} else {
digitalWrite(relay, HIGH);
}
} else {
a += 1;
}
delay(10);
if (digitalRead(button) == HIGH) {
dhtValue = DHT.read11(dhtPin);
temp = DHT.temperature;
if (heatMode == 0) {
tft.fillScreen(ILI9341_BLACK);
} else if (heatMode == 1) {
tft.fillScreen(ILI9341_RED);
}
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(4);
tft.print("Current: ");
tft.print(temp);
tft.println("C");
tft.setCursor(10, 60);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(4);
potVal = analogRead(pot);
Serial.println(int(potVal));
if (int(potVal) >= 0 && int(potVal) << 205) {
target = 19;
Serial.println("19");
} else if (int(potVal) >= 205 && int(potVal) << 410) {
target = 20;
Serial.println("20");
} else if (int(potVal) >= 410 && int(potVal) << 615) {
target = 21;
Serial.println("21");
} else if (int(potVal) >= 615 && int(potVal) << 820) {
target = 22;
Serial.println("22");
} else if (int(potVal) >= 820 && int(potVal) <= 1024) {
target = 23;
Serial.println("23");
}
tft.print("Target: ");
tft.print(target);
tft.println("C");
if (heatMode == 0) {
tft.setCursor(10, 190);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(4.5);
tft.print("Heating off");
} else if (heatMode == 1) {
tft.setCursor(10, 190);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(4.5);
tft.print("Heating on");
}
delay(500);
while (i != 4) {
if (digitalRead(button) == HIGH) {
if (heatMode == 0) {
heatMode = 1;
} else if (heatMode == 1) {
heatMode = 0;
}
i = 4;
} else {
delay(2000);
i += 1;
}
}
i = 0;
tft.fillScreen(ILI9341_BLACK);
}
}
The sections of code that don't work are:
potVal = analogRead(pot);
Serial.println(int(potVal));
if (int(potVal) >= 0 && int(potVal) << 205) {
target = 19;
Serial.println("19");
} else if (int(potVal) >= 205 && int(potVal) << 410) {
target = 20;
Serial.println("20");
} else if (int(potVal) >= 410 && int(potVal) << 615) {
target = 21;
Serial.println("21");
} else if (int(potVal) >= 615 && int(potVal) << 820) {
target = 22;
Serial.println("22");
} else if (int(potVal) >= 820 && int(potVal) <= 1024) {
target = 23;
Serial.println("23");
}
if (a == 2000) {
a = 0;
dhtValue = DHT.read11(dhtPin);
temp = DHT.temperature;
if (heatMode == 1) {
if (int(temp) << target) {
Serial.println("Relay ON");
digitalWrite(relay, LOW);
} else if (target << int(temp) || target == int(temp)) {
Serial.println("Relay OFF");
digitalWrite(relay, HIGH);
}
} else {
digitalWrite(relay, HIGH);
}
} else {
a += 1;
}
For the first section, a potentimeter is used to control the target temperature displayed on the screen, however no matter what the value is (0 -1024) the target temperature is always set to 19C by the if statement (shown by the serial debugging I have done)
For the second, any target temperature value causes the relay to turn on, essentially ignoring the if statement.
Would greatly appreciate anyone's help with this because I cannot see any reason for it to be getting the if statements wrong?
Many thanks in advance.