Hi,
I am hoping that someone is able to help me.
I am very new to this, so if I don’t get the terminology correct, please accept my apologies.
I have the imp set up within my PC to enable me to use it as a physical switch to turn it on and off as required from away from my home.
This is using pin8 on the device, a transistor, and connecting this to the motherboard of my PC.
The device is powered using an internal motherboard to mini B USB cable.
The device disconnects shortly after I send the relevant command to turn the PC on.
The log shows the “Received ON/OFF command” log entry, and then just under 2 minutes later it shows the “Device Disconnected” log entry - after which the only way I can get the imp online again is to physically disconnect the device from the power, and then re-connect.
Is my code correct to allow the device to re-connect by itself, without requiring me to physically disconnect the power, or is there a “reset” function that I haven’t discovered yet?
[I even tried to get the imp to deep sleep for 3 minutes as soon as it receives a command in order to get it to wake up and connect again, but that didn’t work!]
I have the following agent code:
`
// Log the URLs we need
server.log("Turn PC On: " + http.agenturl() + “?PCSwitch=1”);
server.log("Turn PC Off: " + http.agenturl() + “?PCSwitch=0”);
function requestHandler(request, response) {
try {
// check if the user sent PCSwitch as a query parameter
if (“PCSwitch” in request.query) {
// if they did, and PCSwitch=1.. set our variable to 1
if (request.query.PCSwitch == "1" || request.query.PCSwitch == "0") {
// convert the PCSwitch query parameter to an integer
local httpVal = request.query.PCSwitch.tointeger();
// send "PCSwitch" message to device, and send httpVal as the data
device.send("PCSwitch", httpVal);
}
}
// send a response back saying everything was OK.
response.send(200, "OK");
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}
// register the HTTP handler
http.onrequest(requestHandler);
`
And the following device code:
`
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30)
PCSwitch <- hardware.pin8;
PCSwitch.configure(DIGITAL_OUT_OD_PULLUP);
// Event handler when HTTP command is received
function setPinValue(httpVal)
{
// If HTTP receives a “1” hold switch on for 1 second
// If HTTP receives a “0” hold switch on for 6 seconds, forcing a
// hard shutdown
if (httpVal == 1)
{
server.log(“Received ON/OFF command”);
PCSwitch.write(0);
imp.sleep(0.01);
PCSwitch.write(1);
imp.sleep(0.75);
PCSwitch.write(0);
imp.sleep(0.5);
} else if (httpVal == 0)
{
server.log(“Received HARD OFF command”);
PCSwitch.write(0);
imp.sleep(0.01);
PCSwitch.write(1);
imp.sleep(6);
PCSwitch.write(0);
imp.sleep(0.5);
}
server.expectonlinein(180)
imp.deepsleepfor(180)
}
function RecoverFromLostConn(reason)
{
if (reason != SERVER_CONNECTED)
{
server.connect(RecoverFromLostConn, 180)
}
}
agent.on(“PCSwitch”, setPinValue)
server.onunexpecteddisconnect(RecoverFromLostConn)
`