DomoEsp_v2.0/DomoEspSensor/DomoEspApiConexion.h

76 lines
1.7 KiB
C++

#ifndef DomoEspApiConexionDef
#define DomoEspApiConexionDef 1
#include "defines.h"
#include <ESP8266HTTPClient.h>
//sensor Replica, sensor interno que se setea con get y no se publica
class DomoEspApiConexion
{
public:
DomoEspApiConexion()
{
}
bool send(char *topic, char*payload)
{
return true;
}
bool getDataPendiente(char * idArduino, char *topic, char *payloadOut)
{
return true;
}
bool sendPost(char * buff, char* srv, char* datos)
{
HTTPClient http;
WiFiClient client;
int httpCode =-1;
if(buff)
*buff=0;
if(http.begin(client, srv))
{
http.addHeader("Content-Type", "application/json"); // O el tipo de contenido adecuado
//String httpRequestData = "{\"dato1\":\"valor1\", \"dato2\":\"valor2\"}"; // Datos en formato JSON
httpCode = http.POST(datos);
if (httpCode > 0) {
String payload = http.getString();
if(buff)
strcpy( buff, payload.c_str());
}
http.end();
}
return httpCode==200;
}
bool sendGet(char * buff, char* srv, char* datos)
{
HTTPClient http;
WiFiClient client;
int httpCode =-1;
if(buff)
*buff=0;
if(http.begin(client, srv))
{
http.addHeader("Content-Type", "application/json"); // O el tipo de contenido adecuado
//String httpRequestData = "{\"dato1\":\"valor1\", \"dato2\":\"valor2\"}"; // Datos en formato JSON
if(datos)
httpCode = http.sendRequest("GET",datos);
else
httpCode = http.GET();
if (httpCode == HTTP_CODE_OK ) {
String payload = http.getString();
if(buff)
strcpy( buff, payload.c_str());
}
http.end();
}
return httpCode==200;
}
};
#endif