Feed Ipaddress by MQTT

This commit is contained in:
Mathieu Maret 2016-06-18 02:24:00 +02:00
parent 286844dd6d
commit 967c6c736f
3 changed files with 14 additions and 0 deletions

View File

@ -8,6 +8,7 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname)
int MqttPublish(double temp, double pressure); int MqttPublish(double temp, double pressure);
int MqttDhtPublish(float temp, float humidity); int MqttDhtPublish(float temp, float humidity);
int MqttDryPublish(int dry); int MqttDryPublish(int dry);
int MqttIPPublish(const String &ip);
void MqttCheckSubscription(); void MqttCheckSubscription();
void MqttChangeGpioValue(int gpio, int value); void MqttChangeGpioValue(int gpio, int value);
bool MqttIsConfigured(); bool MqttIsConfigured();

View File

@ -9,6 +9,7 @@ Adafruit_MQTT_Publish *mqtt_pressure;
Adafruit_MQTT_Publish *mqtt_dht_temp; Adafruit_MQTT_Publish *mqtt_dht_temp;
Adafruit_MQTT_Publish *mqtt_dht_humidity; Adafruit_MQTT_Publish *mqtt_dht_humidity;
Adafruit_MQTT_Publish *mqtt_dry; Adafruit_MQTT_Publish *mqtt_dry;
Adafruit_MQTT_Publish *mqtt_ip;
#define NB_ELEMENTS(x) (sizeof(x)/ sizeof(x[0])) #define NB_ELEMENTS(x) (sizeof(x)/ sizeof(x[0]))
#define FEED_MAX_SIZE 96 #define FEED_MAX_SIZE 96
@ -27,6 +28,8 @@ const int gpioWatched[] = CONFIG_MQTT_CONTROLLED_GPIO;
#define GPIO_FEED_FORMAT "/feeds/%s/%s/gpio/%d" #define GPIO_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
#define GPIO_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set" #define GPIO_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
#define IP_FEED_FORMAT "/feeds/%s/%s/configuration/ip/addr"
char *mqttId; char *mqttId;
char GPIO_FEED[MAXSUBSCRIPTIONS][FEED_MAX_SIZE] = {}; char GPIO_FEED[MAXSUBSCRIPTIONS][FEED_MAX_SIZE] = {};
char GPIO_SET_FEED[MAXSUBSCRIPTIONS][FEED_MAX_SIZE] = {}; char GPIO_SET_FEED[MAXSUBSCRIPTIONS][FEED_MAX_SIZE] = {};
@ -49,6 +52,7 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname)
mqtt_temp = MqttCreatePublisher(TEMPERATURE_FEED_FORMAT, user, mqttId); mqtt_temp = MqttCreatePublisher(TEMPERATURE_FEED_FORMAT, user, mqttId);
mqtt_pressure = MqttCreatePublisher(PRESSURE_FEED_FORMAT, user, mqttId); mqtt_pressure = MqttCreatePublisher(PRESSURE_FEED_FORMAT, user, mqttId);
mqtt_dry = MqttCreatePublisher(DRY_FEED_FORMAT, user, mqttId); mqtt_dry = MqttCreatePublisher(DRY_FEED_FORMAT, user, mqttId);
mqtt_ip = MqttCreatePublisher(IP_FEED_FORMAT, user, mqttId);
for (uint i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) { for (uint i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) {
snprintf(GPIO_FEED[i], FEED_MAX_SIZE, GPIO_FEED_FORMAT, user, mqttId, gpioWatched[i]); snprintf(GPIO_FEED[i], FEED_MAX_SIZE, GPIO_FEED_FORMAT, user, mqttId, gpioWatched[i]);
@ -125,6 +129,14 @@ int MqttDryPublish(int dry) {
return 0; return 0;
} }
int MqttIPPublish(const String &ip) {
if (MqttConnect() == 0) {
SKETCH_DEBUG_PRINTLN("publishing IP!");
mqtt_ip->publish(ip.c_str());
}
return 0;
}
int MqttDhtPublish(float temp, float humidity) { int MqttDhtPublish(float temp, float humidity) {
if (MqttConnect() == 0) { if (MqttConnect() == 0) {
SKETCH_DEBUG_PRINTLN("publishing DHT!"); SKETCH_DEBUG_PRINTLN("publishing DHT!");

View File

@ -225,6 +225,7 @@ void setup() {
WifiSetup(mode, confSsid, confPassword, confHost, ip_mode, ip, gw, mask, dns, dns2); WifiSetup(mode, confSsid, confPassword, confHost, ip_mode, ip, gw, mask, dns, dns2);
if (mode == BOOTMODE_NORMAL) { if (mode == BOOTMODE_NORMAL) {
MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort, confHost); MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort, confHost);
MqttIPPublish(WiFi.localIP().toString());
} }
if (mode == BOOTMODE_OTA) { if (mode == BOOTMODE_OTA) {