-
Notifications
You must be signed in to change notification settings - Fork 1
/
magicmirror-client.cpp
44 lines (33 loc) · 1.59 KB
/
magicmirror-client.cpp
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
#include <ESP8266HTTPClient.h> // To send HTTP requests.
#include "magicmirror-client.h"
void MagicMirrorClient::setHostUrl(String hostUrl) {
_hostUrl = hostUrl;
}
void MagicMirrorClient::setSensorId(String sensorId) {
_sensorId = sensorId;
}
void MagicMirrorClient::sendTemperature(float temperature, float humidity, float voltage) {
// Calculate battery level percentage from voltage.
float voltageMinLevel = 2.8;
float voltageMaxLevel = 4.2;
float voltageRange = voltageMaxLevel - voltageMinLevel;
float voltageDelta = voltage - voltageMinLevel;
float voltagePercentage = voltageDelta / voltageRange;
int voltagePercentageRounded = round(voltagePercentage * 100);
int temperatureRounded = round(temperature);
int humidityRounded = round(humidity);
String url = _hostUrl + "/remote-temperature";
Serial.println("MagicMirrorClient: Sending POST request to MagicMirror at " + url);
String requestBody = "{ \"temp\": " + String(temperatureRounded) + ", \"humidity\": " + String(humidityRounded) + ", \"battery\": " + String(voltagePercentageRounded) + ", \"sensorId\": \"" + String(_sensorId) + "\" }";
Serial.println("MagicMirrorClient: HTTP request body: " + requestBody);
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type", "application/json");
int statusCode = http.POST(requestBody);
Serial.printf("MagicMirrorClient: Received HTTP status code: %d\r\n", statusCode);
if (statusCode != HTTP_CODE_OK) {
String responseBody = http.getString();
Serial.println("MagicMirrorClient: Received HTTP response body: " + responseBody);
}
http.end();
}