How to make a resource (button) can be accessed both via manual and via platform?


#1

Hello all,
As i mentioned above, i want to make a button that can be accessible via thinger platform and via physical button also to turn ON/Off an electrical appliances. This way, when the device is not connected to internet or the internet is down, we can still ON and Off the appliances using physical button.

I already write the code like this, but still cannot work :

void setup() {
Serial.begin(115200);
Serial.println();
//Set mode for all IO pins
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
pinMode(reedSwitch, INPUT);
pinMode(panicPin, INPUT_PULLUP);
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);
delay(10);
thing.add_wifi(SSID, SSID_PASSWORD);
Serial.print(“connecting”);

thing[“sakelar1”]<< [](pson& in) {
if(in.is_empty()) {
in = (bool)digitalRead(switch1);
state = in ? LOW : HIGH; // control of the GPIO15
}
else {
digitalWrite(output1, state);
}
};

I am using NodeMCU with arduino IDE. The circuit is using input Pull-up with 10K resistor connected to On/Off switch. Anybody has the same experience?


#2

Up. Anyone know how to make this work?


#3

I would do something like this

I declare the thinger input as always

thing["sakelar1"] << digitalPin(15);

and a routine like this:

  val = digitalRead(switch1);  
  if (val == HIGH && pusherIn) 
  {         
    digitalRead(15) ? digitalWrite(15, LOW) : digitalWrite(15, HIGH);
    pusherIn=0; // need to declare this boolean variable to ensure the routine runs once
  } 
  else if (val==LOW && !pusherIn)
  {
    pusherIn=1; // ready for next push
  }

I assuming that the switch1 is a pusher with pulldown resistor, to implement this I recommend to install a capacitor in parallel with pulldown (or pullup resistor), to avoid the rebound effect, if you have issues with that you can do a routine with a timer to ensure the button is pushed by more than 2 continous seconds, for example, to execute the action on pin 15, and by this way you can avoid to install the capacitor.

Hope this helps


#4

Hi, Thanks for your advice.
Currently still trying as suggested, and will update to this.


#5

Hello,
After tried using pulldown resistor (10K) and parallel with capacitor (0,1 uF) and running the code above, i’m still stuck with the result. There is nothing happened on the output when i push the button.

Could you show me how to create timer according to your suggestion?


#6

Hi,

That’s weird, did you debug with serial console to see what’s going on? to see if it is detecting the push button and if it’s running loops as it should?

By timer routine I would do something like this:

Declare this variables

bool actualState, previousState, pusherIn;
unsigned long a;
int timer = 2000; //pressed button timer, in miliseconds
byte buttonPin = xx; // input button pin
byte outputPin = yy; // output pin

and run this code at loop:

actualState = digitalRead(buttonPin);
if (actualState != previousState)
    {
      a = millis();  //register when was detected the state change
      previousState = actualState;
      pusherIn = true; 
    }
if (actualState == HIGH)
  {
    int b = millis()-a;
    if (b >= timer && pusherIn)   
    {
      digitalRead(outputPin) ? digitalWrite(outputPin, LOW) : digitalWrite(outputPin, HIGH);  //output change according its actual state
      pusherIn = false;  //flag to allow just run one time this routine
    }
  }

And keep the pulldown resistor.

Honestly I don’t know if there is a shorter code to do this simple task, but that’s how I try to figure it out.

Let me know how it goes


#7

Hello Ega,

UPDATE:
I use the code above, and change the pulldown resistor to 1K and remove the capacitor. It works seamlessly! No need to add timer on the code.
The disadvantage is only it cannot synchronize with the dashboard On/Off button on the thinger platform or on the API. But nevermind, this is good news for me.

Many thanks!


#8

I’m happy that you get it working :wink:

C yap!