Objective: To assemble a garage door opener with an Electric Imp.

baasopchoppyΑσφάλεια

5 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

64 εμφανίσεις

Objective: To assemble a garage door opener with an Electric Imp.

Prelim: Blink up for the Electric Imp should have been completed.

Step 1:
Constructing the circuit





Step 2: Coding

The following code is for an elect
ric imp garage door opener. This

garage door

opener uses

sensors

to
report a status of the

door being open or closed.

We need it to work without considering the sensors, so edit the code to where it will still work.

https://docs.google.com/document/d/137NiuX6lBMkOMMQGegPJsmspyvG3nN3DF7H5GnsFa
-
s/edit?usp=sharing

// Garage Door Controller

server.log("Sesame Started");



// Sensors are active low

doorEnabled

<
-

0;

openSensor

<
-

1;

closedSensor <
-

1;

tempSensor

<
-

0;




//
--------------------------------------------------------------------------------------------------------

// Ports for communicating to external servers

//
------------------------------------------------------
--------------------------------------------------

local cosmClosedPort

= OutputPort("cosmGarageClosedSensor");

local cosmOpenPort

= OutputPort("cosmGarageOpenSensor");

local garageStatusPort

= OutputPort("garageStatusPort","string");



//
--------
------------------------------------------------------------------------------------------------

// HTML Messages from the iPhone get passed to this port

//
--------------------------------------------------------------------------------------------------------

class GaragePort extends InputPort

{


type = "command"


name = "garageIn"


function set(value) {




// This

code checking for values is not needed. Basically every time this funciton is


// called, all you need to do is pulse pin 7 which closes the relay and then


// releases it simulating a button push. I was curious to see how squirrel compares



// strings....




if( value )


{


server.log("HTTP: " + value );


local startIdx = value.find("Button");


server.log("IDX: " + startIdx );


if( startIdx != null )


{


s
erver.log("Pulse Start");


hardware.pin7.write(1);


imp.sleep(1.0);


hardware.pin7.write(0);


server.log("Pulse End");




}


}


}

}



//
--------------------------------------------------------------------------------------------------------

// Called periodically to poll the status of the door sensors. The door can be in one of four
states

// Open

-

"open sensor" is triggered

// Closi
ng
-

Previous state was Open and no sensors are triggered

// Closed

-

"closed sensor" is triggered

// Opening
-

Previous state was Closed and no sensors are triggered

// Status is sent to a HTTP Request port configured to call an ASP.NET page which will l
og
the data into

// a SQL database and send an SMS message to a bunch of cellphones.

//
--------------------------------------------------------------------------------------------------------

function checkSensorStates()

{


local portString;




temp
Sensor = hardware.pin8.read();




if( tempSensor != openSensor )


{


openSensor= hardware.pin8.read();


if( openSensor == 0 )


{


portString = format("%s,%d,%d,%s", hardware.getimpeeid(), openSensor,
closedSensor, "Open"
);


}


else


{


portString = format("%s,%d,%d,%s", hardware.getimpeeid(), openSensor,
closedSensor, "Closing" );


}




garageStatusPort.set( portString );


server.log( portString );


}




tempSensor

= hardware.pin9.read();




if( tempSensor != closedSensor )


{


closedSensor= hardware.pin9.read();


if( closedSensor == 0 )


{


portString = format("%s,%d,%d,%s", hardware.getimpeeid(), openSensor,
closedSensor, "Closed"

);


}


else


{


portString = format("%s,%d,%d,%s", hardware.getimpeeid(), openSensor,
closedSensor, "Opening" );


}




garageStatusPort.set( portString );


server.log( portString );


}




cosmClosedP
ort.set( closedSensor );


cosmOpenPort.set( openSensor );






// Wake up in 3 seconds and check the status again. This polling continues


// until power is removed!


//server.log("Sensor Status: Open Sensor: " + openSensor

+ " Closed Sensor: " +
closedSensor );


imp.wakeup(3, checkSensorStates );



}




//
--------------------------------------------------------------------------------------------------------

// Main configuration statement required for all Imp programs

//
--------------------------------------------------------------------------------------------------------

imp.configure("Sesame", [GaragePort], [garageStatusPort,cosmClosedPort,cosmOpenPort]
);




//
--------------------------------------------------------------------------------------------------------

// Configure pins. PIN7 = Relay Control, PIN8 = Open Sensor, PIN 9 = Closed Sensor

//
----------------------------------------------------------------
----------------------------------------

hardware.pin7.configure(DIGITAL_OUT);

hardware.pin8.configure(DIGITAL_IN);

hardware.pin9.configure(DIGITAL_IN);



// Make sure pin is low
-

relay off

hardware.pin7.write(0);



//
-------------------------------------
-------------------------------------------------------------------

// Kick start sensor polling.

//
--------------------------------------------------------------------------------------------------------

checkSensorStates();



Step 3: Firmware and the pla
nner



For the garage door application, the planner is configured by adding HTTP IN, HTTP REQUEST
and two COSM Nodes. All you need to get the door working is the HTTP IN node. The other
nodes are there to communicate door status.



To configure a node, click on the slider on the top right of the rectangle and enter the
information in the resulting pop
-
up window. The HTTP IN does not need configuration. The
information displayed is needed by the HTML Client app to make a call into the

firmware to
communicate the button press action. For COSM, you need a node for each data item since
each COSM datastream logs a single value. Since we have two switches that need to be
logged, we create 2 nodes that connect to two datastreams. The API Key

and Feed ID will be
provided when you create the streams in COSM.

The HTTP Request node is the most important one for me because that sends the SMS status
messages any time the door sensors change state. The node is configured to point to an
external Web
Server that hosts the GarageUpdater.aspx page. This page updates the SQL
Database and sends the SMS message to the phone. This will be described in a later step.