Why can I not set part of this up in my planner


#1

my code is below
when I am using 3 HTTP ins for this I can hook up “on” and “off” but not the setting to make one cup.
When i try to hook it up only the on and off show up
What am I doing wrong?
My imp is not online can this be why?
class power_input extends InputPort // THIS WORKS
{
name = "Power On"
type = “number”

function set(value)
{
server.show(“Power On”);

    imp.sleep(2);
    hardware.pin8.configure(DIGITAL_OUT);
    hardware.pin8.write(1);
    imp.sleep(2); 
    hardware.pin8.configure(DIGITAL_OUT);
    hardware.pin8.write(0);

}
}
class power_off extends InputPort //THIS WORKS
{
name = "Power Off"
type = “number”

function set(value)
{

    server.show("Power Off");
    
    imp.sleep(2);
    hardware.pin7.configure(DIGITAL_OUT);
    hardware.pin7.write(1);
    imp.sleep(2); 
    hardware.pin7.configure(DIGITAL_OUT);
    hardware.pin7.write(0);

}
}

class power_cup extends InputPort //THIS DOES NOT WORK
// WHEN I TRY TO SET IT UP IN MY PLANNER IT ONLY SHOWS ON AND OFF NOT HIS ONE
{
name = "Power for one cup"
type = “number”

function set(value)
{

    server.show("Power for one cup");
    
    imp.sleep(2);
    hardware.pin8.configure(DIGITAL_OUT);
    hardware.pin8.write(1);
    imp.sleep(2); 
    hardware.pin8.configure(DIGITAL_OUT);
    hardware.pin8.write(0);
    imp.sleep(20);//sleeps for 20 second
    hardware.pin7.configure(DIGITAL_OUT);
    hardware.pin7.write(1);
    imp.sleep(2); 
    hardware.pin7.configure(DIGITAL_OUT);
    hardware.pin7.write(0);

}
}
imp.configure(“CoffeeControlCenter”, [power_input(), power_off(), power_cup()], []);


#2

A couple of comments:

  • change the server.show’s to server.log’s. Then you can see the history of what happened when.
  • doing imp.sleep(20) is rather bad, because it may well cause the imp to get marked offline. You could rewrite this like:

server.show("Power for one cup"); imp.wakeup(2, function() { hardware.pin8.configure(DIGITAL_OUT); hardware.pin8.write(1); imp.wakeup(2, function() { hardware.pin8.write(0); imp.wakeup(20, function() { hardware.pin7.configure(DIGITAL_OUT); hardware.pin7.write(1); imp.wakeup(2, function() { hardware.pin7.write(0); }) }) }) });

…ie, 4 nested callbacks. This way, the imp will continue processing events. You also don’t need to keep configuring the pins; generally you would configure them once at the start of your code.


#3

How do I keep my imp online as long as it is plugged in?
If I make your changes will it always be online?
If I click on then wait 20 seconds then click off it will not be online?
thank you


#4

There’s nothing specific to do to keep the imp online; release 25 has a known issue which affects some users, and 25.2 addresses this pretty effectively in almost all cases (we’re still investigating 2 users who have seen problems).

The reason I say 20 seconds is bad is because when the server pings the client, which it does regularly as part of link maintenance, it expects a reply within 10 seconds. If it sent the ping during your sleep, it wouldn’t get a response in time and would close the connection.

It will also make your imp rather unresponsive to (eg) the run button. The code above fixes that.