Refactor GPIO listened/controlled configuration
And improve mqtt connection checking
This commit is contained in:
parent
d20d756677
commit
80252f9307
@ -3,6 +3,7 @@
|
||||
#include "utils.h"
|
||||
#include "MQTT.h"
|
||||
|
||||
#define MAX_GPIO_OBSERVED (MAXSUBSCRIPTIONS*2)
|
||||
Adafruit_MQTT_Client *mqtt;
|
||||
Adafruit_MQTT_Publish *mqtt_temp;
|
||||
Adafruit_MQTT_Publish *mqtt_pressure;
|
||||
@ -12,6 +13,7 @@ Adafruit_MQTT_Publish *mqtt_dry;
|
||||
Adafruit_MQTT_Publish *mqtt_ip;
|
||||
Adafruit_MQTT_Publish *mqttGpio[MAXSUBSCRIPTIONS] = {};
|
||||
Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {};
|
||||
Adafruit_MQTT_Publish *mqttGpioObserved[MAX_GPIO_OBSERVED] = {};
|
||||
|
||||
#define FEED_MAX_SIZE 96
|
||||
|
||||
@ -27,11 +29,6 @@ Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {};
|
||||
#define PWM_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
|
||||
#define IP_FEED_FORMAT "/feeds/%s/%s/configuration/ip/addr"
|
||||
|
||||
// Should have less that MAXSUBSCRIPTIONS elements
|
||||
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
|
||||
const int gpioControlled[] = CONFIG_CONTROLLED_GPIO;
|
||||
const int pwmWatched[] = CONFIG_CONTROLLED_PWM;
|
||||
|
||||
char *mqttId;
|
||||
|
||||
bool isMqttConfigured = false;
|
||||
@ -60,21 +57,31 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char *hostname)
|
||||
mqtt_dry = MqttCreatePublisher(DRY_FEED_FORMAT, user, mqttId);
|
||||
mqtt_ip = MqttCreatePublisher(IP_FEED_FORMAT, user, mqttId);
|
||||
|
||||
if( NB_ELEMENTS(gpioControlled) + NB_ELEMENTS(pwmWatched) > MAXSUBSCRIPTIONS){
|
||||
if (NB_ELEMENTS(gpioControlled) + NB_ELEMENTS(pwmControlled) > MAXSUBSCRIPTIONS){
|
||||
SKETCH_DEBUG_PRINTF("Too much gpio/pwm to control\n Nb gpio %d Nb pwm %d Max is %d",
|
||||
NB_ELEMENTS(gpioControlled), NB_ELEMENTS(pwmWatched), MAXSUBSCRIPTIONS);
|
||||
NB_ELEMENTS(gpioControlled), NB_ELEMENTS(pwmControlled), MAXSUBSCRIPTIONS);
|
||||
return -1;
|
||||
}
|
||||
if (NB_ELEMENTS(gpioObserved) > MAX_GPIO_OBSERVED){
|
||||
SKETCH_DEBUG_PRINTF("Too much gpio observed\n Nb gpio %d Nb is %d",
|
||||
NB_ELEMENTS(gpioObserved), MAX_GPIO_OBSERVED);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled) && i < MAXSUBSCRIPTIONS; i++) {
|
||||
for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled); i++) {
|
||||
mqtt->subscribe(MqttCreateSubscribe(GPIO_SET_FEED_FORMAT, user, mqttId, gpioControlled[i]));
|
||||
mqttGpio[i] = MqttCreatePublisher(GPIO_FEED_FORMAT, user, mqttId, gpioControlled[i]);
|
||||
}
|
||||
|
||||
for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled) && i < MAXSUBSCRIPTIONS; i++) {
|
||||
mqtt->subscribe(MqttCreateSubscribe(PWM_SET_FEED_FORMAT, user, mqttId, pwmWatched[i]));
|
||||
mqttPwm[i] = MqttCreatePublisher(PWM_FEED_FORMAT, user, mqttId, pwmWatched[i]);
|
||||
for (uint i = 0 ; i < NB_ELEMENTS(gpioObserved) && i < MAX_GPIO_OBSERVED ; i++) {
|
||||
mqttGpioObserved[i] = MqttCreatePublisher(GPIO_FEED_FORMAT, user, mqttId, gpioObserved[i]);
|
||||
}
|
||||
|
||||
for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled); i++) {
|
||||
mqtt->subscribe(MqttCreateSubscribe(PWM_SET_FEED_FORMAT, user, mqttId, pwmControlled[i]));
|
||||
mqttPwm[i] = MqttCreatePublisher(PWM_FEED_FORMAT, user, mqttId, pwmControlled[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -97,7 +104,7 @@ Adafruit_MQTT_Subscribe *MqttCreateSubscribe(const char *fmt, ...){
|
||||
}
|
||||
|
||||
int MqttIsConnected() {
|
||||
return (isMqttConfigured) ? mqtt->connected() : 0;
|
||||
return (isMqttConfigured && (mode == BOOTMODE_NORMAL)) ? mqtt->connected() : 0;
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
@ -105,7 +112,7 @@ int MqttIsConnected() {
|
||||
int MqttConnect() {
|
||||
int8_t ret;
|
||||
|
||||
if(!isMqttConfigured)
|
||||
if (!isMqttConfigured || mode != BOOTMODE_NORMAL)
|
||||
return -1;
|
||||
|
||||
// Stop if already connected.
|
||||
@ -166,9 +173,15 @@ int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription, const char *p
|
||||
}
|
||||
|
||||
void MqttNofity(int gpio, int value){
|
||||
int watchIdx = findIndex(gpio, gpioControlled);
|
||||
if (watchIdx >= 0 && isMqttConfigured) {
|
||||
mqttGpio[watchIdx]->publish(value);
|
||||
if (MqttIsConnected()) {
|
||||
int watchIdx = findIndex(gpio, gpioControlled);
|
||||
if (watchIdx >= 0 ) {
|
||||
mqttGpio[watchIdx]->publish(value);
|
||||
} else {
|
||||
watchIdx = findIndex(gpio, gpioObserved);
|
||||
if (watchIdx >= 0 )
|
||||
mqttGpioObserved[watchIdx]->publish(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -196,7 +209,7 @@ void MqttCheckSubscription() {
|
||||
}
|
||||
|
||||
gpio = getGpioFromSubscription(subscription, "/pwm/");
|
||||
if (gpio > 0 && findIndex(gpio, pwmWatched) >= 0) {
|
||||
if (gpio > 0 && findIndex(gpio, pwmControlled) >= 0) {
|
||||
SKETCH_DEBUG_PRINTF("Got Subscription for PWM %d\n", gpio);
|
||||
char *value = (char *) subscription->lastread;
|
||||
SKETCH_DEBUG_PRINTF("Receive data: %s\n", value);
|
||||
|
@ -1,6 +1,4 @@
|
||||
#ifndef CONFIG_DISABLE_WEB
|
||||
const int gpioWebConf[] = CONFIG_CONTROLLED_GPIO;
|
||||
const int pwmWebConf[] = CONFIG_CONTROLLED_PWM;
|
||||
|
||||
String gpioControlHTML = "";
|
||||
String pwmControlHTML = "";
|
||||
@ -234,26 +232,26 @@ void WebHandleWifiStatus() {
|
||||
}
|
||||
|
||||
void WebBuildGpioControl(){
|
||||
if (NB_ELEMENTS(gpioWebConf) > 0){
|
||||
if (NB_ELEMENTS(gpioControlled) > 0){
|
||||
gpioControlHTML += "<fieldset>"
|
||||
"<legend>Relay</legend>";
|
||||
for (uint i = 0 ; i < NB_ELEMENTS(gpioWebConf) ; i++) {
|
||||
gpioControlHTML += "Relay " + String(gpioWebConf[i]) + " " + "<a href=\"/gpio?gpio=" + String(gpioWebConf[i]) + "&value=1\">ON</a>/";
|
||||
gpioControlHTML += "<a href=\"/gpio?gpio=" + String(gpioWebConf[i]) + "&value=0\">OFF</a><br/>";
|
||||
for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled) ; i++) {
|
||||
gpioControlHTML += "Relay " + String(gpioControlled[i]) + " " + "<a href=\"/gpio?gpio=" + String(gpioControlled[i]) + "&value=1\">ON</a>/";
|
||||
gpioControlHTML += "<a href=\"/gpio?gpio=" + String(gpioControlled[i]) + "&value=0\">OFF</a><br/>";
|
||||
}
|
||||
gpioControlHTML += "</fieldset>";
|
||||
}
|
||||
}
|
||||
|
||||
void WebBuildPwmControl(){
|
||||
if (NB_ELEMENTS(pwmWebConf) > 0){
|
||||
if (NB_ELEMENTS(pwmControlled) > 0){
|
||||
pwmControlHTML += "<fieldset>"
|
||||
"<legend>PWM</legend>";
|
||||
for (uint i = 0 ; i < NB_ELEMENTS(pwmWebConf) ; i++) {
|
||||
pwmControlHTML += "PWM " + String(pwmWebConf[i]) + "<br/>";
|
||||
for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled) ; i++) {
|
||||
pwmControlHTML += "PWM " + String(pwmControlled[i]) + "<br/>";
|
||||
pwmControlHTML += "<input type=\"range\" min=\"0\" max=\"1023\""
|
||||
"style=\"background:#eee\""
|
||||
"onChange=\"setPWM(this.value," + String(pwmWebConf[i]) + ")\" />";
|
||||
"onChange=\"setPWM(this.value," + String(pwmControlled[i]) + ")\" />";
|
||||
}
|
||||
pwmControlHTML += "<script type=\"text/javascript\">"
|
||||
"function setPWM(newValue, gpio){"
|
||||
|
@ -64,6 +64,11 @@ float dhtTemp, dhtHumidity;
|
||||
int dryness;
|
||||
uint8_t mode;
|
||||
productConfig conf = {BOOTMODE_SETUP, "", "", "", "", "", "", 0, 0, 0, 0, 0, 0, 0};
|
||||
// Should have less that MAXSUBSCRIPTIONS elements
|
||||
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
|
||||
const int gpioControlled[] = CONFIG_CONTROLLED_GPIO;
|
||||
const int gpioObserved[] = CONFIG_OBSERVED_GPIO;
|
||||
const int pwmControlled[] = CONFIG_CONTROLLED_PWM;
|
||||
|
||||
/* Set these to your desired credentials. */
|
||||
const char *ssid = CONFIG_SSID_NAME;
|
||||
|
@ -43,8 +43,8 @@
|
||||
#define CONFIG_CONTROLLED_PWM {}
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_WEB_CONTROLLED_GPIO
|
||||
#define CONFIG_WEB_CONTROLLED_GPIO {}
|
||||
#ifndef CONFIG_OBSERVED_GPIO
|
||||
#define CONFIG_OBSERVED_GPIO {}
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_CONTROLLED_GPIO
|
||||
|
@ -52,6 +52,9 @@
|
||||
// Should have less value than MAXSUBSCRIPTIONS
|
||||
#define CONFIG_CONTROLLED_GPIO {2,13}
|
||||
|
||||
// GPIO that can be get by mqtt and http
|
||||
//#define CONFIG_OBSERVED_GPIO {}
|
||||
|
||||
// GPIO used in PWM
|
||||
//#define CONFIG_CONTROLLED_PWM {}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user