Hi everybody
I could use some help with my project.
The system works fine, however, my problem is that I would like to count each time the relay is on (pin goes High) and to accumulative the number of times that the relay is on pin 03 (RX) during the time the system is powered.
I also would like to do the same with the relay time on and time on accumulatively.
I have the time running using Millis().
The counter keeps running every time timer runs currently set every 1 sec.
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#define BLYNK_DEBUG
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
BlynkTimer timer;
int target_Temp;
int temp_Down;
int temp_Up;
float debug = 0;
WidgetLED relayActive_LED(V1);
const char* ssid = "xxxxx";
const char* password = "xxxxx";
char auth[] = "xxxxxxxxxx";
// Variables will change:
int previousMillis = 0; // will store last time Relay was updated
unsigned long previousTime = 0;
byte secs ;
byte mins;
byte hours ;
int state = LOW;
int lastState = LOW;
int count = 0;
// ###########################
// this constant won't change:
const int relayPin = 03; // RX pin on ESP01 D1 pin that the Relay is attached to
// Variables that will change:
int relayCounter = 0; // counter for the number of on times
int relayState = 0; // current state of the Relay
int lastrelayState = 0; // previous state of the Relay
// ###########################
#define ONE_WIRE_BUS 02 // DS18B20 on 02 on Esp-01"
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float temp;
float Fahrenheit = 0;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, password);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, password, IPAddress(192,168,88,24), 8080);
// This function will run every time Blynk connection is established
DS18B20.begin();
timer.setInterval(2000L, getSendData);
pinMode(relayPin, OUTPUT);
}
void loop()
{
timer.run(); // Initiates SimpleTimer
Blynk.run();
}
BLYNK_CONNECTED()
{
//Blynk.syncVirtual(V5); //Get data stored in virtual pin V5 from server
// BLYNK_WRITE(V5);
Blynk.syncVirtual(V0);
}
//***********************************
BLYNK_WRITE(V0)//slider
{
target_Temp = param.asInt();
}
//***********************************
BLYNK_WRITE(V14) // target temp button down
{
Blynk.syncVirtual(V0); // read slider
temp_Down = param.asInt();
if (temp_Down == 1)
{
target_Temp = target_Temp - 1;
delay(100);
Blynk.virtualWrite(V5, target_Temp);
Blynk.virtualWrite(V0, target_Temp);
Blynk.virtualWrite(V19, 1939);
}
}
BLYNK_WRITE(V15) // target temp button up
{
Blynk.syncVirtual(V0); // read slider
temp_Up = param.asInt();
if (temp_Up == 1)
{
target_Temp = target_Temp + 1;
delay(100);
Blynk.virtualWrite(V5, target_Temp);
Blynk.virtualWrite(V0, target_Temp);
}
}
BLYNK_WRITE(3) // Led at V1 as Input
{
relayState = param.asInt(); // assigning incoming value from pin 3 (RX) to a variable
// however I do not get a high or a low
Blynk.virtualWrite(V19, relayState);
// V19 only displays the pin number 3
}
//***************************************************
// Send Sensor data
// **************************************************/
void getSendData()
{
DS18B20.requestTemperatures();
temp = DS18B20.getTempCByIndex(0); // Celcius
Fahrenheit = DS18B20.toFahrenheit(temp); // Fahrenheit
Serial.println(temp);
Serial.println(Fahrenheit);
Blynk.virtualWrite(V3, temp); // virtual pin V3 Celsius + Graph
Blynk.virtualWrite(V4, Fahrenheit); // virtual pin V4 Fahrenheit
Blynk.virtualWrite(V5, target_Temp); // Temperature Setpoint
if (target_Temp > temp)
{
digitalWrite(relayPin, 1); // Temp < Setpont turn relay on
relayActive_LED.on();
relayCounter++; // Keeps counting due to timer runs every 2 sec
Blynk.virtualWrite(V17, relayCounter);
}
else
{ digitalWrite(relayPin, 0); // Temp > target_Temp turn relay off
relayActive_LED.off();
Blynk.virtualWrite(V17, relayCounter);
}
// ######### Time ##############################
// Microsecs millis() is even more accurate
if (millis() >= (previousTime))
{
previousTime = previousTime + 1000; // use 100000 for uS
secs = secs + 1;
if (secs == 60)
{
secs = 0;
mins = mins + 1;
}
if (mins == 60)
{
mins = 0;
hours = hours + 1;
}
if (hours == 13)
{
hours = 1;
}
String secs_o = "Secs";
String mins_o = "Mins : ";
String hours_o = "Hrs : ";
Blynk.virtualWrite(V18, hours + hours_o + mins + mins_o + secs + secs_o );
Serial.print (hours, DEC);
Serial.print (":");
Serial.print (mins, DEC);
Serial.print (":");
Serial.println(secs, DEC);
}
// ############ End of Time####################################
}