Computational Cameras + Redial Midterm with Crys Moore

Meet our camera Bob..

This week we successfully established a phone connection and interact with Bob. In an active phone connection with the camera, dialing 1 would move it to the left, 2 would set it in the center and 3 would move it to the right. There are a lotta moving pieces in this project and the flow of data is currently setup like this:

Phone –> Asterisk (Phone Server) –> AGI Script/Sinatra/Database <-- Arduino/Ethernet Shield (set up as web client) --> Camera mounted on servo

At some point we ran into a wall, and we couldn’t get to arduino to access information on a live connection. After the whole weekend of debugging, this was redemption..

Thank you Zeven Rodriguez for letting us use the ethernet shield and walking through the process and many thanks to Gavin Hackeling, Matt Richardson and Peter Darche for code references.

Code for this project

ARDUINO CODE

——————————————————————————————-

#include
#include
#include
#include
Servo myservo;

boolean Connected = false;
char message[500];

int keyPressed = 0;
int counter = 2000;
int responseSize = 0;
boolean Read = true;

boolean movedRight = false;
boolean movedCenter = false;
boolean movedLeft = false;

byte mac[] = {
0×02, 0xAA, 0xBB, 0xCC, 0×00, 0x1A };
IPAddress server(128,122,157,182); //stu.itp.nyu.edu
EthernetClient client;

void setup() {
Serial.begin(9600);
myservo.attach(9);

// start the serial library:
Serial.begin(9600);
// start the Ethernet connection
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// give the Ethernet shield a second to initialize:
delay(1000);
}

void connectRead() {
if(!Connected){

Serial.println(“connecting…”);

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println(“connected”);
// Make a HTTP request:
client.println(“GET /~db2497/sinatra/main_agi/digitinfo HTTP/1.1″);
client.println(“Host: stu.itp.nyu.edu”);
client.println();
Connected = true;
// responseSize = 0;

}

else {
Serial.println(“connection failed”);
}

}
}

void readPage() {
// memset( message, 0, 1000 );

if(Read){

if (client.available()) {
// Serial.print(“reading.. “);

char c = client.read();

// Serial.print(c);
message[responseSize] +=c;
responseSize++;

if (c == ‘*’) {
Serial.println();
Serial.print(“size of file is “);
Serial.println(responseSize);
for (int i = 0; i < responseSize; i++){
// Serial.print(message[i]);
if (message[i] == '^'){

if(message[i+1] == '^'){

keyPressed = int(message[i+2]);
Serial.print("keypressed is ");
Serial.println(keyPressed);
Read = false;

// }
}
}
}

}
if (!Read) {
// responseSize=0;

client.stop();
Serial.println("disconnecting.");
client.flush();

delay (2000);

// responseSize =0;

Serial.println();
Read = true;
Connected = false;
// responseSize=0;

}

}
}
}

void moveCamera(){

if (keyPressed == 49 && movedLeft == false) {
myservo.write(179);
Serial.println("Attempting to move Servo Left..");
movedLeft = true;
}

else if (keyPressed == 50 && movedCenter == false ) {

myservo.write(90);
Serial.println("Attempting to move Servo Center..");
movedCenter = true;
// keyPressed = 0;
}

else if (keyPressed == 51 && movedRight == false) {
myservo.write(0);
Serial.println("Attempting to move Servo Right..");
movedRight = true;

}

message[responseSize] = 0;
// Serial.println("Clearing");

}

void loop()
{
// delay (3000);
connectRead();
readPage();
moveCamera();

// responseSize=0;

}

-------------------------------------------------------------------------------------------

Web/Sinatra Code - App.rb
------------------------------------------------------------------------------------------

#!/usr/bin/ruby
require 'sinatra'
require 'rubygems'
require 'data_mapper'

#------- set up database

DataMapper.setup(:default, {
:adapter => ‘mysql’,
:host => ‘localhost’,
:username => ‘db2497′ ,
:password => ‘aylb+nbomwy’,
:database => ‘db2497′})

#keypress model for database interaction
class Keypress
include DataMapper::Resource
property :id, Serial
property :last_digit, String, :required => true
property :last_callerid, String
end

# Main route – this is the form where we take the input
get ‘/’ do
erb :index
end

get ‘/digitinfo’ do
keypress = Keypress.get(1)
# “#{keypress.last_callerid},#{keypress.last_digit}”
“^^#{keypress.last_digit}*”
end

——————————————————————————————-

Web/Sinatra Code – index.erb
——————————————————————————————



Call x100:

Button:

none.


——————————————————————————————-

Phone/Asterisk Code – DialPlan : extention.conf
——————————————————————————————

[db2497]

exten => s,1,Wait(1)
exten => s,n,Playback(hello-world)
exten => s,n,Goto(db2497_ajax,s,1)
exten => s,n,Wait(10)

[db2497_bridge]
exten => _1NXXNXXXXXX,1,Set(CALLERID(num)=16466429290)
exten => _1NXXNXXXXXX,n,Dial(SIP/itp_jnctn/${EXTEN},30,r);

[db2497_ajax]
exten => s,1,AGI(/home/db2497/sinatra/main_agi/digit_writer.rb)
;run a script to reset the database on hangup (This isn’t agi, just a script)
exten => h,1,System(/home/db2497/sinatra/main_agi/digit_reset.rb)

——————————————————————————————

Phone/Asterisk Code – Ruby Script: digit_writer.rb
——————————————————————————————

#!/usr/bin/ruby
require ‘rubygems’
require ‘ruby-agi’
require ‘data_mapper’ #for mysql database integration

agi = AGI.new #new agi object

#set up database

DataMapper.setup(:default, {
:adapter => ‘mysql’,
:host => ‘localhost’,
:username => ‘db2497′ ,
:password => ‘aylb+nbomwy’,
:database => ‘db2497′})

#keypress model for database interaction
class Keypress
include DataMapper::Resource
property :id, Serial
property :last_digit, String, :required => true
property :last_callerid, String
end

# Automatically create the tables if they don’t exist
DataMapper.auto_upgrade!
# Finish setup
DataMapper.finalize
keypress = Keypress.first_or_create({:id => 1} , { :last_digit => ‘-’ })
keypress.update(:last_callerid => agi.callerid)
# start agi scripting
agi.stream_file(“vm-extension”)
while true
result = agi.wait_for_digit(-1) # wait forever
if result.digit
keypress.update(:last_digit => result.digit)
end
end

——————————————————————————————

Phone/Asterisk Code – Ruby Script: digit_reset.rb
——————————————————————————————

#!/usr/bin/ruby
require ‘rubygems’
require ‘data_mapper’ #for mysql database integration

#set up database

DataMapper.setup(:default, {
:adapter => ‘mysql’,
:host => ‘localhost’,
:username => ‘db2497′ ,
:password => ‘aylb+nbomwy’,
:database => ‘db2497′})

#keypress model for database interaction
class Keypress
include DataMapper::Resource
property :id, Serial
property :last_digit, String, :required => true
property :last_callerid, String
end

# Automatically create the tables if they don’t exist
DataMapper.auto_upgrade!
# Finish setup
DataMapper.finalize
keypress = Keypress.first_or_create({:id => 1} , { :last_digit => ‘-’ })
keypress.update(:last_callerid => ‘ – ‘, :last_digit => ‘ – ‘)

——————————————————————————————

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>