Skip to content

Commit 890a024

Browse files
author
barakuda21
committed
Main
Sunrise.h and displayhandler.h added Platform ini updated
1 parent ad0fab1 commit 890a024

14 files changed

+319
-0
lines changed

.pio/build/esp32dev/tmpahxafcje.tmp

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

.pio/libdeps/esp32dev/Adafruit BusIO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 186bfb005c6d4b597a09290cb3e451c30a5baa97
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 5ea4be3ffeae918899aeec8cbb67f87359cf0026
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 88711efc3e4b4a1a3fb238a1c5aa9590697f9c02
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 8f7211a50f19726a0c3698112827d92993c1cc7f
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit ca8ac5f919d02bea07b474531981ddbfd64de97c

.pio/libdeps/esp32dev/CAN

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 68f4b7fb4c5366d067a363f642d270355d813115
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit f71e3d427b5be9791a8a2c93cf8079792c3a9a26

.pio/libdeps/esp32dev/ESPUI

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 5cf0ce6afa38511e80038131030b46a385de4f32

.pio/libdeps/esp32dev/NTPClient

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 1c04e07f273c6a6614ba28136e9216b5ac401d66

.pio/libdeps/esp32dev/PubSubClient

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 2d228f2f862a95846c65a8518c79f48dfc8f188c

.pio/libdeps/esp32dev/WiFiManager

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 297123b5d7670e6c734836c13bf5ffd09b094889

src/DisplayHandler.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
This Libary handles the data to the Display
3+
https://randomnerdtutorials.com/esp32-ssd1306-oled-display-arduino-ide/
4+
https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/
5+
// Pinout Reference
6+
https://randomnerdtutorials.com/esp32-pinout-reference-gpios/
7+
https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/
8+
9+
// Display Driver SSD 1306
10+
// Size 0,96 Inch 128*64 pixel monocolour
11+
// Pin wiring
12+
//Vin 3,3V
13+
// GND GND#
14+
// SCL GPIO21 #define Display_I2C_SDA1 21
15+
// SDA GPIO22 #define Display_I2C_SLC1 22
16+
17+
/*********
18+
Rui Santos
19+
Complete project details at https://randomnerdtutorials.com
20+
*********/
21+
#include <Wire.h>
22+
//#include <spi.h>
23+
#include <Adafruit_GFX.h>
24+
#include <Adafruit_SSD1306.h>
25+
26+
#define SCREEN_WIDTH 128 // OLED display width, in pixels
27+
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
28+
29+
// Declaration for an SSD1306 display connected to I2C (SDA GPIO21, SCL GPIO22 pins)
30+
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
31+
32+
33+
34+
35+
void setup_display(){
36+
37+
38+
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
39+
Serial.println(F("SSD1306 allocation failed"));
40+
for(;;);
41+
}
42+
delay(2000);
43+
display.clearDisplay();
44+
45+
display.setTextSize(2);
46+
display.setTextColor(WHITE);
47+
display.setCursor(0, 10);
48+
// Display static text
49+
display.println("Hui,");
50+
display.println("Battary");
51+
display.println("Storage!");
52+
53+
display.display();
54+
55+
}

src/sunrise.cpp

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
2+
3+
#include <NTPClient.h>
4+
#include <WiFiUdp.h>
5+
6+
7+
#define PI 3.1415926536
8+
int dayofyear=0;
9+
10+
// Define NTP Client to get time
11+
WiFiUDP ntpUDP;
12+
NTPClient timeClient(ntpUDP, "pool.ntp.org");
13+
14+
//Week Days
15+
String weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
16+
17+
//Month names
18+
String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
19+
20+
21+
//________________________________________________________________________________________calculate sunrise and sunset
22+
// die folgenden Formeln wurden der Seite http://lexikon.astronomie.info/zeitgleichung/ entnommen
23+
24+
25+
// subfunction to compute Sonnendeklination
26+
float sonnendeklination(int T) {
27+
// Deklination der Sonne in Radians
28+
// Formula 2008 by Arnold(at)Barmettler.com, fit to 20 years of average declinations (2008-2017)
29+
return 0.409526325277017*sin(0.0169060504029192*(T-80.0856919827619));
30+
}
31+
32+
// subfunction to compute Zeitdifferenz
33+
// Dauer des halben Tagbogens in Stunden: Zeit von Sonnenaufgang (Hoehe h) bis zum hoechsten Stand im Sueden
34+
float zeitdifferenz(float Deklination, float B) {
35+
36+
return 12.0*acos((sin(-(50.0/60.0)*PI/180.0) - sin(B)*sin(Deklination)) / (cos(B)*cos(Deklination)))/PI;
37+
}
38+
39+
// subfunction to compute Zeitgleichung
40+
float zeitgleichung(int T) {
41+
return -0.170869921174742*sin(0.0336997028793971 * T + 0.465419984181394) - 0.129890681040717*sin(0.0178674832556871*T - 0.167936777524864);
42+
}
43+
44+
// subfunction to compute sunrise
45+
float aufgang(int T, float B) {
46+
float DK = sonnendeklination(T);
47+
return 12 - zeitdifferenz(DK, B) - zeitgleichung(T);
48+
}
49+
50+
// subfunction to compute sunset
51+
float untergang(int T, float B) {
52+
float DK = sonnendeklination(T);
53+
return 12 + zeitdifferenz(DK, B) - zeitgleichung(T);
54+
}
55+
56+
// subfunction to compute it all  
57+
58+
/*void initsunset(int T) {
59+
float Laenge = 10.98;
60+
float Breite = 48.25;
61+
int Zone = 2; // Unterschied zu UTC-Zeit ( = 2 in der Sommerzeit, 1 in der Winterzeit )
62+
float B = Breite*PI/180.0; // geogr. Breite in Radians
63+
64+
// Berechnung von Sonnenauf- und -Untergang
65+
float Aufgang = aufgang(T, B); // Sonnenaufgang bei 0 Grad Laenge
66+
float Untergang = untergang(T, B); // Sonnenuntergang bei 0 Grad Laenge
67+
68+
Aufgang = Aufgang - Laenge /15.0 + Zone; // Sonnenaufgang bei gesuchter Laenge und Zeitzone in Stunden
69+
Untergang = Untergang - Laenge /15.0 + Zone; // Sonnenuntergang bei gesuchter Laenge und Zeitzone in Stunden
70+
Serial.print("Tag: ");Serial.print(T);
71+
Serial.print(" Aufgang: ");Serial.print(Aufgang);Serial.print(" Untergang: ");Serial.println(Untergang);
72+
}
73+
*/
74+
float computeAufgang(int T, float Laenge, float Breite) {
75+
76+
int Zone = 1; // Unterschied zu UTC-Zeit ( = 2 in der Sommerzeit, 1 in der Winterzeit )
77+
float B = Breite*PI/180.0; // geogr. Breite in Radians
78+
79+
// Berechnung von Sonnenaufgang
80+
float Aufgang = aufgang(T, B); // Sonnenaufgang bei 0 Grad Laenge
81+
82+
Aufgang = Aufgang - Laenge /15.0 + Zone; // Sonnenaufgang bei gesuchter Laenge und Zeitzone in Stunden
83+
return Aufgang;
84+
}
85+
86+
float computeUntergang(int T, float Laenge, float Breite) {
87+
88+
int Zone = 1; // Unterschied zu UTC-Zeit ( = 2 in der Sommerzeit, 1 in der Winterzeit )
89+
float B = Breite*PI/180.0; // geogr. Breite in Radians
90+
91+
// Berechnung von Sonnenuntergang
92+
93+
float Untergang = untergang(T, B); // Sonnenuntergang bei 0 Grad Laenge
94+
95+
Untergang = Untergang - Laenge /15.0 + Zone; // Sonnenuntergang bei gesuchter Laenge und Zeitzone in Stunden
96+
return Untergang;
97+
}
98+
99+
//___________________________________________________________end calculate sunrise and sunset
100+
101+
//_____________________________________________________________Calculate Day of year
102+
/*************************************************
103+
Program to calculate day of year from the date
104+
*
105+
* Enter date (MM/DD/YYYY): 12/30/2006
106+
* Day of year: 364
107+
*
108+
************************************************/
109+
110+
int caldayofyear(int day,int mon,int year)
111+
{
112+
int days_in_feb = 28;
113+
int doy; // day of year
114+
115+
116+
doy = day;
117+
118+
// check for leap year
119+
if( (year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0) )
120+
{
121+
days_in_feb = 29;
122+
}
123+
124+
switch(mon)
125+
{
126+
case 2:
127+
doy += 31;
128+
break;
129+
case 3:
130+
doy += 31+days_in_feb;
131+
break;
132+
case 4:
133+
doy += 31+days_in_feb+31;
134+
break;
135+
case 5:
136+
doy += 31+days_in_feb+31+30;
137+
break;
138+
case 6:
139+
doy += 31+days_in_feb+31+30+31;
140+
break;
141+
case 7:
142+
doy += 31+days_in_feb+31+30+31+30;
143+
break;
144+
case 8:
145+
doy += 31+days_in_feb+31+30+31+30+31;
146+
break;
147+
case 9:
148+
doy += 31+days_in_feb+31+30+31+30+31+31;
149+
break;
150+
case 10:
151+
doy += 31+days_in_feb+31+30+31+30+31+31+30;
152+
break;
153+
case 11:
154+
doy += 31+days_in_feb+31+30+31+30+31+31+30+31;
155+
break;
156+
case 12:
157+
doy += 31+days_in_feb+31+30+31+30+31+31+30+31+30;
158+
break;
159+
}
160+
161+
162+
163+
return doy; // return day of year
164+
}
165+
//_________________________________________________________________End calculate day of year
166+
167+
//____________________________________________________________________SetPV flag
168+
// this function calculates start and end of the PV production
169+
// based on sunrise and sunset and a lag factor
170+
171+
bool setPVstartflag(float lagmorning, float lagevening)
172+
{
173+
bool pvstartflag=false;
174+
float startpv, stoppv;
175+
float laenge = 9.0658; // Postition of PV
176+
float breite = 48.7990; // Position of PV
177+
float currenttime; // this is the time in decimal hours i.e. 8.5
178+
179+
180+
timeClient.update();
181+
time_t epochTime = timeClient.getEpochTime();
182+
float currentHour = timeClient.getHours();// get current hour
183+
float currentMinute = timeClient.getMinutes(); // get current menutes
184+
struct tm *ptm = gmtime ((time_t *)&epochTime);
185+
int monthDay = ptm->tm_mday;// get current day of the month
186+
int currentMonth = ptm->tm_mon+1;//get current month
187+
int currentYear = ptm->tm_year+1900; // get current year
188+
189+
dayofyear=caldayofyear(monthDay,currentMonth,currentYear);// calculate current day of the year
190+
191+
startpv=computeAufgang(dayofyear, laenge, breite) + lagmorning; // statring time of pv
192+
stoppv=computeUntergang(dayofyear,laenge,breite)-lagevening; // stop time of PV
193+
194+
currenttime=currentHour + (currentMinute/60); // calculate current ime in decimal hours
195+
196+
// figure out wethere pv can produce power or not
197+
// flag pvstartflag is true at daytime when pv can procuce power
198+
if (currenttime>=startpv & currenttime<=stoppv){
199+
pvstartflag=true;
200+
}else pvstartflag= false;
201+
202+
//__________________________-Test ausgabe
203+
204+
Serial.print ("Sonnenaufgang: ");
205+
Serial.println(computeAufgang(dayofyear, laenge, breite));
206+
Serial.print("Sonnenuntergang: ");
207+
Serial.println(computeUntergang(dayofyear,laenge,breite));
208+
209+
210+
Serial.print("Day of the year: ");
211+
Serial.println (dayofyear);
212+
213+
Serial.print("Current minute: ");
214+
Serial.println (currentMinute);
215+
216+
217+
Serial.print("Current time decimal: ");
218+
Serial.println (currenttime);
219+
220+
Serial.print("start PV: ");
221+
Serial.println (startpv);
222+
223+
Serial.print("stoppv PV: ");
224+
Serial.println (stoppv);
225+
226+
Serial.print("pvstartflag: ");
227+
Serial.println (pvstartflag);
228+
229+
String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
230+
Serial.print("Current date: ");
231+
Serial.println(currentDate);
232+
233+
return pvstartflag; // return pv start produce flag
234+
}
235+
236+
237+
//____________________________________________________________________end day of year
238+
239+
240+
//________________________________________________START NTP Client
241+
// Initialize a NTPClient to get time to calculate sunrise and sunset
242+
void setuptimeClient (){
243+
// Set offset time in seconds to adjust for your timezone, for example:
244+
// GMT +1 = 3600
245+
// GMT +8 = 28800
246+
// GMT -1 = -3600
247+
// GMT 0 = 0
248+
timeClient.begin();
249+
timeClient.setTimeOffset(3600);
250+
251+
}
252+
//________________________________________________END NTP client

0 commit comments

Comments
 (0)