Hi Guys,
I have a small greenhouse in my backyard that I would like to monitor and control. The parts that I used are as follow:
- Wemos D1 R2
- Arduino sensor shield
- 16x2 I2C LCD
- 2 relay module, 1 is connected to a fan, 2 is connected to a humidifier
- Dht22
The first code that I wrote is about controlling the wemos using Blynk application. It works fine but the downside is I cannot set the setpoints for temp and humidity. I would like to implement EEPROM functions so that I would not lose my setting after reboot or power loss. Also, this device will be placed at another rural location with no internet connection. Therefore, I added 3 push buttons (Menu, Up, Down) so that I could set the setpoints manually.
The flow of the menu will be:
Page 1 (Homepage) will display Fan Status and Pump status on 1st line. Temp and humidity readings on 2nd line.
Page 2 and 3 will be setting the temperature higher and lower setpoints
Page 4 and 5 will be setting the humidity higher and lower setpoints.
If the menu button is pressed, the LCD display will return to homepage after a few seconds of no input and the setting will be saved.
#define BLYNK_TIMEOUT_MS 750
#define BLYNK_HEARTBEAT 17
#define BLYNK_PRINT Serial
#define DHTPIN D2 //arduino shield v5 pin 4
#define button D3
#define DHTTYPE DHT22
#define LCDWIDTH 16
#define LCDHEIGHT 2
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h"
#include <EEPROM.h>
BlynkTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
//char auth[] = ""; //blynk cloud
char auth[] = ""; //local server
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
//char server[] = "blynk-cloud.com";
//unsigned int port = 8442;
int LED = D5; //arduino shield v5 pin 7
int relayFan = D3; //arduino shield v5 pin 8
int relayHum = D4; //arduino shield v5 pin 9
int status = WL_IDLE_STATUS;
float hum;
float temp;
float humLow = 80.0;
float humHigh = 95.0;
float tempLow = 30.0;
float tempHigh = 32.0;
char blank[] = " ";
int fanStatus = 0;
int humStatus = 0;
char welcomeMessage[] = "TEMP & HUMIDITY METER";
char fanString[] = "FAN:";
char fanStringStatus[10] = "";
char pumpString[] = "PUMP:";
char pumpStringStatus[10] = "";
char statusString[25] = "FAN:OFF PUMP:OFF";
char ledOn[] = "LED ON";
char ledOff[] = "LED OFF";
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7);
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
dht.begin();
lcd.begin(16,2);
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight (HIGH);
lcd.clear();
lcd.home();
lcd.print(welcomeMessage); //if use this line sensor is working fine
// Debug console
Serial.begin(115200);
pinMode(LED, OUTPUT);
pinMode(button, INPUT);
pinMode(relayFan, OUTPUT);
pinMode(relayHum, OUTPUT);
Blynk.connectWiFi(ssid, pass);
timer.setInterval(11000L, CheckConnection);
Blynk.config(auth, IPAddress(192,168,1,188), 8080);
Blynk.connect();
//Blynk.begin(auth, ssid, pass);
Blynk.virtualWrite(V7, tempHigh);
Blynk.virtualWrite(V8, tempLow);
Blynk.virtualWrite(V9, humHigh);
Blynk.virtualWrite(V10, humLow);
}
void loop()
{
timer.run();
if(Blynk.connected())
{
Blynk.run();
}
readSensor();
printSerial();
printLcd();
switchFan(tempLow, tempHigh);
switchHum(humLow, humHigh);
}
BLYNK_WRITE(V3)
{
int pinValue = param.asInt(); // Assigning incoming value from pin V3 to a variable
if (pinValue == 1)
{
digitalWrite(LED, HIGH); // Turn LED on.
printLcdText(ledOn, 0, 1);
}
else
{
digitalWrite(LED, LOW); // Turn LED off.
printLcdText(ledOff, 0, 1);
}
}
BLYNK_WRITE(V7) //set tempHigh
{
float pinValue = param.asFloat(); // Assigning incoming value from pin V3 to a variable
int newTemp = ((pinValue + 0.005)*10);
tempHigh = (newTemp/10.0);
if(tempHigh > tempLow)
{
switchFan(tempLow, tempHigh);
}
else
{
tempHigh = tempLow; //set tempHigh = tempLow limit if tempHigh is lower than tempLow
Blynk.virtualWrite(V7, tempLow);
}
}
BLYNK_WRITE(V8) //set tempLow
{
float pinValue = param.asFloat(); // Assigning incoming value from pin V3 to a variable
int newTemp = ((pinValue + 0.005)*10);
tempLow = (newTemp/10.0);
if(tempLow<tempHigh)
{
switchFan(tempLow, tempHigh);
}
else
{
tempLow = tempHigh;
Blynk.virtualWrite(V8, tempHigh);
}
}
BLYNK_WRITE(V9) //set humHigh
{
float pinValue = param.asFloat(); // Assigning incoming value from pin V3 to a variable
int newHum = ((pinValue + 0.05)*10);
humHigh = (newHum/10.0);
if(humHigh> humLow)
{
switchHum(humLow, humHigh);
}
else
{
humHigh = humLow; //set tempHigh = tempLow limit if tempHigh is lower than tempLow
Blynk.virtualWrite(V9, humLow);
}
}
BLYNK_WRITE(V10) //set humLow
{
float pinValue = param.asFloat(); // Assigning incoming value from pin V3 to a variable
int newHum = ((pinValue + 0.05)*10);
humLow = (newHum/10.0);
if( humLow < humHigh)
{
switchHum(humLow, humHigh);
}
else
{
humLow = humHigh;
Blynk.virtualWrite(V10, humHigh);
}
}
void printLcdText(char *text, int lcdCol, int lcdRow)
{
lcd.setCursor(lcdCol, lcdRow);
lcd.print(blank);
lcd.setCursor(lcdCol, lcdRow);
lcd.print(text);
}
void readSensor()
{
// Wait a few seconds between measurements.
delay(2000);
hum = dht.readHumidity();
temp = dht.readTemperature();
}
void CheckConnection()
{
// check every 11s if connected to Blynk server
if(!Blynk.connected())
{
Serial.println("Not connected to Blynk server");
Blynk.connect(); // try to connect to server with default timeout
}
else{
Serial.println("Connected to Blynk server");
}
}
void printSerial()
{
Serial.println(statusString);
Serial.print(F("Temperature: "));
Serial.print(temp);
Serial.print(F("°C Humidity: "));
Serial.print(hum);
Serial.println(F("% "));
//setpoint printout
Serial.print(F("tempHigh: "));
Serial.print(tempHigh);
Serial.print(F(" tempLow: "));
Serial.println(tempLow);
Serial.print(F("humHigh: "));
Serial.print(humHigh);
Serial.print(F(" humLow: "));
Serial.println(humLow);
Serial.print(F("fanStatus: "));
Serial.println(fanStatus);
}
void printLcd()
{
if (isnan(hum) || isnan(temp))
{
lcd.setCursor(0,1);
lcd.print("Gagal sensor");
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
lcd.setCursor(0,0);
lcd.print(statusString);
lcd.setCursor(0,1);
lcd.print(F("T:"));
lcd.print(temp, 1);
lcd.print(F("C "));
lcd.print(F("H:"));
lcd.print(hum, 1);
lcd.print(F("%"));
updateBlynk();
}
void updateBlynk()
{
Blynk.virtualWrite(V5, hum);
Blynk.virtualWrite(V6, temp);
if (fanStatus == 0)
{
Blynk.virtualWrite(V11, "OFF");
}else
{
Blynk.virtualWrite(V11, "ON");
}
if (humStatus == 0)
{
Blynk.virtualWrite(V12, "OFF");
}else
{
Blynk.virtualWrite(V12, "ON");
}
}
void switchFan(float low, float high)
{
if (temp >= high)
{
digitalWrite(relayFan, HIGH); // Turn Fan on.
fanStatus = 1;
updateBlynk();
strcpy(statusString, fanString);
strcat(statusString, "ON ");
if (humStatus == 0)
{
strcat(statusString, pumpString);
strcat(statusString, "OFF");
}
else
{
strcat(statusString, pumpString);
strcat(statusString, "ON ");
}
}
else if(temp <= low)
{
digitalWrite(relayFan, LOW); // Turn Fan off.
fanStatus = 0;
updateBlynk();
strcpy(statusString, fanString);
strcat(statusString, "OFF ");
strcat(statusString, pumpString);
strcat(statusString, "OFF");
}
}
void switchHum(float lowH, float highH)
{
if (hum <= lowH && fanStatus == 1)
{
digitalWrite(relayHum, HIGH); // Turn Pump on.
humStatus = 1;
updateBlynk();
strcpy(statusString, fanString);
strcat(statusString, "ON ");
strcat(statusString, pumpString);
strcat(statusString, "ON ");
}
else if(hum >= highH && fanStatus ==1)
{
digitalWrite(relayHum, LOW); // Turn Pump off.
humStatus = 0;
updateBlynk();
strcpy(statusString, fanString);
strcat(statusString, "ON ");
strcat(statusString, pumpString);
strcat(statusString, "OFF");
}
}