I’ve got a sensor hooked up to an ESP8266, and I want to send the data to my Synology NAS chat server.
The following line of code works to send a message to the chat server. I could concatenate this string together with a new payload, but is there a better way to create and send it?
int httpResponseCode = http.POST("http://192.168.1.xxx:port/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=%22blahblahblah%22&payload={%22text%22:%22Does this work?%22}");
I’ve got a sensor hooked up to an ESP8266, and I want to send the data to my Synology NAS chat server.
The following line of code works to send a message to the chat server. I could concatenate this string together with a new payload, but is there a better way to create and send it?
int httpResponseCode = http.POST("http://192.168.1.xxx:port/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=%22blahblahblah%22&payload={%22text%22:%22Does this work?%22}");
I appreciate everyone's feedback and I'm sorry if this is question was a duplicate. The other question either didn't fit my needs, or I didn't know how to implement it (probably the case). I'm just tinkering with some electronics and a bit of programming for fun.
Here is a working solution that works by just concatenating the string:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
//wifi stuff
#define STASSID "ssid"
#define STAPSK "pw"
String ApiStart = "http://192.168.1.199:5000/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=";
String apiToken = "%22BigStringofStuff%22";
String PayloadHeader = "&payload={%22text%22:%22";
String PayloadFooter = "%22}";
String finalString = "";
String ChatMessage = "";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin(STASSID, STAPSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
// post once
if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
http.begin(client, ApiStart);
// Add headers
http.addHeader("Content-Type", "application/json");
ChatMessage = "Dumb Message.";
finalString = ApiStart + apiToken + PayloadHeader + ChatMessage + PayloadFooter;
Serial.println(finalString);
int httpResponseCode = http.POST(finalString);
// Check the response
if (httpResponseCode > 0) {
Serial.printf("HTTP response code: %d\n", httpResponseCode);
String response = http.getString();
Serial.println("Response:");
Serial.println(response);
} else {
Serial.printf("HTTP response code: %d\n", httpResponseCode);
}
// Close the connection
http.end();
}
}
void loop() {
// put your main code here, to run repeatedly:
}