66 char server[] =
"api.aqi.eco",
int id = 0)
68 client = Supla::ClientBuilder();
69 client->setSSLEnabled(
true);
70#if !defined(SUPLA_ALLOW_INSECURE_EXTERNAL_TLS)
71 client->setCACert(LETS_ENCRYPT_CA_CERT);
75 strncpy(serverAddress, server, 32);
76 serverAddress[32] = 0;
79 if (strlen(token) == 32) {
80 strncpy(apiToken, token, 32);
89 refreshTime = refresh;
91 SUPLA_LOG_DEBUG(
"aqi.eco: refresh time: %d", refreshTime);
96 _network->getMacAddr(mac);
97 sensorId = ((mac[2]*256+mac[3])*256+mac[4])*256+mac[5];
108 bool sendData()
override {
109 if (strlen(apiToken) != 32) {
110 SUPLA_LOG_DEBUG(
"aqi.eco: expected token length 32, got %d",
111 static_cast<int>(strlen(apiToken)));
115 StaticJsonDocument<768> jsonBuffer;
116 JsonObject json = jsonBuffer.to<JsonObject>();
118 json[
"esp8266id"] = sensorId;
119 json[
"software_version"] =
"Supla_" SUPLA_SHORT_VERSION;
120 JsonArray sensordatavalues = json.createNestedArray(
"sensordatavalues");
122 for (
int i=0; i < MAXSENSORS; i++) {
124 double value = getSensorValue(i);
125 String type =
"unknown";
127 case Supla::SenorType::PM1:
130 case Supla::SenorType::PM2_5:
133 case Supla::SenorType::PM4:
136 case Supla::SenorType::PM10:
139 case Supla::SenorType::TEMP:
140 type =
"BME280_temperature";
142 case Supla::SenorType::HUMI:
143 type =
"BME280_humidity";
145 case Supla::SenorType::PRESS:
146 type =
"BME280_pressure";
149 case Supla::SenorType::LIGHT:
150 type =
"ambient_light";
152 case Supla::SenorType::WIND:
155 case Supla::SenorType::RAIN:
158 case Supla::SenorType::CO2:
159 type =
"conc_co2_ppm";
164 JsonObject jo = sensordatavalues.createNestedObject();
165 jo[
"value_type"] = type;
173 serializeJson(json, output, 768);
174 SUPLA_LOG_DEBUG(
"aqi.eco: JSON: %s", output);
176 if (client->connect(serverAddress, 443)) {
177 client->print(
"POST /update/");
178 client->print(apiToken);
179 client->println(
" HTTP/1.1");
180 client->print(
"Host: ");
181 client->println(serverAddress);
182 client->println(
"Content-Type: application/json");
183 client->print(
"Content-Length: ");
184 client->println(strlen(output));
186 client->println(output);
188 SUPLA_LOG_DEBUG(
"aqi.eco: sended %d bytes to %s/update/%s",
189 strlen(output), serverAddress, apiToken);
193 if (!client->available()) {
194 SUPLA_LOG_DEBUG(
"aqi.eco: no bytes to read from %s", serverAddress);
197 int responseLength = client->available();
198 SUPLA_LOG_DEBUG(
"aqi.eco: reading from %s: %d bytes",
199 serverAddress, responseLength);
201 if (responseLength >=
static_cast<int>(
sizeof(output))) {
202 responseLength =
sizeof(output) - 1;
205 for (
int i = 0; i < responseLength; i++) {
206 int responseChar = client->read();
207 if (responseChar < 0) {
211 output[i] =
static_cast<char>(responseChar);
212 if (output[i] ==
'\n') {
216 output[responseLength] = 0;
217 SUPLA_LOG_DEBUG(
"aqi.eco: response from %s: %s", serverAddress, output);
226 char serverAddress[33];
227 uint32_t sensorId = 0;