I think that it’s working now, but it desconect, maybe because of server flooding error, does’t say but I think. It’s conectects to the server an then disconect whitout any error messange. This is the new code, only add the RTC in the code and in the app.
#include <SoftwareSerial.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <TimeLib.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WidgetRTC.h>
#define BLYNK_PRINT Serial
#define echoPin 3 // Echo Pin
#define trigPin 5 // Trigger Pin
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
BlynkTimer timer; // define a timer for use by SimpleTimer library
WidgetRTC rtc;
int WM1=6;
int EncWV1=6; // Global variable used in Slider widget and runEveryMinute()
int ApagWV1=6; // Global variable used in Slider widget and runEveryMinute()
int WM2=7;
int EncWV2=7; // Global variable used in Slider widget and runEveryMinute()
int ApagWV2=7; // Global variable used in Slider widget and runEveryMinute()
int x, y, z, a;
int CounterOn = 0;
int CounterOff = 0;
int ledStatus = 0;
int CounterOn2 = 0;
int CounterOff2 = 0;
int ledStatus2 = 0;
int AutoRelleno=8;
int SumpLamp=9;
float Celsius = 0;
float Fahrenheit = 0;
char auth[] = "c046b3f28da144598e5119d4d2cfb74d";
long duration, distance; // Duration used to calculate distance
int NivelMax=0;
int NivelMin=0;
BLYNK_CONNECTED()
{
Blynk.syncAll(); // Synchronize hardware with App widgets when connected
}
BLYNK_WRITE(V0)
{ // add a slider to your project on V0 range 0 to 120 (seconds)
x = param.asInt(); // set variable as Slider value
EncWV1 = x;
Serial.print("Wave Maker 1 On Timer set to: ");
Serial.print(EncWV1);
Serial.println(" seconds");
}
BLYNK_WRITE(V1)
{ // add a slider to your project on V1 range 0 to 120 (seconds)
y = param.asInt(); // set variable as Slider value
ApagWV1 = y;
Serial.print("Wave Maker 1 Off Timer set to: ");
Serial.print(ApagWV1);
Serial.println(" seconds");
}
BLYNK_WRITE(V2)
{ // add a slider to your project on V0 range 0 to 120 (seconds)
z = param.asInt(); // set variable as Slider value
EncWV2 = z;
Serial.print("Wave Maker 2 On Timer set to: ");
Serial.print(EncWV2);
Serial.println(" seconds");
}
BLYNK_WRITE(V3)
{ // add a slider to your project on V1 range 0 to 120 (seconds)
a = param.asInt(); // set variable as Slider value
ApagWV2 = a;
Serial.print("Wave Maker 2 Off Timer set to: ");
Serial.print(ApagWV2);
Serial.println(" seconds");
}
BLYNK_WRITE(V7)
{ // add a slider to your project on V1 range 0 to 120 (seconds)
NivelMax = param.asInt(); // set variable as Slider value
Serial.print("Encender bomba en: ");
Serial.print(NivelMax);
Serial.println(" segundos");
}
BLYNK_WRITE(V8)
{ // add a slider to your project on V0 range 0 to 120 (seconds)
NivelMin = param.asInt(); // set variable as Slider value
Serial.print("Encender bomba en: ");
Serial.print(NivelMin);
Serial.println(" segundos");
}
/*
BLYNK_WRITE(V10) {
TimeInputParam t(param);
// Process start time
if (t.hasStartTime())
{
Serial.println(String("Start: ") +
t.getStartHour() + ":" +
t.getStartMinute() + ":" +
t.getStartSecond());
digitalWrite(SumpLamp,HIGH);
WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
SL.on();
}
else if (t.isStartSunrise())
{
Serial.println("Start at sunrise");
digitalWrite(SumpLamp,HIGH);
WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
SL.on();
}
else if (t.isStartSunset())
{
Serial.println("Start at sunset");
digitalWrite(SumpLamp,HIGH);
WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
SL.on();
}
else
{
// Do nothing
}
// Process stop time
if (t.hasStopTime())
{
Serial.println(String("Stop: ") +
t.getStopHour() + ":" +
t.getStopMinute() + ":" +
t.getStopSecond());
digitalWrite(SumpLamp,LOW);
WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
SL.off();
}
else if (t.isStopSunrise())
{
Serial.println("Stop at sunrise");
digitalWrite(SumpLamp,LOW);
WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
SL.off();
}
else if (t.isStopSunset())
{
Serial.println("Stop at sunset");
digitalWrite(SumpLamp,LOW);
WidgetLED SL(9); //means you need to have LED on app configured on virtual pin 1.
SL.off();
}
else
{
// Do nothing: no stop time was set
}
// Process timezone
// Timezone is already added to start/stop time
Serial.println(String("Time zone: ") + t.getTZ());
// Get timezone offset (in seconds)
Serial.println(String("Time zone offset: ") + t.getTZ_Offset());
// Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
for (int i = 1; i <= 7; i++) {
if (t.isWeekdaySelected(i)) {
Serial.println(String("Day ") + i + " is selected");
}
}
Serial.println();
}*/
void led_control()
{
//First Wave Maker
if ((CounterOn > 0) && (ledStatus == 0))
{
digitalWrite(WM1,HIGH); //write to pin to turn the LED on.
Serial.print("Wave Maker 1: ON\n");
ledStatus = 1;
CounterOff = 0;
}
if ((CounterOn > 0) && (ledStatus == 1))
{
CounterOn = (CounterOn - 1);
}
if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 1))
{
CounterOff = ApagWV1;
}
if ((CounterOff > 0) && (ledStatus == 1))
{
digitalWrite(WM1,LOW); //write to pin to turn the LED off.
Serial.print("Wave Maker 1: OFF\n");
ledStatus = 0;
CounterOn = 0;
}
if ((CounterOff > 0) && (ledStatus == 0))
{
CounterOff = (CounterOff - 1);
}
if ((CounterOff == 0) && (CounterOn == 0) && (ledStatus == 0))
{
CounterOn = EncWV1+1;
}
//Second Wave Maker
if ((CounterOn2 > 0) && (ledStatus2 == 0))
{
digitalWrite(WM2,HIGH); //write to pin to turn the LED on.
Serial.print("Wave Maker 2: ON\n");
ledStatus2 = 1;
CounterOff2 = 0;
}
if ((CounterOn2 > 0) && (ledStatus2 == 1))
{
CounterOn2 = (CounterOn2 - 1);
}
if ((CounterOff2 == 0) && (CounterOn2 == 0) && (ledStatus2 == 1))
{
CounterOff2 = ApagWV2;
}
if ((CounterOff2 > 0) && (ledStatus2 == 1))
{
digitalWrite(WM2,LOW); //write to pin to turn the LED off.
Serial.print("Wave Maker 2: OFF\n");
ledStatus2 = 0;
CounterOn2 = 0;
}
if ((CounterOff2 > 0) && (ledStatus2 == 0))
{
CounterOff2 = (CounterOff2 - 1);
}
if ((CounterOff2 == 0) && (CounterOn2 == 0) && (ledStatus2 == 0))
{
CounterOn2 = EncWV2+1;
}
}
void MeasureCm()
{
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
Serial.print("Distance: ");
Serial.println(distance);
Blynk.virtualWrite(4,distance);
if (distance>NivelMax)
{
digitalWrite(AutoRelleno, HIGH);
WidgetLED AR(5); //means you need to have LED on app configured on virtual pin 1.
AR.on();
Serial.print("Bomba de Autorelleno Encendida");
}
if (distance<NivelMin)
{
digitalWrite(AutoRelleno, LOW);
WidgetLED AR(5); //means you need to have LED on app configured on virtual pin 5.
AR.off();
Serial.print("Bomba de Autorelleno Apagada");
}
}
void Temperatura()
{
sensors.requestTemperatures();
Celsius = sensors.getTempCByIndex(0);
Blynk.virtualWrite(6,Celsius);
Serial.print("Temperatura: ");
Serial.print(Celsius);
Serial.print(" C");
}
void setup()
{
if (EncWV1== 0)
{
EncWV1=1;
}
if (ApagWV1== 0)
{
ApagWV1=1;
}
sensors.begin();
Serial.begin(9600);
Blynk.begin(auth);
rtc.begin();
CounterOn = EncWV1+1;
// run timer every 1 second to check for led On/Off action
timer.setInterval(1000L, led_control);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
timer.setInterval(1000L, MeasureCm);
timer.setInterval(20000L, Temperatura);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates Timer to ensure the timer keeps running
}
I have to comment the code for advanced time input because what I said before, It’s desconects from the server. I’ll use your example @Gunner, it’s pretty much what I want to do, I’ll only miss the part of sunset/sunrise working.