Add hostname in mqtt topics
So we are able to have several devices
This commit is contained in:
parent
d51df5dd64
commit
dfb5746180
@ -5,26 +5,39 @@ Adafruit_MQTT_Publish *mqtt_temp;
|
||||
Adafruit_MQTT_Publish *mqtt_pressure;
|
||||
|
||||
#define NB_ELEMENTS(x) (sizeof(x)/ sizeof(x[0]))
|
||||
#define FEED_MAX_SIZE 64
|
||||
|
||||
const char TEMPERATURE_FEED[] = "/feeds/temperature";
|
||||
const char PRESSURE_FEED[] = "/feeds/pressure";
|
||||
#define TEMPERATURE_FEED_FORMAT "/feeds/%s/temperature"
|
||||
#define PRESSURE_FEED_FORMAT "/feeds/%s/pressure"
|
||||
char temperatureFeed[FEED_MAX_SIZE] = {};
|
||||
char pressureFeed[FEED_MAX_SIZE] = {};
|
||||
|
||||
// Should have less that MAXSUBSCRIPTIONS elements
|
||||
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
|
||||
const int gpioWatched[] = {12, 13};
|
||||
|
||||
#define GPIO_BASE "/feeds/gpio/"
|
||||
const char *GPIO_FEED[] = { GPIO_BASE"12", GPIO_BASE"13"};
|
||||
const char *GPIO_SET_FEED[] = { GPIO_BASE"12/set", GPIO_BASE"13/set"};
|
||||
#define GPIO_FEED_FORMAT "/feeds/%s/gpio/%d"
|
||||
#define GPIO_SET_FEED_FORMAT "/feeds/%s/gpio/%d/set"
|
||||
|
||||
char *mqttId;
|
||||
char GPIO_FEED[MAXSUBSCRIPTIONS][FEED_MAX_SIZE] = {};
|
||||
char GPIO_SET_FEED[MAXSUBSCRIPTIONS][FEED_MAX_SIZE] = {};
|
||||
|
||||
Adafruit_MQTT_Publish * mqttGpio[MAXSUBSCRIPTIONS] = {};
|
||||
|
||||
int MqttSetup(char *server, char *user, char *passwd, int port) {
|
||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||
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);
|
||||
|
||||
mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd);
|
||||
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED);
|
||||
mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, PRESSURE_FEED);
|
||||
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, temperatureFeed);
|
||||
mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, pressureFeed);
|
||||
|
||||
for (int i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) {
|
||||
snprintf(GPIO_FEED[i], FEED_MAX_SIZE, GPIO_FEED_FORMAT, mqttId, gpioWatched[i]);
|
||||
snprintf(GPIO_SET_FEED[i], FEED_MAX_SIZE, GPIO_SET_FEED_FORMAT, mqttId, gpioWatched[i]);
|
||||
|
||||
Adafruit_MQTT_Subscribe *gpioSet = new Adafruit_MQTT_Subscribe(mqtt, GPIO_SET_FEED[i]);
|
||||
mqtt->subscribe(gpioSet);
|
||||
|
||||
@ -72,9 +85,10 @@ int MqttPublish(double temp, double pressure) {
|
||||
}
|
||||
|
||||
int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription) {
|
||||
if (!strstr(subscription->topic, GPIO_BASE))
|
||||
char *temp = strstr(subscription->topic, "/gpio/");
|
||||
if (!temp)
|
||||
return -1;
|
||||
String gpioStr(subscription->topic + strlen(GPIO_BASE));
|
||||
String gpioStr(temp + strlen("/gpio/"));
|
||||
int idx = gpioStr.indexOf("/");
|
||||
int gpio = gpioStr.substring(0, idx).toInt();
|
||||
|
||||
|
@ -75,7 +75,7 @@ bool BMP180IsConnected();
|
||||
/* MQTT decl */
|
||||
int MqttConnect();
|
||||
int MqttIsConnected();
|
||||
int MqttSetup(char *server, char *user, char *passwd, int port);
|
||||
int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname);
|
||||
int MqttPublish(double temp, double pressure);
|
||||
void MqttCheckSubscription();
|
||||
void MqttChangeGpioValue(int gpio, int value);
|
||||
@ -197,7 +197,7 @@ void setup() {
|
||||
Serial.println();
|
||||
|
||||
WifiSetup(mode, txStatus == 0, confSsid, confPassword, confHost);
|
||||
MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort);
|
||||
MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort, confHost);
|
||||
|
||||
if (mode == BOOTMODE_OTA) {
|
||||
OTASetup();
|
||||
|
Loading…
Reference in New Issue
Block a user