Hey guys I have a problem on my test project. I have 3 different component to control in my NodeMCU
1-)Pump
2-)Led1
3-)FIDE(other LED)
In below code, everything seems fine with working below 1 hour. But when I increase the time to 18 hour (you may see the below code) , it does not seem work, i believe it resets itself.
Can you guys help me to solve it ? Thanks!
#if defined(ESP8266)
#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#else
#include <WiFi.h> //ESP32 Core WiFi Library
#include <WebServer.h> //Local DNS Server used for redirecting all requests to the configuration portal ( https://github.com/zhouhan0126/DNSServer---esp32 )
#endif
#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <DNSServer.h> //Local WebServer used to serve the configuration portal ( https://github.com/zhouhan0126/DNSServer---esp32 )
#include <WiFiManager.h> // WiFi Configuration Magic ( https://github.com/zhouhan0126/DNSServer---esp32 ) >> https://github.com/zhouhan0126/DNSServer---esp32 (ORIGINAL)
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <DHT.h>
const String baseUrl = “xxxxx”;
const String arduinoKey ="20200728102624511e69ebad07cc64bb7ae485257a4f06ae2";
const String urlControl="ProductControl/";
void setWiFi(){
WiFiManager wifiManager;
wifiManager.autoConnect(“wireless”);
}
const int rlyOn = HIGH;
const int rlyOff = LOW;
#define DHTPIN D1
#define DHTTYPE DHT21
DHT dht(DHTPIN, DHTTYPE,11);
unsigned long sensorMillis;
unsigned long sensorInterval;
unsigned long LedLastMillis;
unsigned long LedOnInterval;
unsigned long LedOffInterval;
#define RLY_WTRPUMP D2
#define RLY_LED D4
#define RLY_FIDE D8
#define BTN_LED D6
int prevBtnLed;
#define BTN_WTRPUMP D5
int prevBtnWtrPump;
unsigned long pumpLastMillis;
unsigned long pumpOnInterval;
unsigned long pumpOffInterval;
#define BTN_FIDE D7
int prevBtnFide;
unsigned long fideLastMillis;
unsigned long fideOnInterval;
unsigned long fideOffInterval;
#define WaterLevelPin D0
void registerDefaultValues(){ //intervals set for 3 components
sensorMillis = 0;
sensorInterval = 60000 * 15ul ; // 15 min
pumpLastMillis = 0;
pumpOnInterval = 60000 * 10ul ; // 10 min
pumpOffInterval = 60000 * 50ul ; // 50 min
LedLastMillis = 0;
LedOnInterval = 3600000 * 18ul; // 18 hour
LedOffInterval = 3600000 * 6ul; // 6 hour
fideLastMillis = 0;
fideOnInterval =3600000 * 18ul ; // 18 hour
fideOffInterval =3600000 * 6ul; // 6 hour
}
void setUp(){
dht.begin();
pinMode(RLY_LED, OUTPUT);
pinMode(BTN_LED, INPUT);
pinMode(RLY_WTRPUMP, OUTPUT);
pinMode(BTN_WTRPUMP, INPUT);
pinMode(RLY_FIDE, OUTPUT);
pinMode(BTN_FIDE, INPUT);
pinMode(WaterLevelPin,INPUT_PULLUP);
}
void manuelLedAutomation(){
int rlyCurrent = digitalRead(RLY_LED);
unsigned long currentMillis = millis();
unsigned long difInterval = (currentMillis - LedLastMillis);
int btnStatus=digitalRead(BTN_LED);
if(btnStatus==1)
{
if(rlyCurrent == LOW){
if(difInterval >= LedOffInterval){
LedLastMillis = currentMillis;
digitalWrite(RLY_LED,rlyOn);
}
else if(prevBtnLed==1)
{
digitalWrite(RLY_LED,rlyOn);
prevBtnLed=0;
}
}
else
{ // Işık açık ise
if(difInterval >= LedOnInterval){
LedLastMillis = currentMillis;
digitalWrite(RLY_LED,rlyOff);
}
}
}
else{
digitalWrite(RLY_LED,rlyOff);
prevBtnLed=1;
}
}
void manuelFideAutomation(){
unsigned long currentMillis = millis();
unsigned long difInterval = (currentMillis - fideLastMillis);
int rlyCurrent = digitalRead(RLY_FIDE);
int btnStatus = digitalRead(BTN_FIDE);
if(btnStatus==1)
{
if(rlyCurrent == LOW)
{
if(difInterval >= fideOffInterval){
fideLastMillis = currentMillis;
digitalWrite(RLY_FIDE,rlyOn);
}
else if(prevBtnFide==1){
digitalWrite(RLY_FIDE,rlyOn);
prevBtnFide=0;
}
}
else
{
if(difInterval >= fideOnInterval){
fideLastMillis = currentMillis;
digitalWrite(RLY_FIDE,rlyOff);
}
}
}
else{
digitalWrite(RLY_FIDE,rlyOff);
prevBtnFide=1;
}
}
void manuelPumpAutomation(){
unsigned long currentMillis = millis();
unsigned long difInterval = (currentMillis - pumpLastMillis);
int rlyCurrent = digitalRead(RLY_WTRPUMP);
int btnStatus=digitalRead(BTN_WTRPUMP);
if(btnStatus==1)
{
if(rlyCurrent == LOW){
if(difInterval >= pumpOffInterval)
{
pumpLastMillis = currentMillis;
digitalWrite(RLY_WTRPUMP,rlyOn);
}
else if(prevBtnWtrPump==1){
digitalWrite(RLY_WTRPUMP,rlyOn);
prevBtnWtrPump=0;
}
}
else
{
if(difInterval >= pumpOnInterval)
{
pumpLastMillis = currentMillis;
digitalWrite(RLY_WTRPUMP,rlyOff);
}
}
}
else
{
digitalWrite(RLY_WTRPUMP,rlyOff);
prevBtnWtrPump=1;
}
}
void saveWaterLevelAlert(){
if(WiFi.status() == WL_CONNECTED){
String url = baseUrl+"Arduino/alert";
DynamicJsonDocument doc(2048);
doc["arduinoKey"] = arduinoKey;
doc["type"] = 1;
doc["MessageTemplateCode"] = "CriticalWaterLevel";
String request="";
serializeJson(doc, request);
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type","application/json");
int httpCode = http.POST(request);
if(httpCode == 200){
String response = http.getString();
}
http.end();
}
}
void snoozeWaterLevelAlert(){
if(WiFi.status() == WL_CONNECTED){
String url = baseUrl+"Arduino/alert/snooze";
DynamicJsonDocument doc(2048);
doc["arduinoKey"] = arduinoKey;
doc["type"] = 1;
doc["MessageTemplateCode"] = "CriticalWaterLevel";
String request="";
serializeJson(doc, request);
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type","application/json");
int httpCode = http.POST(request);
if(httpCode == 200){
String response = http.getString();
}
http.end();
}
}
void checkWaterLevel(){
int waterLevel = digitalRead(WaterLevelPin);
if(waterLevel == LOW ){
saveWaterLevelAlert();
}
else if(waterLevel == HIGH ){
snoozeWaterLevelAlert();
}
}
void setSensorData(){
if(WiFi.status() == WL_CONNECTED){
// SENSOR SET
float h = dht.readHumidity();
float t = dht.readTemperature();
if (!isnan(h) && !isnan(t)) {
int temperature = (t * 100);
int humidity = (h * 100);
DynamicJsonDocument doc(2048);
doc["ArduinoKey"] = arduinoKey;
doc["Temperature"] = temperature;
doc["Humidity"] = humidity;
String request="";
serializeJson(doc, request);
HTTPClient http;
String url = baseUrl+"Sensor";
http.begin(url);
http.addHeader("Content-Type","application/json");
int httpCode = http.POST(request);
if(httpCode == 200){
String response = http.getString();
}
http.end();
}
}
}
void setup() {
//Serial.begin(115200);
setWiFi();
setUp();
registerDefaultValues();
}
void loop() {
unsigned long currentMillis = millis();
unsigned long difSensor = (currentMillis - sensorMillis); // send sensor everytime.
if(difSensor>=sensorInterval)
{
sensorMillis=currentMillis;
setSensorData();
checkWaterLevel();
}
manuelPumpAutomation();
manuelLedAutomation();
manuelFideAutomation();
}