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 like this:
- example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=pin,1|mode,s|state,1
- This would do the same as writing hardware.pin1.write(1);
- example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=pin,5|mode,s|state,0
- This would do the same as writing hardware.pin1.write(0);
- To initate a row of pulses, do as above, but like this:
- example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=pin,1|mode,t|pulses,10|delay,1
- This would make pin 1 pulse 10 times with 1 second delay
- example: https://api.electricimp.com/v1/0000000000000000/0000000000000000?value=pin,5|mode,t|pulses,30|delay,0.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
- */
- // *** IMPORTANT ***
- // This defines the node ID, get that from the database controlling the site
- noteID <- 1;
- // Only used to toggle between the + and - symbol
- lastCheckin <- 0;
- //We need this for having an output with a message
- local outputMessage = OutputPort("Message", "string");
- //We need this for having an output with a pin statuses
- local outputPinStatus = OutputPort("Pin status", "string");
- //1 = allow sending a message
- debounceWifiWarning <- 1;
- //Trigger where a warning will be send if it goes below
- wifiSignalWarning <- -69
- //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.05 second
- imp.wakeup(0.05, actOff);
- if (imp.rssi() < wifiSignalWarning && debounceWifiWarning == 1)
- {
- outputMessage.set("Low WiFi signal. (" + imp.rssi() + "dBm)");
- debounceWifiWarning = 0;
- //Only allow one warning per 30 minutes
- imp.wakeup(30*60, enableWifiWarning);
- }
- }
- function enableWifiWarning()
- {
- debounceWifiWarning = 1;
- }
- //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.2 ;
- //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);
- }
- //Temperature average start
- hardware.pin8.configure(ANALOG_IN);
- //Hold the index for the last value we updated
- temperatureIndex <- 0;
- //Create the array and fill it with zeros
- local temperatureArray = array(20, [0]);
- // Reading the TMP36GT9 temperature sensor
- // get the raw voltage value from temp sensor (0-65535)
- // in this case that needs mapping to the range 0-3.3v
- local reading = hardware.pin8.read();
- local tempTemp;
- // get the ratio
- local ratio = 65535.0 / reading;
- // make units milivolts and get voltage we can work with
- //local voltage = 3300 / ratio;
- local voltage = (hardware.voltage()*1000) / ratio;
- // get temperature in degrees Celsius
- tempTemp = (voltage - 500) / 10.0;
- local i = 0;
- for(i = 0; i < temperatureArray.len(); i++)
- {
- temperatureArray[i] = tempTemp;
- }
- function getSetTemp()
- {
- hardware.pin8.configure(ANALOG_IN);
- // Reading the TMP36GT9 temperature sensor
- // get the raw voltage value from temp sensor (0-65535)
- // in this case that needs mapping to the range 0-3.3v
- local reading = hardware.pin8.read();
- local tempTemp;
- // get the ratio
- local ratio = 65535.0 / reading;
- // make units milivolts and get voltage we can work with
- //local voltage = 3300 / ratio;
- local voltage = (hardware.voltage()*1000) / ratio;
- // get temperature in degrees Celsius
- tempTemp = (voltage - 500) / 10.0;
- temperatureArray[temperatureIndex] = tempTemp;
- local countTemp = 0;
- for(i = 0; i < temperatureArray.len(); i++)
- {
- countTemp = countTemp + temperatureArray[i];
- //server.log(i + " " + temperatureArray[i]);
- }
- temperatureIndex++;
- if (temperatureIndex >= temperatureArray.len())
- {
- temperatureIndex = 0;
- }
- return countTemp/temperatureArray.len();
- }
- //Temperature average stop
- class handleInput extends InputPort
- {
- constructor()
- {
- base.constructor();
- }
- function set(value)
- {
- //Split the message we got in
- local inputString = split(value, "|");
- //Hold pin number
- local pin = -1;
- //s = fixed pin state / t = trigger sending pulses
- local pinMode = "";
- //Hold pin state
- local pinState = -1;
- local pinPulses = 0;
- //Hold the delay for the trigger mode, if no delay is set this will be the delay used
- local pulseDelay = triggerPulseDuration/2;
- foreach(sub in inputString)
- {
- local tempString = split(sub, ",");
- //This was pin number, set this as an integer
- if (tempString[0] == "pin") pin = tempString[1].tointeger();
- //Found mode, save this as a string / s = permanent pin state / t = trigger sending pulses
- if (tempString[0] == "mode") pinMode = tempString[1].tolower();
- //Find what we are going to do with the pin in fixed pin state and save it as an integer
- if (tempString[0] == "state") { pinState = tempString[1].tointeger(); pinState = invertPins-pinState; }
- //Found delay, save this as a float so we can go below 1 second
- if (tempString[0] == "pulses") pinPulses = tempString[1].tointeger();
- //Found delay, save this as a float so we can go below 1 second
- if (tempString[0] == "delay") pulseDelay = tempString[1].tofloat();
- tempString = {};
- }
- //Empty the array again to save memory
- inputString = {};
- //If the target pin is not set, say so and stop here.
- if (pin < 0)
- {
- server.log("Pin number seems to be missing.");
- return;
- }
- //If the pin mode is not set, say so and stop here.
- if (pinMode == "")
- {
- server.log("Pin mode seems to be missing.");
- return;
- }
- //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 pinMode is s, it means we want the function that sets the pin to a permanent state
- if (pinMode == "s")
- {
- //Set only the target pin to an output without pullup, even if it is already done once before
- channelPin[pin].configure(DIGITAL_OUT);
- //Set the pin state
- channelPin[pin].write(pinState);
- //Show what we just did to which pin
- server.log("Relay " + pin + ": " + (pinState?"open":"closed"));
- }
- //If pinMode is t, is what we are going to use instead
- else if (pinMode == "t")
- {
- //If this pin is already pulsing, say so and stop
- if (triggersPin[pin] > 0)
- {
- if (pinPulses <= 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] = pinPulses;
- //If we are told to do no pulses, we can just stop here and say so
- if (pinPulses <= 0)
- {
- server.log("Can't pulse " + pin + " zero times");
- return;
- }
- //Save the delay in the array
- triggersDelay[pin] = pulseDelay;
- server.log("Pulsing pin " + pin + ", " + pinPulses + " time(s) with " + pulseDelay + "s delay");
- //Set only the target pin to an output without pullup, even if it is already done once before
- channelPin[pin].configure(DIGITAL_OUT);
- 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 this for having an output for the voltage the imp is running on
- local outputVoltage = OutputPort("Voltage (V)", "number");
- //We need this for having an output with the wifi signal strength
- local outputSignal = OutputPort("Signal (dBm)", "number");
- //We need this for having an output with the free memory
- local outputMemory = OutputPort("Memory (bytes)", "number");
- //We need this for having an output with the temperature
- local outputTemp = OutputPort("Temperature (c)", "number");
- //We need this for having an output with the outputSync
- local outputSync = OutputPort("Syncronize", "string");
- //We need this for having the temperature send to the database
- local outputDBtemp = OutputPort("DB output temperature", "string");
- function getStats()
- {
- //Set outputVoltage to the current voltage
- outputVoltage.set(hardware.voltage());
- //Set outputSignal to the current signal strength
- outputSignal.set(imp.rssi());
- //Set outputMemory to the current amount of free bytes
- outputMemory.set(imp.getmemoryfree());
- //Read temperature sensor
- local tempTemp = getSetTemp();
- //server.log("Temperature: " + tempTemp + "c")
- outputTemp.set(tempTemp);
- outputDBtemp.set(noteID + "|8|" + tempTemp);
- //Do this again in 60 seconds
- imp.wakeup(60, getStats);
- }
- function syncronize()
- {
- server.log("Syncronizing...")
- outputSync.set(noteID);
- //Do this again in 60 minutes
- imp.wakeup(60*60, syncronize);
- }
- //Configure imp
- imp.configure("H.A.Node V0.031a", [handleInput], [outputVoltage, outputSignal, outputMemory, outputTemp, outputMessage, outputSync, outputDBtemp]);
- // Set pin 9 to output with PWM for the ACT led
- 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();
- //Syncronize imp with database
- syncronize();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement