-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpNotifier.cpp
More file actions
41 lines (33 loc) · 976 Bytes
/
Copy pathHttpNotifier.cpp
File metadata and controls
41 lines (33 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "HttpNotifier.h"
#include <curl/curl.h>
#include <cstring>
HttpNotifier::HttpNotifier(string url): m_url(url) {
curl_global_init(CURL_GLOBAL_ALL);
}
HttpNotifier::~HttpNotifier() {
curl_global_cleanup();
}
void HttpNotifier::sendValue(float value) {
CURL *curl;
CURLcode res;
/* get a curl handle */
curl = curl_easy_init();
if (curl) {
char * cstr = new char [m_url.length()+1];
strcpy (cstr, m_url.c_str());
curl_easy_setopt(curl, CURLOPT_URL, cstr);
/* Set the POST data
Format expected by web server is "reading=x.xxxx" */
char post_data[20];
sprintf(post_data, "reading=%f", value);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
/* Send request */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
}