Turn pins high/low remotely for multiple pins

I am working on a home automation node where the imp will be the unit controlling it. I have been looking over multiple other projects using the imp to control one output, but I need it to control multiple.

So far I got this:
`lastCheckin <- 0;

function checkIn()
server.show(“Signal: " + imp.rssi() + “dBm " + (lastCheckin?”+”:"-"));

lastCheckin = 1-lastCheckin;

imp.wakeup(5.0, checkIn);


function actOff()

class MsgInput extends InputPort

function set(value)
    if (value =="p1s1")
        server.log("Relay 1: closed");
    else if (value =="p1s0")
        server.log("Relay 1: open");
    imp.wakeup(0.15, actOff);


imp.configure(“H.A.Node V0.01a”, [MsgInput], []);
This can control pin 1 by sending ?value=p1s1 or p1s0, but is there a way to make it do it for multiple, without having to make a ‘if’ for each of them?

The previous node I made used an arduino and RF to send the string and it then pulled the pin number and state out of it and processed it.

Another reason for this is that I also use it for a garage door, so instead of s for state, I can send it t for trigger, where it then will just pulse once instead of only switching. The last version I made of the arduino powered then also had the option to send it how many pulses with variable interval between.

I havent actually tried it (and have no access to an IMP so I can test the code), but I think it should be possible to do something like this:

Inside the class:

OutPin = hardware.pin; 

    // You only need to configure pins once

function set(pin){
    // Missing some sort of validation of the pins written to.
    server.log(format("Relay %d: closed"),pin);

function clr(pin){
    // Missing some sort of validation of the pins written to.
    server.log(format("Relay %d: open"),pin);


I don’t remember the exact format of the concatenating of the string and the pin number, but I’m sure I saw it done some time ago. Hopefully others can correct me.

If I totally miss-understood your question, and that you actually want to write to (or read from) more then one pin at one time, then I’d very much like to know, too.

You did understand the question right, but it can’t be done the way you do it, but I can make a function that takes care of that instead. The biggest problem is the thing you also left out, which is how to get the pin number and state out of the string supplied by ‘value’ from http-in.

MikeyDK I drive multiple outputs on April type board simply by sending 10 for o/p1 Off 11 for On. I’m not that bright so I’ve ended up with the code below. I use JQuery mobile as the front End. I’m also about to try some relays with 3v coils but will switch 8amp. Hope it helps.

`// Pins array channel 1 is channelPin[0] in array
local channelPin = [ hardware.pin1, hardware.pin2, hardware.pin5, hardware.pin7, hardware.pin8, hardware.pin9 ];

// Input from web service
class pwsIn extends InputPort
name = "wsIn"
type = “number”

function set(value)

// Digital outputs
    case 10: // Ch1 off
    case 11: // Ch1 on

    case 20: // Ch2 off
    case 21: // Ch2 on

    case 30: // Ch3 off
    case 31: // Ch3 on


This would work too, but it isn’t what I am looking for either This also requires me to make one case for each pin and each function for that. What I am looking for is a way to make one case for each function, then be able to apply that function to the set pin.

And about your 3v relay, make sure the pin you are connecting it to can supply the amount of current it needs, and also be sure to have a diode in so the coil in the relay won’t destroy your imp.

Normally you would use the imp to only control and transistor, which would then drive the relay coil. (this one too needs the diode for protecting the transistor)

Ok the way I do that is to build up a string ch1:method:value|ch2:… like poor mans Json then use string split but as you can see I not programmer, noted the rules on the relay been using a 24v ones via a trans but want to see if I can do a battery powered one for very occasional use to reset external devices.

As I already wrote in the initial post, I got a way of setting the settings in a string already. What I need is a way to get the right things out of the string. I can’t find any documentation on how to search in a string and get the index of what is searched for, or how to handle sub-string and pull the found data out.

if output = 0x00 -> all off
if output = 0x01 -> only pin 1 = on
if output = 0x02 -> only pin 2 = on
if output = 0x03 -> only pin 2 and pin 2 = on (=0x01+ 0x02)
if output = 0x04 -> only pin 3 = on
if output = 0x05 -> only pin 1 and pin 3 = on (=0x01+ 0x04)
if output = 0x06 -> only pin 2 and pin 3 = on (=0x02+ 0x04)

if output = 0x08 -> only pin 4 = on

if output = 0x10 -> only pin 5 = on

if output = 0x20 -> only pin 6 = on

if output = 0x3f -> all pins = on (= 0x01 + 0x01 + 0x04 + 0x08 +0x10 + 0x20 )

so you can set the state of all pins with one variable (output) in hex value

What you describe also requires one for each pin, even one for each pin configuration. That is way more than I want to put in there, plus it would be a even bigger mess when I get around to also make it do just pulses.

Again, what I need is a way to search a string to get something out of it. I don’t need a way to turn pins on and off because that is only the final thing of the command. I can not make a ‘if’ for each combination, because later it will be impossible. The reason it will be impossible is because I could end up sending p1t5d60 which then would be pin 1 sending a pulse 5 times with 60 seconds delay between those pulses could be everything from 1 to 1000 each with a delay from maybe 1 second to an hour.

I already got this system working with some arduino node’s I made, but I need a way to do it with an Imp. If it is impossible to search in a string (which all other languages I know are capable of), would it then be possible to send more than only ‘value’ to the imp, so I could send ?value=pin&state=1/0 or ?value=pin&trigger=5&delay=60

Okay, Mikey.

What you need seems to be this part of the Squirrel 3.0 refs.:


returns the string length

converts the string to integer and returns it

converts the string to float and returns it

returns a section of the string as new string. Copies from start to the end (not included). If start is negative the index is calculated as length + start, if end is negative the index is calculated as length + end. If end is omitted end is equal to the string length.

search a sub string(substr) starting from the index startidx and returns the index of its first occurrence. If startidx is omitted the search operation starts from the beginning of the string. The function returns null if substr is not found.

Read more here: http://squirrel-lang.org/doc/squirrel3.html

Hope this is more help.

Awesome! Seems like that is the documentation I was looking for. :slight_smile:

YESSSSSSSSS! This was EXACTLY what I was looking for! This seems to work just fine, just a bit different than php but can get it to do what I want! :slight_smile:

But looks like tointeger() is not implemented?

2/2/2013 23.02.48: ERROR: the index ‘tointeger’ does not exist
2/2/2013 23.02.48: ERROR: at set:48

The squirrel documentation really needs some work to be easily accessible for all of us.

I’m looking forward to see the squirrel + IMP specifics documented in a way similar to http://dk.php.net/quickref.php

I did a search on the forum for “tointeger”, and found the following from Peter:

Yes, this isn't very well-documented in Squirrel. The string.tointeger method is for strings such as "2012"; it's the equivalent of C's atoi() function. The thing you want, to access the byte value of part of a string, is indexing using square brackets [], again like in C, though I can't find it mentioned in the Squirrel manual.

So I guess you have to calculate it yourself. :frowning:

Or perhaps it should just be used like this:

a = "2345"; b = a.tointeger();

Again, I havent tried it - just guessing.

Yep, you were right!

Version 0.2a: http://pastebin.com/A8H9Yy3n

Documentation on how to use it is in the top of the code.

Next target is to have the trigger function in it too.

Nice to know.

Thanks for sharing. :smiley:

And here it is: http://pastebin.com/sqZNALd0

This can now pulse all pins with a individual specified amount of pulses along with an individual set delay.

Documentation can be found at the top of this code too. :slight_smile:

Two things that might be helpful!

There’s a nice clean example of defining a class and instantiating it with different constructor arguments right here: http://devwiki.electricimp.com/doku.php?id=example:inputport. You might want to use a similar strategy to allow you to just instantiate class objects to manage whatever pins you wind up needing.

Secondly, we’ve got a new feature coming along shortly called agents - they’re a cloud-side partner to the imp firmware. You write agents in squirrel, just like your firmware, but there’s a nice HTTP api for the agents, and they’ll be able to do something else that’ll help you out - they can parse JSON.

Since JSON parsing isn’t there yet, I recently had to manually tear up a response string from wunderground in squirrel and build up a string to go to an 8-char display I built. Lines 25 to 40 here should give you a little boost until the parsing is ready.