Advertisement
bld

H.A.Node V0.3a

bld
Feb 4th, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.49 KB | None | 0 0
  1. /*
  2.     How to use this.
  3.    
  4.     To control your Electric Imp with this code you need to go into the planner and add a HTTP IN
  5.    
  6.     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]
  7.    
  8.     example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=p1s1
  9.     This would do the same as writing hardware.pin1.write(1);
  10.    
  11.     example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=p1s0
  12.     This would do the same as writing hardware.pin1.write(0);
  13.    
  14.     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)
  15.    
  16.     example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=p1t10d1
  17.     This would make pin 1 pulse 10 times with 1 second delay
  18.    
  19.     example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=p5t30d0.5
  20.     This would make pin 5 pulse 30 times with 0.5 second delay
  21.    
  22.     If a delay is not speficied half of the triggerPulseDuration will be used
  23.  
  24. */
  25.  
  26. // Only used to toggle between the + and - symbol
  27. lastCheckin <- 0;
  28.  
  29. //Sending signal strength and heartbeat back to the planner
  30. function checkIn()
  31. {
  32.     //Return the signal to be shown in planner with toggling +/- sign to indicate it being alive
  33.     server.show("Signal: " + imp.rssi() + "dBm " + (lastCheckin?"+":"-"));
  34.    
  35.     //Toggle so the other symbol will be displayed next time
  36.     lastCheckin = 1-lastCheckin;
  37.    
  38.     //Run this function again in 5 seconds
  39.     imp.wakeup(5.0, checkIn);
  40.    
  41.     //Use PWM to make the led blink lower for its heartbeat
  42.     hardware.pin9.write(0.8);
  43.    
  44.     //Turn the led off again after 0.1 second
  45.     imp.wakeup(0.1, actOff);
  46. }
  47.  
  48. //Turn the ACT led off
  49. function actOff()
  50. {
  51.     hardware.pin9.write(1);
  52. }
  53.  
  54. //Array of pins, the na entry is not used for anything else than changing range to match pin numbers
  55. local channelPin = ["na", hardware.pin1, hardware.pin2, "na", "na", hardware.pin5, "na", hardware.pin7, hardware.pin8, hardware.pin9];
  56.  
  57. //The duration a trigger pulse lasts
  58. triggerPulseDuration <- 0.1;
  59.  
  60. //If the state of the pulses needs to be inverted
  61. invertPins <- 0;
  62.  
  63. //Array of remaining trigger pulses each pin got left to do
  64. local triggersPin = ["na", 0, 0, "na", "na", 0, "na", 0, 0, 0];
  65.  
  66. //Array of the delay between each trigger pulse for each pin
  67. local triggersDelay = ["na", 0, 0, "na", "na", 0, "na", 0, 0, 0];
  68.  
  69. function startPulses(pin)
  70. {
  71.     if (pin == 1) pulsePin1();
  72.     if (pin == 2) pulsePin2();
  73.     if (pin == 5) pulsePin5();
  74.     if (pin == 7) pulsePin7();
  75.     if (pin == 8) pulsePin8();
  76.     if (pin == 9) pulsePin9();
  77. }
  78.  
  79. //Pulse functions for pin 1
  80. function pulsePin1()
  81. {
  82.     triggersPin[1] = triggersPin[1] - 1;
  83.     imp.wakeup(triggerPulseDuration, offPin1);
  84.     hardware.pin1.write(invertPins - 1);
  85.    
  86.     if (triggersPin[1] > 0) imp.wakeup(triggersDelay[1], pulsePin1);
  87. }
  88.  
  89. function offPin1()
  90. {
  91.     hardware.pin1.write(invertPins - 0);
  92. }
  93.  
  94. //Pulse functions for pin 2
  95. function pulsePin2()
  96. {
  97.     triggersPin[2] = triggersPin[2] - 1;
  98.     imp.wakeup(triggerPulseDuration, offPin2);
  99.     hardware.pin2.write(invertPins - 1);
  100.    
  101.     if (triggersPin[2] > 0) imp.wakeup(triggersDelay[2], pulsePin2);
  102. }
  103.  
  104. function offPin2()
  105. {
  106.     hardware.pin2.write(invertPins - 0);
  107. }
  108.  
  109. //Pulse functions for pin 5
  110. function pulsePin5()
  111. {
  112.     triggersPin[5] = triggersPin[5] - 1;
  113.     imp.wakeup(triggerPulseDuration, offPin5);
  114.     hardware.pin5.write(invertPins - 1);
  115.    
  116.     if (triggersPin[5] > 0) imp.wakeup(triggersDelay[5], pulsePin5);
  117. }
  118.  
  119. function offPin5()
  120. {
  121.     hardware.pin5.write(invertPins - 0);
  122. }
  123.  
  124. //Pulse functions for pin 7
  125. function pulsePin7()
  126. {
  127.     triggersPin[7] = triggersPin[7] - 1;
  128.     imp.wakeup(triggerPulseDuration, offPin7);
  129.     hardware.pin7.write(invertPins - 1);
  130.    
  131.     if (triggersPin[7] > 0) imp.wakeup(triggersDelay[7], pulsePin7);
  132. }
  133.  
  134. function offPin7()
  135. {
  136.     hardware.pin7.write(invertPins - 0);
  137. }
  138.  
  139. //Pulse functions for pin 8
  140. function pulsePin8()
  141. {
  142.     triggersPin[8] = triggersPin[8] - 1;
  143.     imp.wakeup(triggerPulseDuration, offPin8);
  144.     hardware.pin8.write(invertPins - 1);
  145.    
  146.     if (triggersPin[8] > 0) imp.wakeup(triggersDelay[8], pulsePin8);
  147. }
  148.  
  149. function offPin8()
  150. {
  151.     hardware.pin8.write(invertPins - 0);
  152. }
  153.  
  154. class handleInput extends InputPort
  155. {
  156.     constructor()
  157.     {
  158.         base.constructor();
  159.     }
  160.  
  161.     function set(value)
  162.     {
  163.         //Get where in the string we set the target pin
  164.         local pin = value.find("p", 0);
  165.        
  166.         //Get where s for state is set in the string
  167.         local modeState = value.find("s", 0);
  168.        
  169.         //Get where t for trigger is set in the string
  170.         local modeTrigger = value.find("t", 0);
  171.        
  172.        
  173.         //If the target pin is not set, say so and stop here.
  174.         if (pin < 0)
  175.         {
  176.             server.log("Pin number seems to be missing.");
  177.             return;
  178.         }
  179.        
  180.         //Set pin to the target pin number
  181.         pin = value.slice(pin+1,pin+2);
  182.        
  183.         //Convert to integer
  184.         pin = pin.tointeger();
  185.        
  186.         //Check if the target pin is one that can be used for this
  187.         //Pin 9 are capable of this too, but it is used as activity led
  188.         if (pin == "3" || pin == "4" || pin == "6" || pin == "9")
  189.         {
  190.             server.log("Only pin 1, 2, 5, 7, and 8 can be used.");
  191.             return;
  192.         }
  193.        
  194.         //If modeState is over, or is 0 it means we want the function that sets the pin to a permanent state
  195.         if (modeState >= 0)
  196.         {
  197.             //Get the state we want to set the pin into
  198.             modeState = value.slice(modeState+1);
  199.            
  200.             //Convert to integer
  201.             modeState = modeState.tointeger();
  202.            
  203.             //Set only the target pin to an output without pullup, even if it is already done once before
  204.             channelPin[pin].configure(DIGITAL_OUT_OD_PULLUP);
  205.            
  206.             //Set the pin state
  207.             channelPin[pin].write(modeState);
  208.            
  209.             //Show what we just did to which pin
  210.             server.log("Relay " + pin + ": " + (modeState?"open":"closed"));
  211.         }
  212.        
  213.         //If modeState was not entered, then we might want to use modeTrigger instead
  214.         //so if modeTrigger is over, or is 0 this is what we are going to use
  215.         else if (modeTrigger >= 0)
  216.         {
  217.             //Get where d for delay is set in the string
  218.             local delay = value.find("d", 0);
  219.            
  220.             //If a delay is set, we need to get the number between t and d in the string
  221.             if (delay >= 0)
  222.             {
  223.                 //Get the amount of pulses we want to make
  224.                 modeTrigger = value.slice(modeTrigger+1, delay);
  225.             }
  226.             else //IF not, we can just get it form t and to the end of the string
  227.             {
  228.                 //Get the amount of pulses we want to make
  229.                 modeTrigger = value.slice(modeTrigger+1);
  230.             }
  231.            
  232.             //Convert to integer
  233.             modeTrigger = modeTrigger.tointeger();
  234.            
  235.             //If this pin is already pulsing, say so and stop
  236.             if (triggersPin[pin] > 0)
  237.             {
  238.                 if (modeTrigger <= 0)
  239.                 {
  240.                     triggersPin[pin] = 0;
  241.                     server.log("Pin " + pin + " has been stopped.")
  242.                     return;
  243.                 }
  244.                 else
  245.                 {
  246.                     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");
  247.                     return;
  248.                 }
  249.             }            
  250.                        
  251.             //Set amount of pulses we need to make in the array for later use
  252.             triggersPin[pin] = modeTrigger;
  253.            
  254.             //If we are told to do no pulses, we can just stop here and say so
  255.             if (modeTrigger < 1)
  256.             {
  257.                 server.log("Can't pulse " + pin + " zero times");
  258.                 return;
  259.             }
  260.            
  261.             //If a delay is set, we need to store that in the array for later use
  262.             if (delay >= 0)
  263.             {
  264.                 //Get the value from the string
  265.                 delay = value.slice(delay+1);
  266.                
  267.                 //Convert to float so we can have delays less than a second long
  268.                 delay = delay.tofloat();
  269.                
  270.                 //Save the delay in the array
  271.                 triggersDelay[pin] = delay;
  272.                
  273.                 //Show what we are going to do to which pin
  274.                 server.log("Pulsing pin " + pin + ", " + modeTrigger + " time(s) with " + delay + "s delay");
  275.             }
  276.             else
  277.             {
  278.                 triggersDelay[pin] = triggerPulseDuration/2;
  279.                
  280.                 //Show what we are going to do to which pin
  281.                 server.log("Pulsing pin " + pin + ", " + modeTrigger + " time(s)");
  282.             }
  283.            
  284.             //Set only the target pin to an output without pullup, even if it is already done once before
  285.             channelPin[pin].configure(DIGITAL_OUT_OD_PULLUP);
  286.            
  287.             startPulses(pin);
  288.         }
  289.        
  290.         //Turn the ACT led on
  291.         hardware.pin9.write(0);
  292.        
  293.         //Schedule ACT led to be turned off in 0.1 second
  294.         imp.wakeup(0.1, actOff);
  295.     }
  296. }
  297.  
  298. //We need these for having an output for the voltage the imp is running on
  299. local outputVoltage = OutputPort("Voltage (V)", "number");
  300. local voltageV;
  301.  
  302. //We need these for having an output with the wifi signal strength
  303. local outputSignal = OutputPort("Signal (dBm)", "number");
  304. local signaldBm;
  305.  
  306. function getStats()
  307. {
  308.     //Set outputVoltage to the current voltage
  309.     outputVoltage.set(hardware.voltage());
  310.    
  311.     //Set outputSignal to the current signal strength
  312.     outputSignal.set(imp.rssi());
  313.    
  314.     //Do this again in 60 seconds
  315.     imp.wakeup(60, getStats);
  316. }
  317.  
  318.  
  319. //Configure imp
  320. imp.configure("H.A.Node V0.03a", [handleInput], [outputVoltage, outputSignal]);
  321.  
  322. // Set pin 9 to output for the ACT led
  323. //hardware.pin9.configure(DIGITAL_OUT_OD_PULLUP);
  324. hardware.pin9.configure(PWM_OUT, 1.0/500.0, 1.0);
  325.  
  326. //Make sure ACT led is off and do the first check-in
  327. actOff();
  328. checkIn();
  329.  
  330. //Collect stats
  331. getStats();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement