Try mqtt subscription

This commit is contained in:
Mathieu Maret 2016-03-21 00:56:27 +01:00
parent e99f63ef41
commit 7ee04eecc4
2 changed files with 30 additions and 1 deletions

View File

@ -4,10 +4,14 @@ Adafruit_MQTT_Client *mqtt;
Adafruit_MQTT_Publish *mqtt_temp; Adafruit_MQTT_Publish *mqtt_temp;
Adafruit_MQTT_Publish *mqtt_pressure; Adafruit_MQTT_Publish *mqtt_pressure;
Adafruit_MQTT_Subscribe *pumpButton;
const char TEMPERATURE_FEED[] = "/feeds/temperature"; const char TEMPERATURE_FEED[] = "/feeds/temperature";
const char PRESSURE_FEED[] = "/feeds/pressure"; const char PRESSURE_FEED[] = "/feeds/pressure";
boolean mqttIsConfigured; const char PUMP_CMD[] = "/feeds/pump/set";
boolean mqttIsConfigured = false;
int publishMQTT(double temp, double pressure) { int publishMQTT(double temp, double pressure) {
if (MQTT_connect() == 0) { if (MQTT_connect() == 0) {
@ -15,6 +19,22 @@ int publishMQTT(double temp, double pressure) {
mqtt_temp->publish(temp); mqtt_temp->publish(temp);
mqtt_pressure->publish(pressure); mqtt_pressure->publish(pressure);
} }
return 0;
}
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;
} }
int setupMQTT(char *server, char *user, char *passwd, int port) { int setupMQTT(char *server, char *user, char *passwd, int port) {
@ -22,7 +42,9 @@ int setupMQTT(char *server, char *user, char *passwd, int port) {
mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd); mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd);
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED); mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED);
mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, PRESSURE_FEED); mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, PRESSURE_FEED);
pumpButton = new Adafruit_MQTT_Subscribe(mqtt, PUMP_CMD);
mqtt->subscribe(pumpButton);
return 0; return 0;
} }

View File

@ -76,6 +76,8 @@ int MQTT_connect();
int MQTT_isConnected(); int MQTT_isConnected();
int setupMQTT(char *server, char *user, char *passwd, int port); int setupMQTT(char *server, char *user, char *passwd, int port);
int publishMQTT(double temp, double pressure); int publishMQTT(double temp, double pressure);
bool mqttSubAvailable();
char *mqttGetSubData();
void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) { void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
IPAddress myIP; IPAddress myIP;
@ -209,7 +211,12 @@ void loop() {
ArduinoOTA.handle(); ArduinoOTA.handle();
} else { } else {
server.handleClient(); server.handleClient();
if(mqttSubAvailable()){
Serial.println("Mqtt sub available: ");
Serial.println(mqttGetSubData());
}
delay(WEB_DELAY_MS); delay(WEB_DELAY_MS);
nbCycle++; nbCycle++;
if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) { if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) {
if (isBMPConnected() && getTempAndPressure(temp, pressure) == 0) { if (isBMPConnected() && getTempAndPressure(temp, pressure) == 0) {