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
- To change the state of a pin, 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);
- To initate a row of pulses, do as above, but with ?value=p[1,2,5,7,8]t[1,x](,d[0.1, x)
- example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=p1t10d1
- This would make pin 1 pulse 10 times with 1 second delay
- example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=p5t30d0.5
- This would make pin 5 pulse 30 times with 0.5 second delay
- If a delay is not speficied half of the triggerPulseDuration will be used
- */
- // Only used to toggle between the + and - symbol
- lastCheckin <- 0;
- //Sending signal strength and heartbeat back to the planner
- function checkIn()
- {
- //Return the signal to be shown in planner with toggling +/- sign to indicate it being alive
- 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);
- //Use PWM to make the led blink lower for its heartbeat
- hardware.pin9.write(0.8);
- //Turn the led off again after 0.1 second
- imp.wakeup(0.1, actOff);
- }
- //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];
- //The duration a trigger pulse lasts
- triggerPulseDuration <- 0.1;
- //If the state of the pulses needs to be inverted
- invertPins <- 0;
- //Array of remaining trigger pulses each pin got left to do
- local triggersPin = ["na", 0, 0, "na", "na", 0, "na", 0, 0, 0];
- //Array of the delay between each trigger pulse for each pin
- local triggersDelay = ["na", 0, 0, "na", "na", 0, "na", 0, 0, 0];
- function startPulses(pin)
- {
- if (pin == 1) pulsePin1();
- if (pin == 2) pulsePin2();
- if (pin == 5) pulsePin5();
- if (pin == 7) pulsePin7();
- if (pin == 8) pulsePin8();
- if (pin == 9) pulsePin9();
- }
- //Pulse functions for pin 1
- function pulsePin1()
- {
- triggersPin[1] = triggersPin[1] - 1;
- imp.wakeup(triggerPulseDuration, offPin1);
- hardware.pin1.write(invertPins - 1);
- if (triggersPin[1] > 0) imp.wakeup(triggersDelay[1], pulsePin1);
- }
- function offPin1()
- {
- hardware.pin1.write(invertPins - 0);
- }
- //Pulse functions for pin 2
- function pulsePin2()
- {
- triggersPin[2] = triggersPin[2] - 1;
- imp.wakeup(triggerPulseDuration, offPin2);
- hardware.pin2.write(invertPins - 1);
- if (triggersPin[2] > 0) imp.wakeup(triggersDelay[2], pulsePin2);
- }
- function offPin2()
- {
- hardware.pin2.write(invertPins - 0);
- }
- //Pulse functions for pin 5
- function pulsePin5()
- {
- triggersPin[5] = triggersPin[5] - 1;
- imp.wakeup(triggerPulseDuration, offPin5);
- hardware.pin5.write(invertPins - 1);
- if (triggersPin[5] > 0) imp.wakeup(triggersDelay[5], pulsePin5);
- }
- function offPin5()
- {
- hardware.pin5.write(invertPins - 0);
- }
- //Pulse functions for pin 7
- function pulsePin7()
- {
- triggersPin[7] = triggersPin[7] - 1;
- imp.wakeup(triggerPulseDuration, offPin7);
- hardware.pin7.write(invertPins - 1);
- if (triggersPin[7] > 0) imp.wakeup(triggersDelay[7], pulsePin7);
- }
- function offPin7()
- {
- hardware.pin7.write(invertPins - 0);
- }
- //Pulse functions for pin 8
- function pulsePin8()
- {
- triggersPin[8] = triggersPin[8] - 1;
- imp.wakeup(triggerPulseDuration, offPin8);
- hardware.pin8.write(invertPins - 1);
- if (triggersPin[8] > 0) imp.wakeup(triggersDelay[8], pulsePin8);
- }
- function offPin8()
- {
- hardware.pin8.write(invertPins - 0);
- }
- 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);
- //Get where t for trigger is set in the string
- local modeTrigger = value.find("t", 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 over, or is 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);
- //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"));
- }
- //If modeState was not entered, then we might want to use modeTrigger instead
- //so if modeTrigger is over, or is 0 this is what we are going to use
- else if (modeTrigger >= 0)
- {
- //Get where d for delay is set in the string
- local delay = value.find("d", 0);
- //If a delay is set, we need to get the number between t and d in the string
- if (delay >= 0)
- {
- //Get the amount of pulses we want to make
- modeTrigger = value.slice(modeTrigger+1, delay);
- }
- else //IF not, we can just get it form t and to the end of the string
- {
- //Get the amount of pulses we want to make
- modeTrigger = value.slice(modeTrigger+1);
- }
- //Convert to integer
- modeTrigger = modeTrigger.tointeger();
- //If this pin is already pulsing, say so and stop
- if (triggersPin[pin] > 0)
- {
- if (modeTrigger <= 0)
- {
- triggersPin[pin] = 0;
- server.log("Pin " + pin + " has been stopped.")
- return;
- }
- else
- {
- server.log("Pin " + pin + " is already pulsing, it should be done in " + (triggersPin[pin] * triggersDelay[pin]) + " second(s), if you want to stop it pulsing set 't' to zero");
- return;
- }
- }
- //Set amount of pulses we need to make in the array for later use
- triggersPin[pin] = modeTrigger;
- //If we are told to do no pulses, we can just stop here and say so
- if (modeTrigger < 1)
- {
- server.log("Can't pulse " + pin + " zero times");
- return;
- }
- //If a delay is set, we need to store that in the array for later use
- if (delay >= 0)
- {
- //Get the value from the string
- delay = value.slice(delay+1);
- //Convert to float so we can have delays less than a second long
- delay = delay.tofloat();
- //Save the delay in the array
- triggersDelay[pin] = delay;
- //Show what we are going to do to which pin
- server.log("Pulsing pin " + pin + ", " + modeTrigger + " time(s) with " + delay + "s delay");
- }
- else
- {
- triggersDelay[pin] = triggerPulseDuration/2;
- //Show what we are going to do to which pin
- server.log("Pulsing pin " + pin + ", " + modeTrigger + " time(s)");
- }
- //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);
- startPulses(pin);
- }
- //Turn the ACT led on
- hardware.pin9.write(0);
- //Schedule ACT led to be turned off in 0.1 second
- imp.wakeup(0.1, 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.03a", [handleInput], [outputVoltage, outputSignal]);
- // Set pin 9 to output for the ACT led
- //hardware.pin9.configure(DIGITAL_OUT_OD_PULLUP);
- hardware.pin9.configure(PWM_OUT, 1.0/500.0, 1.0);
- //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