V1.3.1 - MQTT published BME-Werte

This commit is contained in:
rxf
2024-07-22 18:03:22 +02:00
parent ad9d426a23
commit bcba2bc319
6 changed files with 112 additions and 48 deletions

View File

@@ -132,7 +132,7 @@ void lv_create_main_gui(void)
// Text als Überschrift an die Axen
lv_obj_t *feuchte_label = lv_label_create(lv_screen_active());
lv_label_set_text(feuchte_label, "Feuchte");
lv_obj_align(feuchte_label, LV_ALIGN_RIGHT_MID, -3, 20);
lv_obj_align(feuchte_label, LV_ALIGN_RIGHT_MID, -3, 15);
// Set font type and font size. More information: https://docs.lvgl.io/master/overview/font.html
static lv_style_t style_feuchte_label;
@@ -142,7 +142,7 @@ void lv_create_main_gui(void)
lv_obj_t *temp_label = lv_label_create(lv_screen_active());
lv_label_set_text(temp_label, "Temperatur");
lv_obj_align(temp_label, LV_ALIGN_LEFT_MID, 3, 20);
lv_obj_align(temp_label, LV_ALIGN_LEFT_MID, 3, 15);
// Set font type and font size. More information: https://docs.lvgl.io/master/overview/font.html
static lv_style_t style_temp_label;
@@ -192,7 +192,7 @@ void setValuetoArc(ARC arc, float value)
}
}
#define CONTAINER_WIDTH SCREEN_WIDTH
#define CONTAINER_HEIGHT SCREEN_HEIGHT - 145
#define CONTAINER_HEIGHT SCREEN_HEIGHT - 140
#define LEFTAXIS_WIDTH 25
#define RIGHTAXIS_WIDTH 15
#define CHART_WIDTH (CONTAINER_WIDTH - LEFTAXIS_WIDTH - RIGHTAXIS_WIDTH - 40)
@@ -222,7 +222,7 @@ void drawChart()
lv_obj_set_size(scaleT, 30, 75);
lv_scale_set_mode(scaleT, LV_SCALE_MODE_VERTICAL_LEFT);
static const char *scaleT_labels[5] = {"10°C", "15°C", "20°C", "25°C", "30°C"};
static const char *scaleT_labels[6] = {"15°C", "20°C", "25°C", "30°C", "35°C"};
lv_scale_set_text_src(scaleT, scaleT_labels);
lv_scale_set_label_show(scaleT, true);
@@ -234,7 +234,7 @@ void drawChart()
lv_obj_set_style_line_width(scaleT, 0, LV_PART_ITEMS);
lv_obj_set_style_line_width(scaleT, 0, LV_PART_INDICATOR);
lv_obj_set_style_line_width(scaleT, 0, LV_PART_MAIN);
lv_scale_set_range(scaleT, 10, 30);
lv_scale_set_range(scaleT, 15, 35);
static lv_style_t style_scaleT;
lv_style_init(&style_scaleT);
@@ -251,7 +251,7 @@ void drawChart()
lv_chart_set_point_count(chart, MAXREADINGS);
lv_chart_set_div_line_count(chart, 5, 5);
lv_chart_set_range(chart, LV_CHART_AXIS_SECONDARY_Y, 40, 100);
lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 10, 30);
lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 15, 35);
lv_obj_set_style_size(chart, 0, 0, LV_PART_INDICATOR); // No points on line
lv_obj_set_scrollbar_mode(chart, LV_SCROLLBAR_MODE_OFF);
lv_obj_set_style_border_width(chart, 0, LV_PART_MAIN); // No border
@@ -302,21 +302,38 @@ void drawChart()
lv_style_set_text_font(&style_unitxAx_label, &lv_font_montserrat_12);
lv_obj_add_style(unitxAx_label, &style_unitxAx_label, 0);
*/
/* ganz rechts unten die Version anzeigen */
lv_obj_t *version_label = lv_label_create(chart);
/* ganz unten etwas Info anzeigen */
// Style dafür
static lv_style_t style_info_label;
lv_style_init(&style_info_label);
lv_style_set_text_font(&style_info_label, &lv_font_montserrat_8);
lv_style_set_text_color(&style_info_label, LV_COLOR_MAKE(0x40, 0x40, 0x40));
// Versionsnummer und Datum
lv_obj_t *version_label = lv_label_create(lv_screen_active());
char ver[20];
sprintf(ver, "V %s %s", VERSION, VDATE);
lv_label_set_text(version_label, ver);
lv_obj_align(version_label, LV_ALIGN_BOTTOM_RIGHT, 0, 9);
lv_obj_align(version_label, LV_ALIGN_BOTTOM_RIGHT, -10, 0);
lv_obj_add_style(version_label, &style_info_label, LV_PART_MAIN);
// Set font type and font size. More information: https://docs.lvgl.io/master/overview/font.html
static lv_style_t style_version_label;
lv_style_init(&style_version_label);
lv_style_set_text_font(&style_version_label, &lv_font_montserrat_8);
lv_style_set_text_color(&style_version_label, lv_palette_main(LV_PALETTE_GREY));
lv_obj_add_style(version_label, &style_version_label, LV_PART_MAIN);
// links davon die Abtastrate
bmr_label = lv_label_create(lv_screen_active());
char rate[20];
sprintf(rate, "Rate: %ds", updateTime / 1000);
lv_label_set_text(bmr_label, rate);
lv_obj_align(bmr_label, LV_ALIGN_BOTTOM_LEFT, 10, 0);
lv_obj_add_style(bmr_label, &style_info_label, LV_PART_MAIN);
}
void showRate(int r) {
char rate[20];
sprintf(rate, "Rate: %ds", r);
lv_label_set_text(bmr_label, rate);
}
void updatechartData(float value, float humvalue)
{
int i = 0;

View File

@@ -31,18 +31,18 @@ typedef struct {
} ARC;
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define BACKGROUND_COLOR 0xD3F0FF
#ifdef MAIN
ARC arcTemp = {NULL, "temp", 40, -1, 110, 30.0, "%.1f°C", "°C", 10, 30, 20, 25, -11};
ARC arcHum = {NULL, "humi", 170, -1, 110, 60.0, "%.0f%%", "%", 40, 100, 65, 70, 2};
ARC arcTemp = {NULL, "temp", 40, -6, 110, 30.0, "%.1f°C", "°C", 15, 35, 20, 25, -11};
ARC arcHum = {NULL, "humi", 170, -6, 110, 60.0, "%.0f%%", "%", 40, 100, 65, 70, 2};
lv_obj_t * date_label;
lv_obj_t * time_label;
lv_obj_t * ta_label;
lv_obj_t * fa_label;
lv_obj_t * bmr_label;
#else
extern ARC arcTemp;
extern ARC arcHum;
@@ -50,6 +50,7 @@ extern lv_obj_t * date_label;
extern lv_obj_t * time_label;
extern lv_obj_t * ta_label;
extern lv_obj_t * fa_label;
extern lv_obj_t * bmr_label;
#endif
@@ -59,7 +60,7 @@ void setValuetoArc(ARC arc, float value);
void drawChart();
void updatechartData(float value, float value_hum);
void setAussenDataValues(float val, bool what);
void showRate(int r);

View File

@@ -30,11 +30,10 @@
#include <locale.h>
#define BMWREADTIME 10000
#define NTPREADTIME 1000
long bmeReadtimer = 0;
long ntpReadtimer = 0;
long updatetimer = 0;
BME280Data bmedata;
@@ -42,6 +41,14 @@ int currentSecond = -1;
void showDateTime(struct tm tinfo);
void setBMEreadtime(int time) {
bmeReadTime = time * 1000;
}
void setUpdatetime(int time) {
updateTime = time * 1000;
}
void setup() {
setlocale(LC_ALL, "de_DE");
String LVGL_Arduino = String("LVGL Library Version: ") + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
@@ -98,14 +105,18 @@ void loop() {
if (timeinfo.tm_sec == 0) { // minute over
showDateTime(timeinfo);
}
if (millis() - bmeReadtimer > BMWREADTIME) {
if (millis() - bmeReadtimer > bmeReadTime) {
bmeReadtimer = millis();
bmedata = readBME280();
publishMQTTmessage(bmedata.temp, bmedata.hum);
}
if (millis() - updatetimer > updateTime) {
updatetimer = millis();
updatechartData(bmedata.temp, bmedata.hum);
setValuetoArc(arcTemp, bmedata.temp);
setValuetoArc(arcHum, bmedata.hum);
updatechartData(bmedata.temp, bmedata.hum);
}
lv_task_handler(); // let the GUI do its work
lv_tick_inc(5); // tell LVGL how much time has passed
delay(5); // let this time pass

View File

@@ -6,8 +6,8 @@ extern "C" {
#include "freertos/timers.h"
}
#define VERSION "1.2.0"
#define VDATE "2024-07-21"
#define VERSION "1.3.1"
#define VDATE "2024-07-22"
//#define MAXREADINGS 224
#define MAXREADINGS 200
@@ -15,8 +15,18 @@ extern "C" {
#ifdef MAIN
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
int bmeReadTime = 10000;
int updateTime = 30000;
#else
extern TimerHandle_t mqttReconnectTimer;
extern TimerHandle_t wifiReconnectTimer;
extern int bmeReadTime;
extern int updateTime;
#endif
void setBMEreadtime(int time);
#endif

View File

@@ -1,6 +1,7 @@
#define MQTT
#include "grafik.h"
#include "main.h"
#include "mqtt.h"
void setupMQTT() {
@@ -30,7 +31,7 @@ void onMqttConnect(bool sessionPresent) {
// Subscribe to topic "message" when it connects to the broker
char topic[50];
strcpy(topic, MQTT_TOPIC);
strcat(topic,"/+");
strcat(topic,"#");
Serial.println(topic);
uint16_t packetIdSub = mqttClient.subscribe(topic, 2);
//Serial.print("Subscribing at QoS 2, packetId: ");
@@ -61,28 +62,53 @@ void onMqttUnsubscribe(uint16_t packetId) {
JsonDocument doc;
void parseMQTTmessages(char *topic, String msg) {
// Parse the JSON message
DeserializationError error = deserializeJson(doc, msg);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
// Extract values
if (strcmp(topic, "CYD/set/hm/aussen") == 0) {
if(doc.containsKey("temperature")) {
setAussenDataValues(doc["temperature"], true);
}
if(doc.containsKey("humidity")) {
setAussenDataValues(doc["humidity"], false);
}
} else if (strcmp(topic, "CYD/set/parameter") == 0) {
if(doc.containsKey("bmerate")) {
setBMEreadtime(doc["bmerate"]);
// showRate(doc["bmerate"]);
}
if(doc.containsKey("updaterate")) {
setBMEreadtime(doc["updaterate"]);
showRate(doc["updaterate"]);
}
}
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
// Do whatever you want when you receive a message
// Save the message in a variable
Serial.println(topic);
String received_message;
for (int i = 0; i < len; i++) {
received_message += (char)payload[i];
}
Serial.printf("%s:%s\n", topic, received_message.c_str());
// Display the text message on the display receive from the MQTT topic "message"
float wert;
bool what;
if (strcmp(topic, MQTT_TOPIC_T) == 0) {
deserializeJson(doc, received_message);
Serial.print("AT: ");
wert = doc["val"];
what = true;
} else if (strcmp(topic, MQTT_TOPIC_H) == 0) {
deserializeJson(doc, received_message);
Serial.print("AF: ");
wert = doc["val"];
what = false;
parseMQTTmessages(topic, received_message);
}
#define MQTT_PUB_TEMP "CYD/get/bme280"
void publishMQTTmessage(float temp, float hum) {
// Publish an MQTT message on topic esp32/ds18b20/temperature
char payload[50];
sprintf(payload, "{\"temperature\":%.1f,\"humidity\":%.1f}", temp, hum);
uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, payload);
// Serial.printf("Publishing on topic %s at QoS 1, packetId: ", MQTT_PUB_TEMP);
// Serial.println(packetIdPub1);
}
Serial.println(wert);
setAussenDataValues(wert, what);
}

View File

@@ -12,9 +12,7 @@
#define BROKER_USER "rxf"
#define BROKER_PASS "Tux4S!ech"
#define MQTT_TOPIC "hm/status/Aussen_Temp:1"
#define MQTT_TOPIC_T "hm/status/Aussen_Temp:1/TEMPERATURE"
#define MQTT_TOPIC_H "hm/status/Aussen_Temp:1/HUMIDITY"
#define MQTT_TOPIC "CYD/set/"
#ifdef MQTT
AsyncMqttClient mqttClient;
@@ -29,6 +27,7 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
void onMqttSubscribe(uint16_t packetId, uint8_t qos);
void onMqttUnsubscribe(uint16_t packetId);
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
void publishMQTTmessage(float temp, float hum);
#endif