Blynk+esp8266+HTU21D(si70)+relay2
Hello everyone, I want to create a climate control for my orchids, but I am new to working with Arduino IDE and Blynk. At the moment I have experience in creating a meteorological station on esp8266 and a Blynk as well as a Wi-Fi socket. I collected all this on the server blink.
But with the climate control I have trouble, I can’t write the project myself, so I found the one most suitable for my tasks and decided to modify it to fit my hardware. At the moment, the code is completely ready, but the hysteresis unit does not work for me (upon reaching setpoint does not work the relay) For 4 days I have been racking my brains and still can not understand why. I hope for your understanding and support
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <EEPROM.h>
#include <Adafruit_Si7021.h>
#include <SimpleTimer.h>
#define I2C_SCL 5 //d1
#define I2C_SDA 4 //d2
bool metric = false;
float hum3, temp5;
uint8_t temppin ; // LOW/HIGH
uint8_t eeprom_temp5 ; // Allow/Disallow
uint8_t min_temp5;
uint8_t max_temp5;
bool flag_temp5_off_state = true;
bool flag_temp5_on_state = true;
uint8_t lastsettemp5;
uint8_t hum3pin ; // LOW/HIGH
uint8_t eeprom_hum3 ; // Allow/Disallow
uint8_t min_hum3;
uint8_t max_hum3;
bool flag_fany_off_state = true;
bool flag_fany_on_state = true;
uint8_t lastm;
uint8_t lastbutton;
uint8_t lastp;
uint8_t lastsethum3;
char auth[] = "9babcd88889420000b8565574b3f1";
char ssid[] = "999";
char pass[] = "000";
Adafruit_Si7021 sensor;
SimpleTimer timer1; // sensor timer
SimpleTimer timer2; // humcheck
SimpleTimer timer3; // tempcheck
unsigned long timestamp3;
BLYNK_WRITE(V50) // reset
{
int pinValue = param.asInt();
if (pinValue != lastp && pinValue > 0)
{
lastp = 0;
ESP.reset();
}
}
//---------------------TEMP---------------------------------
BLYNK_WRITE(V19) // write EEPROM temp
{
int pinValue = param.asInt();
if (pinValue != lastp && pinValue > 0 && eeprom_temp5 == 1)
{
lastp = 0;
int EEaddress = 164;
EEPROM.write(EEaddress, min_temp5);
EEaddress++;
EEPROM.write(EEaddress, max_temp5);
EEaddress++;
EEPROM.write(EEaddress, temppin);
EEPROM.commit();
//ESP.reset();
}
}
BLYNK_WRITE(V25) // eeprom access temp
{
int pinValue = param.asInt();
eeprom_temp5 = pinValue;
}
BLYNK_WRITE(V26) // level temp
{
int pinValue = param.asInt();
temppin = pinValue;
}
BLYNK_WRITE(V20) // setup temp
{
int pinValue = param.asInt();
lastsettemp5 = pinValue;
}
BLYNK_WRITE(V21) // temp down
{
int pinValue = param.asInt();
if (lastsettemp5 == 1)
{
if (pinValue != lastp && pinValue > 0 && min_temp5 > 0)
{
lastp = 0;
min_temp5--;
Blynk.virtualWrite(V23, min_temp5);
}
}
if (lastsettemp5 == 0)
{
if (pinValue != lastp && pinValue > 0 && max_temp5 - min_temp5 > 1)
{
lastp = 0;
max_temp5--;
Blynk.virtualWrite(V24, max_temp5);
}
}
}
BLYNK_WRITE(V22) // temp up
{
int pinValue = param.asInt();
if (lastsettemp5 == 1)
{
if (pinValue != lastp && pinValue > 0 && max_temp5 - min_temp5 > 1)
{
lastp = 0;
min_temp5++;
Blynk.virtualWrite(V23, min_temp5);
}
}
if (lastsettemp5 == 0)
{
if (pinValue != lastp && pinValue > 0 && max_temp5 < 100)
{
lastp = 0;
max_temp5++;
Blynk.virtualWrite(V24, max_temp5);
}
}
}
//---------------------TEMP END-----------------------------
//---------------------HUM---------------------------------
BLYNK_WRITE(V13) // write EEPROM hum
{
int pinValue = param.asInt();
if (pinValue != lastp && pinValue > 0 && eeprom_hum3 == 1)
{
lastp = 0;
int EEaddress = 161;
EEPROM.write(EEaddress, min_hum3);
EEaddress++;
EEPROM.write(EEaddress, max_hum3);
EEaddress++;
EEPROM.write(EEaddress, hum3pin);
EEPROM.commit();
//ESP.reset();
}
}
BLYNK_WRITE(V9) // eeprom access hum
{
int pinValue = param.asInt();
eeprom_hum3 = pinValue;
}
BLYNK_WRITE(V12) // level hum
{
int pinValue = param.asInt();
hum3pin = pinValue;
}
BLYNK_WRITE(V14) // setup hum
{
int pinValue = param.asInt();
lastsethum3 = pinValue;
}
BLYNK_WRITE(V15) // hum down
{
int pinValue = param.asInt();
if (lastsethum3 == 1)
{
if (pinValue != lastp && pinValue > 0 && min_hum3 > 0)
{
lastp = 0;
min_hum3--;
Blynk.virtualWrite(V16, min_hum3);
}
}
if (lastsethum3 == 0)
{
if (pinValue != lastp && pinValue > 0 && max_hum3 - min_hum3 > 1)
{
lastp = 0;
max_hum3--;
Blynk.virtualWrite(V18, max_hum3);
}
}
}
BLYNK_WRITE(V17) // hum up
{
int pinValue = param.asInt();
if (lastsethum3 == 1)
{
if (pinValue != lastp && pinValue > 0 && max_hum3 - min_hum3 > 1)
{
lastp = 0;
min_hum3++;
Blynk.virtualWrite(V16, min_hum3);
}
}
if (lastsethum3 == 0)
{
if (pinValue != lastp && pinValue > 0 && max_hum3 < 100)
{
lastp = 0;
max_hum3++;
Blynk.virtualWrite(V18, max_hum3);
}
}
}
//---------------------HUM END-----------------------------
void tempcheck()
{
if (temp5 <= min_temp5 && flag_temp5_on_state)
{
if (temppin == 1 )
{
digitalWrite(15, LOW); // вкл
Blynk.virtualWrite(V27, 1023); // вкл
Blynk.virtualWrite(V28, 0); // выкл
} else {
digitalWrite(15, HIGH); // выкл
Blynk.virtualWrite(V27, 0); // выкл
Blynk.virtualWrite(V28, 1023); // вкл
}
digitalWrite(15, LOW); // вкл
Blynk.virtualWrite(V10, 1023); // вкл
flag_temp5_on_state = false;
flag_temp5_off_state = true;
}
if (temp5 >= max_temp5 && flag_temp5_off_state)
{
if (temppin == 1 )
{
digitalWrite(15, HIGH); // вкл
Blynk.virtualWrite(V27, 0); // вкл
Blynk.virtualWrite(V28, 1023); // выкл
} else {
digitalWrite(15, LOW); // выкл
Blynk.virtualWrite(V27, 1023); // выкл
Blynk.virtualWrite(V28, 0); // выкл
}
digitalWrite(15, HIGH); // выкл
Blynk.virtualWrite(V10, 0); // выкл
flag_temp5_off_state = false;
flag_temp5_on_state = true;
}
}
void humcheck()
{
if (hum3 <= min_hum3 && flag_fany_on_state)
{
if (hum3pin == 1 )
{
digitalWrite(13, LOW); // вкл
Blynk.virtualWrite(V10, 1023); // вкл
Blynk.virtualWrite(V11, 0); // выкл
} else {
digitalWrite(13, HIGH); // выкл
Blynk.virtualWrite(V10, 0); // выкл
Blynk.virtualWrite(V11, 1023); // выкл
}
digitalWrite(13, LOW); // увлажнитель вкл
Blynk.virtualWrite(V10, 1023); // увлажнитель вкл
flag_fany_on_state = false;
flag_fany_off_state = true;
}
if (hum3 >= max_hum3 && flag_fany_off_state)
{
if (hum3pin == 0 )
{
digitalWrite(13, HIGH); // вкл
Blynk.virtualWrite(V10, 0); // вкл
Blynk.virtualWrite(V11, 1023); // выкл
} else {
digitalWrite(13, LOW); // выкл
Blynk.virtualWrite(V10, 1023); // выкл
Blynk.virtualWrite(V11, 0); // выкл
}
digitalWrite(13, HIGH); // увлажнитель выкл
Blynk.virtualWrite(V10, 0); // увлажнитель выкл
flag_fany_off_state = false;
flag_fany_on_state = true;
}
}
void sendSensor1()
{
float temp5 = sensor.readTemperature();
float hum3 = sensor.readHumidity();
Blynk.virtualWrite(V3, hum3);
Blynk.virtualWrite(V5, temp5);
}
void readEEPROM(int startAdr, int maxLength, char* dest)
{
EEPROM.begin(256);
delay(10);
for (int i = 0; i < maxLength; i++)
{
dest[i] = char(EEPROM.read(startAdr + i));
}
EEPROM.end();
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
Wire.begin(I2C_SDA, I2C_SCL);
EEPROM.begin(256);
min_hum3 = EEPROM.read(161);
max_hum3 = EEPROM.read(162);
hum3pin = EEPROM.read(163);
min_temp5 = EEPROM.read(164);
max_temp5 = EEPROM.read(165);
temppin = EEPROM.read(166);
timer1.setInterval(1500L, sendSensor1);
timer2.setInterval(500L, humcheck);
timer3.setInterval(500L, tempcheck);
pinMode(13, OUTPUT); // увлажнитель
digitalWrite(13, HIGH); // увлажнитель выкл
pinMode(15, OUTPUT); //
digitalWrite(15, HIGH); //
Blynk.virtualWrite(V16, min_hum3);
Blynk.virtualWrite(V18, max_hum3);
Blynk.virtualWrite(V24, max_temp5);
Blynk.virtualWrite(V23, min_temp5);
if (min_hum3 <= 30 || min_hum3 >= 99 || max_hum3 <= 30 || max_hum3 >= 99) {
min_hum3 = 45;
max_hum3 = 50;
}
if (min_temp5 <= 0 || min_temp5 >= 99 || max_temp5 <= 0 || max_temp5 >= 99) {
min_temp5 = 20;
max_temp5 = 25;
}
if (!sensor.begin())
{
Serial.println("Couldn't find sensor!");
while (1);
}
}
void loop()
{
timer1.run();
timer2.run();
timer3.run();
Blynk.run();
}