Advertisement
bld

Untitled

bld
May 5th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.83 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 like this:
  7.  
  8. example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=pin,1|mode,s|state,1
  9. This would do the same as writing hardware.pin1.write(1);
  10.  
  11. example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=pin,5|mode,s|state,0
  12. This would do the same as writing hardware.pin1.write(0);
  13.  
  14. To initate a row of pulses, do as above, but like this:
  15.  
  16. example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=pin,1|mode,t|pulses,10|delay,1
  17. This would make pin 1 pulse 10 times with 1 second delay
  18.  
  19. example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=pin,5|mode,t|pulses,30|delay,0.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. // *** IMPORTANT ***
  26. // This defines the node ID, get that from the database controlling the site
  27. noteID <- 1;
  28.  
  29. // Only used to toggle between the + and - symbol
  30. lastCheckin <- 0;
  31.  
  32. //We need this for having an output with a message
  33. local outputMessage = OutputPort("Message", "string");
  34.  
  35. //We need this for having an output with a pin statuses
  36. local outputPinStatus = OutputPort("Pin status", "string");
  37.  
  38. //1 = allow sending a message
  39. debounceWifiWarning <- 1;
  40.  
  41. //Trigger where a warning will be send if it goes below
  42. wifiSignalWarning <- -69
  43.  
  44. //Sending signal strength and heartbeat back to the planner
  45. function checkIn()
  46. {
  47. //Return the signal to be shown in planner with toggling +/- sign to indicate it being alive
  48. server.show("Signal: " + imp.rssi() + "dBm " + (lastCheckin?"+":"-"));
  49.  
  50. //Toggle so the other symbol will be displayed next time
  51. lastCheckin = 1-lastCheckin;
  52.  
  53. //Run this function again in 5 seconds
  54. imp.wakeup(5.0, checkIn);
  55.  
  56. //Use PWM to make the led blink lower for its heartbeat
  57. hardware.pin9.write(0.8);
  58.  
  59. //Turn the led off again after 0.05 second
  60. imp.wakeup(0.05, actOff);
  61.  
  62. if (imp.rssi() < wifiSignalWarning && debounceWifiWarning == 1)
  63. {
  64. outputMessage.set("Low WiFi signal. (" + imp.rssi() + "dBm)");
  65. debounceWifiWarning = 0;
  66.  
  67. //Only allow one warning per 30 minutes
  68. imp.wakeup(30*60, enableWifiWarning);
  69. }
  70. }
  71.  
  72. function enableWifiWarning()
  73. {
  74. debounceWifiWarning = 1;
  75. }
  76.  
  77. //Turn the ACT led off
  78. function actOff()
  79. {
  80. hardware.pin9.write(1);
  81. }
  82.  
  83. //Array of pins, the na entry is not used for anything else than changing range to match pin numbers
  84. local channelPin = ["na", hardware.pin1, hardware.pin2, "na", "na", hardware.pin5, "na", hardware.pin7, hardware.pin8, hardware.pin9];
  85.  
  86. //The duration a trigger pulse lasts
  87. triggerPulseDuration <- 0.2 ;
  88.  
  89. //If the state of the pulses needs to be inverted
  90. invertPins <- 0;
  91.  
  92. //Array of remaining trigger pulses each pin got left to do
  93. local triggersPin = ["na", 0, 0, "na", "na", 0, "na", 0, 0, 0];
  94.  
  95. //Array of the delay between each trigger pulse for each pin
  96. local triggersDelay = ["na", 0, 0, "na", "na", 0, "na", 0, 0, 0];
  97.  
  98. function startPulses(pin)
  99. {
  100. if (pin == 1) pulsePin1();
  101. if (pin == 2) pulsePin2();
  102. if (pin == 5) pulsePin5();
  103. if (pin == 7) pulsePin7();
  104. if (pin == 8) pulsePin8();
  105. if (pin == 9) pulsePin9();
  106. }
  107.  
  108. //Pulse functions for pin 1
  109. function pulsePin1()
  110. {
  111. triggersPin[1] = triggersPin[1] - 1;
  112. imp.wakeup(triggerPulseDuration, offPin1);
  113. hardware.pin1.write(invertPins - 1);
  114.  
  115. if (triggersPin[1] > 0) imp.wakeup(triggersDelay[1], pulsePin1);
  116. }
  117.  
  118. function offPin1()
  119. {
  120. hardware.pin1.write(invertPins - 0);
  121. }
  122.  
  123. //Pulse functions for pin 2
  124. function pulsePin2()
  125. {
  126. triggersPin[2] = triggersPin[2] - 1;
  127. imp.wakeup(triggerPulseDuration, offPin2);
  128. hardware.pin2.write(invertPins - 1);
  129.  
  130. if (triggersPin[2] > 0) imp.wakeup(triggersDelay[2], pulsePin2);
  131. }
  132.  
  133. function offPin2()
  134. {
  135. hardware.pin2.write(invertPins - 0);
  136. }
  137.  
  138. //Pulse functions for pin 5
  139. function pulsePin5()
  140. {
  141. triggersPin[5] = triggersPin[5] - 1;
  142. imp.wakeup(triggerPulseDuration, offPin5);
  143. hardware.pin5.write(invertPins - 1);
  144.  
  145. if (triggersPin[5] > 0) imp.wakeup(triggersDelay[5], pulsePin5);
  146. }
  147.  
  148. function offPin5()
  149. {
  150. hardware.pin5.write(invertPins - 0);
  151. }
  152.  
  153. //Pulse functions for pin 7
  154. function pulsePin7()
  155. {
  156. triggersPin[7] = triggersPin[7] - 1;
  157. imp.wakeup(triggerPulseDuration, offPin7);
  158. hardware.pin7.write(invertPins - 1);
  159.  
  160. if (triggersPin[7] > 0) imp.wakeup(triggersDelay[7], pulsePin7);
  161. }
  162.  
  163. function offPin7()
  164. {
  165. hardware.pin7.write(invertPins - 0);
  166. }
  167.  
  168. //Pulse functions for pin 8
  169. function pulsePin8()
  170. {
  171. triggersPin[8] = triggersPin[8] - 1;
  172. imp.wakeup(triggerPulseDuration, offPin8);
  173. hardware.pin8.write(invertPins - 1);
  174.  
  175. if (triggersPin[8] > 0) imp.wakeup(triggersDelay[8], pulsePin8);
  176. }
  177.  
  178. function offPin8()
  179. {
  180. hardware.pin8.write(invertPins - 0);
  181. }
  182.  
  183. //Temperature average start
  184. hardware.pin8.configure(ANALOG_IN);
  185.  
  186. //Hold the index for the last value we updated
  187. temperatureIndex <- 0;
  188.  
  189. //Create the array and fill it with zeros
  190. local temperatureArray = array(20, [0]);
  191.  
  192. // Reading the TMP36GT9 temperature sensor
  193. // get the raw voltage value from temp sensor (0-65535)
  194. // in this case that needs mapping to the range 0-3.3v
  195. local reading = hardware.pin8.read();
  196. local tempTemp;
  197.  
  198. // get the ratio
  199. local ratio = 65535.0 / reading;
  200.  
  201. // make units milivolts and get voltage we can work with
  202. //local voltage = 3300 / ratio;
  203. local voltage = (hardware.voltage()*1000) / ratio;
  204.  
  205. // get temperature in degrees Celsius
  206. tempTemp = (voltage - 500) / 10.0;
  207.  
  208. local i = 0;
  209. for(i = 0; i < temperatureArray.len(); i++)
  210. {
  211. temperatureArray[i] = tempTemp;
  212. }
  213.  
  214. function getSetTemp()
  215. {
  216. hardware.pin8.configure(ANALOG_IN);
  217. // Reading the TMP36GT9 temperature sensor
  218. // get the raw voltage value from temp sensor (0-65535)
  219. // in this case that needs mapping to the range 0-3.3v
  220. local reading = hardware.pin8.read();
  221. local tempTemp;
  222.  
  223. // get the ratio
  224. local ratio = 65535.0 / reading;
  225.  
  226. // make units milivolts and get voltage we can work with
  227. //local voltage = 3300 / ratio;
  228. local voltage = (hardware.voltage()*1000) / ratio;
  229.  
  230. // get temperature in degrees Celsius
  231. tempTemp = (voltage - 500) / 10.0;
  232.  
  233. temperatureArray[temperatureIndex] = tempTemp;
  234.  
  235. local countTemp = 0;
  236.  
  237. for(i = 0; i < temperatureArray.len(); i++)
  238. {
  239. countTemp = countTemp + temperatureArray[i];
  240. //server.log(i + " " + temperatureArray[i]);
  241. }
  242.  
  243. temperatureIndex++;
  244.  
  245. if (temperatureIndex >= temperatureArray.len())
  246. {
  247. temperatureIndex = 0;
  248. }
  249.  
  250. return countTemp/temperatureArray.len();
  251. }
  252.  
  253. //Temperature average stop
  254.  
  255. class handleInput extends InputPort
  256. {
  257. constructor()
  258. {
  259. base.constructor();
  260. }
  261.  
  262. function set(value)
  263. {
  264. //Split the message we got in
  265. local inputString = split(value, "|");
  266.  
  267. //Hold pin number
  268. local pin = -1;
  269.  
  270. //s = fixed pin state / t = trigger sending pulses
  271. local pinMode = "";
  272.  
  273. //Hold pin state
  274. local pinState = -1;
  275.  
  276. local pinPulses = 0;
  277.  
  278.  
  279. //Hold the delay for the trigger mode, if no delay is set this will be the delay used
  280. local pulseDelay = triggerPulseDuration/2;
  281.  
  282. foreach(sub in inputString)
  283. {
  284. local tempString = split(sub, ",");
  285.  
  286. //This was pin number, set this as an integer
  287. if (tempString[0] == "pin") pin = tempString[1].tointeger();
  288.  
  289. //Found mode, save this as a string / s = permanent pin state / t = trigger sending pulses
  290. if (tempString[0] == "mode") pinMode = tempString[1].tolower();
  291.  
  292. //Find what we are going to do with the pin in fixed pin state and save it as an integer
  293. if (tempString[0] == "state") { pinState = tempString[1].tointeger(); pinState = invertPins-pinState; }
  294.  
  295. //Found delay, save this as a float so we can go below 1 second
  296. if (tempString[0] == "pulses") pinPulses = tempString[1].tointeger();
  297.  
  298. //Found delay, save this as a float so we can go below 1 second
  299. if (tempString[0] == "delay") pulseDelay = tempString[1].tofloat();
  300.  
  301. tempString = {};
  302. }
  303.  
  304. //Empty the array again to save memory
  305. inputString = {};
  306.  
  307. //If the target pin is not set, say so and stop here.
  308. if (pin < 0)
  309. {
  310. server.log("Pin number seems to be missing.");
  311. return;
  312. }
  313.  
  314. //If the pin mode is not set, say so and stop here.
  315. if (pinMode == "")
  316. {
  317. server.log("Pin mode seems to be missing.");
  318. return;
  319. }
  320.  
  321. //Check if the target pin is one that can be used for this
  322. //Pin 9 are capable of this too, but it is used as activity led
  323. if (pin == "3" || pin == "4" || pin == "6" || pin == "9")
  324. {
  325. server.log("Only pin 1, 2, 5, 7, and 8 can be used.");
  326. return;
  327. }
  328.  
  329. //If pinMode is s, it means we want the function that sets the pin to a permanent state
  330. if (pinMode == "s")
  331. {
  332. //Set only the target pin to an output without pullup, even if it is already done once before
  333. channelPin[pin].configure(DIGITAL_OUT);
  334.  
  335. //Set the pin state
  336. channelPin[pin].write(pinState);
  337.  
  338. //Show what we just did to which pin
  339. server.log("Relay " + pin + ": " + (pinState?"open":"closed"));
  340. }
  341.  
  342. //If pinMode is t, is what we are going to use instead
  343. else if (pinMode == "t")
  344. {
  345. //If this pin is already pulsing, say so and stop
  346. if (triggersPin[pin] > 0)
  347. {
  348. if (pinPulses <= 0)
  349. {
  350. triggersPin[pin] = 0;
  351. server.log("Pin " + pin + " has been stopped.")
  352. return;
  353. }
  354. else
  355. {
  356. 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");
  357. return;
  358. }
  359. }
  360.  
  361. //Set amount of pulses we need to make in the array for later use
  362. triggersPin[pin] = pinPulses;
  363.  
  364. //If we are told to do no pulses, we can just stop here and say so
  365. if (pinPulses <= 0)
  366. {
  367. server.log("Can't pulse " + pin + " zero times");
  368. return;
  369. }
  370.  
  371. //Save the delay in the array
  372. triggersDelay[pin] = pulseDelay;
  373.  
  374. server.log("Pulsing pin " + pin + ", " + pinPulses + " time(s) with " + pulseDelay + "s delay");
  375.  
  376. //Set only the target pin to an output without pullup, even if it is already done once before
  377. channelPin[pin].configure(DIGITAL_OUT);
  378.  
  379. startPulses(pin);
  380. }
  381.  
  382. //Turn the ACT led on
  383. hardware.pin9.write(0);
  384.  
  385. //Schedule ACT led to be turned off in 0.1 second
  386. imp.wakeup(0.1, actOff);
  387. }
  388. }
  389.  
  390. //We need this for having an output for the voltage the imp is running on
  391. local outputVoltage = OutputPort("Voltage (V)", "number");
  392.  
  393. //We need this for having an output with the wifi signal strength
  394. local outputSignal = OutputPort("Signal (dBm)", "number");
  395.  
  396. //We need this for having an output with the free memory
  397. local outputMemory = OutputPort("Memory (bytes)", "number");
  398.  
  399. //We need this for having an output with the temperature
  400. local outputTemp = OutputPort("Temperature (c)", "number");
  401.  
  402. //We need this for having an output with the outputSync
  403. local outputSync = OutputPort("Syncronize", "string");
  404.  
  405. //We need this for having the temperature send to the database
  406. local outputDBtemp = OutputPort("DB output temperature", "string");
  407.  
  408. function getStats()
  409. {
  410. //Set outputVoltage to the current voltage
  411. outputVoltage.set(hardware.voltage());
  412.  
  413. //Set outputSignal to the current signal strength
  414. outputSignal.set(imp.rssi());
  415.  
  416. //Set outputMemory to the current amount of free bytes
  417. outputMemory.set(imp.getmemoryfree());
  418.  
  419. //Read temperature sensor
  420. local tempTemp = getSetTemp();
  421. //server.log("Temperature: " + tempTemp + "c")
  422. outputTemp.set(tempTemp);
  423. outputDBtemp.set(noteID + "|8|" + tempTemp);
  424.  
  425. //Do this again in 60 seconds
  426. imp.wakeup(60, getStats);
  427. }
  428.  
  429. function syncronize()
  430. {
  431. server.log("Syncronizing...")
  432. outputSync.set(noteID);
  433. //Do this again in 60 minutes
  434. imp.wakeup(60*60, syncronize);
  435. }
  436.  
  437. //Configure imp
  438. imp.configure("H.A.Node V0.031a", [handleInput], [outputVoltage, outputSignal, outputMemory, outputTemp, outputMessage, outputSync, outputDBtemp]);
  439.  
  440. // Set pin 9 to output with PWM for the ACT led
  441. hardware.pin9.configure(PWM_OUT, 1.0/500.0, 1.0);
  442.  
  443. //Make sure ACT led is off and do the first check-in
  444. actOff();
  445. checkIn();
  446.  
  447. //Collect stats
  448. getStats();
  449.  
  450. //Syncronize imp with database
  451. syncronize();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement