Hello, I want to command from a web interface a common-cathode RGB LED connected to the imp’s pins 1, 2, and 5.
I want to turn then on or off or to change the color. I have some problem doing this in ajax I don’t know how to write.
the agent code
`// At the start, print a message to say we’re online, and print the agent URL:
server.log("LED Web Control Agent Online: " + http.agenturl());
// requestHandler handles all http requests coming into the agent. It’s only
// setup to look for a select few requests: “led”, “rgb”, “user” and “timer”.
http.onrequest(function (request, response) {
response.header(“Access-Control-Allow-Origin”, “*”);
response.header(“Access-Control-Allow-Headers”,“Origin, X-Requested-With, Content-Type, Accept”);
response.header(“Access-Control-Allow-Methods”, “POST, GET, OPTIONS”);
try { // Try provides us with exception handling, in case a runtime error occurs
// check if the user sent led as a query parameter
if ("led" in request.query) {
// if they did, and led=1.. set our variable to 1
if ((request.query.led == "1") || (request.query.led == "0"))
{
// convert the led query parameter to an integer
local ledStatus = request.query.led.tointeger();
// send "led" message to device, and send ledState as the data
device.send("led", ledStatus);
}
}
// check if an "rgb" query was received:
if ("rgb" in request.query) {
// colors are sent as a string, we've got to do some work to convert
// them to a number, which is eventually what we'll need to do
// pwm on our RGB led pins.
local color = request.query.rgb; // get the query into a variable
if (color[0] == '#') { // The request should start with '#' (%23)
// We'll construct a table with three parameters: r, g, and b
// Do some work to convert r, g, and b from ASCII characters
// to 0-255 values.
local returnTable = {
r = ASCIItoHex(color[1])*16 + ASCIItoHex(color[2])
g = ASCIItoHex(color[3])*16 + ASCIItoHex(color[4])
b = ASCIItoHex(color[5])*16 + ASCIItoHex(color[6])
};
device.send("rgb", returnTable); // send our color table to the imp
}
}
// check if a "timer" query was received:
if ("timer" in request.query) {
// convert to an integer, and pass it out to the imp.
device.send("timer", request.query.timer.tointeger());
}
// send a response back saying everything was OK.
response.send(200, "OK");
}
catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
});
// This function converts an ASCII character to a number
function ASCIItoHex(colorNibble) {
if ((colorNibble >= ‘0’) && (colorNibble <= ‘9’)) {
return colorNibble - 48;
}
else if ((colorNibble >= ‘a’) && (colorNibble <= ‘f’)) {
return colorNibble - 87;
}
else if ((colorNibble >= ‘A’) && (colorNibble <= ‘F’)) {
return colorNibble - 55;
}
}`
and html and ajax
`!DOCTYPE html
html
head
script src=“http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js”>
/head
body
script type=“text/javascript”
function headingSearch(status)
{
$.ajax({
type: “get”,
url: “https://agent.electricimp.com/…”,
//datatype:“json”,
data: {led:status, rbg:status} ,
success: function(data) {
console.log(“Succes”);
},
contentType:"'Access-Control-Allow-Origin"
});
}
script
input type=“button” name=“led” value=“aprinde” onclick=“headingSearch(1);”>
input type=“button” name=“led” value=“stinge” onclick=“headingSearch(0);”>
input type=“button” name=“rgb” value=“red” onclick=“headingSearch(’#ff0000’);”>
/body
/html`