Basic understanding

Hello everyone,
Im trying to understand the basics of device,agent,web ( a simple code to turn on a led via html). I’ve looked and read a lot today, maybe im complicating things, but i havent got it. I hope someone will help me out here or point me in the right direction. (i probably trying to do many tasks at once, made garage door opener from instructables, now working on servo control blinds). Thanks

ok… I’ll give it a shot (they say the teacher often learns more than the student)

The Device window is where you put code you want to run on the IMP device.
This can be code that blinks an led, watches for a change in state of one of the pins and sounds an alarm connected to another of the IMPs pins, checks the temperature and checks if the temp is out of limits and so communicates back to the agent (agent.send) that an email should be generated to alert you.

The Agent window is where you put code that you want to interact with the Device.
Agent code is optional. (but can be very useful) for example if you want the device to post values or data to a web site (like Xively.com or Ubidots.com) or send you an email (using Mailgun.com for example).

the Device can run along just fine by itself without interacting with the agent. But the Agent code is what makes the IMP so powerful. The agent code can allow you to use a web page to change the state of one of the IMP outputs remotely (device.send), from your smart phone or your work computer, while you are away from home.

A very basic explanation and I am just a novice but sometimes it’s a basic explanation that one needs to clear up the fog of confusion
I hope that helps!
dwight

thanks bud, that cleared up things, ill start from beginning and keep learning one thing at the time, ive spent whole day from 0600 till 2100 just reading, i guess it just was ton of info mushed in my head. Thank you again, at least i can start learning in one direction

OK…this was done quickly and has not been verified, but it will give you an idea of how to make this happen. Others may have a far more elegant solution which would provide for (the agent to display) a page with a pop-up to select either on/off and have the page be submitted to the agent for processing.

Personally, I like to host the web pages on a free service (or stored on my iPhone) and have the resulting message go to the agent. Others like the whole service to be agent based. Obviously, a personal choice.

Agent code
`
//to turn the LED on, enter the agent’s URL with “?led=on” added to the end into your browser address field
//to turn the LED off, enter the agent’s URL with “?led=off” added to the end into your browser address field

function HTTPRequestHandler(request, response){
try {
server.log(“In HTTPRequestHandler”);
// we need to check to see if we got the required parameter
if (“led” in request.query) {
if ((request.query.led ==“on”) || (request.query.led == “off”)) {
if (request.query.led == “on”){
server.log(“turn on LED”);
device.send(“ledHandler”,1);
response.send(200,“OK–LED turned on” );
}
else {
server.log(“turn off LED”);
device.send(“ledHandler”,0);
response.send(200,“OK–LED turned off”);
}
}
else {
response.send(500,“Internal Server Error - Invalid parameter”);
}
}
} catch(ex) {
response.send(500,"Internal Server Error - Invalid request made: "+ ex);
}
}

//This tells the agent what function to call when a HTTP message is received
http.onrequest(HTTPRequestHandler);
`

Device code
`
//Device will turn on/off pin1 based upon message from agent
//Be sure to wire LED properly with a current limiting resistor

led <- hardware.pin1;
led.configure(DIGITAL_OUTPUT);

function ledHandler(state){
//we take the number that was passed from the agent’s call and use this to set
//the state of the output
led.write(state);
}

agent.on(“ledHandler”,ledHandler);
`

Thank you a lot! i like where your head is! I like to store it on my phone as well, i have android and my wife have iPhone (so i need to learn how to make html to be accessed from iPhone as well, didn’t get to that yet) there is just so many things. I appreciate the help gents, Merry Christmas!

Happy new year everyone!
so ive been working on code and finally came up with a device,agent,html code with a password which works! now im working on understanding the apiKey. Im trying to use the random apikey on agent and html. Here is my code

device: led <- hardware.pin9;

led.configure(DIGITAL_OUT);

function pwrButton(pwrState) {
if (pwrState == 1) {
server.log(“LED is On!”);
} else {
server.log(“LED is Off!”);
}

led.write(pwrState);
}

agent.on(“led”, pwrButton);

Agent: function requestHandler(request, response) {
apiKey <- "1124582b-3ad7-456f-931d-a12012011bea";

http.onrequest(function(request, response) {
if (“api-key” in request.headers && request.headers[“api-key”] == apiKey) {
// process request
local data = http.urldecode(request.body);
if (data.request == “POST”){
if(data[“led”] == “On” ){
device.send(“led”, 1);
}else if(data[“led”] == “Off”){
device.send(“led”, 0);
}
}

    response.send(200, "OK");
} else {
    // user was not authorized
    response.send(401, "Unauthorized");
}

});
};
http.onrequest(requestHandler);

and my HTML is


html>
head>
body>
form action=“https://agent.electricimp.com/ffV6Te67AG8-” method=“post”>
div class=“btn-group”>
button name=“led” value=“On” type=“submit”>ON
button name=“led” value=“Off” type=“submit”>OFF
/div>
/form>
body>
/html>

When i check agents url, it says Unauthorized, so i the apikey works, but i no slightest clue on how to implement it into my html. I hope someone will help me out here, Thank you gents!

API Keys are essentially passwords for APIs. You likely don’t want to be using them on a webpage, as the user would need to manually enter it (which would be annoying), or the webpage would need to automagically add it (which would render your security meaningless).

But you’re going down the right path! I’ve built a couple simple examples of projects that use passwords and basic authentication… it’s not a great security scheme, but it is something.

I’ve actually written an instructable that uses an agent hosted webpage + a password to control something that is turned on and off (which looks very similar to your project) - you can find it here:

http://www.instructables.com/id/Impower-The-Internet-Connected-AC-Outlet/

yeah ive been using your code to learn and make thing work=]
thank you, gonna wait more tutorials from you, easy to understand and learn from them, i thank you for your work

Thanks - that’s good to hear. I don’t have quite as much time as I used to to make new tutorials, but it’s something I occasionally tinker at.

Thanks - that's good to hear. I don't have quite as much time as I used to to make new tutorials, but it's something I occasionally tinker at.
Well ill be waiting for new tutorials :) . I like automated things ;)

The imp is just half of it. You need to understand the hardware/electronics … the pieces outside of the imp. Sometimes hooking things up incorrectly will let the smoke out.

working on that as well, too many things to learn at once :wink: