80 explicit AQIECO(
Supla::Network* _network,
char token[],
int refresh = 180,
81 char server[] =
"api.aqi.eco",
int id = 0)
83 client = Supla::ClientBuilder();
84 client->setSSLEnabled(
true);
85#if !defined(SUPLA_ALLOW_INSECURE_EXTERNAL_TLS)
86 client->setCACert(LETS_ENCRYPT_CA_CERT);
90 strncpy(serverAddress, server, 32);
91 serverAddress[32] = 0;
94 if (strlen(token) == 32) {
95 strncpy(apiToken, token, 32);
104 refreshTime = refresh;
106 SUPLA_LOG_DEBUG(
"aqi.eco: refresh time: %d", refreshTime);
111 _network->getMacAddr(mac);
112 sensorId = ((mac[2]*256+mac[3])*256+mac[4])*256+mac[5];
123 bool sendData()
override {
124 if (strlen(apiToken) != 32) {
125 SUPLA_LOG_DEBUG(
"aqi.eco: expected token length 32, got %d",
126 static_cast<int>(strlen(apiToken)));
130 StaticJsonDocument<768> jsonBuffer;
131 JsonObject json = jsonBuffer.to<JsonObject>();
133 json[
"esp8266id"] = sensorId;
134 json[
"software_version"] =
"Supla_" SUPLA_SHORT_VERSION;
135 JsonArray sensordatavalues = json.createNestedArray(
"sensordatavalues");
137 for (
int i=0; i < MAXSENSORS; i++) {
139 double value = getSensorValue(i);
140 String type =
"unknown";
142 case Supla::SenorType::PM1:
145 case Supla::SenorType::PM2_5:
148 case Supla::SenorType::PM4:
151 case Supla::SenorType::PM10:
154 case Supla::SenorType::TEMP:
155 type =
"BME280_temperature";
157 case Supla::SenorType::HUMI:
158 type =
"BME280_humidity";
160 case Supla::SenorType::PRESS:
161 type =
"BME280_pressure";
164 case Supla::SenorType::LIGHT:
165 type =
"ambient_light";
167 case Supla::SenorType::WIND:
170 case Supla::SenorType::RAIN:
173 case Supla::SenorType::CO2:
174 type =
"conc_co2_ppm";
179 JsonObject jo = sensordatavalues.createNestedObject();
180 jo[
"value_type"] = type;
188 serializeJson(json, output, 768);
189 SUPLA_LOG_DEBUG(
"aqi.eco: JSON: %s", output);
191 if (client->connect(serverAddress, 443)) {
192 client->print(
"POST /update/");
193 client->print(apiToken);
194 client->println(
" HTTP/1.1");
195 client->print(
"Host: ");
196 client->println(serverAddress);
197 client->println(
"Content-Type: application/json");
198 client->print(
"Content-Length: ");
199 client->println(strlen(output));
201 client->println(output);
203 SUPLA_LOG_DEBUG(
"aqi.eco: sended %d bytes to %s/update/%s",
204 strlen(output), serverAddress, apiToken);
208 if (!client->available()) {
209 SUPLA_LOG_DEBUG(
"aqi.eco: no bytes to read from %s", serverAddress);
212 int responseLength = client->available();
213 SUPLA_LOG_DEBUG(
"aqi.eco: reading from %s: %d bytes",
214 serverAddress, responseLength);
216 if (responseLength >=
static_cast<int>(
sizeof(output))) {
217 responseLength =
sizeof(output) - 1;
220 for (
int i = 0; i < responseLength; i++) {
221 int responseChar = client->read();
222 if (responseChar < 0) {
226 output[i] =
static_cast<char>(responseChar);
227 if (output[i] ==
'\n') {
231 output[responseLength] = 0;
232 SUPLA_LOG_DEBUG(
"aqi.eco: response from %s: %s", serverAddress, output);
241 char serverAddress[33];
242 uint32_t sensorId = 0;