Hi! I am currently using "P1 monitor" software on a Raspberry to monitor my usage of electricity.
The raspberry is connected to the "smart meter" (Slimme meter). I have solar panels.
My challenge is: can I let a Sonoff S20 Wifi smart socket (with internally an ESP8266) let request the data from the raspberry and in this way create a socket that only offers power when there is a certain amount of "sun" available?
The P1 monitor has an "info" tab, which offers the amount of electricity that is currently fed back in to the public grid. I can retrieve the HTML file in the ESP8266 from the raspberry, but the problem is that the wished data is not in it.
The wanted data is subsequently transferred in a dynamic way due to scripting (Java?), so the ESP8266 must do something more.
We are getting in the world of webscraping!
There is more. I also have access to a server at the provider of the solar panels ("Enlighten").
It offers also a webpage showing the current production of the solar panels. Can the Sonoff socket retrieve the solar panel production data in the same way? Here I have the same problem, the wanted figures are not in the file, when the ESP8266 requests the webpage from Enlighten.
The idea is that when Sonoff is able to retrieve both the amount of energy that is fed back in the public grid and/or the amount of energy generated by the solar panels, Sonoff should be able to determine the moments of "free energy"
Has anybody experience with this?
It turns out to be surprisingly simple!
If you look at the info page of the P1 monitor in your web browser, and you do "view page source", you will see:
function readTextMeterTelegram(){
$('#slimmemeter').load( "/txt/txt-meter.php", function( response, status, xhr ) {
if ( status == "error" ) {
$("#slimmemeter").html('Slimme meter data niet beschikbaar.');
}
});
In other words. if you enter "/info.php" instead of "/txt/txt-meter.php" you will see the "MeterTelegram" (the dynamic data):
/ISK5\2M550T-1012
1-3:0.2.8(50)
....
0-0:96.14.0(0001)
1-0:1.7.0(01.241kW) <----- The returned power. VoilĂ !
1-0:2.7.0(00.000kW)
.....
ESP8266 code:
// Precondition: Wifi connection is established.
String RemoteIpAddress = "http://192.168.1.15"; // The IP address of the P1 monitor.
String Url = RemoteIpAddress + "//txt//txt-meter.php";
Serial.print("Getting data from ");
Serial.println(Url);
if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
{
HTTPClient http; //Declare an object of class HTTPClient
int httpCode = -1;
http.begin(Url); //Specify request destination
delay(100);
httpCode = http.GET(); //Send the request
Serial.printf("%s, httpCode: %d\n\n",Url.c_str(), httpCode);
if (httpCode == HTTP_CODE_OK)
{
String payload = http.getString(); //Get the request response in payload
payload.replace("
","\n");
Serial.println(payload);
// Telegram in string "payload"!
// Now search in "payload" on the line with text "1-0:1.7.0" and find the returned power.
// For SONOFF e.g.: is returned power > 500 Watt, then switch on, is it less than 10 Watt, switch off.
// 500 Watt? Of course depends on how much power is taken via the SONOFF socket.