parseINT32
Posts: 15
Joined: Fri Mar 01, 2013 11:22 am

Anti-theft system

Sat May 04, 2013 9:12 pm

When the door opens (in this case on switch is pressed), a python script will trigger the speaker using a relay and whereupon it'll contact a dedicated server that has installed Asterisk which allows to call to a PSTN line or whatever phone number...

A key-switch allow to enable/disable the system...

Now i'm looking for a way to enable or disable it through an android phone... my main problem is that my isp doesn't allow port forwarding... what can i do?


Video: http://www.youtube.com/watch?v=SW5IdKFRTd8

Image

danneman
Posts: 19
Joined: Thu Jun 21, 2012 9:57 am

Re: Anti-theft system

Sun May 05, 2013 6:50 pm

Hi.

Nice work.

1. One idea is to let your PI connect to a remote socket server. But that requires a remote webserver.

2. If you can't setup a remote socket server you can use an ordinary webserver and let your pi connect to your webserver every 1-5 second to look for enable/disable status. Then you can use any webbrowser to enable/disable your PI.

/Daniel

parseINT32
Posts: 15
Joined: Fri Mar 01, 2013 11:22 am

Re: Anti-theft system

Mon May 06, 2013 10:26 am

danneman wrote:Hi.

Nice work.

1. One idea is to let your PI connect to a remote socket server. But that requires a remote webserver.

2. If you can't setup a remote socket server you can use an ordinary webserver and let your pi connect to your webserver every 1-5 second to look for enable/disable status. Then you can use any webbrowser to enable/disable your PI.

/Daniel
Thanks for replying,
at the moment my pi is already contacting a web server when the switch is pressed, in fact a php page sends commands to asterisk through exec() function.

What you mean with socket server? What is the simplest way to get web page contents from the raspi using python?

danneman
Posts: 19
Joined: Thu Jun 21, 2012 9:57 am

Re: Anti-theft system

Mon May 06, 2013 6:44 pm

Hi.

A socket connection is basically a full duplex connection initiated by the klient. The client (your PI) connects to a socket server at startup and then the connection stays open, unless it is interrupted. But then you reconnect...

This gives you more real-time reaction when you enable/disable.

Read more about network sockets at wikipedia: http://en.m.wikipedia.org/wiki/Network_socket.

It is not so difficult But you probobly need full control of the server.

Google will give you tons of c++, .NET, python examples.

Good luck!

parseINT32
Posts: 15
Joined: Fri Mar 01, 2013 11:22 am

Re: Anti-theft system

Tue May 07, 2013 5:15 pm

I merged my search results to this:

Server:

Code: Select all

#!/usr/bin/python
# the server

from socket import *
import thread

def main():
	sock = socket(AF_INET, SOCK_STREAM)
	sock.bind(("PUBLICADDR", 20320))
	sock.listen(5)
	
	def handler(conn, addr):
		print addr, ' connected'
		conn.send('Welcome, client ' + str(addr) +  '\n')

		while 1:
			data = conn.recv(1024)
			if data :
				print addr, ': ', data    
				conn.send('Massage received')
			else:
				conn.send('chi bo tu?')

	while 1:
		conn, addr = sock.accept()
		thread.start_new_thread(handler, (conn, addr))
		
if __name__ == "__main__":
	main()
Client

Code: Select all

#!/usr/bin/python
# the client

from socket import *

def main():

	sock = socket(AF_INET, SOCK_STREAM)
	try:
		sock.connect(("PUBLICADDR", 20320))
	except:
		print '\nFATAL ERROR: Could not make connection'
		print 'No socket listening on default port\n'

	while 1:
		data = sock.recv(1024)
		print 'Server: ', data
		try:
			txt = raw_input('Insert text: ')
			sock.send(txt)
		except KeyboardInterrupt:  
			sock.close()
		

if __name__ == "__main__":
        main()
It's actually working, but how can i prevent that server stops to listening?

I have a doubt: once the client ends connection, server automatically stops the thread used by client? Should i stop it, or python will do it for me?

Return to “Automation, sensing and robotics”