Академический Документы
Профессиональный Документы
Культура Документы
port
[3
CONNECTER SPECIFICATIONS
5V and 2A micro USB socket powered board
Ethernet socket
data
[4
SOME DIY PROJECTS USING
RASPBERRY
PiPhone
PI
Voice-activated coffee machine
[5
]
[6
SOME DIY PROJECTS USING
RASPBERRY PI
Pi-rate Radio
[7
]
[8
USAGE OF HEADER FILE
RPI_SERVER.PY
USED HEADER FILES
requests
http://www.python-requests.org/en/latest/ : Library for making requests to server
uuid
Library for getting Universally Unique Identifier
socket,fcntl,struct
socket: Library to provides access to socket interface
fcntl: Performs file control and I/O control on file descriptors. [Applicable to Unix
Systems Only]
struct: Functions for converting between strings of bytes and native Python data
types like strings and numbers
USED GLOBAL VARIABLES
1. uid=uuid.getnode()
# Stores Universally Unique Identifier of the device obtained from uuid library
2. ip='10.25.32.184'
#Default IP of the server
3. identity=0
#Id of the raspberry in the server
UTILITY FUNCTIONS
#To be called only if the server ip changes
def set_server_ip(ip_add):
global ip #Tells compiler that this ip is the ip referenced above in global scope
ip=ip_addr
SERVER parameter={uid':uid,'ip':get_ip('eth0')}
#Server takes parameters uuid and ip to register/update a device details in the server.
post_response=requests.post(url='http://'+ ip +'/embedded/register.php',data=parameter)
#Using post methode offered by request library to write values to the server
Usage : rpi_server.register_pi() after importing the header file that contains all
2. REGISTER PORTS AS
INPUTS/OUTPUTS
#Registers rasberry pi ports in the server for use as input/output
#0 for output and 1 for input in the direction field
def register_port(port,direction):
global identity
parameter={'id':identity,'port':port,'io':direction}
#server takes parameters id,port and io to register/update a port in the server
post_response=requests.post(url='http://'+ ip +'/embedded/reg_port.php'
,data=parameter)
if(post_response.text=='OK'):
return True
return False
Usage : rpi_server.register_port(7,0) after importing the header for using pin 7 as
output port
READ PORT DATA FROM
SERVER
#Reads port data from server using unique id of the device and port number
#can be used to read port data from a different device using id of that device
def read_data(ids,port):
parameter={'id':ids,'port':port}
post_response=requests.post(url='http://'+ ip
+'/embedded/retrieve_data.php'
,data=parameter)
if(post_response.text=='error'):
print "Error Reading value !!!!\nNo record exists for the given port number"
else:
return post_response.text
Usage : rpi_server.read_data(rpi_server.get_id(),7) to read data of port 7 as stored in
the database server
WRITE DATA TO SERVER
#Sends data of the given port of the raspberry of given id to the
server
def write_data(ids,port,data):
parameter={'id':ids,'port':port,'data':data}
post_response=requests.post(url='http://'+ ip
+'/embedded/insert_data.php',data=parameter)
print post_response.text
if(post_response.text=='OK'):
return True
return False
Usage : rpi_server.write_data(rpi_server.get_id(),7) to write data of port 7 to database
server