publish bmp280 data
- temperature - pressure - altitude
This commit is contained in:
parent
fb06940cd1
commit
a1b0481416
@ -4,6 +4,22 @@ namespace Bmp {
|
|||||||
|
|
||||||
Adafruit_BMP280 bmp; // I2C Interface
|
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() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.println(F("BMP280 test"));
|
Serial.println(F("BMP280 test"));
|
||||||
|
|||||||
@ -39,6 +39,7 @@ namespace Ir {
|
|||||||
|
|
||||||
void command(const char c[]) {
|
void command(const char c[]) {
|
||||||
avrOn = true;
|
avrOn = true;
|
||||||
|
tCheckWifi.disable();
|
||||||
Wifi::reconnect();
|
Wifi::reconnect();
|
||||||
Display::displayText(c);
|
Display::displayText(c);
|
||||||
}
|
}
|
||||||
@ -76,7 +77,7 @@ namespace Ir {
|
|||||||
command("On");
|
command("On");
|
||||||
break;
|
break;
|
||||||
case 0x84:
|
case 0x84:
|
||||||
Display::displayTemp(Bmp::bmp.readTemperature());
|
Display::displayTemp(Bmp::data.readTemp());
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Display::displayValue(lastCommand);
|
Display::displayValue(lastCommand);
|
||||||
|
|||||||
@ -7,7 +7,9 @@
|
|||||||
namespace Mqtt {
|
namespace Mqtt {
|
||||||
|
|
||||||
void publishCommand();
|
void publishCommand();
|
||||||
|
void publishBmp280();
|
||||||
Task tPublish(TASK_SECOND, TASK_FOREVER, publishCommand, &ts);
|
Task tPublish(TASK_SECOND, TASK_FOREVER, publishCommand, &ts);
|
||||||
|
Task tPublishBmp(TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts);
|
||||||
|
|
||||||
AsyncMqttClient client;
|
AsyncMqttClient client;
|
||||||
|
|
||||||
@ -25,6 +27,8 @@ namespace Mqtt {
|
|||||||
}
|
}
|
||||||
} commands;
|
} commands;
|
||||||
|
|
||||||
|
const char* bmpTopic = "esp_clock/sensor/bmp280/data";
|
||||||
|
|
||||||
void publishCommand() {
|
void publishCommand() {
|
||||||
if (!commands.queue.empty() && client.connected()) {
|
if (!commands.queue.empty() && client.connected()) {
|
||||||
char message[32];
|
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() {
|
void setup() {
|
||||||
client.onConnect([](bool sessionPresent) {
|
client.onConnect([](bool sessionPresent) {
|
||||||
tPublish.enableDelayed();
|
tPublish.enableDelayed();
|
||||||
|
tPublishBmp.enable();
|
||||||
Serial.println("Connected to MQTT");
|
Serial.println("Connected to MQTT");
|
||||||
});
|
});
|
||||||
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
|
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
|
||||||
tPublish.disable();
|
tPublish.disable();
|
||||||
|
tPublishBmp.disable();
|
||||||
Serial.println("Disconnected from MQTT");
|
Serial.println("Disconnected from MQTT");
|
||||||
});
|
});
|
||||||
client.setServer(MQTT_HOST, MQTT_PORT);
|
client.setServer(MQTT_HOST, MQTT_PORT);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user