Demo entry 6352091

server

   

Submitted by anonymous on Mar 23, 2017 at 11:43
Language: Python. Code size: 1.5 kB.

#import necessary package
import socket
import time
import sys
import RPi.GPIO as GPIO
import spidev

HOST_IP = "192.168.0.53"  #define host ip: Raspberry Pi's IP
HOST_PORT = 8888          #define port
print("Starting socket: TCP...")

#a1.create socket object:socket=socket.socket(family,type)
socket_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("TCP server listen @ %s:%d!" %(HOST_IP, HOST_PORT) )
host_addr = (HOST_IP, HOST_PORT) 

#a2.bind socket to IP addr:socket.bind(address)
socket_tcp.bind(host_addr)

#b.listen connection request:socket.listen(backlog)
socket_tcp.listen(1)

#c.waite for client:connection,address=socket.accept()
socket_con, (client_ip, client_port) = socket_tcp.accept()
print("Connection accepted from %s." %client_ip)

spi = spidev.SpiDev()    #open SPIdev to read digital value
spi.open(0,0)            #prerate port 0 and switch slave 0
GPIO.output(26,GPIO.LOW) #turn on the ADC

#d.processing data exchange
while True:
    try:
        f = open('/tmp/log.txt','a') #open log.txt write by adding 
        resp = spi.xfer2([0x00])     #reading the ADC 
        f.write(str(resp))           #write each value as string in log.txt
        print resp                   #display the value on Raspberry Pi
        socket_con.send(str(resp))   #send value to client
        time.sleep(3)                #sleep the next cycle
#e.closing connection
    except Exception:
        socket_con.close()
        sys.exit(1)
    
          

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).