Activity before sunrise or just before sunset

I have a NodeMCU (esp8266) and I written a program in arduino ide to water my vege patch based on moisture content.

Now I am looking at watering only an hour each side of. Sunrise and same at sunset if the moisture is low.

The timing doesn’t need to be accurate though.

So I think the best way to set the times is to use the “time input” in the Blynk app and write these to a virtual pin.

Any suggestions what’s best for checking if the time lays in either of the brackets. Was thinking of using the rtc of the nodemcu and updating it online. Then checking if the time is between the sunrise / sunset values of the virtual pins. Thus seems like I’ll need to read up more on that though.

Any other suggestions? Either easier ways or better ways?

The timing csn be 10 minutes out I don’t care.

Search for @Costas “EziScheduler” (or look here). Setting sunrise/sunet with Time Input WIdget is easy. Making it +/- 1 hour probably needs some creative coding in the scheduler :slight_smile:

So I could consider the following way (Costas post/code mods) included in here as well yes?

I had errors when I tried it initially, something about the WidgetRTC rtc comment I think off memory… If this is a good way I’ll read more into this method. I assume the time() returns the time in seconds which is what I’m after…

I think that code is a bit outdated, but I’m not sure. If you search for “time input” and sort them by “latest post” there is a lot more recent examples.

@Pete_Repeat latest version of EziScheduler is awesome :wink: but I think if I was looking at it today I might consider Webhook with http://domogeek.entropialux.com/static/doc/index.html#api-Domogeek-GetSun

Edit: updated syntax for urls

  1. sunrise and sunset for Nicosia tomorrow
    http://domogeek.entropialux.com/sun/nicosia/all/tomorrow

{"dayduration": "9:51", "sunset": "15:46", "zenith": "10:50", "sunrise": "5:55"}

  1. sunrise and sunset for London today
    http://domogeek.entropialux.com/sun/london/all/now

{"dayduration": "7:56", "sunset": "17:02", "zenith": "13:04", "sunrise": "9:06"}

Thanks for both of your suggestions.

Is there anything wrong with the “time input” way of getting sunrise and sunset? Reason being is I can over ride it in Blynk to be different times if I wish rather than having to reupload code (I could use a flag to override the time restrictions though I guess)…

Any suggestions on the easiest reasonable way to get the time. I don’t need precision like a rtc that is constantly updated online, but is that worth persuing and reading up the details about why I was getting errors when I tried? Blynk also has a tool for rtc built in yes? Is that worth persuing?

I found a web page that gives 3 different ways to get the time for a esp8266 but ea have of them requires a little work with daylight savings and converting from gmt (yes I know that’s not overly difficult).

I will do some more searching in a few hours such as searching time input as suggested however.

Edit: leaning towards the rtc widget and using the example code in the RTC sketch in Blynk if that is still one suggested way?

I’ve had a look at eventor and that seems to be easy to implement by using a virtual pin as a flag for the periods of time (rough times based around sunrise and sunset)… So that seems like an easy way. Except if there is no internet then I assume the watering won’t happen…

So maybe consider the internal rtc on the nodemcu (assuming my firmware has it) and update online every hour or so…

Or is the online time check reliable enough? I could look at if there is no time found online then I could use a millis counter since last time it got the time to add to the last recorded time update online…

I know there is lots online but I’m still not sure the best way…

I will keep reading so don’t waste your own time unless it’s easy to inform/steer me.

OK. SO I have moved to the rtc of Blynk which appears it doesn’t rely on being online all the time.

So the following code (the rtcTimeSeconds = hour() * 3660 + minute() * 60 + second(); ) should give the real time clock results in seconds since midnight yes? And the V20 and V21 from the Blynk time inputs should also be in seconds since midnight?

Also, the rtc will still run when not online? But only update every hour if there is internet (frequency defined by setSyncInterval(3600) ). If there is no internet when a sync occurs then the rtc will remain as it was and not be a value of 0 or something because of the problem with the sync?

The other code is quite long and hard to explain so I have omitted most of the code and just left the rtc and comparisons.

V20 is a timer input (morning time set in Blynk eg 9am or sunrise in SECONDS???)
V21 is a timer input (afternoon time in Blynk eg 6pm or sunset in SECONDS???)

V23 is a slider so when it is a value of 1 the watering can happen whenever, or 0 to never water.

wateringTimePeriod is a flag to allow watering to occur if the soil moisture is low. This can be true when the rtc is between the time periods or if the slider V23 is a value of 1.

All time variables are unsigned long.

And I need to include the rtc widget in Blynk for the particular remote.

This is before setup()

#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = "##"; // Blynk code

// Your WiFi credentials.
char ssid[] = "##"; // wifi name
char pass[] = "##"; // wifi password


BlynkTimer timer;

WidgetRTC rtc;
BLYNK_WRITE(V20)
{
  morningWaterWindowMid = param.asInt(); // assigning incoming value from pin V20 to a variable
  morningWaterWindowStart = morningWaterWindowMid - 3600;
  morningWaterWindowEnd = morningWaterWindowMid + 3600;
}

BLYNK_WRITE(V21)
{
  afternoonWaterWindowMid = param.asInt(); // assigning incoming value from pin V5 to a variable
  afternoonWaterWindowStart = afternoonWaterWindowMid - 9000;
  afternoonWaterWindowEnd = afternoonWaterWindowMid - 1800;
}

BLYNK_WRITE(V23)
{
  wateringWindowOverride = param.asInt(); // assigning incoming value from pin V5 to a variable
}

This is in setup()

  setSyncInterval(3600);
  rtc.begin();

This section is in a function that is run every period of time (Blynk interval timer)

rtcTimeSeconds = hour() * 3660 + minute() * 60 + second();

  if (((rtcTimeSeconds >= morningWaterWindowStart) && (rtcTimeSeconds <= morningWaterWindowEnd)) || ((rtcTimeSeconds >= afternoonWaterWindowStart) && (rtcTimeSeconds <= afternoonWaterWindowEnd)))
  {
    wateringTimePeriodCurrent = 1;
  }
  else
  {
    if (wateringWindowOverride == 1)
    {
      wateringTimePeriodCurrent = 1;
    }
    else
    {
    wateringTimePeriodCurrent = 0;
    }
  }

You have stated 3660 at least twice in your last post, think you mean 3600.

Well picked up thanks. That line of my code was a mistake with 3660 and I copied that line to the post twice… Best fix that.

The rest of the concept seems ok though? Particularly the bold part. From what I have read I think I have it right.

These times are a bit odd.
I’m in London at the moment and the sun rises at about 08:00 (I was actually up and out to see it today for a change!) and sets around 16:00
My first thought was that maybe the times are UTC/Zulu time, but as we’re not on British SummerTime yet there is no difference between GMT and UTC.

It looks like the Domogeek website is French, and the French are on GMT+1 at the moment, so the results returned are correct from a French perspective!
That’s okay of you’re in the same timezone as our cheese-eating friends, but a bit strange if not.

Pete.

1 Like

Remember there are several different sunrise and sunset times! You got astronomical, nautical:

From https://sunrise-sunset.org/api :

{
      "results":
      {
        "sunrise":"7:27:02 AM",
        "sunset":"5:05:55 PM",
        "solar_noon":"12:16:28 PM",
        "day_length":"9:38:53",
        "civil_twilight_begin":"6:58:14 AM",
        "civil_twilight_end":"5:34:43 PM",
        "nautical_twilight_begin":"6:25:47 AM",
        "nautical_twilight_end":"6:07:10 PM",
        "astronomical_twilight_begin":"5:54:14 AM",
        "astronomical_twilight_end":"6:38:43 PM"
      },
       "status":"OK"
    }

I believe there is only one “sunrise” and “sunset” time but domogeek does give French time.

1 Like

I think there’s only one official sunrise and sunset time for a given location. Sunrise is the time when the upper edge of the sun appears over the horizon and sunset is when the upper edge of the sun disappears below the horizon.
There’s a variety of different definitions for “Twilight”, which is when the sun isn’t visible, but it’s still light. The sunrise-sunset website says:

“WHAT EXACTLY ARE THE SUNRISE AND SUNSET?
It’s important to note that both sunrise and sunset are ‘instants’.
The time range during which the day becomes night or vice versa is called twilight. We can distinguish between the morning twilight, that happens between dawn and sunrise and the evening twilight, that happens between sunset and dusk each day. Duration of twilight actually depends on our position on Earth and date of year. For example at Arctic and Antarctic latitudes, on winter, night never gets completely dark.”

For London the sunrise-sunset API gives these numbers for today:

“sunrise”:“8:05:33 AM”,
“sunset”:“4:04:54 PM”,
“solar_noon”:“12:05:13 PM”,
“day_length”:“07:59:21”,
“civil_twilight_begin”:“7:25:58 AM”,
“civil_twilight_end”:“4:44:28 PM”,
“nautical_twilight_begin”:“6:43:12 AM”,
“nautical_twilight_end”:“5:27:14 PM”,
“astronomical_twilight_begin”:“6:02:40 AM”,
“astronomical_twilight_end”:“6:07:47 PM”

and the Dopmogeek website gives these numbers:

“dayduration”: “7:58”,
“sunset”: “17:04”,
“zenith”: “13:05”,
“sunrise”: “9:06”

They both agree that the day will be a fraction under 8 hours long, its just that the Domogeek times are an hour later (and therefore wrong by an hour) compared to those from sunrise-sunset.org

Fortunately the OP only needed an approximate time to water his plants, and either set of times would probably have been okay, but I flagged it up because the inaccuracy of the Domogeek data in relation to local time might catch people out in some more critical situations.
I suppose that for watering plants it is actually the sunrise/set times that are needed, but for most timing applications people will probably be thinking about when lights need to be switched on and off - in which case one of the various flavours of twilight will be more useful to them, so your sunrise-sunset API is probably the best choice anyway.

Time to go and make a coffee and watch the sunrise :grinning:

Pete.

1 Like

True that. If you look at it from a mathematical standpoint, lol. I agree on the sunrise though, I got my appartment on the east side of things … :wink:

I agree the watering is not an exact science. Guess twilight could be indeed handy for some people (vampires and such…).

1 Like

Yeh I’m not that fussed with precise timings of sunset…

I’m still curious why use the http request/website over what’s built into the Blynk timer input, other than the cost to put the widgets on the remote. Both still need the internet anyway huh? Just the Blynk one has more flexibility.

Whichever method you use will require an element of coding.

Webhook is very powerful and flexible so a route I would consider.

1 Like

Ha, that made me smile!
Now I know where to point people when they ask
“Plz help me. I am vampire and need Blynk to close my coffin lid when it is daytime. I am using Arduino and ESP01 wot code do I need?”

Pete.

1 Like

Well, this is a bit off-topic, but we do have a haunted mansion on the model railroad club with coffins and such …

Enjoy :wink:

2 Likes