Browse Source

auto update

main
Hendrik Langer 5 years ago
parent
commit
8000d3809d
  1. 19
      src/main.cpp

19
src/main.cpp

@ -391,7 +391,7 @@ void getSensorMeasurements() {
void receiveMqtt(const char* topic, const char* data, int data_len) { void receiveMqtt(const char* topic, const char* data, int data_len) {
sensor_readings_t* sensor = NULL; sensor_readings_t* sensor = NULL;
ESP_LOGI(TAG, "received MQTT message on subscribed topic %s", topic); ESP_LOGI(TAG, "received MQTT message on subscribed topic \"%s\", len: %d", topic, data_len);
if (strstr(topic, "thomas/sensor/a4cf1211c3e4") == topic) { if (strstr(topic, "thomas/sensor/a4cf1211c3e4") == topic) {
sensor = &sensors_a4cf1211c3e4; sensor = &sensors_a4cf1211c3e4;
@ -405,8 +405,8 @@ void receiveMqtt(const char* topic, const char* data, int data_len) {
char* topic_last = strrchr(topic, '/'); char* topic_last = strrchr(topic, '/');
if (topic_last && sensor) { if (topic_last) {
if (strcmp("/json", topic_last) == 0) { if (strcmp("/json", topic_last) == 0 && sensor) {
StaticJsonDocument<JSON_CAPACITY+120> jsonDoc; StaticJsonDocument<JSON_CAPACITY+120> jsonDoc;
DeserializationError err = deserializeJson(jsonDoc, data, data_len); DeserializationError err = deserializeJson(jsonDoc, data, data_len);
if (err) { if (err) {
@ -429,6 +429,16 @@ void receiveMqtt(const char* topic, const char* data, int data_len) {
ESP_LOGI(TAG, "got new values from %s, timestamp: %lu", topic, sensor->lastUpdate); ESP_LOGI(TAG, "got new values from %s, timestamp: %lu", topic, sensor->lastUpdate);
ESP_LOGI(TAG, "%lu seconds ago", topic, getTimestamp() - sensor->lastUpdate); ESP_LOGI(TAG, "%lu seconds ago", topic, getTimestamp() - sensor->lastUpdate);
} }
} else if (strcmp("/command", topic_last) == 0) {
ESP_LOGW(TAG, "received command");
if (data_len == 6 && strncmp("update", data, data_len) == 0) {
ESP_LOGI(TAG, "update command");
int msg_id = mqtt.publish(topic, "", 0, 1, 1);
mqtt.waitForMsg(msg_id, 5000 / portTICK_PERIOD_MS);
ota.update();
} else {
ESP_LOGW(TAG, "UNKNOWN COMMAND");
}
} }
} }
} }
@ -792,6 +802,9 @@ void setup()
if (!ota.getMAC().equals("246f28d1a080")) mqtt.subscribe("thomas/sensor/246f28d1a080/json", receiveMqtt); if (!ota.getMAC().equals("246f28d1a080")) mqtt.subscribe("thomas/sensor/246f28d1a080/json", receiveMqtt);
if (!ota.getMAC().equals("246f28d1eff4")) mqtt.subscribe("thomas/sensor/246f28d1eff4/json", receiveMqtt); if (!ota.getMAC().equals("246f28d1eff4")) mqtt.subscribe("thomas/sensor/246f28d1eff4/json", receiveMqtt);
static String topic_command = String("thomas/sensor/") + ota.getMAC() + String("/command");
mqtt.subscribe(topic_command.c_str(), receiveMqtt);
if (WiFi.SSID() == "LNet") { if (WiFi.SSID() == "LNet") {
station_height = 135; station_height = 135;
} else if (WiFi.SSID() == "Galaktisches Imperium") { } else if (WiFi.SSID() == "Galaktisches Imperium") {

Loading…
Cancel
Save