Skip to content

Commit

Permalink
Merge branch 'httpApiPost'
Browse files Browse the repository at this point in the history
  • Loading branch information
universam1 committed Nov 2, 2017
2 parents 770ccc7 + 2d22c7d commit 8c730a5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ script:
- platformio run
deploy:
provider: releases
# prerelease: true
prerelease: true
api_key:
secure: bU4iotLTrslYAlXQmSoMaGfCsjpM96TrKKne/2gFTlg++EQwjD/Z/i6T59lO7YVwp1GTD3qp/E4vSXfgkEm559mFIKZkd1IT8Vx/hpjX5CU6/OinGNmj1hYJw5M9DI1kQtc0wFDpbyNDnjPpKqBpFmIpqFyJUC7b3vctk/BMPkiiI5ncuBGp2f35M4qN2FedpTWoZdSAVZms7hsKFP4qZcih6stSlncUNf/niLwf78B31r5Td3G1aYQtHkB0BzyGQSgCvVuZ+PBm6zD/EWqRUGo6OWBCgwykbIZlgu45J4q46yQ2uS1CvRf4DFzIWU79bPg26MmxpjFe3oZucITTUtvOSzHFn8b1nuJE8IajYdM1KHSREl8Piej3A446P+JDQPk6ki3jCRyQentaZofI2ePUuqrvsV3iL4qKXMWpX1cOGU2c6nx0vvl/oft4rEcccibGpGZr0RIL7Ebpp7D4DSQ+bM+2XkuFab3W3QdrqrB6A0DOJuJfu/MvxEgkLtlpypZ2Z3g/F/TDgJkLSNEvX9e6kGAAIiIgkKz/zuuy04eNX+jox22TldovjbNSMCDfeeD6ghfKm0Q1+mdZax2dhvldgKOiwcZFL+TozN3AdPkr5GG/9m+dVaUGdrMK/n6uEJ++FZeUmdT8Pa2u/m3w2A4zeqMUWLM4WZMaUDW4cnA=
file: ".pioenvs/d1_mini_stage/firmware.bin"
Expand Down
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.5"
#define FIRMWAREVERSION "5.6.0"


#define API_FHEM true
Expand Down
72 changes: 55 additions & 17 deletions pio/lib/Sender/Sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ void SenderClass::add(String id, uint32_t value)
_jsonVariant[id] = value;
}

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

if (_client.connect(server.c_str(), port))
{
if (url != "")
{
Serial.println(F("Sender: HTTP posting"));

String msg = String("POST ");
msg += url;
msg += F(" HTTP/1.1\r\nHost: ");
msg += server;
msg += F("\r\nUser-Agent: iSpindel\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: ");
msg += _jsonVariant.measureLength() + 2;
msg += "\r\n";

_client.println(msg);
Serial.println(msg);
}
else
// if (url != "")
// {
// Serial.println(F("Sender: HTTP posting"));

// String msg = String("POST ");
// msg += url;
// msg += F(" HTTP/1.1\r\nHost: ");
// msg += server;
// msg += F("\r\nUser-Agent: iSpindel\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: ");
// msg += _jsonVariant.measureLength() + 2;
// msg += "\r\n";

// _client.println(msg);
// Serial.println(msg);
// }
// else
Serial.println(F("Sender: TCP stream"));
_jsonVariant.printTo(_client);
_client.println();
Expand All @@ -75,6 +75,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
5 changes: 4 additions & 1 deletion 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 sendTCP(String server, 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
12 changes: 7 additions & 5 deletions pio/src/iSpindel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,22 +442,24 @@ 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 == DTCraftBeerPi)
{
SerialOut(F("\ncalling CraftBeerPi"));
return sender.send(my_server, CBP_ENDPOINT, 5000);
SerialOut(F("\ncalling CraftbeerPi"));
// return sender.send(my_server, CBP_ENDPOINT, 5000);
return sender.sendGenericPost(my_server, CBP_ENDPOINT, 5000);
}
else if (service == DTiSPINDELde)
{
SerialOut(F("\ncalling iSPINDELde"));
return sender.send("ispindle.de", "", 9501);
return sender.sendTCP("ispindle.de", 9501);
}
else if (service == DTTCP)
{
SerialOut(F("\ncalling TCP"));
return sender.send(my_server, "", my_port);
return sender.sendTCP(my_server, my_port);
}
}
#endif // DATABASESYSTEM
Expand Down

0 comments on commit 8c730a5

Please sign in to comment.