1 (edited by maddingamer 2020-05-15 16:12:17)

Topic: Physical button in addition to remotexy project

Hi there,

I want to have 2 physical buttons/switches in addition to the control via remotexy-app.
Could somebody maybe give a suggestion on how to implement this?
The buttons are on D5 and D6. But I haven't been able to implement them to work properly.

I'm using NodeMCU v2 (ESP 8266) with dual realy-module and two status-LEDs.

The RemoteXY-project is basicly just 2 switches.
Code and wiring below.

Thanks smile

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__ESP8266WIFI_LIB_POINT
#include <ESP8266WiFi.h>

#include <RemoteXY.h>

// RemoteXY connection settings 
#define REMOTEXY_WIFI_SSID "RemoteTest"
#define REMOTEXY_WIFI_PASSWORD "test1234"
#define REMOTEXY_SERVER_PORT 6377


// RemoteXY configurate  
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,2,0,0,0,69,0,10,13,1,
  2,0,8,53,46,15,2,26,31,31,
  65,78,0,65,85,83,0,2,0,8,
  21,46,15,2,26,31,31,65,78,0,
  65,85,83,0,129,0,20,14,23,6,
  8,82,101,108,97,105,115,32,49,0,
  129,0,20,46,23,6,8,82,101,108,
  97,105,115,32,50,0 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  uint8_t relais_01; // =1 if switch ON and =0 if OFF 
  uint8_t relais_02; // =1 if switch ON and =0 if OFF 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

#define PIN_RELAIS_01 D7
#define PIN_RELAIS_02 D3
#define LED_01 D1
#define LED_02 D2
#define BUTTON_01 D5
#define BUTTON_02 D6

void setup() 
{
  RemoteXY_Init (); 
  
  pinMode (PIN_RELAIS_01, OUTPUT);
  pinMode (PIN_RELAIS_02, OUTPUT);
  pinMode (LED_01, OUTPUT);
  pinMode (LED_02, OUTPUT);
}

void loop() 
{ 
  RemoteXY_Handler ();
  
  digitalWrite(PIN_RELAIS_01, (RemoteXY.relais_01!=0)?LOW:HIGH);
  digitalWrite(PIN_RELAIS_02, (RemoteXY.relais_02!=0)?LOW:HIGH);

  if (RemoteXY.relais_01!=0) digitalWrite(LED_01, HIGH);
  else digitalWrite(LED_01, LOW);

  if (RemoteXY.relais_02!=0) digitalWrite(LED_02, HIGH);
  else digitalWrite(LED_02, LOW);
}

https://i.imgur.com/k7Vesba.png

2 (edited by Guillaume 2020-05-16 13:42:32)

Re: Physical button in addition to remotexy project

Welcome,

You need to add pinMode INPUT_PULLUP for the physical buttons,

You didn't say what you want to do exactly with those buttons. Maybe something like this ?

bool isButtonPressed = RemoteXY.relais_01 == 1 || digitalRead(BUTTON_01) == LOW;
digitalWrite( PIN_RELAIS_01, isButtonPressed ? LOW : HIGH );
digitalWrite( LED_01, isButtonPressed ? HIGH : LOW );

As Remotexy do not allow to SET the value of the Switch, you may want to replace the Switch with a Button and a LED to indicate the vurrent state of the relay.

3

Re: Physical button in addition to remotexy project

Guillaume wrote:

As Remotexy do not allow to SET the value of the Switch, you may want to replace the Switch with a Button and a LED to indicate the vurrent state of the relay.

Do you mean in the RemoteXY interface?

Guillaume wrote:

You didn't say what you want to do exactly with those buttons. Maybe something like this ?

Yes I want to have the app and the physical buttons control the LED and the relay each. So button1 on the remotexy interface switches realy1 and the LED1 on, while the physical button1 does exactly the same when I don't have my phone on me.

4

Re: Physical button in addition to remotexy project

I tried like this, but the switches on the app doesn't do anything now..

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__ESP8266WIFI_LIB_POINT
#include <ESP8266WiFi.h>

#include <RemoteXY.h>

// RemoteXY connection settings 
#define REMOTEXY_WIFI_SSID "RemoteTest"
#define REMOTEXY_WIFI_PASSWORD "test1234"
#define REMOTEXY_SERVER_PORT 6377


// RemoteXY configurate  
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,2,0,0,0,69,0,10,13,1,
  2,0,8,53,46,15,2,26,31,31,
  65,78,0,65,85,83,0,2,0,8,
  21,46,15,2,26,31,31,65,78,0,
  65,85,83,0,129,0,20,14,23,6,
  8,82,101,108,97,105,115,32,49,0,
  129,0,20,46,23,6,8,82,101,108,
  97,105,115,32,50,0 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  uint8_t relais_01; // =1 if switch ON and =0 if OFF 
  uint8_t relais_02; // =1 if switch ON and =0 if OFF 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

#define PIN_RELAIS_01 D7
#define PIN_RELAIS_02 D3
#define LED_01 D1
#define LED_02 D2
#define BUTTON_01 D5
#define BUTTON_02 D6

void setup() 
{
  RemoteXY_Init (); 
  
  pinMode (PIN_RELAIS_01, OUTPUT);
  pinMode (PIN_RELAIS_02, OUTPUT);
  pinMode (LED_01, OUTPUT);
  pinMode (LED_02, OUTPUT);
  pinMode (BUTTON_01, INPUT_PULLUP);
  pinMode (BUTTON_02, INPUT_PULLUP);
}

void loop() 
{ 
  RemoteXY_Handler ();

  bool isButton1Pressed = RemoteXY.relais_01 != 0 || digitalRead(BUTTON_01) == HIGH;
  digitalWrite(PIN_RELAIS_01, isButton1Pressed?LOW:HIGH);
  digitalWrite(LED_01, isButton1Pressed?HIGH:LOW);
  
  bool isButton2Pressed = RemoteXY.relais_02 != 0 || digitalRead(BUTTON_02) == HIGH;
  digitalWrite(PIN_RELAIS_02, isButton2Pressed?LOW:HIGH);
  digitalWrite(LED_02, isButton2Pressed?HIGH:LOW);
}


Also tried buttons.. but they don't work either:

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__ESP8266WIFI_LIB_POINT
#include <ESP8266WiFi.h>

#include <RemoteXY.h>

// RemoteXY connection settings 
#define REMOTEXY_WIFI_SSID "RemoteTest"
#define REMOTEXY_WIFI_PASSWORD "test1234"
#define REMOTEXY_SERVER_PORT 6377


// RemoteXY configurate  
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,2,0,2,0,67,0,10,13,1,
  129,0,20,14,23,6,8,82,101,108,
  97,105,115,32,49,0,129,0,20,46,
  23,6,8,82,101,108,97,105,115,32,
  50,0,65,1,41,21,12,12,1,0,
  15,56,22,22,2,31,88,0,1,0,
  15,20,22,22,2,31,88,0,65,2,
  40,58,12,12 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  uint8_t xy_button_2; // =1 if button pressed, else =0 
  uint8_t xy_button_1; // =1 if button pressed, else =0 

    // output variables
  uint8_t xy_led_1_b; // =0..255 LED Blue brightness 
  uint8_t xy_led_2_g; // =0..255 LED Green brightness 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

#define PIN_RELAIS_01 D7
#define PIN_RELAIS_02 D3
#define LED_01 D1
#define LED_02 D2
#define BUTTON_01 D5
#define BUTTON_02 D6

void setup() 
{
  RemoteXY_Init (); 
  
  pinMode (PIN_RELAIS_01, OUTPUT);
  pinMode (PIN_RELAIS_02, OUTPUT);
  pinMode (LED_01, OUTPUT);
  pinMode (LED_02, OUTPUT);
  pinMode (BUTTON_01, INPUT_PULLUP);
  pinMode (BUTTON_02, INPUT_PULLUP);
}

void loop() 
{ 
  RemoteXY_Handler ();
  
  bool isButton1Pressed = RemoteXY.xy_button_1 == 1 || digitalRead(BUTTON_01) == HIGH;
  digitalWrite(PIN_RELAIS_01, isButton1Pressed?LOW:HIGH);
  digitalWrite(LED_01, isButton1Pressed?HIGH:LOW);
  RemoteXY.xy_led_1_b = (isButton1Pressed?255:0);
  
  bool isButton2Pressed = RemoteXY.xy_button_2 == 1 || digitalRead(BUTTON_02) == HIGH;
  digitalWrite(PIN_RELAIS_02, isButton2Pressed?LOW:HIGH);
  digitalWrite(LED_02, isButton2Pressed?HIGH:LOW);
  RemoteXY.xy_led_2_g = (isButton2Pressed?255:0);
}

5 (edited by Guillaume 2020-05-16 13:51:10)

Re: Physical button in addition to remotexy project

I made a mistake, it should be

bool isButton1Pressed = RemoteXY.xy_button_1 == 1 || digitalRead(BUTTON_01) == LOW;

Since you are using internal pullup, the physical button will read LOW when pressed

Actually your code will turn the relay ON when the button is pressed, and OFF when the button is released. Is it what you want?

Or do you want to press button once to turn the relay ON, and press again to turn relay OFF ?

6 (edited by maddingamer 2020-05-16 14:41:38)

Re: Physical button in addition to remotexy project

Guillaume wrote:

Or do you want to press button once to turn the relay ON, and press again to turn relay OFF ?

Yes you're right, it should stay ON/OFF after button is released.


Now it's working properly (except the release button thing..)


//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__ESP8266WIFI_LIB_POINT
#include <ESP8266WiFi.h>

#include <RemoteXY.h>

// RemoteXY connection settings 
#define REMOTEXY_WIFI_SSID "RemoteTest"
#define REMOTEXY_WIFI_PASSWORD "test1234"
#define REMOTEXY_SERVER_PORT 6377


// RemoteXY configurate  
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,2,0,2,0,49,0,10,13,1,
  65,1,41,24,12,12,1,0,16,47,
  22,22,2,31,82,101,108,97,105,115,
  32,50,0,1,0,15,20,22,22,2,
  31,82,101,108,97,105,115,32,49,0,
  65,2,41,52,12,12 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  uint8_t xy_button_1; // =1 if button pressed, else =0 
  uint8_t xy_button_2; // =1 if button pressed, else =0 

    // output variables
  uint8_t xy_led_1_b; // =0..255 LED Blue brightness 
  uint8_t xy_led_2_g; // =0..255 LED Green brightness 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////


#define PIN_RELAIS_01 D3
#define PIN_RELAIS_02 D7
#define LED_01 D1
#define LED_02 D2
#define BUTTON_01 D5
#define BUTTON_02 D6

void setup() 
{
  RemoteXY_Init (); 
  
  pinMode (PIN_RELAIS_01, OUTPUT);
  pinMode (PIN_RELAIS_02, OUTPUT);
  pinMode (LED_01, OUTPUT);
  pinMode (LED_02, OUTPUT);
  pinMode (BUTTON_01, INPUT_PULLUP);
  pinMode (BUTTON_02, INPUT_PULLUP);
}

void loop() 
{ 
  RemoteXY_Handler ();
  
  bool isButton1Pressed = RemoteXY.xy_button_2 == 1 || digitalRead(BUTTON_01) == LOW;
  digitalWrite(PIN_RELAIS_01, isButton1Pressed?LOW:HIGH);
  digitalWrite(LED_01, isButton1Pressed?HIGH:LOW);
  RemoteXY.xy_led_1_b = (isButton1Pressed?255:0);
  
  bool isButton2Pressed = RemoteXY.xy_button_1 == 1 || digitalRead(BUTTON_02) == LOW;
  digitalWrite(PIN_RELAIS_02, isButton2Pressed?LOW:HIGH);
  digitalWrite(LED_02, isButton2Pressed?HIGH:LOW);
  RemoteXY.xy_led_2_g = (isButton2Pressed?255:0);
}

7

Re: Physical button in addition to remotexy project

maddingamer wrote:
Guillaume wrote:

Or do you want to press button once to turn the relay ON, and press again to turn relay OFF ?

Yes you're right, it should stay ON/OFF after button is released.

Do you have an idea on how to achieve this? hmm

8 (edited by Guillaume 2020-05-21 14:35:02)

Re: Physical button in addition to remotexy project

Yes, see this https://www.arduino.cc/en/Tutorial/StateChangeDetection . Instead of doing "buttonPushCounter ++;" simply invert a bool : "relayState = !relayState;"

9

Re: Physical button in addition to remotexy project

Guillaume wrote:

Yes, see this https://www.arduino.cc/en/Tutorial/StateChangeDetection . Instead of doing "buttonPushCounter ++;" simply invert a bool : "relayState = !relayState;"

Could you give me another hand on this? I can't get it to work as I'm not sure on how to migrate the example code from the url into mine existing code hmm

10

Re: Physical button in addition to remotexy project

Here is an example http://forum.remotexy.com/viewtopic.php?pid=2081#p2081