Added MQTT_TOPIC_WEATHER for receiving and storing weather data.

This commit is contained in:
Fabian Schlenz 2019-06-07 06:08:21 +02:00
parent a821985479
commit c153ce915d
2 changed files with 26 additions and 0 deletions

View File

@ -10,6 +10,9 @@ void mqtt_callback(char* complete_topic, byte* pl, unsigned int length);
boolean mqtt_connect();
extern uint8_t weather_icon_ids[6];
extern int8_t weather_temperatures[6];
void mqtt_setup();
void mqtt_loop();

View File

@ -22,9 +22,31 @@ WiFiClient wifi;
PubSubClient mqtt_client(wifi);
long mqtt_last_reconnect_attempt = 0;
uint8_t weather_icon_ids[6] = {0, 0, 0, 0, 0, 0};
int8_t weather_temperatures[6] = {0, 0, 0, 0, 0, 0};
void mqtt_callback(char* complete_topic, byte* pl, unsigned int length) {
pl[length] = '\0';
char* payload = (char*)pl;
LOG("MQTT * Received data for topic "); LOGln(complete_topic);
if (strncmp(complete_topic, MQTT_TOPIC_WEATHER, strlen(MQTT_TOPIC_WEATHER))==0) {
// Weather stuff
complete_topic += strlen(MQTT_TOPIC_WEATHER);
if (strncmp(complete_topic, "icons/", 6)==0) {
complete_topic += 6; // strlen("icons/")=6
uint8_t id = complete_topic[0] - '0'; // ID is 1 digit. Substract '0' from it to get it as number.
if (id>=6) return;
weather_icon_ids[id] = atol(payload);
} else if (strncmp(complete_topic, "temperatures/", 13)==0) {
complete_topic += 13; // strlen("temperatures/")=13
uint8_t id = complete_topic[0] - '0'; // ID is 1 digit. Substract '0' from it to get it as number.
if (id>=6) return;
weather_temperatures[id] = atol(payload);
}
return;
}
char* topic = complete_topic + strlen(MQTT_TOPIC); // Strip MQTT_TOPIC from the beginning
// Here, payload is a char* (but has to be casted).
@ -67,6 +89,7 @@ boolean mqtt_connect() {
LOGln("MQTT * Connected.");
mqtt_client.publish(MQTT_TOPIC "status", "ONLINE");
mqtt_client.subscribe(MQTT_TOPIC "+");
mqtt_client.subscribe(MQTT_TOPIC_WEATHER "#");
}
return mqtt_client.connected();
}