Hi,
im really new to electricimp, but i like it a lot till now.
I made this code that sends out DMX-Data to LED-Light (uart), triggered by http-request send to the agent.
So when starting up the Imp (giving power), the device is sending out smth, that the light connected flickering in all kind of colors etc.
Is there a way to run code on the device till the connection with the agent is established (so sending out 0 as Values through uart, so the light stays off till the connection is done)
device code:
rts <- hardware.pinC;
uart <- hardware.uartABCD;
tx <- hardware.pinA;
local led = 0;
// initialise a 512-device DMX frame
local outBlob = blob(512);
for (local i = 0; i < outBlob.len(); i++) {
outBlob.writen(0, 'b');
}
function setLedState(state) {
server.log("Set LED to state: " + state);
if (state == 1){
setLevel(1,0);
setLevel(2,0);
setLevel(3,255);
setLevel(4,255);
setLevel(5,0);
setLevel(6,255);
setLevel(7,0);
setLevel(8,0);
}
else {
setLevel(1,0);
setLevel(2,0);
setLevel(3,255);
setLevel(4,255);
setLevel(5,0);
setLevel(6,0);
setLevel(7,0);
setLevel(8,0);
}
}
function setLevel(addr, level) {
// send DMX512 command to set device at "addr"
outBlob.seek(addr);
outBlob.writen(level, 'b');
// the frame will automatically be sent on the next refresh
}
function refresh() {
// manually send out the break and mark-after-break
tx.configure(DIGITAL_OUT, 0);
imp.sleep(0.0001);
// mark-after-break is implicitly sent here; bus idles high while we configure the UART in SW
uart.configure(250000, 8, PARITY_NONE, 2, NO_CTSRTS);
uart.write(outBlob); // send the frame
imp.wakeup(0.1, refresh); // schedule next refresh
}
// ----- main program -----
server.log("DMX - " + imp.getsoftwareversion());
server.log(imp.net.info());
rts.configure(DIGITAL_OUT);
rts.write(1);
refresh();
//Green
/*
setLevel(1,0);
setLevel(2,0);
setLevel(3,0);
setLevel(4,255);
setLevel(5,255);
setLevel(6,0);
setLevel(7,0);
setLevel(8,0);
*/
// Register a handler for incoming "set.led" messages from the agent
agent.on("set.led", setLedState);
agent code:
// Log the URLs we need
server.log("Turn LED On: " + http.agenturl() + “?led=1”);
server.log("Turn LED Off: " + http.agenturl() + “?led=0”);
function requestHandler(request, response) {
// 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 ledState = request.query.led.tointeger();
response.send(200, "OK");
imp.sleep(0.1);
// Send "set.led" message to device, and send ledState as the data
device.send("set.led", 1);
imp.sleep(10);
device.send("set.led", 0);
}
// Send a response back to the browser saying everything was OK.
}
// Register the HTTP handler to begin watching for HTTP requests from your browser
http.onrequest(requestHandler);
I hope you understand what i want to do
thx
greetings
Julius