Simplify MQTT sensor setup

This commit is contained in:
Mathieu Maret 2016-04-14 01:15:42 +02:00
parent 6ce9e63a22
commit 8101e7bd36
2 changed files with 12 additions and 26 deletions

View File

@ -1,4 +1,6 @@
#include <stdarg.h>
#include "MQTT.h"
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
Adafruit_MQTT_Client *mqtt;
@ -15,11 +17,6 @@ Adafruit_MQTT_Publish *mqtt_dht_humidity;
#define TEMPERATURE_DHT_FEED_FORMAT "/feeds/%s/dht/temperature"
#define HUMIDITY_DHT_FEED_FORMAT "/feeds/%s/dht/humidity"
char temperatureFeed[FEED_MAX_SIZE] = {};
char pressureFeed[FEED_MAX_SIZE] = {};
char temperatureDhtFeed[FEED_MAX_SIZE] = {};
char humidityDhtFeed[FEED_MAX_SIZE] = {};
// Should have less that MAXSUBSCRIPTIONS elements
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
const int gpioWatched[] = CONFIG_MQTT_CONTROLLED_GPIO;
@ -35,20 +32,17 @@ Adafruit_MQTT_Publish * mqttGpio[MAXSUBSCRIPTIONS] = {};
bool isMqttConfigured = false;
int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname) {
mqttId = hostname;
snprintf(temperatureFeed, FEED_MAX_SIZE, TEMPERATURE_FEED_FORMAT, mqttId);
snprintf(pressureFeed, FEED_MAX_SIZE, PRESSURE_FEED_FORMAT, mqttId);
snprintf(temperatureDhtFeed, FEED_MAX_SIZE, TEMPERATURE_DHT_FEED_FORMAT, mqttId);
snprintf(humidityDhtFeed, FEED_MAX_SIZE, HUMIDITY_DHT_FEED_FORMAT, mqttId);
isMqttConfigured = server[0] != '\0';
if(!isMqttConfigured)
return 0;
mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd);
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, temperatureFeed);
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';
mqtt_dht_temp = MqttCreatePublisher("/feeds/%s/dht/temperature", mqttId);
mqtt_dht_humidity = MqttCreatePublisher("/feeds/%s/dht/humidity", mqttId);
for (uint i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) {
snprintf(GPIO_FEED[i], FEED_MAX_SIZE, GPIO_FEED_FORMAT, mqttId, gpioWatched[i]);
@ -64,7 +58,7 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname)
}
Adafruit_MQTT_Publish *MqttCreatePublisher(const char *fmt, ...){
char buf[128];
char buf[FEED_MAX_SIZE];
va_list args;
va_start (args, fmt);
vsnprintf(buf, sizeof(buf), (const char *)fmt, args);
@ -78,7 +72,7 @@ bool MqttIsConfigured() {
}
int MqttIsConnected() {
return (mqtt != NULL) ? mqtt->connected() : 0;
return (isMqttConfigured) ? mqtt->connected() : 0;
}
// Function to connect and reconnect as necessary to the MQTT server.

View File

@ -41,6 +41,7 @@
#include "debug_sketch.h"
#include "BMP180.h"
#include "sensor_DHT.h"
#include "MQTT.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
@ -84,15 +85,6 @@ void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **ho
int &mqttPort, int &ip_config, uint32_t &ip, uint32_t &gw,
uint32_t &mask, uint32_t &dns, uint32_t &dns2);
/* MQTT decl */
int MqttConnect();
int MqttIsConnected();
int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname);
int MqttPublish(double temp, double pressure);
int MqttDhtPublish(float temp, float humidity);
void MqttCheckSubscription();
void MqttChangeGpioValue(int gpio, int value);
bool MqttIsConfigured();
void WifiSetup(int bootmode, char *confSsid, char *confPassword, char *confHost, int ip_config, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t dns, uint32_t dns2) {
IPAddress myIP;