Library for receiving data from fake inverter.

Committer:
jump_man
Date:
Wed Mar 17 16:13:45 2021 +0000
Revision:
0:e2b263279a61
Child:
1:1b1e78592710
Working revision.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jump_man 0:e2b263279a61 1 #include "Inverter.h"
jump_man 0:e2b263279a61 2
jump_man 0:e2b263279a61 3 Inverter::Inverter(char* url, int port, EthernetInterface* ethIn)
jump_man 0:e2b263279a61 4 {
jump_man 0:e2b263279a61 5 eth = ethIn;
jump_man 0:e2b263279a61 6 eth->gethostbyname(url, &a);
jump_man 0:e2b263279a61 7 a.set_port(port);
jump_man 0:e2b263279a61 8 }
jump_man 0:e2b263279a61 9
jump_man 0:e2b263279a61 10 int Inverter::getPower()
jump_man 0:e2b263279a61 11 {
jump_man 0:e2b263279a61 12 socket.open(eth);
jump_man 0:e2b263279a61 13 socket.connect(a);
jump_man 0:e2b263279a61 14
jump_man 0:e2b263279a61 15 char sbuffer[] = "GET /solar_api/v1/GetInverterRealtimeData.cgi HTTP/1.1\r\nHost: int-sol-ref.herokuapp.com\r\n\r\n";
jump_man 0:e2b263279a61 16 int scount = socket.send(sbuffer, sizeof sbuffer);
jump_man 0:e2b263279a61 17
jump_man 0:e2b263279a61 18 char rbuffer[500];
jump_man 0:e2b263279a61 19 int rcount = socket.recv(rbuffer, sizeof rbuffer);
jump_man 0:e2b263279a61 20
jump_man 0:e2b263279a61 21 char *data = strstr(rbuffer, "{\"Body");
jump_man 0:e2b263279a61 22
jump_man 0:e2b263279a61 23 StaticJsonDocument<128> doc;
jump_man 0:e2b263279a61 24 DeserializationError error = deserializeJson(doc, data);
jump_man 0:e2b263279a61 25
jump_man 0:e2b263279a61 26 if (error) {
jump_man 0:e2b263279a61 27 printf("deserializeJson() failed.\r\n");
jump_man 0:e2b263279a61 28 return 0;
jump_man 0:e2b263279a61 29 }
jump_man 0:e2b263279a61 30
jump_man 0:e2b263279a61 31 int power = doc["Body"]["Data"]["PAC"]["Value"];
jump_man 0:e2b263279a61 32 socket.close();
jump_man 0:e2b263279a61 33 return power;
jump_man 0:e2b263279a61 34 }