-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_data.h
56 lines (47 loc) · 1.69 KB
/
send_data.h
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* \file send_data.h
*
* \brief function to send json data to an API
*
* This is the files that contains the functions to send json data to an API.
*
* \date 19.08.2019
*/
/// Externally define variable for Chip ID
extern String esp32_chipid;
/// Externally define variable to check if using bme280
extern bool use_bme280;
/// Externally define variable to check if using dht22
extern bool use_dht22;
/**************************************************************************/
/*!
@brief Send JSON data to API
@param data_json the JSON Message to be sent
@param destination the destination API madavi or luftdaten
@param pin the pin-code values
@returns void
*/
/**************************************************************************/
void sendData(const char *data_json, char destination[], String pin) {
HTTPClient http;
http.begin(destination); //Specify destination for HTTP request
http.addHeader("Content-Type", "application/json"); //Specify content-type header
http.addHeader("X-PIN", pin);
http.addHeader("X-Sensor", "esp32-"+esp32_chipid);
int httpResponseCode = http.POST(data_json); //Send the actual POST request
Serial.print("data: ");
Serial.println(data_json);
if(httpResponseCode>0){
String response = http.getString(); //Get the response to the request
Serial.print("httpResponseCode: ");
Serial.println(httpResponseCode); //Print return code
Serial.print("response: "); //Print request answer
Serial.println(response);
// return "hallo";
}
else{
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); //Free resources
}