Skip to content

Commit

Permalink
Add support for Up / Down mechanical buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
eg321 committed Apr 28, 2021
1 parent ce86d49 commit 4764d2e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
- 2-3 pins for steppers are not reversed now. Please check that initilization of steppers is ok for you (near 86 line: Stepper1(D1, D2, D3, D4))

# Latest changes:
## 1.4.2 (29 April 2021)
- Added support for Up / Down mechanical buttons (long press to tune position). Default pins are 22/23 (initialized with INPUT_PULLUP and activated with GND). You can disable that feature with "USE_BUTTONS" macro.

## 1.4.1 (14 March 2021)
- improve MQTT connection reliability. While MQTT hub is unavailable, controller will try to reconnect once per 60 seconds. In this time web interface will be still available.
- fix update notifications and links.
Expand Down
67 changes: 65 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#ifndef USE_BUTTONS //use mechanical up/down buttons? Check PIN_UP_BUTTON / PIN_DOWN_BUTTON values below...
#define USE_BUTTONS true
#define LONG_PRESS_MS 1000 //How much ms you should press button to switch to "long press" mode (tune blinds position)
#define PIN_UP_BUTTON 22 //Button up pin
#define PIN_DOWN_BUTTON 23 //Button down pin
#endif

#define _WIFIMGR_LOGLEVEL_ 4
#define USE_AVAILABLE_PAGES true

Expand All @@ -11,10 +18,14 @@
#include "index_html.h"
#include <string>

#ifdef USE_BUTTONS
#include "Button2.h"
#endif

//----------------------------------------------------

// Version number for checking if there are new code releases and notifying the user
String version = "1.4.1-egor";
String version = "1.4.2";

NidayandHelper helper = NidayandHelper();

Expand Down Expand Up @@ -69,6 +80,11 @@ boolean loadDataSuccess = false;
boolean saveItNow = false; //If true will store positions to filesystem
boolean initLoop = true; //To enable actions first time the loop is run

#ifdef USE_BUTTONS
Button2 buttonUp = Button2(PIN_UP_BUTTON);
Button2 buttonDown = Button2(PIN_DOWN_BUTTON);
#endif

#define M1_1 25
#define M1_2 26
#define M1_3 32
Expand Down Expand Up @@ -422,6 +438,46 @@ void setupOTA() {
ArduinoOTA.begin();
}

#ifdef USE_BUTTONS
void onPressHandler(Button2& btn) {
Serial.println("onPressHandler");
if (btn == buttonUp) {
Serial.println("Up button clicked");
processMsg("auto", "0", 1, 0);
processMsg("auto", "0", 2, 0);
processMsg("auto", "0", 3, 0);
} else if (btn == buttonDown) {
Serial.println("Down button clicked");
processMsg("auto", "100", 1, 0);
processMsg("auto", "100", 2, 0);
processMsg("auto", "100", 3, 0);
}
}

void onReleaseHandler(Button2& btn) {
Serial.print("onReleaseHandler. Button released after (ms): ");
Serial.println(btn.wasPressedFor());
if (btn.wasPressedFor() > LONG_PRESS_MS) {
processMsg("manual", "STOP", 1, 0);
processMsg("manual", "STOP", 2, 0);
processMsg("manual", "STOP", 3, 0);
}
}

void setupButtons() {
buttonUp.setPressedHandler(onPressHandler);
buttonDown.setPressedHandler(onPressHandler);

buttonUp.setReleasedHandler(onReleaseHandler);
buttonDown.setReleasedHandler(onReleaseHandler);
}

void processButtons() {
buttonUp.loop();
buttonDown.loop();
}
#endif

void setup(void) {
Serial.begin(115200);
delay(100);
Expand Down Expand Up @@ -451,6 +507,10 @@ void setup(void) {
action2 = "";
action3 = "";

#ifdef USE_BUTTONS
setupButtons();
#endif

//Set MQTT properties
outputTopic = helper.mqtt_gettopic("out");
inputTopic1 = helper.mqtt_gettopic("in1");
Expand Down Expand Up @@ -594,6 +654,9 @@ void loop(void)
ESP.wdtFeed();
#endif

#ifdef USE_BUTTONS
processButtons();
#endif

/**
Serving the webpage
Expand Down Expand Up @@ -691,7 +754,7 @@ void loop(void)
if (action1 != "" || action2 != "" || action3 != "") {
// Running mode
long now = millis();
if (now - lastPublish > 1000) {
if (now - lastPublish > 3000) {
lastPublish = now;
sendmsg(outputTopic);
}
Expand Down

0 comments on commit 4764d2e

Please sign in to comment.