Browse Source

Set inching mode through MQTT

fastled
Xose Pérez 8 years ago
parent
commit
4a58037638
3 changed files with 63 additions and 19 deletions
  1. +1
    -4
      code/src/button.ino
  2. +4
    -4
      code/src/config/hardware.h
  3. +58
    -11
      code/src/relay.ino

+ 1
- 4
code/src/button.ino View File

@ -99,10 +99,7 @@ void buttonLoop() {
} }
#ifdef ITEAD_1CH_INCHING #ifdef ITEAD_1CH_INCHING
if (i == 1) { if (i == 1) {
byte relayInch = getSetting("relayInch", String(RELAY_INCHING)).toInt();
relayInch = (relayInch == RELAY_INCHING_NONE) ? RELAY_INCHING_OFF : RELAY_INCHING_NONE;
setSetting("relayInch", String() + relayInch);
digitalWrite(LED_INCHING, relayInch != RELAY_INCHING_NONE);
relayInchingToggle();
continue; continue;
} }
#endif #endif


+ 4
- 4
code/src/config/hardware.h View File

@ -136,11 +136,11 @@
#define MANUFACTURER "ITEAD" #define MANUFACTURER "ITEAD"
#define DEVICE "1CH_INCHING" #define DEVICE "1CH_INCHING"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON2_PIN 9
#define BUTTON2_PIN 15
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define LED_PIN 13
#define LED_PIN_INVERSE 0
#define LED_INCHING 5
#define LED1_PIN 13
#define LED1_PIN_INVERSE 0
#define LED_INCHING 14
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Electrodragon boards // Electrodragon boards


+ 58
- 11
code/src/relay.ino View File

@ -69,9 +69,9 @@ void relayInchingBack(unsigned char id) {
inching.detach(); inching.detach();
} }
void relayInching(unsigned char id) {
void relayInchingStart(unsigned char id) {
byte relayInch = getSetting("relayInch", String(RELAY_INCHING)).toInt();
byte relayInch = getSetting("relayInch", RELAY_INCHING).toInt();
if (relayInch == RELAY_INCHING_NONE) return; if (relayInch == RELAY_INCHING_NONE) return;
bool status = relayStatus(id); bool status = relayStatus(id);
@ -82,13 +82,53 @@ void relayInching(unsigned char id) {
} }
inching.attach( inching.attach(
getSetting("relayInchTime", String(RELAY_INCHING_TIME)).toInt(),
getSetting("relayInchTime", RELAY_INCHING_TIME).toInt(),
relayInchingBack, relayInchingBack,
id id
); );
} }
unsigned int relayInching() {
unsigned int relayInch = getSetting("relayInch", RELAY_INCHING).toInt();
return relayInch;
}
void relayInching(unsigned int relayInch, bool report) {
setSetting("relayInch", relayInch);
/*
if (report) {
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
char topic[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 10];
sprintf(topic, "%s/inching%s", MQTT_RELAY_TOPIC, mqttGetter.c_str());
char value[2];
sprintf(value, "%d", relayInch);
mqttSend(topic, value);
}
*/
char message[20];
sprintf(message, "{\"relayInch\": %d}", relayInch);
wsSend(message);
#ifdef LED_INCHING
digitalWrite(LED_INCHING, relayInch != RELAY_INCHING_NONE);
#endif
}
void relayInching(unsigned int relayInch) {
relayInching(relayInch, true);
}
void relayInchingToggle() {
unsigned int relayInch = relayInching();
relayInch = (relayInch == RELAY_INCHING_NONE) ? RELAY_INCHING_OFF : RELAY_INCHING_NONE;
relayInching(relayInch);
}
bool relayStatus(unsigned char id, bool status, bool report) { bool relayStatus(unsigned char id, bool status, bool report) {
if (id >= _relays.size()) return false; if (id >= _relays.size()) return false;
@ -115,7 +155,7 @@ bool relayStatus(unsigned char id, bool status, bool report) {
#endif #endif
if (!recursive) { if (!recursive) {
relayInching(id);
relayInchingStart(id);
relaySync(id); relaySync(id);
relaySave(); relaySave();
} }
@ -209,10 +249,16 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0; bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0;
if (type == MQTT_CONNECT_EVENT) { if (type == MQTT_CONNECT_EVENT) {
relayMQTT(); relayMQTT();
char buffer[strlen(MQTT_RELAY_TOPIC) + mqttSetter.length() + 3];
char buffer[strlen(MQTT_RELAY_TOPIC) + mqttSetter.length() + 10];
sprintf(buffer, "%s/+%s", MQTT_RELAY_TOPIC, mqttSetter.c_str()); sprintf(buffer, "%s/+%s", MQTT_RELAY_TOPIC, mqttSetter.c_str());
mqttSubscribe(buffer); mqttSubscribe(buffer);
sprintf(buffer, "%s/inching%s", MQTT_RELAY_TOPIC, mqttSetter.c_str());
mqttSubscribe(buffer);
} }
if (type == MQTT_MESSAGE_EVENT) { if (type == MQTT_MESSAGE_EVENT) {
@ -222,11 +268,13 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
if (!t.startsWith(MQTT_RELAY_TOPIC)) return; if (!t.startsWith(MQTT_RELAY_TOPIC)) return;
if (!t.endsWith(mqttSetter)) return; if (!t.endsWith(mqttSetter)) return;
// If relayMode is not SAME avoid responding to a retained message
if (sameSetGet && isFirstMessage) {
isFirstMessage = false;
byte relayMode = getSetting("relayMode", RELAY_MODE).toInt();
if (relayMode != RELAY_MODE_SAME) return;
// Get value
unsigned int value = (char)payload[0] - '0';
// Inching topic
if (t.indexOf("inching") > 0) {
relayInching(value, !sameSetGet);
return;
} }
// Get relay ID // Get relay ID
@ -234,7 +282,6 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
if (relayID >= relayCount()) relayID = 0; if (relayID >= relayCount()) relayID = 0;
// Action to perform // Action to perform
unsigned int value = (char)payload[0] - '0';
if (value == 2) { if (value == 2) {
relayToggle(relayID); relayToggle(relayID);
} else { } else {


Loading…
Cancel
Save