Remotely Turn on a Desktop Computer - Need code & IDE help

Hello Members!
I am new to Electric Imp and trying this project Some of the instructions are old because they talk about and Firmware which does not exist in the new IDE. I understand that HTTP In has been replaced by http.onrequest(). I have looked into Dev Center resources but can’t figure out how to write http.onrequest() for this particular project. Also, I don’t know what part of code would go to Agent and what would go to Device? I’ve got the hardware assembled and Imp connected to WiFi. All I need is some guidance on the code part. Any help is highly appreciated.

Some things you might want to know. Your agent has its own URL, and you can have an HTML web page on the agent. That means if you don’t have your own website, you can use the agent as the website. And you won’t need to download an app to your smartphone … just use the browser to access the agent URL.

So the agent web page could have the on/off switch for your Desktop Computer, or you might even have a clock programmed to power it on or off at specified times.

I wanted to find the example, but this site has such a horrible search function, I can’t find it. This is all I found …

I know there is an better example. Maybe someone else can find it. = wins award for the worst search function of all websites.

My, that Instructables guide is old. Take a look at the imp API cookbook recipes in the Dev Center’s Sample Code section, @amitdogra. There are examples showing how to write HTTP input handlers that will go in your project’s agent code (the part that runs in the cloud), and the messaging system that underpins agent <-> device communications.

To give you hand, here’s some code you can use for the task in hand. You’ll need the ID of your agent: look for the URL displayed right above the agent code in the IDE. If you need a hand with the IDE, check out our Getting Started Guide and IDE User Guide.


`// impBoot
// Cloud Wake-On-LAN. Remotely boots a desktop through the motherboard's switch
// Author: Shawn Hymel
// Date: November 15, 2012
// Hardware:
//     Connect 1k resistor to pin 8 of the imp. Connect that to the base
//     of an NPN transistor. Connect the collector to the + of the POWER
//     SW on the motherboard and the emitter to the - of the POWER SW on
//     the motherboard.
// You will need the Electric Imp Toggle Android app (or something similar).
// Based on the example Imp code provided by Taylor Alexander, which can
// be found at
// License: BeerWare (thanks Sparkfun!)
//     Please use, reuse, and modify this code as you need.
//     We hope it saves you some time, or helps you learn something!
//     If you find it handy, and we meet some day, you can buy me a beer
//     or iced tea in return.

// Function definitions

// Initialize pin 8 to be output to control motherboard POWER SWITCH
function initSwitch()

function flipSwitch(value)
	if (value == 1)
		server.log("Received ON command.");
	else if (value == 0)
		server.log("Received OFF command.");

// Start of program


// Configure pins on the Imp

// Configure agent messaging
agent.on("change.switch.state", flipSwitch);`



`// Define an HTTP request handler
// Expects to be triggered by a URL-encoded input:
// Assumes value of switch is 1 (on) or 0 (off)

function requestHandler(request, response) {
    try {
        if ("switch" in request.query) {
            // 'switch' is a URL-encoded parameter, ie. '?switch=1'
            local state = request.query.switch;
            if (state == "on" || state = "1") state = 1;
            if (state == "off" || state = "0") state = 0;
            // Use the 'response' object to acknowledge reception of the request
            // to the request's source. '200' is HTTP status code for 'OK'
            response.send(200, "Setting received and applied");
            // Send the command to the device
            device.send("change.switch.state", state);
        } else {
        	response.send(200, "Unknown command");
    } catch (error) {
        // Something went wrong; inform the source of the request
        // '500' is HTTP status code for 'Internal Server Error'
        response.send(500, error);
// Register the handler function as a callback

server.log("Agent running");`


Thanks a lot @mlseim and @smittytone for your help.
Appreciate smittytone’s effort in putting the agent and device code properly. After fixing some syntax errors I was able to get this code running. Debug messages tell me that the logic is working however the hardware (NPN transistor’s emittor and collector) are not responding. I have double checked the components, breadboard and connectors. Everything seems to be OK. Is there a way to check if pin8 is really getting the write(1) instruction?

You should have a multimeter as a hobbyist. Do a voltage check to see if the pin switches, and to test the transistor. I also recommend buying a quality multimeter, not something on ebay for $20, but a mid priced Fluke multimeter.

I did check with my multimeter but didnt notice any change. I was checking emittor and collector if i get the signal that will power on the computer. Let me now check pin8 as well as transistor as you suggested. Thanks for the tip :slight_smile:

DC Volts.
Black lead on GND
Red lead on pin 8

First put Red lead on V3.3 and see if your meter reads 3.3VDC
That would verify your meter is working properly.

No matter what the transistor does, pin 8 should either go Hi or go Lo. I didn’t study the Instructable enough to know whether they are sinking or sourcing.

Just a note for those who want to perform a better search in the forum archives: you can do a Google search within the forum by entering before the text terms you want to find in the site. Hope this helps.

Could the script this forum uses put that in automatically? That is sort of inconvenient.

@amitdogra…how do you fix that syntax error…
my error was here ( local state = request.query.switch;)
can you guys help me…