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

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?

Up. Anyone know how to make this work?

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

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

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?

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

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!

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

C yap!

Hi @ega ,

I just realize that when the internet is off, that code didn’t work.
So, i can only use the physical button when the device is already connected to the internet.

Any coding suggestion on how can i use physical button when the device is offline?

Hi @astonix,

What do I think? That your device is stuck in a connection loop, trying to reach thinger platform, so if it doesn’t get connected, it keeps trying until it accomplishes it, that’s why it doesn’t do anything else, I’m not sure, just thinking what is happening to explain your situation.

I was able to make the device work, disconnect and keep working at the process loop, and reconnect again, how I did it? I made a script to every 10 minutes stop calling the “thinger.handle();” instruction and call “thinger.stop;” once, so it stops thinger library and keep the device working without thinger connection, and after like 30 sec I call “thinger.handle();” and the device connected again to thinger platform.

What would I try to do?
I would try to detect with the device the connection actual status in order to stop thinger library if it detects wifi disconnected, if you connect by GSM I guess there are some AT commands to diagnose the connection status.
But always there will be the risk to get the device stuck, because if it runs thinger instruction and it is without connection, will get stuck trying to connect (assuming my hypotesis is true), but
surely you will reduce this risk diagnosing connection status just before calling thinger instruction.
And if there is an issue with internet access and the wifi router keeps working, it will detect wifi connection and will call thinger instruction but as the device can’t reach thinger platform, will get stuck.
I think the solution to this situation should be implemented at thinger library, something like if at 30 secs trying connection doesn’t get, run main process loop and try again after 5 mins, and a “thinger sucessfull connection flag” so it can be watched physically in the device with a led, for example.

Hope this helps