calculate sunrise and sunup ?

68 views
Skip to first unread message

joergern gpunkt

unread,
Nov 25, 2020, 8:33:16 PM11/25/20
to Annex WiFi RDS
hi, have anyone a peace of code for Annex which is calculate the sunrise and sunup ?

cicciocb

unread,
Nov 26, 2020, 7:23:02 AM11/26/20
to Annex WiFi RDS
If your module is connected to internet, you can gather these information from OpenWeatherAPI.

Fernando Perez

unread,
Nov 26, 2020, 7:41:19 AM11/26/20
to Annex WiFi RDS
As cicciocb says, it may be the simplest way.
Register (if you aren't already) at openweathermap.org to get your free API KEY. Find out the ID of your city. Replace these two pieces of information in the following program.
It may seem strange, but I have used a push button to get data, as I think the number of queries in the free api is limited, and using a loop could quickly exhaust the daily quota.
The problem remains that the time obtained is in UTC format, but "that's another story".

id$ = "3109718" ' Santander (Spain)                    
api$ = "899exxxxxxxxxxxxxxxxxxxxxx" 'Here is your personal API

pin.mode 5, input, pullup    ' GPIO5 = D1 (Wemos D1 Mini)
interrupt 5, Get
wait

Get:
do
 pause 10 
loop until pin(5) = 0

do
  pause 10
loop until pin(5) = 1

wlog "getting data"

z$ = wget$("api.openweathermap.org/data/2.5/weather?id="+id$+"&appid="+api$, 80)
wlog z$

sunrise$ = unixtime$(VAL(json$(z$,"sunrise")))
sunset$ =  unixtime$(VAL(json$(z$,"sunset")))

wlog "sunrise: " + sunrise$ 
wlog "sunset: " + sunset$

return



Frank M

unread,
Nov 26, 2020, 10:49:30 AM11/26/20
to Annex WiFi RDS
Hello,
I use a sunset api to get these informations, where it is not necessary to register.
I wanted to optimize my solar heating system, but actually it is so cloudy, that I cannot test it in reality.

hope, that will help you
Frank M


CODE: xxxx.bas
'Auslesen von sunset sunrise depending on local coordinates
' My example is for Berlin (Germany)
' I use it in a program to control my solar heating unit
' description of the api is found at ' https://sunrise-sunset.org/api'


flag=1
CES=1 ' 1=MEZ,  2=MESZ
gosub analyseflag1
Wlog "aktueller Tag    :";datumeuro$
wlog "Sonnenaufgang    :";sonnenauf$
wlog "Sonnenuntergang  :";sonnenunter$
wlog "Mittagsstunde    :";mittagstunde$
wlog "Tageslaenge      :";tageslaenge$
end
 
analyseflag1:
   if flag=0 then goto analyseflag0  ' nur beim ersten Start die api auslesen
   test$ = WGET$("api.sunrise-sunset.org/json?lat=52.5243700&lng=13.4105300&date=today&formatted=0", 443)
   FILE.SAVE "/zeit.txt", test$  ' beim ersten Start String der api in Datei sichern
   analyseflag0:
   test$= FILE.READ$("/zeit.txt")  ' für alle weiteren Aufrufe api-string aus Datei holen
  wlog "teststring  "; test$
  sonnauf$ = json$(test$,"sunrise")  'holt die Daten und Zeiten fuer Sonnenaufgang aus der api
  t$ = right$(sonnauf$, 14) : t$=left$(t$,8)
  d$ = left$(sonnauf$, 10)
  day  = val(word$(d$,3,"-"))
  mth  = val(word$(d$,2,"-"))
  year = val(word$(d$,1,"-"))
   sonnenaufhour = val(word$(t$,1,":")) + CES         ' +1 = MEZ   '+2 fuer MESZ
  sonnenaufmin  = val(word$(t$,2,":"))
    if sonnenaufmin < 10 then sonnenaufmin$="0"+str$(sonnenaufmin) else sonnenaufmin$=str$(sonnenaufmin)
  sonnenauf$=str$(sonnenaufhour)+":"+sonnenaufmin$  ' Sonnenaufgangsstunde
  datumeuro$=str$(day)+"."+str$(mth)+"."+str$(year)  ' Sonnenaufgangsdatum - derzeit nirgendwo verwendet
    
  sonnunter$ = json$(test$,"sunset")   'holt die Daten und Zeiten fuer Sonnenuntergang aus der api
  t$ = right$(sonnunter$, 14) : t$=left$(t$,8)
  sonnenunterhour = val(word$(t$,1,":")) + CES   '+1 für MEZ  2 fuer MESZ
  sonnenuntermin  = val(word$(t$,2,":"))
  if sonnenuntermin < 10 then sonnenuntermin$="0"+str$(sonnenuntermin) else sonnenuntermin$=str$(sonnenuntermin)
  sonnenunter$=str$(sonnenunterhour)+":"+ sonnenuntermin$  'Sonnenuntergangsstunde
 
  
  sonnmittag$ = json$(test$,"solar_noon")
  t$ = right$(sonnmittag$, 14) : t$=left$(t$,8)
  mittaghour = val(word$(t$,1,":")) + CES       ' +1 für MEZ +2 fuer MESZ
  mittagmin  = val(word$(t$,2,":"))             ' Uhrzeit des höchsten Sonnenstandes 
 if mittagmin < 10 then mittagmin$="0"+str$(mittagmin) else mittagmin$=str$(mittagmin)
  mittagstunde$ = str$(mittaghour)+":"+ mittagmin$ 
  tageslaenge$ = json$(test$,"day_length")
  tlen= (sonnenunterhour*3600 +sonnenuntermin*60)  - (sonnenaufhour*3600 + sonnenaufmin * 60)
  tlen$=unixtime$(tlen)
  tageslaenge$=left$(tlen$,5)
return



Electroguard

unread,
Nov 26, 2020, 11:31:09 AM11/26/20
to Annex WiFi RDS
Another possibility is to detect it locally without internet using an Annex device with an LDR or similar behind a window, setting a variable eg: dark = 1 when the light value drops below an appropriate level.

I do this for many things, including syncing date & time with other non-internet devices from a 'time-server' device which has an RTC.

Yves BOURDON

unread,
Nov 26, 2020, 11:56:10 AM11/26/20
to Electroguard, Annex WiFi RDS
If you are interested  I wrote a code years ago wich computes what you need from NTP time and geographic location. I used Mikrobasic Pro for this. I can share the code and it will be easy to translate it using Annex !

Envoyé de mon iPhone 11 Pro

Le 26 nov. 2020 à 17:31, Electroguard <electrogua...@gmail.com> a écrit :


--
You received this message because you are subscribed to the Google Groups "Annex WiFi RDS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to annex_wifi_rd...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/annex_wifi_rds/3e62cb83-9616-4544-8340-a168fede3dd3n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages