Giter Club home page Giter Club logo

Comments (6)

peter-valkov avatar peter-valkov commented on August 12, 2024

"toggle" for me have different meaning from what you described. It is (current_state XOR 1) i.e. if it is OFF switch it ON - if it is ON switch it OFF.

Back to your question:

  1. there is need for additional parameter - delay after it will be switched OFF
  2. how the following situation should be handled:
  • we have request for "ON-OFF"
  • before delay for switching OFF is expired we receive another "ON-OFF" request
  • should we switch it OFF after the delay of first request is expired or should we wait for the second?!?

There can be arguments for either solution.

Why two requests bother you?

On-OFF is easily implemented using JavaScript setTimeout() function.

function relaySet(socket, state) {
    socket.send(
        JSON.stringify(
            {
                URL: '/relay',
                Method: 'POST',
                Data: {
                    Relay: state
                }
            }
        )
    );
}

function relayOnOff(socket, delay) {
    relaySet(socket, 1);
    setTimeout(
        function () {
            relaySet(socket, 0);
        },
        delay
    );
}

var socket = new WebSocket('ws://192.168.4.1/events');
relayOnOff(socket, 500);

from esp8266.

bobybc avatar bobybc commented on August 12, 2024

I don't use browser, but Android application to open my automated door. It happens that in a slow mobile connection (GPRS) the delay b/w ON and OFF requests is more than 3 sec, even 5 sometimes. This reflects on a different mode for my door - it switches to so called pedestrian mode where the door is not fully open.
That's why i need to have ON-OFF with one request...

And let we simplify the case - currently you code has setRelay(1)/setRelay(0) to ON/OFF relay.
Can we just put delay interval (in ms) as parameter to this function?
Server side it can be implemented like : if the value is greater than 1, just switch ON, wait time in ms described in the value and then switch OFF.
What do you think on that?

from esp8266.

peter-valkov avatar peter-valkov commented on August 12, 2024

I prefer to make relay state signed integer so if the state:

  • is equal to 0 - switch OFF
  • is greater than 0 - switch ON
  • is lower than 0 - switch ON wait abs(state) ms then switch OFF

What about the case with double requests?

  • receive request for ON-OFF after 500ms
  • switch relay ON
  • 200ms later second request is received for new 500ms

What should I do?

  • switch relay OFF 500ms after first request
  • switch relay OFF 500ms after second request i.e. 700ms after first request

from esp8266.

bobybc avatar bobybc commented on August 12, 2024

The short answer is - switch relay OFF 500ms after first request
But I think that it will be good if it is synchronized i.e. until first request is not completed, do not execute new one

from esp8266.

peter-valkov avatar peter-valkov commented on August 12, 2024

@bobybc your proposal is done. Now you can set the relay state as follows:

  • If [state] is 0 relay is switched OFF.
  • If [state] is 1 relay is switched ON.
  • If [state] is greater than 1 relay is switched ON for [state]ms and then switched OFF. During this interval all other commands are ignored.

Due to some technical difficulties signed integer approach did not worked.

Hope your door will work as expected ;-)

from esp8266.

bobybc avatar bobybc commented on August 12, 2024

10x a lot Peter
Cheers :)

from esp8266.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.