PWM controlled by mqtt
This commit is contained in:
parent
34b624b9a0
commit
c911376cce
@ -10,6 +10,7 @@ Adafruit_MQTT_Publish *mqtt_dht_humidity;
|
||||
Adafruit_MQTT_Publish *mqtt_dry;
|
||||
Adafruit_MQTT_Publish *mqtt_ip;
|
||||
Adafruit_MQTT_Publish *mqttGpio[MAXSUBSCRIPTIONS] = {};
|
||||
Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {};
|
||||
|
||||
#define FEED_MAX_SIZE 96
|
||||
|
||||
@ -21,11 +22,14 @@ Adafruit_MQTT_Publish *mqttGpio[MAXSUBSCRIPTIONS] = {};
|
||||
#define DRY_FEED_FORMAT "/feeds/%s/%s/dry"
|
||||
#define GPIO_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
|
||||
#define GPIO_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
|
||||
#define PWM_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
|
||||
#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 gpioWatched[] = CONFIG_MQTT_CONTROLLED_GPIO;
|
||||
const int pwmWatched[] = CONFIG_CONTROLLED_PWM;
|
||||
|
||||
char *mqttId;
|
||||
|
||||
@ -53,10 +57,21 @@ 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(gpioWatched) + NB_ELEMENTS(pwmWatched) > MAXSUBSCRIPTIONS){
|
||||
SKETCH_DEBUG_PRINTF("Too much gpio/pwm to control\n Nb gpio %d Nb pwm %d Max is %d",
|
||||
NB_ELEMENTS(gpioWatched), NB_ELEMENTS(pwmWatched), MAXSUBSCRIPTIONS);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (uint i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) {
|
||||
mqtt->subscribe(MqttCreateSubscribe(GPIO_SET_FEED_FORMAT, user, mqttId, gpioWatched[i]));
|
||||
mqttGpio[i] = MqttCreatePublisher(GPIO_FEED_FORMAT, user, mqttId, gpioWatched[i]);
|
||||
}
|
||||
|
||||
for (uint i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) {
|
||||
mqtt->subscribe(MqttCreateSubscribe(PWM_SET_FEED_FORMAT, user, mqttId, pwmWatched[i]));
|
||||
mqttPwm[i] = MqttCreatePublisher(PWM_FEED_FORMAT, user, mqttId, pwmWatched[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -143,11 +158,11 @@ int MqttDhtPublish(float temp, float humidity) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription) {
|
||||
char *temp = strstr(subscription->topic, "/gpio/");
|
||||
int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription, const char *pattern) {
|
||||
char *temp = strstr(subscription->topic, pattern);
|
||||
if (!temp)
|
||||
return -1;
|
||||
String gpioStr(temp + strlen("/gpio/"));
|
||||
String gpioStr(temp + strlen(pattern));
|
||||
int idx = gpioStr.indexOf("/");
|
||||
int gpio = gpioStr.substring(0, idx).toInt();
|
||||
|
||||
@ -157,18 +172,18 @@ int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int getGpioWatchedIndex(int gpio) {
|
||||
for ( uint i = 0; i < NB_ELEMENTS(gpioWatched); i++) {
|
||||
if (gpio == gpioWatched[i])
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void MqttChangeGpioValue(int gpio, int value) {
|
||||
pinMode(gpio, OUTPUT);
|
||||
digitalWrite(gpio, value);
|
||||
int watchIdx = getGpioWatchedIndex(gpio);
|
||||
int watchIdx = findIndex(gpio, gpioWatched);
|
||||
if (watchIdx >= 0 ) {
|
||||
mqttGpio[watchIdx]->publish(value);
|
||||
}
|
||||
}
|
||||
|
||||
void MqttChangePWMValue(int gpio, int value) {
|
||||
analogWrite(gpio, value);
|
||||
int watchIdx = findIndex(gpio, gpioWatched);
|
||||
if (watchIdx >= 0 ) {
|
||||
mqttGpio[watchIdx]->publish(value);
|
||||
}
|
||||
@ -178,13 +193,21 @@ void MqttCheckSubscription() {
|
||||
if (MqttConnect() == 0) {
|
||||
Adafruit_MQTT_Subscribe *subscription;
|
||||
while ((subscription = mqtt->readSubscription(0))) {
|
||||
int gpio = getGpioFromSubscription(subscription);
|
||||
SKETCH_DEBUG_PRINTF("Got Subscription for gpio %d\n", gpio);
|
||||
if (gpio > 0 && getGpioWatchedIndex(gpio) >= 0) {
|
||||
int gpio = getGpioFromSubscription(subscription, "/gpio/");
|
||||
if (gpio > 0 && findIndex(gpio, gpioWatched) >= 0) {
|
||||
SKETCH_DEBUG_PRINTF("Got Subscription for GPIO %d\n", gpio);
|
||||
char *value = (char *) subscription->lastread;
|
||||
SKETCH_DEBUG_PRINTF("Receive data: %s\n", value);
|
||||
MqttChangeGpioValue(gpio, atoi(value));
|
||||
}
|
||||
|
||||
gpio = getGpioFromSubscription(subscription, "/pwm/");
|
||||
if (gpio > 0 && findIndex(gpio, pwmWatched) >= 0) {
|
||||
SKETCH_DEBUG_PRINTF("Got Subscription for PWM %d\n", gpio);
|
||||
char *value = (char *) subscription->lastread;
|
||||
SKETCH_DEBUG_PRINTF("Receive data: %s\n", value);
|
||||
MqttChangePWMValue(gpio, atoi(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,6 +39,10 @@
|
||||
#define CONFIG_SSID_NAME "ESPConfigurator"
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_CONTROLLED_PWM
|
||||
#define CONFIG_CONTROLLED_PWM {}
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_WEB_CONTROLLED_GPIO
|
||||
#define CONFIG_WEB_CONTROLLED_GPIO {}
|
||||
#endif
|
||||
|
@ -28,6 +28,9 @@
|
||||
// Web controlled GPIO
|
||||
#define CONFIG_WEB_CONTROLLED_GPIO {2}
|
||||
|
||||
// GPIO used in PWM
|
||||
#define CONFIG_CONTROLLED_PWM {}
|
||||
|
||||
|
||||
/* DEFAULT VALUE ALSO DEFINED IN CONFIG.H */
|
||||
//If this GPIO is LOW at boot, device will enter setup mode
|
||||
|
@ -4,3 +4,11 @@
|
||||
|
||||
#define STRINGIFY(x) #x
|
||||
#define TOSTRING(x) STRINGIFY(x)
|
||||
|
||||
int findIndex(int el, const int array[]){
|
||||
for ( uint i = 0; i < NB_ELEMENTS(array); i++) {
|
||||
if (el == array[i])
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user