General way to deal with GPIO by mqtt

This commit is contained in:
Mathieu Maret 2016-03-23 00:34:50 +01:00
parent a391cd0369
commit d729750ed5
3 changed files with 34 additions and 41 deletions

View File

@ -11,32 +11,26 @@ const char PRESSURE_FEED[] = "/feeds/pressure";
// Should have less that MAXSUBSCRIPTIONS elements
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
const int gpioWatched[] = {2};
const int gpioWatched[] = {12, 13};
#define GPIO_BASE "/feeds/gpio/"
const char *GPIO_FEED[] = { GPIO_BASE"2"};
const char *GPIO_SET_FEED[] = { GPIO_BASE"2/set"};
const char *GPIO_FEED[] = { GPIO_BASE"12", GPIO_BASE"13"};
const char *GPIO_SET_FEED[] = { GPIO_BASE"12/set", GPIO_BASE"13/set"};
Adafruit_MQTT_Publish * mqttGpio[MAXSUBSCRIPTIONS] = {};
const char PUMP_CMD[] = "/feeds/pump/set";
boolean mqttIsConfigured = false;
int setupMQTT(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.
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);
for (int i = 0 ; i < NB_ELEMENTS(gpioWatched); i++) {
for (int i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) {
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;
}
return 0;
}
@ -78,20 +72,33 @@ int publishMQTT(double temp, double pressure) {
}
int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription) {
if(!strstr(subscription->topic, GPIO_BASE))
if (!strstr(subscription->topic, GPIO_BASE))
return -1;
String gpioStr(subscription->topic+strlen(GPIO_BASE));
String gpioStr(subscription->topic + strlen(GPIO_BASE));
int idx = gpioStr.indexOf("/");
int gpio = gpioStr.substring(idx).toInt();
if(gpio >= 0 && gpio < 32 )
int gpio = gpioStr.substring(0, idx).toInt();
if (gpio >= 0 && gpio < 32 )
return gpio;
else
return -1;
}
bool isGpioWatched(int gpio) {
return false;
int getGpioWatchedIndex(int gpio) {
for ( int i = 0; i < NB_ELEMENTS(gpioWatched); i++) {
if (gpio == gpioWatched[i])
return i;
}
return -1;
}
void changeGpioValue(int gpio, int value) {
pinMode(gpio, OUTPUT);
digitalWrite(gpio, value);
int watchIdx = getGpioWatchedIndex(gpio);
if (watchIdx >= 0 ) {
mqttGpio[watchIdx]->publish(value);
}
}
void checkMqttSubscription() {
@ -99,24 +106,14 @@ void checkMqttSubscription() {
Adafruit_MQTT_Subscribe *subscription;
while (subscription = mqtt->readSubscription(0)) {
int gpio = getGpioFromSubscription(subscription);
if (gpio > 0 && isGpioWatched(gpio)) {
Serial.print("Got Subscription for gpio ");
Serial.println(gpio);
if (gpio > 0 && getGpioWatchedIndex(gpio) >= 0) {
char *value = (char *) subscription->lastread;
Serial.print("Receive data: ");
Serial.println(value);
changeGpioValue(gpio, atoi(value));
}
}
}
}
//char *mqttGetSubData() {
// return (char *)pumpButton->lastread;
//}
//
//Adafruit_MQTT_Subscribe *subscription;
//bool mqttSubAvailable() {
// if (MQTT_connect() == 0) {
// subscription = mqtt->readSubscription(0);
// if (subscription == pumpButton) {
// return true;
// }
// }
// return false;
//}

View File

@ -51,8 +51,7 @@ void handleGpio() {
return;
}
pinMode(server.arg("gpio").toInt(), OUTPUT);
digitalWrite(server.arg("gpio").toInt(), server.arg("value").toInt());
changeGpioValue(server.arg("gpio").toInt(), server.arg("value").toInt());
server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
}

View File

@ -76,8 +76,8 @@ int MQTT_connect();
int MQTT_isConnected();
int setupMQTT(char *server, char *user, char *passwd, int port);
int publishMQTT(double temp, double pressure);
bool mqttSubAvailable();
char *mqttGetSubData();
void checkMqttSubscription();
void changeGpioValue(int gpio, int value);
void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
IPAddress myIP;
@ -211,10 +211,7 @@ void loop() {
ArduinoOTA.handle();
} else {
server.handleClient();
// if(mqttSubAvailable()){
// Serial.println("Mqtt sub available: ");
// Serial.println(mqttGetSubData());
// }
checkMqttSubscription();
delay(WEB_DELAY_MS);
nbCycle++;