Browse Source

Moved MQTT callback in Domoticz module

fastled
Xose Pérez 7 years ago
parent
commit
a780f5812f
5 changed files with 28 additions and 94 deletions
  1. +0
    -2
      code/build-fs
  2. +0
    -0
      code/build.sh
  3. +0
    -0
      code/debug.sh
  4. +0
    -64
      code/deploy
  5. +28
    -28
      code/espurna/domoticz.ino

+ 0
- 2
code/build-fs View File

@ -1,2 +0,0 @@
#!/bin/bash
node node_modules/gulp/bin/gulp.js

code/build-all → code/build.sh View File


code/debug → code/debug.sh View File


+ 0
- 64
code/deploy View File

@ -1,64 +0,0 @@
#!/bin/bash
#
# ESPurna Deploy Script
#
# Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
MQTT_HOST=192.168.1.10
function help() {
echo "Syntax: $0 <device> <target>"
devices
}
function devices() {
echo "Defined devices:"
cat platformio.ini | grep 'device]' | sed 's/\[env:/ - /g' | sed 's/\-device]//g'
}
function valid_ip() {
local stat=0
rx='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
if [[ $ip =~ ^$rx\.$rx\.$rx\.$rx$ ]]; then
stat=1
fi
return $stat
}
# Check arguments
if [ "$#" -ne 2 ]; then
help
exit 1
fi
device=$1-device
target=$2
# Get IP
topic=`cat platformio.ini | grep $device -A 2 | grep "topic" | cut -d' ' -f3`
if [ "$topic" == "" ]; then
echo "Unknown device $device or topic not defined"
devices
exit 2
fi
ip=`mosquitto_sub -t $topic -h $MQTT_HOST -N -C 1`
if valid_ip $ip; then
echo "Could not get a valid IP from MQTT broker"
exit 3
fi
platformio run -vv -e $device --target $target --upload-port $ip

+ 28
- 28
code/espurna/domoticz.ino View File

@ -44,44 +44,44 @@ int relayToIdx(unsigned int relayID) {
return getSetting(buffer).toInt();
}
void domoticzSetup() {
mqttRegister([](unsigned int type, const char * topic, const char * payload) {
String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
void _domoticzMqtt(unsigned int type, const char * topic, const char * payload) {
if (type == MQTT_CONNECT_EVENT) {
mqttSubscribeRaw(dczTopicOut.c_str());
}
String dczTopicOut = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC);
if (type == MQTT_MESSAGE_EVENT) {
if (type == MQTT_CONNECT_EVENT) {
mqttSubscribeRaw(dczTopicOut.c_str());
}
// Check topic
if (dczTopicOut.equals(topic)) {
if (type == MQTT_MESSAGE_EVENT) {
// Parse response
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject((char *) payload);
if (!root.success()) {
DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
return;
}
// Check topic
if (dczTopicOut.equals(topic)) {
// IDX
unsigned long idx = root["idx"];
int relayID = relayFromIdx(idx);
if (relayID >= 0) {
unsigned long value = root["nvalue"];
DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %d for IDX %d\n"), value, idx);
relayStatus(relayID, value == 1);
}
// Parse response
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject((char *) payload);
if (!root.success()) {
DEBUG_MSG_P(PSTR("[DOMOTICZ] Error parsing data\n"));
return;
}
// IDX
unsigned long idx = root["idx"];
int relayID = relayFromIdx(idx);
if (relayID >= 0) {
unsigned long value = root["nvalue"];
DEBUG_MSG_P(PSTR("[DOMOTICZ] Received value %d for IDX %d\n"), value, idx);
relayStatus(relayID, value == 1);
}
}
});
}
}
};
void domoticzSetup() {
mqttRegister(_domoticzMqtt);
}
#endif

Loading…
Cancel
Save