dht: add mqtt entries

This commit is contained in:
Mathieu Maret 2016-04-07 02:04:38 +02:00
parent c497069bf5
commit 50bf6c7d57

View File

@ -3,14 +3,21 @@ WiFiClient client;
Adafruit_MQTT_Client *mqtt; Adafruit_MQTT_Client *mqtt;
Adafruit_MQTT_Publish *mqtt_temp; Adafruit_MQTT_Publish *mqtt_temp;
Adafruit_MQTT_Publish *mqtt_pressure; Adafruit_MQTT_Publish *mqtt_pressure;
Adafruit_MQTT_Publish *mqtt_dht_temp;
Adafruit_MQTT_Publish *mqtt_dht_humidity;
#define NB_ELEMENTS(x) (sizeof(x)/ sizeof(x[0])) #define NB_ELEMENTS(x) (sizeof(x)/ sizeof(x[0]))
#define FEED_MAX_SIZE 64 #define FEED_MAX_SIZE 64
#define TEMPERATURE_FEED_FORMAT "/feeds/%s/temperature" #define TEMPERATURE_FEED_FORMAT "/feeds/%s/temperature"
#define PRESSURE_FEED_FORMAT "/feeds/%s/pressure" #define PRESSURE_FEED_FORMAT "/feeds/%s/pressure"
#define TEMPERATURE_DHT_FEED_FORMAT "/feeds/%s/dht/temperature"
#define HUMIDITY_DHT_FEED_FORMAT "/feeds/%s/dht/humidity"
char temperatureFeed[FEED_MAX_SIZE] = {}; char temperatureFeed[FEED_MAX_SIZE] = {};
char pressureFeed[FEED_MAX_SIZE] = {}; char pressureFeed[FEED_MAX_SIZE] = {};
char temperatureDhtFeed[FEED_MAX_SIZE] = {};
char humidityDhtFeed[FEED_MAX_SIZE] = {};
// Should have less that MAXSUBSCRIPTIONS elements // Should have less that MAXSUBSCRIPTIONS elements
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h // MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
@ -31,10 +38,14 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname)
mqttId = hostname; mqttId = hostname;
snprintf(temperatureFeed, FEED_MAX_SIZE, TEMPERATURE_FEED_FORMAT, mqttId); snprintf(temperatureFeed, FEED_MAX_SIZE, TEMPERATURE_FEED_FORMAT, mqttId);
snprintf(pressureFeed, FEED_MAX_SIZE, PRESSURE_FEED_FORMAT, mqttId); snprintf(pressureFeed, FEED_MAX_SIZE, PRESSURE_FEED_FORMAT, mqttId);
snprintf(temperatureDhtFeed, FEED_MAX_SIZE, TEMPERATURE_FEED_FORMAT, mqttId);
snprintf(humidityDhtFeed, FEED_MAX_SIZE, PRESSURE_FEED_FORMAT, mqttId);
mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd); mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd);
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, temperatureFeed); mqtt_temp = new Adafruit_MQTT_Publish(mqtt, temperatureFeed);
mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, pressureFeed); mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, pressureFeed);
mqtt_dht_temp = new Adafruit_MQTT_Publish(mqtt, temperatureDhtFeed);
mqtt_dht_humidity = new Adafruit_MQTT_Publish(mqtt, humidityDhtFeed);
isMqttConfigured = server[0] != '\0'; isMqttConfigured = server[0] != '\0';
@ -51,7 +62,7 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname)
return 0; return 0;
} }
bool MqttIsConfigured(){ bool MqttIsConfigured() {
return isMqttConfigured; return isMqttConfigured;
} }
int MqttIsConnected() { int MqttIsConnected() {
@ -91,6 +102,15 @@ int MqttPublish(double temp, double pressure) {
return 0; return 0;
} }
int MqttDhtPublish(double temp, double humidity) {
if (MqttConnect() == 0) {
SKETCH_DEBUG_PRINTLN("publishing DHT!");
mqtt_dht_temp->publish(temp);
mqtt_dht_humidity->publish(humidity);
}
return 0;
}
int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription) { int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription) {
char *temp = strstr(subscription->topic, "/gpio/"); char *temp = strstr(subscription->topic, "/gpio/");
if (!temp) if (!temp)