Library for receiving data from fake inverter.

Inverter.cpp

Committer:
jump_man
Date:
2021-03-17
Revision:
0:e2b263279a61
Child:
1:1b1e78592710

File content as of revision 0:e2b263279a61:

#include "Inverter.h"

Inverter::Inverter(char* url, int port, EthernetInterface* ethIn)
{
    eth = ethIn;
    eth->gethostbyname(url, &a);
    a.set_port(port);
}

int Inverter::getPower()
{
    socket.open(eth);
    socket.connect(a);

    char sbuffer[] = "GET /solar_api/v1/GetInverterRealtimeData.cgi HTTP/1.1\r\nHost: int-sol-ref.herokuapp.com\r\n\r\n";
    int scount = socket.send(sbuffer, sizeof sbuffer);

    char rbuffer[500];
    int rcount = socket.recv(rbuffer, sizeof rbuffer);

    char *data = strstr(rbuffer, "{\"Body");
    
    StaticJsonDocument<128> doc;
    DeserializationError error = deserializeJson(doc, data);

    if (error) {
        printf("deserializeJson() failed.\r\n");
        return 0;
    }

    int power = doc["Body"]["Data"]["PAC"]["Value"];
    socket.close();
    return power;
}