35 lines
920 B
C++
35 lines
920 B
C++
#include "values.hpp"
|
|
|
|
#include <ArduinoJson.h>
|
|
#include <WiFi.h>
|
|
|
|
static inline double round2(double value) {
|
|
return (int)(value * 100 + 0.5) / 100.0;
|
|
}
|
|
|
|
bool Values::read(Adafruit_seesaw* ss) {
|
|
moisture = ss->touchRead(0);
|
|
temperature = ss->getTemp();
|
|
battery = analogRead(A13) * 3.3 / 4095.0 * 2;
|
|
rssi = WiFi.RSSI();
|
|
return true;
|
|
}
|
|
|
|
bool Values::send(PubSubClient* mqtt, const char* topic) {
|
|
StaticJsonDocument<96> doc;
|
|
doc["moisture"] = moisture;
|
|
doc["temperature"] = round2(temperature);
|
|
doc["battery_level"] = round2(battery);
|
|
doc["battery"] = (int)((battery - 3) / 0.95 * 100);
|
|
doc["rssi"] = rssi;
|
|
doc["boot_count"] = boot_count;
|
|
|
|
String msg;
|
|
serializeJson(doc, msg);
|
|
|
|
auto payload = msg.c_str();
|
|
Serial.printf("Publishing sensor values (%d bytes) to %s\n",
|
|
strlen(payload), topic);
|
|
return mqtt->publish(topic, payload);
|
|
}
|