-
Notifications
You must be signed in to change notification settings - Fork 8
/
Sonne.h
133 lines (109 loc) · 5.38 KB
/
Sonne.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
This software is a QUICK&DIRTY SW for debugging/controlling the Hoymiles inverters over RF(NRF24)
Based on the orig. SW from Hubi's earlier stage from his (https://github.com/hm-soft/Hoymiles-DTU-Simulation)
recoded and expanded for the Hoymiles microinverter family MI, by Ziyat T.
Project initiated here: https://www.mikrocontroller.net/topic/525778
Do not expect any quality from this SW!!!
------------------------------------------------------------------------------------------------------------------------
Configuration are in Settings.h and secrets.h !!
------------------------------------------------------------------------------------------------------------------------
*/
#ifndef __SONNE_H
#define __SONNE_H
#include <TimeLib.h>
#include "Settings.h"
#include "Debug.h"
#include "wifi.h"
static long SunDown, SunUp;
static uint16_t YYYY, MON, DD, HH, MiN, SEK;
void calcSunUpDown (time_t date) {
//--------------------------------------------------------------------------------------------------------------
//SunUpDown res = new SunUpDown();
boolean isSummerTime = true; // TODO TimeZone.getDefault().inDaylightTime(new Date(date));
//- Bogenma�
double brad = geoBreite / 180.0 * PI;
// - H�he Sonne -50 Bogenmin.
double h0 = -50.0 / 60.0 / 180.0 * PI;
//- Deklination dek, Tag des Jahres d0
int tage = 30 * month(date) - 30 + day(date);
double dek = 0.40954 * sin (0.0172 * (tage - 79.35));
double zh1 = sin (h0) - sin (brad) * sin(dek);
double zh2 = cos(brad) * cos(dek);
double zd = 12*acos (zh1/zh2) / PI;
double zgl = -0.1752 * sin (0.03343 * tage + 0.5474) - 0.134 * sin (0.018234 * tage - 0.1939);
//-Sonnenuntergang
double tsu = 12 + zd - zgl;
double su = (tsu + (15.0 - geoLaenge) / 15.0);
int std = (int)su;
int minute = (int) ((su - std)*60);
if (isSummerTime) std++;
SunDown = (100*std + minute) * 100;
//- Sonnenaufgang
double tsa = 12 - zd - zgl;
double sa = (tsa + (15.0 - geoLaenge) /15.0);
std = (int) sa;
minute = (int) ((sa - std)*60);
if (isSummerTime) std++;
SunUp = (100*std + minute) * 100;
//DEBUG_OUT.printf("Sonnenaufgang %i Sonnenuntergang %i\r\n", SunUp,SunDown);
}//----calcSunUpDown----------------------------------------------------------------------------------------------------
void getDate(int tz_offset){
//----------------------------------------------------------------------------------------------------------------------
// we need hour as an integer, in zeroexprot later, lets calculate every date ;-)
time_t localtime = getNow() + (tz_offset * 60 * 60);
DD = (localtime / 86400);
HH = (((localtime - DD * 86400) / 3600) % 24);
MiN = ((((localtime - DD * 86400) - HH * 3600) / 60) % 60);
SEK = (((((localtime - DD * 86400) - HH * 3600) - MiN * 60)) % 60);
/* Get number of 4years to accomodate for leap years */
uint16_t q_years = (DD / 1461);
/* Recalculate the number of years */
YYYY = q_years * 4 + (DD - (q_years * 1461)) / 365;
/* Determine no. of days in the current year */
uint16_t days_last = (DD - (q_years * 1461)) % 365;
static uint8_t days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/* Fix for leap year */
if (YYYY % 4 == 0){
days_in_month[1] = 29;
days_last++;
}
/* Retrace current month */
MON = 0;
while (days_last > days_in_month[MON]) {
days_last -= days_in_month[MON++];
}
YYYY= 1970 + YYYY;
MON= MON + 1;
DD=days_last;
DEBUG_OUT.printf("%d-%d-%d %d:%d:%d ", YYYY, MON ,DD , HH, MiN, SEK);
}//---getDate-----------------------------------------------------------------------------------------------------------
bool isDayTime(int tz_offset) {
//----------------------------------------------------------------------------------------------------------------------
//const int offset=60*15;// 900 = 15 Minuten, vor Sonnenaufgang und nach -untergang
const int offset = -60*25;// 25 Minuten, nach Sonnenaufgang und vor -untergang
time_t jetzt = getNow();
calcSunUpDown(jetzt);
getDate(tz_offset);
long jetztMinuteU = (100 * hour(jetzt+offset) + minute(jetzt+offset)) * 100;
long jetztMinuteO = (100 * hour(jetzt-offset) + minute(jetzt-offset)) * 100;
//DEBUG_OUT.printf("jetztU %i SunUp %i jetztO %i SunDown %i ",jetztMinuteU,SunUp,jetztMinuteO,SunDown);
if ((jetztMinuteU >= SunUp) &&(jetztMinuteO <= SunDown)){
DEBUG_OUT.printf(" it's daytime!, SunDown at %5.2f\r\n",(float)SunDown/10000);
return true;
}
else {
DEBUG_OUT.printf(" it's nighttime!, SunUP at %5.2f\r\n",(float)SunUp/10000);
return false;
}
}//---isDayTime-----------------------------------------------------------------------------------------------
int dayofweek(time_t now, int tz_offset) {
//----------------------------------------------------------------------------------------------------------------------
// Calculate number of seconds since midnight 1 Jan 1970 local time
time_t localtime = now + (tz_offset * 60 * 60);
// Convert to number of days since 1 Jan 1970
int days_since_epoch = localtime / 86400;
// 1 Jan 1970 was a Thursday, so add 4 so Sunday is day 0, and mod 7
int day_of_week = (days_since_epoch + 4) % 7;
return day_of_week;
}//---dayofweek---------------------------------------------------------------------------------------------------------
#endif