mqtt deal with IRQ for GPIO

This commit is contained in:
Mathieu Maret 2016-12-14 00:08:47 +01:00
parent 80252f9307
commit 3957d9645f
4 changed files with 30 additions and 3 deletions

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#ifndef CONFIG_DISABLE_MQTT #ifndef CONFIG_DISABLE_MQTT
#include "Adafruit_MQTT.h" #include "Adafruit_MQTT.h"
typedef struct {uint8_t updated; int value;} gpioInfo;
Adafruit_MQTT_Publish *MqttCreatePublisher(const char *fmt, ...); Adafruit_MQTT_Publish *MqttCreatePublisher(const char *fmt, ...);
int MqttConnect(); int MqttConnect();
int MqttIsConnected(); int MqttIsConnected();
@ -12,9 +12,12 @@ int MqttPublishDHT(float temp, float humidity);
int MqttPublishDry(int dry); int MqttPublishDry(int dry);
int MqttPublishIp(const String &ip); int MqttPublishIp(const String &ip);
void MqttCheckSubscription(); void MqttCheckSubscription();
void MqttCheckIRQ();
void MqttChangeGpioValue(int gpio, int value); void MqttChangeGpioValue(int gpio, int value);
void MqttChangePWMValue(int gpio, int value); void MqttChangePWMValue(int gpio, int value);
bool MqttIsConfigured(); bool MqttIsConfigured();
void MqttNofityIRQ(uint8_t gpio, int value);
void MqttNofity(int gpio, int value);
#else #else
int MqttConnect(){return 0;} int MqttConnect(){return 0;}
int MqttIsConnected(){return 0;} int MqttIsConnected(){return 0;}
@ -25,7 +28,10 @@ int MqttPublishDHT(float temp, float humidity){return 0;}
int MqttPublishDry(int dry){return 0;} int MqttPublishDry(int dry){return 0;}
int MqttPublishIP(const String &ip){return 0;} int MqttPublishIP(const String &ip){return 0;}
void MqttCheckSubscription(){} void MqttCheckSubscription(){}
void MqttCheckIRQ(){}
void MqttChangeGpioValue(int gpio, int value){} void MqttChangeGpioValue(int gpio, int value){}
void MqttChangePWMValue(int gpio, int value){} void MqttChangePWMValue(int gpio, int value){}
bool MqttIsConfigured(){} bool MqttIsConfigured(){}
void MqttNofityIRQ(uint8_t gpio, int value){}
void MqttNofity(int gpio, int value){}
#endif #endif

View File

@ -3,6 +3,7 @@
#include "utils.h" #include "utils.h"
#include "MQTT.h" #include "MQTT.h"
#define MAX_PIN 15
#define MAX_GPIO_OBSERVED (MAXSUBSCRIPTIONS*2) #define MAX_GPIO_OBSERVED (MAXSUBSCRIPTIONS*2)
Adafruit_MQTT_Client *mqtt; Adafruit_MQTT_Client *mqtt;
Adafruit_MQTT_Publish *mqtt_temp; Adafruit_MQTT_Publish *mqtt_temp;
@ -14,6 +15,7 @@ Adafruit_MQTT_Publish *mqtt_ip;
Adafruit_MQTT_Publish *mqttGpio[MAXSUBSCRIPTIONS] = {}; Adafruit_MQTT_Publish *mqttGpio[MAXSUBSCRIPTIONS] = {};
Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {}; Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {};
Adafruit_MQTT_Publish *mqttGpioObserved[MAX_GPIO_OBSERVED] = {}; Adafruit_MQTT_Publish *mqttGpioObserved[MAX_GPIO_OBSERVED] = {};
gpioInfo mqttIRQ[MAX_PIN + 1] = {};
#define FEED_MAX_SIZE 96 #define FEED_MAX_SIZE 96
@ -75,6 +77,7 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char *hostname)
for (uint i = 0 ; i < NB_ELEMENTS(gpioObserved) && i < MAX_GPIO_OBSERVED ; i++) { for (uint i = 0 ; i < NB_ELEMENTS(gpioObserved) && i < MAX_GPIO_OBSERVED ; i++) {
mqttGpioObserved[i] = MqttCreatePublisher(GPIO_FEED_FORMAT, user, mqttId, gpioObserved[i]); mqttGpioObserved[i] = MqttCreatePublisher(GPIO_FEED_FORMAT, user, mqttId, gpioObserved[i]);
new HIB(gpioObserved[i], HIGH, MqttNofityIRQ , MqttNofityIRQ, NULL );
} }
for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled); i++) { for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled); i++) {
@ -172,6 +175,11 @@ int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription, const char *p
return -1; return -1;
} }
void MqttNofityIRQ(uint8_t gpio, int value){
mqttIRQ[gpio].updated = 1;
mqttIRQ[gpio].value = value;
}
void MqttNofity(int gpio, int value){ void MqttNofity(int gpio, int value){
if (MqttIsConnected()) { if (MqttIsConnected()) {
int watchIdx = findIndex(gpio, gpioControlled); int watchIdx = findIndex(gpio, gpioControlled);
@ -196,6 +204,16 @@ void MqttChangePWMValue(int gpio, int value) {
MqttNofity(gpio, value); MqttNofity(gpio, value);
} }
void MqttCheckIRQ() {
for (uint i = 0 ; i < NB_ELEMENTS(mqttIRQ); i++) {
if(mqttIRQ[i].updated == 1){
mqttIRQ[i].updated = 0;
MqttNofity(i, mqttIRQ[i].value);
}
}
}
void MqttCheckSubscription() { void MqttCheckSubscription() {
if (MqttConnect() == 0) { if (MqttConnect() == 0) {
Adafruit_MQTT_Subscribe *subscription; Adafruit_MQTT_Subscribe *subscription;

View File

@ -255,8 +255,10 @@ void loop() {
ArduinoOTA.handle(); ArduinoOTA.handle();
} else { } else {
server.handleClient(); server.handleClient();
if (mode == BOOTMODE_NORMAL) if (mode == BOOTMODE_NORMAL){
MqttCheckSubscription(); MqttCheckSubscription();
MqttCheckIRQ();
}
delay(CONFIG_WEB_DELAY_MS); delay(CONFIG_WEB_DELAY_MS);
nbCycle++; nbCycle++;

View File

@ -53,7 +53,8 @@
#define CONFIG_CONTROLLED_GPIO {2,13} #define CONFIG_CONTROLLED_GPIO {2,13}
// GPIO that can be get by mqtt and http // GPIO that can be get by mqtt and http
//#define CONFIG_OBSERVED_GPIO {} // Pin 6 to 11 and 16 can not be used for mqtt
#define CONFIG_OBSERVED_GPIO {}
// GPIO used in PWM // GPIO used in PWM
//#define CONFIG_CONTROLLED_PWM {} //#define CONFIG_CONTROLLED_PWM {}