Advertisement
blddk

H.A.Node 0.2a

Feb 2nd, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.12 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.     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. */
  15.  
  16. // Only used to toggle between the + and - symbol
  17. lastCheckin <- 0;
  18.  
  19. //Sending signal strength and heartbeat back to the planner
  20. function checkIn()
  21. {
  22.     server.show("Signal: " + imp.rssi() + "dBm " + (lastCheckin?"+":"-"));
  23.    
  24.     //Toggle so the other symbol will be displayed next time
  25.     lastCheckin = 1-lastCheckin;
  26.    
  27.     //Run this function again in 5 seconds
  28.     imp.wakeup(5.0, checkIn);
  29. }
  30.  
  31. //Turn the ACT led off
  32. function actOff()
  33. {
  34.     hardware.pin9.write(1);
  35. }
  36.  
  37. //Array of pins, the na entry is not used for anything else than changing range to match pin numbers
  38. local channelPin = ["na", hardware.pin1, hardware.pin2, "na", "na", hardware.pin5, "na", hardware.pin7, hardware.pin8, hardware.pin9];
  39.  
  40. class handleInput extends InputPort
  41. {
  42.     constructor()
  43.     {
  44.         base.constructor();
  45.     }
  46.  
  47.     function set(value)
  48.     {
  49.         //Get where in the string we set the target pin
  50.         local pin = value.find("p", 0);
  51.        
  52.         //Get where s for state is set in the string
  53.         local modeState = value.find("s", 0);
  54.        
  55.         //If the target pin is not set, say so and stop here.
  56.         if (pin < 0)
  57.         {
  58.             server.log("Pin number seems to be missing.");
  59.             return;
  60.         }
  61.        
  62.         //Set pin to the target pin number
  63.         pin = value.slice(pin+1,pin+2);
  64.        
  65.         //Convert to integer
  66.         pin = pin.tointeger();
  67.        
  68.         //Check if the target pin is one that can be used for this
  69.         //Pin 9 are capable of this too, but it is used as activity led
  70.         if (pin == "3" || pin == "4" || pin == "6" || pin == "9")
  71.         {
  72.             server.log("Only pin 1, 2, 5, 7, and 8 can be used.");
  73.             return;
  74.         }
  75.        
  76.         //If modeState is not 0 it means we want the function that sets the pin to a permanent state
  77.         if (modeState != 0)
  78.         {
  79.             //Get the state we want to set the pin into
  80.             modeState = value.slice(modeState+1,modeState+2);
  81.            
  82.             //Convert to integer
  83.             modeState = modeState.tointeger();
  84.            
  85.             //Set only the target pin to an output without pullup, even if it is already done once before
  86.             channelPin[pin].configure(DIGITAL_OUT_OD_PULLUP);
  87.            
  88.             //Set the pin state
  89.             channelPin[pin].write(modeState);
  90.            
  91.             //Show what we just did to which pin
  92.             server.log("Relay " + pin + ": " + (modeState?"open":"closed"));
  93.         }
  94.        
  95.         //Turn the ACT led on
  96.         hardware.pin9.write(0);
  97.        
  98.         //Schedule ACT led to be turned off in 0.15 seconds
  99.         imp.wakeup(0.15, actOff);
  100.     }
  101. }
  102.  
  103.  
  104. //We need these for having an output for the voltage the imp is running on
  105. local outputVoltage = OutputPort("Voltage (V)", "number");
  106. local voltageV;
  107.  
  108. //We need these for having an output with the wifi signal strength
  109. local outputSignal = OutputPort("Signal (dBm)", "number");
  110. local signaldBm;
  111.  
  112. function getStats()
  113. {
  114.     //Set outputVoltage to the current voltage
  115.     outputVoltage.set(hardware.voltage());
  116.    
  117.     //Set outputSignal to the current signal strength
  118.     outputSignal.set(imp.rssi());
  119.    
  120.     //Do this again in 60 seconds
  121.     imp.wakeup(60, getStats);
  122. }
  123.  
  124.  
  125. //Configure imp
  126. imp.configure("H.A.Node V0.02a", [handleInput], [outputVoltage, outputSignal]);
  127.  
  128. // Set pin 9 to output for the ACT led
  129. hardware.pin9.configure(DIGITAL_OUT_OD_PULLUP);
  130.  
  131. //Make sure ACT led is off and do the first check-in
  132. actOff();
  133. checkIn();
  134.  
  135. //Collect stats
  136. getStats();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement