Skip to content

Commit

Permalink
using http library for post
Browse files Browse the repository at this point in the history
  • Loading branch information
universam1 committed Oct 8, 2017
1 parent 1f1bbb1 commit 2d22c7d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pio/lib/Globals/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extern Ticker flasher;
// #include <stdint.h>

// defines go here
#define FIRMWAREVERSION "5.5.3"
#define FIRMWAREVERSION "5.5.4"


#define API_FHEM true
Expand Down
40 changes: 39 additions & 1 deletion pio/lib/Sender/Sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,44 @@ bool SenderClass::send(String server, String url, uint16_t port)
return true;
}

bool SenderClass::sendGenericPost(String server, String url, uint16_t port)
{
_jsonVariant.printTo(Serial);

HTTPClient http;

Serial.println(F("HTTPAPI: posting"));
// configure traged server and url
http.begin(server, port, url);
http.addHeader("User-Agent", "iSpindel");
http.addHeader("Connection", "close");
http.addHeader("Content-Type", "application/json");

// size_t len = _jsonVariant.measureLength() + 1;
// uint8_t json[len];
// _jsonVariant.printTo(json, len);
String json;
_jsonVariant.printTo(json);
auto httpCode = http.POST(json);
Serial.println(String("code: ") + httpCode);

// httpCode will be negative on error
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK)
{
Serial.println(http.getString());
}
}
else
{
Serial.print(F("[HTTP] POST... failed, error: "));
Serial.println(http.errorToString(httpCode));
}

http.end();
}

bool SenderClass::sendUbidots(String token, String name)
{
_jsonVariant.printTo(Serial);
Expand Down Expand Up @@ -126,7 +164,7 @@ bool SenderClass::sendFHEM(String server, uint16_t port, String name)

String msg = String("GET /fhem?cmd.Test=set%20");
msg += name;

for (const auto &kv : _jsonVariant.as<JsonObject>())
{
msg += "%20";
Expand Down
3 changes: 3 additions & 0 deletions pio/lib/Sender/Sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
#define _SENDER_H_

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson

class SenderClass
{
public:
SenderClass();
bool send(String server, String url, uint16_t port = 80);
bool sendGenericPost(String server, String url, uint16_t port = 80);

bool sendUbidots(String token, String name);
bool sendFHEM(String server, uint16_t port, String name);
bool sendTCONTROL(String server, uint16_t port);
Expand Down
6 changes: 4 additions & 2 deletions pio/src/iSpindel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,14 @@ bool uploadData(uint8_t service)
if (service == DTHTTP)
{
SerialOut(F("\ncalling HTTP"));
return sender.send(my_server, my_url, my_port);
// return sender.send(my_server, my_url, my_port);
return sender.sendGenericPost(my_server, my_url, my_port);
}
else if (service == DTCraftbeepPi)
{
SerialOut(F("\ncalling CraftbeepPi"));
return sender.send(my_server, CBP_ENDPOINT, 5000);
// return sender.send(my_server, CBP_ENDPOINT, 5000);
return sender.sendGenericPost(my_server, CBP_ENDPOINT, 5000);
}
else if (service == DTiSPINDELde)
{
Expand Down

0 comments on commit 2d22c7d

Please sign in to comment.