36 explicit AQIECO(
Supla::Network* _network,
char token[],
int refresh = 180,
37 char server[] =
"api.aqi.eco",
int id = 0)
40 strncpy(serverAddress, server, 32);
41 serverAddress[32] = 0;
44 if (strlen(token) == 32) {
45 strncpy(apiToken, token, 32);
50 SUPLA_LOG_DEBUG(
"aqi.eco: token: %s", apiToken);
56 refreshTime = refresh;
58 SUPLA_LOG_DEBUG(
"aqi.eco: refresh time: %d", refreshTime);
63 _network->getMacAddr(mac);
64 sensorId = ((mac[2]*256+mac[3])*256+mac[4])*256+mac[5];
70 bool sendData()
override {
71 if (strlen(apiToken) != 32) {
72 SUPLA_LOG_DEBUG(
"aqi.eco: wrong token length: %s", apiToken);
76 StaticJsonDocument<768> jsonBuffer;
77 JsonObject json = jsonBuffer.to<JsonObject>();
79 json[
"esp8266id"] = sensorId;
80 json[
"software_version"] =
"Supla_" SUPLA_SHORT_VERSION;
81 JsonArray sensordatavalues = json.createNestedArray(
"sensordatavalues");
83 for (
int i=0; i < MAXSENSORS; i++) {
85 double value = getSensorValue(i);
86 String type =
"unknown";
88 case Supla::SenorType::PM1:
91 case Supla::SenorType::PM2_5:
94 case Supla::SenorType::PM4:
97 case Supla::SenorType::PM10:
100 case Supla::SenorType::TEMP:
101 type =
"BME280_temperature";
103 case Supla::SenorType::HUMI:
104 type =
"BME280_humidity";
106 case Supla::SenorType::PRESS:
107 type =
"BME280_pressure";
110 case Supla::SenorType::LIGHT:
111 type =
"ambient_light";
113 case Supla::SenorType::WIND:
116 case Supla::SenorType::RAIN:
119 case Supla::SenorType::CO2:
120 type =
"conc_co2_ppm";
125 JsonObject jo = sensordatavalues.createNestedObject();
126 jo[
"value_type"] = type;
134 serializeJson(json, output, 768);
135 SUPLA_LOG_DEBUG(
"aqi.eco: JSON: %s", output);
137 WiFiClientSecure client;
138 client.setInsecure();
139 if (client.connect(serverAddress, 443)) {
140 client.print(
"POST /update/");
141 client.print(apiToken);
142 client.println(
" HTTP/1.1");
143 client.print(
"Host: ");
144 client.println(serverAddress);
145 client.println(
"Content-Type: application/json");
146 client.print(
"Content-Length: ");
147 client.println(strlen(output));
149 client.println(output);
151 SUPLA_LOG_DEBUG(
"aqi.eco: sended %d bytes to %s/update/%s",
152 strlen(output), serverAddress, apiToken);
156 if (!client.available()) {
157 SUPLA_LOG_DEBUG(
"aqi.eco: no bytes to read from %s", serverAddress);
160 SUPLA_LOG_DEBUG(
"aqi.eco: reading from %s: %d bytes",
161 serverAddress, client.available());
163 output[client.available()] = 0;
164 for (
int i=0; client.available(); i++) {
165 output[i] = client.read();
166 if (output[i] ==
'\n') {
170 SUPLA_LOG_DEBUG(
"aqi.eco: response from %s: %s", serverAddress, output);
178 char serverAddress[33];
179 uint32_t sensorId = 0;