Skip to content

Commit

Permalink
Updated sunrise functions; dynamic load calculatio
Browse files Browse the repository at this point in the history
  • Loading branch information
gropi75 committed Mar 19, 2023
1 parent e87e391 commit d73ad05
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 180 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,13 @@ src/main.cpp
.pio/libdeps/esp32dev/ArduinoJson/logo.svg
.pio/libdeps/esp32dev/ArduinoJson/idf_component.yml
.pio/libdeps/esp32dev/ArduinoJson/LICENSE.txt
.pio/build/esp32dev/tmpahxafcje.tmp
.pio/build/esp32dev/tmpahxafcje.tmp
.pio/build/esp32dev/tmpao0pn_tn.tmp
.pio/build/esp32dev/tmpezdmcyso.tmp
.pio/build/esp32dev/tmpj2jtsnn0.tmp
.pio/build/esp32dev/tmpn8d7uo8a.tmp
.pio/build/esp32dev/tmpot58lxer.tmp
.pio/build/esp32dev/tmpq6hmdh87.tmp
.pio/build/esp32dev/tmpwft6of8y.tmp
.pio/build/esp32dev/tmpahxafcje.tmp
1 change: 0 additions & 1 deletion .pio/build/esp32dev/tmpahxafcje.tmp

This file was deleted.

11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# ESP32_R4850g2
# Solar Energy Manager for ESP32 (SEM.32)
![GitHub Repo stars](https://img.shields.io/github/stars/gropi75/ESP32_r4850g2?style=plastic)
![GitHub watchers](https://img.shields.io/github/watchers/gropi75/ESP32_r4850g2?style=plastic)
![GitHub forks](https://img.shields.io/github/forks/gropi75/ESP32_r4850g2?style=plastic)
![Release](https://img.shields.io/github/v/release/gropi75/ESP32_r4850g2?include_prereleases)

ESP32 project to control the Huawei R4850G2 Power Supply and a Soyosource GNT1200 inverter for a solar powered LIFEPO4 48V battery pack
SEM.32 (Solar Energy Manager 32) based on ESP32 controller, Soyosource Inverter, Huawei r4850g2 power supply, JK-BMS and 48V battery. It helps You to maximize the usage of self generated solar power.


[TOC]

# Main features:
- control of inverter to minimize power usage out of the grid (over RS485)
Expand All @@ -31,11 +31,14 @@ ESP32 project to control the Huawei R4850G2 Power Supply and a Soyosource GNT120
- Type: JK-BD6A20S8P
- Software V.: V10.XY
- Serial No: 2032812241
- **Battary cells:**
- **Battery cells:**
- Type: EVE LF90 (3,2V 90AH, Nominal)
- Datasheet No.: LF90-73103
- Cut off Voltage: 3.65V/2.5V
- Std. charge/Discharge: 1.0C
- **Power Meter:**
- Type: Tasmota based smart meter from [Hitchi](https://www.photovoltaikforum.com/thread/173032-lesekopf-bei-heise-getestet/)


- **Controller:**
- ESPRESSIF ESP32-WROOM-32U (with ext. Antenna)
Expand Down
7 changes: 7 additions & 0 deletions lib/PowerFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ int CalculatePower(int ActGridPower, int ActBatPower, int PowResCharger, int Max

// Get actual power from the central power meter.
int getActualPower(char ip[40], char cmd[40], char resp[20], char resp_power[20]);

// calculate max inverter load to have a balanced discharge of the battery over the whole night
int CalculateBalancedDischargePower(int capacity, float voltage, int actualSOC, int targetSOC, float sunset, float sunrise);

// get solar prognosis
float getSolarPrognosis(char token[40], char id[4], char today[9], char tomorrow[9]);

22 changes: 22 additions & 0 deletions lib/secrets_dummy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// rename this file to secrets.h

const char g_WIFI_SSID[] = "SSID";
const char g_WIFI_Passphrase[] = "password";

char mqtt_server[40] = "192.168.188.80";
char mqtt_port[6] = "1883";
char current_clamp_ip[40] = "192.168.188.127";
char current_clamp_cmd[40] = "/cm?cmnd=status+10";
char sensor_resp[20] = "SML"; // or "MT175"
char sensor_resp_power[20] = "DJ_TPWRCURR"; // or "P"
bool g_EnableMQTT = true;

char solarprognose_token[40] = "token"; // token to get prognosis from http://www.solarprognose.de
char solarprognose_id[4] = "999";

//
float lagmorning=0; // time in decimal hours between real sunset and start of PV
float lagevening=0; // time in decimal hours between end of PV power production and real sunset

float laenge = 9.1234; // Postition of PV
float breite = 48.9999; // Position of PV
34 changes: 33 additions & 1 deletion lib/sunrise.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#ifndef sunrise_H
#define sunrise_H

struct TimeStruct {
float sunrise_today; // sunrise today
float sunset_today; // sunset today
float sunrise_tomorrow; // sunrise tomorrow
int day_of_week; // shows which day is today, Monday.... Sunday
char* date_today; // date of today with format yyyymmdd (20230312)
char* date_tomorrow; // date for tomorro with format yyyymmdd (20230312)
};
void TimeData(struct TimeStruct *s,float lagmorning, float lagevening, float laenge, float breite);

// Function to setup the time client
void setuptimeClient ();
Expand All @@ -14,5 +25,26 @@ void setuptimeClient ();

// This function figures out wethere PV can produce power or not
// flag pvstartflag is true at daytime when pv can procuce power and false when PV can not produce power
bool setPVstartflag(float lagmorning, float lagevening);
bool setPVstartflag(float lagmorning, float lagevening, float laenge, float breite);

// current time in decimal hours
float factualtimeinhours();

// add on Issue in Github Provide time information #16
//float sunrisetoday(); // sunrise of current day in decimal hours including lag
//float sunsettoday(); // sunset of current Day including lag
//float sunrisetomorow(); // sunrise today of next day including lag
//float actualtimeinhours(); // current time in decimal hours
//int dayofweek(); //current day of week as integer 1 monday ... 7 sunday
//char date_today(); // current datedate of today with format yyyymmdd (20230312)
//char date_tomorow();// date of tomorow with format yyyymmdd (20230312)

//float fstartpv(float lagmorning, float lagevening, float laenge, float breite); // time when PV starts producing= sunrise + lagmorning
//float fstoppv(float lagmorning, float lagevening,float laenge, float breite); // time when PV stopps producing= sunset - lagevening
//float fstartpvtomorow(float lagmorning, float lagevening,float laenge, float breite); // Time when PV starts producing next day= sunrise + lagmorning

//int fweekday(); //function returns the current week day as a char
//char* fdate_today(); // current date of today with format yyyymmdd (20230312)
//char* fdate_tomorow(); // date of tomorow with format yyyymmdd (20230312)

#endif //sunrise_H
41 changes: 41 additions & 0 deletions src/PowerFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
WiFiClient current_clamp_client; // or WiFiClientSecure for HTTPS; to connect to current sensor
HTTPClient http; // to connect to current sensor

WiFiClient solarprognose_client; // to connect to Solarprognose.de
HTTPClient http_solar;

// Positive ActPower means, we import power, so we need the inverter
// Negative ActPower means, we export power, so we can charge the batter
// Calculate the set power both for charger and inverter.
Expand Down Expand Up @@ -72,4 +75,42 @@ int getActualPower(char ip[40], char cmd[40], char resp[20], char resp_power[20]
actPower = StatusSNS_SML[String(resp_power)]; // -2546
}
return actPower;
}

// calculate max inverter load to have a balanced discharge of the battery over the whole night
int CalculateBalancedDischargePower(int capacity, float voltage, int actualSOC, int targetSOC, float sunset, float sunrise)
{
// shall we use https://github.com/buelowp/sunset
return (int)(capacity * voltage * 0.01 * (actualSOC - targetSOC) / (sunrise - sunset + 24));
}

// get solar prognosis
// http://www.solarprognose.de/web/solarprediction/api/v1?access-token=454jelfd&item=inverter&id=2&type=daily
float getSolarPrognosis(char token[40], char id[4], char today[9], char tomorrow[9])
{
float prognosis_today, prognosis_tomorrow;
char solarprognose_adress[83] = "http://www.solarprognose.de/web/solarprediction/api/v1?_format=json&access-token=";
http_solar.begin(solarprognose_client, solarprognose_adress + String(token) + "&item=location&id=" + String(id) + "&type=daily");
int httpCode = http_solar.GET(); // send the request
if (httpCode > 0)
{
String payload = http_solar.getString(); // Get the request response payload
StaticJsonDocument<768> doc;
Serial.println(payload);
DeserializationError error = deserializeJson(doc, payload);

if (error)
{
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return 0;
}
prognosis_today = doc["data"][String(today)]; // 9.322
prognosis_tomorrow = doc["data"][String(tomorrow)]; // 20.653
Serial.println(String(today));
Serial.println(String(prognosis_today));
Serial.println(String(tomorrow));
Serial.println(String(prognosis_tomorrow));
}
return prognosis_tomorrow;
}
Loading

0 comments on commit d73ad05

Please sign in to comment.