Domotique/WifiControlSensor/MQTT.ino

120 lines
3.4 KiB
Arduino
Raw Normal View History

2016-03-14 01:47:43 +01:00
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
Adafruit_MQTT_Client *mqtt;
Adafruit_MQTT_Publish *mqtt_temp;
2016-03-16 00:54:13 +01:00
Adafruit_MQTT_Publish *mqtt_pressure;
2016-03-14 01:47:43 +01:00
2016-03-22 20:01:05 +01:00
#define NB_ELEMENTS(x) (sizeof(x)/ sizeof(x[0]))
2016-03-21 00:56:27 +01:00
2016-03-14 01:47:43 +01:00
const char TEMPERATURE_FEED[] = "/feeds/temperature";
2016-03-16 00:54:13 +01:00
const char PRESSURE_FEED[] = "/feeds/pressure";
2016-03-14 01:47:43 +01:00
2016-03-22 20:01:05 +01:00
// Should have less that MAXSUBSCRIPTIONS elements
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
2016-03-23 00:34:50 +01:00
const int gpioWatched[] = {12, 13};
2016-03-21 00:56:27 +01:00
2016-03-22 20:01:05 +01:00
#define GPIO_BASE "/feeds/gpio/"
2016-03-23 00:34:50 +01:00
const char *GPIO_FEED[] = { GPIO_BASE"12", GPIO_BASE"13"};
const char *GPIO_SET_FEED[] = { GPIO_BASE"12/set", GPIO_BASE"13/set"};
2016-03-14 01:47:43 +01:00
2016-03-22 20:01:05 +01:00
Adafruit_MQTT_Publish * mqttGpio[MAXSUBSCRIPTIONS] = {};
2016-03-21 00:56:27 +01:00
int MqttSetup(char *server, char *user, char *passwd, int port) {
2016-03-14 01:47:43 +01:00
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
2016-03-14 17:18:36 +01:00
mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd);
2016-03-14 01:47:43 +01:00
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED);
2016-03-16 00:54:13 +01:00
mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, PRESSURE_FEED);
2016-03-23 00:34:50 +01:00
for (int i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) {
2016-03-22 20:01:05 +01:00
Adafruit_MQTT_Subscribe *gpioSet = new Adafruit_MQTT_Subscribe(mqtt, GPIO_SET_FEED[i]);
mqtt->subscribe(gpioSet);
Adafruit_MQTT_Publish *gpio = new Adafruit_MQTT_Publish(mqtt, GPIO_FEED[i]);
mqttGpio[i] = gpio;
}
2016-03-14 01:47:43 +01:00
return 0;
}
int MqttIsConnected() {
2016-03-14 18:05:14 +01:00
return mqtt->connected();
}
2016-03-11 01:31:03 +01:00
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
int MqttConnect() {
2016-03-11 01:31:03 +01:00
int8_t ret;
// Stop if already connected.
2016-03-14 01:47:43 +01:00
if (mqtt->connected()) {
2016-03-11 01:31:03 +01:00
return 0;
}
uint8_t retries = 3;
2016-03-14 01:47:43 +01:00
while ((ret = mqtt->connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt->connectErrorString(ret));
2016-03-11 01:31:03 +01:00
Serial.println("Retrying MQTT connection ...");
2016-03-14 01:47:43 +01:00
mqtt->disconnect();
2016-03-11 01:31:03 +01:00
delay(100); // wait
retries--;
if (retries == 0) {
return -1;
}
}
return 0;
}
2016-03-22 20:01:05 +01:00
int MqttPublish(double temp, double pressure) {
if (MqttConnect() == 0) {
2016-03-22 20:01:05 +01:00
Serial.println("publishing !");
mqtt_temp->publish(temp);
mqtt_pressure->publish(pressure);
}
return 0;
}
int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription) {
2016-03-23 00:34:50 +01:00
if (!strstr(subscription->topic, GPIO_BASE))
2016-03-22 20:01:05 +01:00
return -1;
2016-03-23 00:34:50 +01:00
String gpioStr(subscription->topic + strlen(GPIO_BASE));
2016-03-22 20:01:05 +01:00
int idx = gpioStr.indexOf("/");
2016-03-23 00:34:50 +01:00
int gpio = gpioStr.substring(0, idx).toInt();
if (gpio >= 0 && gpio < 32 )
2016-03-22 20:01:05 +01:00
return gpio;
else
return -1;
}
2016-03-23 00:34:50 +01:00
int getGpioWatchedIndex(int gpio) {
for ( int i = 0; i < NB_ELEMENTS(gpioWatched); i++) {
if (gpio == gpioWatched[i])
return i;
}
return -1;
}
void MqttChangeGpioValue(int gpio, int value) {
2016-03-23 00:34:50 +01:00
pinMode(gpio, OUTPUT);
digitalWrite(gpio, value);
int watchIdx = getGpioWatchedIndex(gpio);
if (watchIdx >= 0 ) {
mqttGpio[watchIdx]->publish(value);
}
2016-03-22 20:01:05 +01:00
}
void MqttCheckSubscription() {
if (MqttConnect() == 0) {
2016-03-22 20:01:05 +01:00
Adafruit_MQTT_Subscribe *subscription;
while (subscription = mqtt->readSubscription(0)) {
int gpio = getGpioFromSubscription(subscription);
2016-03-23 00:34:50 +01:00
Serial.print("Got Subscription for gpio ");
Serial.println(gpio);
if (gpio > 0 && getGpioWatchedIndex(gpio) >= 0) {
2016-03-22 20:01:05 +01:00
char *value = (char *) subscription->lastread;
2016-03-23 00:34:50 +01:00
Serial.print("Receive data: ");
Serial.println(value);
MqttChangeGpioValue(gpio, atoi(value));
2016-03-22 20:01:05 +01:00
}
}
}
}