publish bmp280 data

- temperature
- pressure
- altitude
This commit is contained in:
Nicu Hodos 2021-12-09 22:53:31 +01:00
parent fb06940cd1
commit a1b0481416
3 changed files with 33 additions and 1 deletions

View File

@ -4,6 +4,22 @@ namespace Bmp {
Adafruit_BMP280 bmp; // I2C Interface
struct {
const char* topic = "esp_clock/sensor/bmp280/data";
float temp;
float pressure;
float altitude;
float readTemp() {
temp = bmp.readTemperature() - 2;
return temp;
}
void readAll() {
readTemp();
pressure = bmp.readPressure() / 100;
altitude = bmp.readAltitude(1006);
}
} data;
void setup() {
Serial.begin(9600);
Serial.println(F("BMP280 test"));

View File

@ -39,6 +39,7 @@ namespace Ir {
void command(const char c[]) {
avrOn = true;
tCheckWifi.disable();
Wifi::reconnect();
Display::displayText(c);
}
@ -76,7 +77,7 @@ namespace Ir {
command("On");
break;
case 0x84:
Display::displayTemp(Bmp::bmp.readTemperature());
Display::displayTemp(Bmp::data.readTemp());
break;
default:
Display::displayValue(lastCommand);

View File

@ -7,7 +7,9 @@
namespace Mqtt {
void publishCommand();
void publishBmp280();
Task tPublish(TASK_SECOND, TASK_FOREVER, publishCommand, &ts);
Task tPublishBmp(TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts);
AsyncMqttClient client;
@ -25,6 +27,8 @@ namespace Mqtt {
}
} commands;
const char* bmpTopic = "esp_clock/sensor/bmp280/data";
void publishCommand() {
if (!commands.queue.empty() && client.connected()) {
char message[32];
@ -38,13 +42,24 @@ namespace Mqtt {
}
}
void publishBmp280() {
if (client.connected()) {
Bmp::data.readAll();
char message[255];
sprintf(message, "{\"temperature\":%.2f, \"pressure\":%.2f, \"altitude\": %.2f}", Bmp::data.temp, Bmp::data.pressure, Bmp::data.altitude);
client.publish(bmpTopic, 0, true, message);
}
}
void setup() {
client.onConnect([](bool sessionPresent) {
tPublish.enableDelayed();
tPublishBmp.enable();
Serial.println("Connected to MQTT");
});
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
tPublish.disable();
tPublishBmp.disable();
Serial.println("Disconnected from MQTT");
});
client.setServer(MQTT_HOST, MQTT_PORT);