MQTT: add user name in URL

So we can support several user on the same server.
URL are /feeds/USER/DEVICE/....
This commit is contained in:
Mathieu Maret 2016-06-01 00:49:42 +02:00
parent 85f0bf5c78
commit 4d38c2c7a5

View File

@ -10,19 +10,20 @@ Adafruit_MQTT_Publish *mqtt_dht_temp;
Adafruit_MQTT_Publish *mqtt_dht_humidity; 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 96
#define TEMPERATURE_FEED_FORMAT "/feeds/%s/temperature" //FEED have the following formats /feeds/USER/DEVICE_NAME/....
#define PRESSURE_FEED_FORMAT "/feeds/%s/pressure" #define TEMPERATURE_FEED_FORMAT "/feeds/%s/%s/temperature"
#define TEMPERATURE_DHT_FEED_FORMAT "/feeds/%s/dht/temperature" #define PRESSURE_FEED_FORMAT "/feeds/%s/%s/pressure"
#define HUMIDITY_DHT_FEED_FORMAT "/feeds/%s/dht/humidity" #define TEMPERATURE_DHT_FEED_FORMAT "/feeds/%s/%s/dht/temperature"
#define HUMIDITY_DHT_FEED_FORMAT "/feeds/%s/%s/dht/humidity"
// 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
const int gpioWatched[] = CONFIG_MQTT_CONTROLLED_GPIO; const int gpioWatched[] = CONFIG_MQTT_CONTROLLED_GPIO;
#define GPIO_FEED_FORMAT "/feeds/%s/gpio/%d" #define GPIO_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
#define GPIO_SET_FEED_FORMAT "/feeds/%s/gpio/%d/set" #define GPIO_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
char *mqttId; char *mqttId;
char GPIO_FEED[MAXSUBSCRIPTIONS][FEED_MAX_SIZE] = {}; char GPIO_FEED[MAXSUBSCRIPTIONS][FEED_MAX_SIZE] = {};
@ -41,14 +42,14 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname)
return 0; return 0;
mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd); mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd);
mqtt_dht_temp = MqttCreatePublisher(TEMPERATURE_DHT_FEED_FORMAT, mqttId); mqtt_dht_temp = MqttCreatePublisher(TEMPERATURE_DHT_FEED_FORMAT, user, mqttId);
mqtt_dht_humidity = MqttCreatePublisher(HUMIDITY_DHT_FEED_FORMAT, mqttId); mqtt_dht_humidity = MqttCreatePublisher(HUMIDITY_DHT_FEED_FORMAT, user, mqttId);
mqtt_temp = MqttCreatePublisher(TEMPERATURE_FEED_FORMAT, mqttId); mqtt_temp = MqttCreatePublisher(TEMPERATURE_FEED_FORMAT, user, mqttId);
mqtt_pressure = MqttCreatePublisher(PRESSURE_FEED_FORMAT, mqttId); mqtt_pressure = MqttCreatePublisher(PRESSURE_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, mqttId, gpioWatched[i]); snprintf(GPIO_FEED[i], FEED_MAX_SIZE, GPIO_FEED_FORMAT, user, mqttId, gpioWatched[i]);
snprintf(GPIO_SET_FEED[i], FEED_MAX_SIZE, GPIO_SET_FEED_FORMAT, mqttId, gpioWatched[i]); snprintf(GPIO_SET_FEED[i], FEED_MAX_SIZE, GPIO_SET_FEED_FORMAT, user, mqttId, gpioWatched[i]);
Adafruit_MQTT_Subscribe *gpioSet = new Adafruit_MQTT_Subscribe(mqtt, GPIO_SET_FEED[i]); Adafruit_MQTT_Subscribe *gpioSet = new Adafruit_MQTT_Subscribe(mqtt, GPIO_SET_FEED[i]);
mqtt->subscribe(gpioSet); mqtt->subscribe(gpioSet);