Browse Source

Merge branch 'inching' into dev

Conflicts:
	code/espurna/data/index.html.gz
fastled
Xose Pérez 8 years ago
parent
commit
4890de1638
8 changed files with 169 additions and 6 deletions
  1. +18
    -0
      code/espurna/button.ino
  2. +10
    -0
      code/espurna/config/general.h
  3. +18
    -0
      code/espurna/config/hardware.h
  4. BIN
      code/espurna/data/index.html.gz
  5. +84
    -3
      code/espurna/relay.ino
  6. +7
    -0
      code/espurna/web.ino
  7. +23
    -3
      code/html/index.html
  8. +9
    -0
      code/platformio.ini

+ 18
- 0
code/espurna/button.ino View File

@ -106,6 +106,12 @@ void buttonSetup() {
_buttons.push_back({new DebounceEvent(BUTTON4_PIN), BUTTON4_RELAY});
#endif
#ifdef LED_PULSE
pinMode(LED_PULSE, OUTPUT);
byte relayPulseMode = getSetting("relayPulseMode", String(RELAY_PULSE_MODE)).toInt();
digitalWrite(LED_PULSE, relayPulseMode != RELAY_PULSE_NONE);
#endif
DEBUG_MSG("[BUTTON] Number of buttons: %d\n", _buttons.size());
}
@ -114,20 +120,32 @@ void buttonLoop() {
for (unsigned int i=0; i < _buttons.size(); i++) {
if (_buttons[i].button->loop()) {
uint8_t event = _buttons[i].button->getEvent();
DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event);
#ifdef MQTT_BUTTON_TOPIC
buttonMQTT(i);
#endif
if (i == 0) {
if (event == EVENT_DOUBLE_CLICK) createAP();
if (event == EVENT_LONG_CLICK) ESP.reset();
}
#ifdef ITEAD_1CH_INCHING
if (i == 1) {
relayPulseToggle();
continue;
}
#endif
if (event == EVENT_SINGLE_CLICK) {
if (_buttons[i].relayID > 0) {
relayToggle(_buttons[i].relayID - 1);
}
}
}
}


+ 10
- 0
code/espurna/config/general.h View File

@ -20,12 +20,22 @@
#define RELAY_SYNC_ONE 2
#define RELAY_SYNC_SAME 3
#define RELAY_PULSE_NONE 0
#define RELAY_PULSE_OFF 1
#define RELAY_PULSE_ON 2
// Pulse time in seconds
#define RELAY_PULSE_TIME 1
// 0 means OFF, 1 ON and 2 whatever was before
#define RELAY_MODE RELAY_MODE_OFF
// 0 means ANY, 1 zero or one and 2 one and only one
#define RELAY_SYNC RELAY_SYNC_ANY
// 0 means no pulses, 1 means normally off, 2 normally on
#define RELAY_PULSE_MODE RELAY_PULSE_NONE
//--------------------------------------------------------------------------------
// LED
//--------------------------------------------------------------------------------


+ 18
- 0
code/espurna/config/hardware.h View File

@ -137,6 +137,24 @@
#define LED1_PIN 13
#define LED1_PIN_INVERSE 1
#elif defined(ITEAD_1CH_INCHING)
// Note: definitions for this board are based on third party data
// and have not been fully tested yet. If you have the chance to
// test them, please report back. Thank you.
#define MANUFACTURER "ITEAD"
#define DEVICE "1CH_INCHING"
#define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define BUTTON2_PIN 15
#define BUTTON2_RELAY 0
#define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13
#define LED1_PIN_INVERSE 0
#define LED_PULSE 14
// -----------------------------------------------------------------------------
// Electrodragon boards
// -----------------------------------------------------------------------------


BIN
code/espurna/data/index.html.gz View File


+ 84
- 3
code/espurna/relay.ino View File

@ -7,6 +7,7 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
*/
#include <EEPROM.h>
#include <Ticker.h>
#include <ArduinoJson.h>
#include <vector>
#include <functional>
@ -20,6 +21,7 @@ std::vector<relay_t> _relays;
#ifdef SONOFF_DUAL
unsigned char dualRelayStatus = 0;
#endif
Ticker pulseTicker;
bool recursive = false;
@ -50,6 +52,71 @@ bool relayStatus(unsigned char id) {
#endif
}
void relayPulseBack(unsigned char id) {
relayToggle(id);
pulseTicker.detach();
}
void relayPulse(unsigned char id) {
byte relayPulseMode = getSetting("relayPulseMode", RELAY_PULSE_MODE).toInt();
if (relayPulseMode == RELAY_PULSE_NONE) return;
bool status = relayStatus(id);
bool pulseStatus = (relayPulseMode == RELAY_PULSE_ON);
if (pulseStatus == status) {
pulseTicker.detach();
return;
}
pulseTicker.attach(
getSetting("relayPulseTime", RELAY_PULSE_TIME).toInt(),
relayPulseBack,
id
);
}
unsigned int relayPulseMode() {
unsigned int value = getSetting("relayPulseMode", RELAY_PULSE_MODE).toInt();
return value;
}
void relayPulseMode(unsigned int value, bool report) {
setSetting("relayPulseMode", value);
/*
if (report) {
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
char topic[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 10];
sprintf(topic, "%s/pulse%s", MQTT_RELAY_TOPIC, mqttGetter.c_str());
char value[2];
sprintf(value, "%d", value);
mqttSend(topic, value);
}
*/
char message[20];
sprintf(message, "{\"relayPulseMode\": %d}", value);
wsSend(message);
#ifdef LED_PULSE
digitalWrite(LED_PULSE, value != RELAY_PULSE_NONE);
#endif
}
void relayPulseMode(unsigned int value) {
relayPulseMode(value, true);
}
void relayPulseToggle() {
unsigned int value = relayPulseMode();
value = (value == RELAY_PULSE_NONE) ? RELAY_PULSE_OFF : RELAY_PULSE_NONE;
relayPulseMode(value);
}
bool relayStatus(unsigned char id, bool status, bool report) {
if (id >= _relays.size()) return false;
@ -77,6 +144,7 @@ bool relayStatus(unsigned char id, bool status, bool report) {
if (report) relayMQTT(id);
if (!recursive) {
relayPulse(id);
relaySync(id);
relaySave();
relayWS();
@ -93,7 +161,6 @@ bool relayStatus(unsigned char id, bool status, bool report) {
}
bool relayStatus(unsigned char id, bool status) {
if (id >= _relays.size()) return false;
return relayStatus(id, status, true);
}
@ -300,10 +367,16 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0;
if (type == MQTT_CONNECT_EVENT) {
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());
mqttSubscribe(buffer);
sprintf(buffer, "%s/pulse%s", MQTT_RELAY_TOPIC, mqttSetter.c_str());
mqttSubscribe(buffer);
}
if (type == MQTT_MESSAGE_EVENT) {
@ -313,6 +386,15 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
if (!t.startsWith(MQTT_RELAY_TOPIC)) return;
if (!t.endsWith(mqttSetter)) return;
// Get value
unsigned int value = (char)payload[0] - '0';
// Pulse topic
if (t.indexOf("pulse") > 0) {
relayPulseMode(value, !sameSetGet);
return;
}
// Get relay ID
unsigned int relayID = topic[strlen(topic) - mqttSetter.length() - 1] - '0';
if (relayID >= relayCount()) {
@ -321,7 +403,6 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
}
// Action to perform
unsigned int value = (char)payload[0] - '0';
if (value == 2) {
relayToggle(relayID);
} else {


+ 7
- 0
code/espurna/web.ino View File

@ -264,6 +264,11 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
setCurrentRatio(getSetting("emonRatio").toFloat());
#endif
#if ITEAD_1CH_INCHING
byte relayPulseMode = getSetting("relayPulseMode", String(RELAY_PULSE_MODE)).toInt();
digitalWrite(LED_PULSE, relayPulseMode != RELAY_PULSE_NONE);
#endif
// Check if we should reconfigure MQTT connection
if (changedMQTT) {
mqttDisconnect();
@ -313,6 +318,8 @@ void _wsStart(uint32_t client_id) {
relay.add(relayStatus(relayID));
}
root["relayMode"] = getSetting("relayMode", RELAY_MODE);
root["relayPulseMode"] = getSetting("relayPulseMode", RELAY_PULSE_MODE);
root["relayPulseTime"] = getSetting("relayPulseTime", RELAY_PULSE_TIME);
if (relayCount() > 1) {
root["multirelayVisible"] = 1;
root["relaySync"] = getSetting("relaySync", RELAY_SYNC);


+ 23
- 3
code/html/index.html View File

@ -197,7 +197,7 @@
<div class="pure-g">
<label class="pure-u-1 pure-u-md-1-4" for="relayMode">Relay boot mode</label>
<div class="pure-u-1 pure-u-md-3-4">
<select name="relayMode" class="pure-u-1-2" tabindex="2">
<select name="relayMode" class="pure-u-3-4" tabindex="2">
<option value="0">Always OFF</a>
<option value="1">Always ON</a>
<option value="2">Same as before</a>
@ -210,7 +210,7 @@
<div class="pure-g module module-multirelay">
<label class="pure-u-1 pure-u-md-1-4" for="relaySync">Relay sync mode</label>
<div class="pure-u-1 pure-u-md-3-4">
<select name="relaySync" class="pure-u-1-2" tabindex="3">
<select name="relaySync" class="pure-u-3-4" tabindex="3">
<option value="0">No synchonisation</a>
<option value="1">Zero or one relays active</a>
<option value="2">One and just one relay active</a>
@ -221,9 +221,29 @@
<div class="pure-u-1 pure-u-md-3-4 hint">Define how the different relays should be synchronized.</div>
</div>
<div class="pure-g">
<label class="pure-u-1 pure-u-md-1-4" for="relayPulseMode">Relay pulse mode</label>
<div class="pure-u-1 pure-u-md-3-4">
<select name="relayPulseMode" class="pure-u-3-4" tabindex="4">
<option value="0">Don't pulse</a>
<option value="1">Normally OFF (stays on for a short time)</a>
<option value="2">Normally ON (stays off for a short time)</a>
</select>
</div>
<div class="pure-u-0 pure-u-md-1-4">&nbsp;</div>
<div class="pure-u-1 pure-u-md-3-4 hint">When pulse mode is enabled the relay will automatically switch back to its normal state after the pulse time (below).</div>
</div>
<div class="pure-g">
<label class="pure-u-1 pure-u-md-1-4" for="relayPulseTime">Relay pulse time</label>
<input name="relayPulseTime" class="pure-u-1 pure-u-md-3-4" type="number" min="1" tabindex="5" />
<div class="pure-u-0 pure-u-md-1-4">&nbsp;</div>
<div class="pure-u-1 pure-u-md-3-4 hint">Pulse time in seconds.</div>
</div>
<div class="pure-g module module-fauxmo">
<div class="pure-u-1 pure-u-sm-1-4"><label for="fauxmoEnabled">Alexa integration</label></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="fauxmoEnabled" /></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="fauxmoEnabled" tabindex="6" /></div>
</div>
</fieldset>


+ 9
- 0
code/platformio.ini View File

@ -213,6 +213,15 @@ upload_speed = 115200
upload_port = "192.168.4.1"
upload_flags = --auth=fibonacci --port 8266
[env:1ch-inching-debug]
platform = espressif8266
framework = arduino
board = esp01_1m
lib_deps = ${common.lib_deps}
lib_ignore = ${common.lib_ignore}
extra_script = pio_hooks.py
build_flags = -g -Wl,-Tesp8266.flash.1m128.ld -DDEBUG_PORT=Serial -DITEAD_1CH_INCHING
[env:electrodragon-debug]
platform = espressif8266
framework = arduino


Loading…
Cancel
Save