Lol I would if was able to prgramme in the imp IDE, however as I don’t have my imp yet and it has not yet been blinked up I’m forced to write all programmes on notes for now, such is life
Oh ok thanks for that Joel, I’ll change it back now and once I receive the prototype I’ll try both the device code you wrote and the agent code, thanks again Joel really appreciate it. I’ll hopefully update you soon.
OK the prototype doesn’t seem to be working for some reason, pretty sure its a hardware issue, however I just wanted to make sure the code was correct. Here is the code I used:
Agent Code
const API_KEY = "1234"; http.onrequest(function (req, resp) { try { local data = http.jsondecode(req.body); server.log("Received: " + req.body); if ("api-key" in req.headers && req.headers["api-key"] == API_KEY) { server.log(req.headers["api-key"]); if ("button1" in data) { device.send("button1", data.button1); device.on("button1", function(d) { local json = "{ \"status\" : { \"button1\" : \"" + d + "\" }}"; resp.send(200,json); }); } else if ("button2" in data) { device.send("button2", data.button2); device.on("button2", function(d) { local json = "{ \"status\" : { \"button2\" : \"" + d + "\" }}"; resp.send(200,json); }); } else if ("button3" in data) { device.send("button3", data.button3); device.on("button3", function(d) { local json = "{ \"status\" : { \"button3\" : \"" + d + "\" }}"; resp.send(200,json); }); } } else { local json = "{ \"status\" : { \"auth\" : \"no\" }}"; resp.send(401, json); } } catch (ex) { resp.send(500, "Internal Server Error: " + ex); } });
Device code: Unchanged from the code Joel very kindly provided
`agent.on(“button1”, function(a) {
local data = “”;
if (a == “”) {
hardware.pin1.write(1);
data = “Button 1 pressed.”;
}
agent.send(“button1”, data)
});
agent.on(“button2”, function(a) {
local data = “”;
if (a == “”) {
hardware.pin1.write(0);
hardware.pin2.write(0);
data = “Button 2 pressed.”;
}
agent.send(“button2”, data)
});
agent.on(“button3”, function(a) {
local data = “”;
if (a == “”) {
hardware.pin2.write(1);
data = “Button 3 pressed.”;
}
agent.send(“button3”, data)
});
In the Pitchfork app I inputted the correct URL and I entered the correct API key. The log in the in the IDE confrims everything when I press a button in the app.
It looks correct, but if you have your pin configuration statements commented out they won’t do anything.
Server.log() is your debugging friend. Stick them in all over the place to make sure things are happening the way you want them to.
Do you have a multimeter to see if pins 1 & 2 are going high when you send the commands? You can have the Imp check as well with something like:
I found a very small and inexpensive motor controller at Pololu.com that I used with an Attacknid. You can drive two motors in either direction at variable speeds.
You have to configure your pins or they won’t do anything. Having them commented out is the same as not configuring them at all. If you just want the pins configured as DIGITAL_OUT, then just remove the // before the statements in your code.