- Thu Jan 18, 2018 2:27 pm
#73266
hi i want to build a thermostat.i have a nodemcu board and LCD and high level trigger relay and dht11 now i can see and control min and max with button and LCD,and only i can see the temperature and humidity in HTML with web server and i cant control that., i write the form for input but i don't know how to write code to Equal the text box input number to variables of min and max.please help me.i am new in arduino and esp word.sorry about my bad English.
Code: Select all#include <Wire.h>
#include <LiquidCrystal_I2C.h> //D1&D2
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#define DHTTYPE DHT11
#define UP 1 //RX
#define SET D0
#define DOWN 3 //TX
#define HEATER D7
#define STEAM D8
DHT dht1( D4, DHTTYPE);
byte termometer[8] = //icon for termometer
{
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,
B11111,
B01110
};
byte droplet[8] = //icon for water droplet
{
B00100,
B00100,
B01010,
B01010,
B10001,
B10001,
B10001,
B01110,
};
byte Degree[8] = //icon for Degree
{
B01110,
B01010,
B01110,
B00000,
B00110,
B01000,
B01000,
B00110
};
byte mini[8] = //icon for minimum
{
B01110,
B01110,
B01110,
B01110,
B11111,
B11111,
B01110,
B00100
};
byte maxi[8] = //icon for maximum
{
B00100,
B01110,
B11111,
B11111,
B01110,
B01110,
B01110,
B01110
};
IPAddress ip(192, 168, 0, 107);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress DNS(8, 8, 8, 8);
const char* ssid = "DALI_CHAI";
const char* password = "";
ESP8266WebServer server(80);
float Temp;
float Humidity;
int count=1;
int menu=1;
unsigned long previousMillis = 0;
unsigned long currentMillis =0;
const long interval = 10000;
int heatmin=0;
int heatmax=0;
int humimin=0;
int humimax=0;
int maxTemp =-100;
int minTemp=100;
int maxHumidity =-100;
int minHumidity=100;
LiquidCrystal_I2C lcd(0x3F, 16, 2);
void setup()
{
Serial.begin(9600);
WiFi.config(ip, gateway, subnet, DNS);
WiFi.begin(ssid, password);
EEPROM.begin(1024);
server.begin();
lcd.begin();
lcd.createChar(1,termometer);
lcd.createChar(2,droplet);
lcd.createChar(3,Degree);
lcd.createChar(4,mini);
lcd.createChar(5,maxi);
dht1.begin();
heatmin=EEPROM.read(1);
heatmax=EEPROM.read(2);
humimin=EEPROM.read(3);
humimax=EEPROM.read(4);
pinMode(UP,INPUT);
pinMode(DOWN,INPUT);
pinMode(SET,INPUT);
pinMode(HEATER,OUTPUT);
pinMode(STEAM,OUTPUT);
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(1);
lcd.write(2);
delay(1000);
lcd.setCursor(14,0);
lcd.write(2);
lcd.write(1);
delay(1000);
lcd.setCursor(3,0);
lcd.print("DALI CHAI");
delay(2000);
lcd.setCursor(0,1);
lcd.print("Smart Farme WIFI");
delay(4000);
startup();
}
void startup(){
currentMillis = millis();
//------------------------------- HTML CODE ----------------------------------
server.on("/", []() {
float temp = dht1.readTemperature();
float Humidity = dht1.readHumidity();
String cssClass = "mediumhot";
if (temp < 0)
cssClass = "cold";
else if (temp > 20)
cssClass = "hot";
String message = "<!DOCTYPE html><html><head><title>""</title><meta charset=\"utf-8\" /><meta name=\"viewport\" content=\"width=device-width\" /><style>\n";
message += "html {height: 100%;}";
message += "div {color: #fff;font-family: 'Arial Black';font-weight: 400;left: 50%;position: absolute;text-align: center;top: 50%;transform: translateX(-50%) translateY(-50%);}";
message += "h1 {font-size: xlarge;font-weight: 400; margin: 0}";
message += "h2 {font-size: xxxlarge;font-weight: 400; margin: 0}";
message += "h3 {font-size: small;font-weight: 400; margin: 0}";
message += "h4 {font-size: xsmall;font-weight: 400; margin: 0}";
message += "body {height: 100%;}";
message += ".cold {background: linear-gradient(to bottom, #7abcff, #0665e0 );}";
message += ".mediumhot {background: linear-gradient(to bottom, #81ef85,#057003);}";
message += ".hot {background: linear-gradient(to bottom, #fcdb88,#d32106);}";
message += "</style></head><body class=\"" + cssClass + "\"><div>"
"<h1>""Office""<br><br></h1>"
"<h2>""T : "+temp +"°C""<br></h2><h3>""Max Temperature : "+maxTemp+"°C"+"<br>""Min Temperature : "+minTemp+"°C""</h3>""<br>"
"<h2>""H : "+Humidity +"%""<br></h2><h3>""Max Humidity : "+maxHumidity+"%"+"<br>""Min Humidity : "+minHumidity+"%""</h3><br>"+"</div></body></html>";
message +="<h4><form action='http://192.168.0.107/submit' method='POST'> Enter Min T:<input name=\"formTemp\" type=\"number\"> <input type=\"submit\"> </form></h4><br>";
message +="<form action='http://192.168.0.107/submit' method='POST'> Enter Max T:<input name=\"formTemp\" type=\"number\"> <input type=\"submit\"> </form> <br>";
message +="<form action='http://192.168.0.107/submit' method='POST'> Enter Min H:<input name=\"formTemp\" type=\"number\"> <input type=\"submit\"> </form> <br>";
message +="<form action='http://192.168.0.107/submit' method='POST'> Enter Max H:<input name=\"formTemp\" type=\"number\"> <input type=\"submit\"> </form> <br>";
message +=("<meta http-equiv=\"refresh\" content=\"10\">");
server.send(200, "text/html", message);
});
//------------------------------- LOADING ... --------------------------------
menu=1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Please Wait "); delay(500);
lcd.print(".");delay(500);
lcd.print(".");delay(500);
lcd.print(".");delay(500);
lcd.print(".");delay(500);
lcd.clear();
delay(1000);
//------------------------------- MENU 1 --------------------------------
while(menu==1){
lcd.setCursor(0, 0);
lcd.write(1);
lcd.setCursor(1, 0);
lcd.print(" ");
lcd.setCursor(2, 0);
lcd.print("Enter min T:");
lcd.setCursor(0, 1);
lcd.print(heatmin);
lcd.setCursor(3, 1);
lcd.write(3);
if (digitalRead(UP)) {heatmin++;delay(200);}
if (digitalRead(DOWN)) {heatmin--;delay(200);}
if (heatmin < 10 ) heatmin=10;
if (heatmin > 99) heatmin=99;
if (digitalRead(SET)) {EEPROM.write(1,heatmin);EEPROM.commit(); lcd.clear(); lcd.print("Saved ..."); delay(2000); menu=2;}
if (millis() - currentMillis > 30000) { lcd.clear(); break;}
}
//------------------------------- MENU 2 --------------------------------
while(menu==2){
lcd.setCursor(0, 0);
lcd.write(1);
lcd.setCursor(1, 0);
lcd.print(" ");
lcd.setCursor(2, 0);
lcd.print("Enter max T:");
lcd.setCursor(0, 1);
lcd.print(heatmax);
lcd.setCursor(3, 1);
lcd.write(3);
if (digitalRead(UP)) {heatmax++;delay(200);}
if (digitalRead(DOWN)) {heatmax--;delay(200);}
if (heatmax < 10 ) heatmax=10;
if (heatmax > 99) heatmax=99;
if (digitalRead(SET)) {EEPROM.write(2,heatmax);EEPROM.commit(); lcd.clear(); lcd.print("Saved ..."); delay(2000); menu=3;}
if (millis() - currentMillis > 60000) { lcd.clear(); break;}
}
//------------------------------- MENU 3 --------------------------------
while(menu==3){
lcd.setCursor(0, 0);
lcd.write(2);
lcd.setCursor(1, 0);
lcd.print(" ");
lcd.setCursor(2, 0);
lcd.print("Enter min H:");
lcd.setCursor(0, 1);
lcd.print(humimin);
lcd.setCursor(3, 1);
lcd.print("%");
if (digitalRead(UP)) {humimin++;delay(200);}
if (digitalRead(DOWN)) {humimin--;delay(200);}
if (humimin < 10 ) humimin=10;
if (humimin > 99) humimin=99;
if (digitalRead(SET)) {EEPROM.write(3,humimin); EEPROM.commit();lcd.clear(); lcd.print("Saved ..."); delay(2000); menu=4;}
if (millis() - currentMillis > 90000) { lcd.clear(); break;}
}
//------------------------------- MENU 4 --------------------------------
while(menu==4){
lcd.setCursor(0, 0);
lcd.write(2);
lcd.setCursor(1, 0);
lcd.print(" ");
lcd.setCursor(2, 0);
lcd.print("Enter max H:");
lcd.setCursor(0, 1);
lcd.print(humimax);
lcd.setCursor(3, 1);
lcd.print("%");
if (digitalRead(UP)) {humimax++;delay(200);}
if (digitalRead(DOWN)) {humimax--;delay(200);}
if (humimax < 10 ) humimax=10;
if (humimax > 99) humimax=99;
if (digitalRead(SET)) {EEPROM.write(4,humimax);EEPROM.commit(); lcd.clear(); lcd.print("Saved ..."); delay(2000); lcd.clear(); break;}
if (millis() - currentMillis > 120000) { lcd.clear(); break;}
}
{
EEPROM.end();}
}
void loop() {
Humidity = dht1.readHumidity();
Temp = dht1.readTemperature();
if (Temp<minTemp){minTemp = Temp;}
if (Temp>maxTemp){maxTemp = Temp;}
if (Humidity<minHumidity){minHumidity= Humidity;}
if (Humidity>maxHumidity){maxHumidity = Humidity;}
//----------------------------- Serial Print -----------------------------
if (isnan(Temp) || isnan(Humidity)) {
Serial.println("Failed to read from DHT #1");
} else {
Serial.print("Humidity: ");
Serial.print(Humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(Temp);
Serial.println(" *C\t");
Serial.print("MAX Humidity: ");
Serial.print(maxHumidity);
Serial.print(" %\t");
Serial.print("MIN Humidity: ");
Serial.print(minHumidity);
Serial.println(" %\t");
Serial.print("MAX Temperature: ");
Serial.print(maxTemp);
Serial.print(" *C\t");
Serial.print("MIN Temperature: ");
Serial.print(minTemp);
Serial.println(" *C\t");
}
//----------------------------- Relay Control -----------------------------------
if ( Temp < heatmin ) digitalWrite(HEATER,HIGH);
if ( Temp > heatmax ) digitalWrite(HEATER,LOW);
else digitalWrite(HEATER,HIGH);
if ( Humidity < humimin ) digitalWrite(STEAM,HIGH);
if ( Humidity > humimax ) digitalWrite(STEAM,LOW);
else digitalWrite(STEAM,HIGH);
//---------------------------- LCD ----------------------------------------------
switch (count){
case 1:
lcd.setCursor(0, 0);
lcd.write(1);
lcd.setCursor(2, 0);
lcd.print(Temp,1);
lcd.setCursor(6, 0);
lcd.write(3);
lcd.setCursor(8, 0);
lcd.write(5);
lcd.setCursor(9, 0);
lcd.print(maxTemp);
lcd.setCursor(12, 0);
lcd.write(4);
lcd.setCursor(13, 0);
lcd.print(minTemp);
lcd.setCursor(0,1);
lcd.write(2);
lcd.setCursor(2, 1);
lcd.print(Humidity,1);
lcd.setCursor(6, 1);
lcd.print("%");
lcd.setCursor(8, 1);
lcd.write(5);
lcd.setCursor(9, 1);
lcd.print(maxHumidity);
lcd.setCursor(12, 1);
lcd.write(4);
lcd.setCursor(13, 1);
lcd.print(minHumidity,1);
}
count++;
if (count>4) count=1;
delay(1000);
if (digitalRead(SET)) startup();
server.handleClient();
}