Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- How to use this.
- To control your Electric Imp with this code you need to go into the planner and add a HTTP IN
- Take the url from the HTTP IN node and call it with ?value=p[1,2,5,7,8]s[1,0]
- example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=p1s1
- This would do the same as writing hardware.pin1.write(1);
- example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=p1s0
- This would do the same as writing hardware.pin1.write(0);
- */
- // Only used to toggle between the + and - symbol
- lastCheckin <- 0;
- //Sending signal strength and heartbeat back to the planner
- function checkIn()
- {
- server.show("Signal: " + imp.rssi() + "dBm " + (lastCheckin?"+":"-"));
- //Toggle so the other symbol will be displayed next time
- lastCheckin = 1-lastCheckin;
- //Run this function again in 5 seconds
- imp.wakeup(5.0, checkIn);
- }
- //Turn the ACT led off
- function actOff()
- {
- hardware.pin9.write(1);
- }
- //Array of pins, the na entry is not used for anything else than changing range to match pin numbers
- local channelPin = ["na", hardware.pin1, hardware.pin2, "na", "na", hardware.pin5, "na", hardware.pin7, hardware.pin8, hardware.pin9];
- class handleInput extends InputPort
- {
- constructor()
- {
- base.constructor();
- }
- function set(value)
- {
- //Get where in the string we set the target pin
- local pin = value.find("p", 0);
- //Get where s for state is set in the string
- local modeState = value.find("s", 0);
- //If the target pin is not set, say so and stop here.
- if (pin < 0)
- {
- server.log("Pin number seems to be missing.");
- return;
- }
- //Set pin to the target pin number
- pin = value.slice(pin+1,pin+2);
- //Convert to integer
- pin = pin.tointeger();
- //Check if the target pin is one that can be used for this
- //Pin 9 are capable of this too, but it is used as activity led
- if (pin == "3" || pin == "4" || pin == "6" || pin == "9")
- {
- server.log("Only pin 1, 2, 5, 7, and 8 can be used.");
- return;
- }
- //If modeState is not 0 it means we want the function that sets the pin to a permanent state
- if (modeState != 0)
- {
- //Get the state we want to set the pin into
- modeState = value.slice(modeState+1,modeState+2);
- //Convert to integer
- modeState = modeState.tointeger();
- //Set only the target pin to an output without pullup, even if it is already done once before
- channelPin[pin].configure(DIGITAL_OUT_OD_PULLUP);
- //Set the pin state
- channelPin[pin].write(modeState);
- //Show what we just did to which pin
- server.log("Relay " + pin + ": " + (modeState?"open":"closed"));
- }
- //Turn the ACT led on
- hardware.pin9.write(0);
- //Schedule ACT led to be turned off in 0.15 seconds
- imp.wakeup(0.15, actOff);
- }
- }
- //We need these for having an output for the voltage the imp is running on
- local outputVoltage = OutputPort("Voltage (V)", "number");
- local voltageV;
- //We need these for having an output with the wifi signal strength
- local outputSignal = OutputPort("Signal (dBm)", "number");
- local signaldBm;
- function getStats()
- {
- //Set outputVoltage to the current voltage
- outputVoltage.set(hardware.voltage());
- //Set outputSignal to the current signal strength
- outputSignal.set(imp.rssi());
- //Do this again in 60 seconds
- imp.wakeup(60, getStats);
- }
- //Configure imp
- imp.configure("H.A.Node V0.02a", [handleInput], [outputVoltage, outputSignal]);
- // Set pin 9 to output for the ACT led
- hardware.pin9.configure(DIGITAL_OUT_OD_PULLUP);
- //Make sure ACT led is off and do the first check-in
- actOff();
- checkIn();
- //Collect stats
- getStats();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement