View Single Post
  #1   Spotlight this post!  
Unread 05-08-2016, 22:01
Technologyman00's Avatar
Technologyman00 Technologyman00 is offline
Registered User
AKA: Collin
FRC #1792 (Round Table Robotics)
Team Role: Mechanical
 
Join Date: Nov 2015
Rookie Year: 2015
Location: Oak Creek WI
Posts: 23
Technologyman00 is an unknown quantity at this point
Python Socket Between Devices

I am currently working on controlling a RPI with a Socket Server and Client but when I do it on my PC using 127.0.0.1 but now it wont work on my home network. I know the IP address of the Devices I'm using but it never sees the other device.

PLZ HELP

Server:
Code:
#!/usr/bin/env python

import socket
#from neopixel import *

# Create NeoPixel object with appropriate configuration.
#strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL,
#                          LED_STRIP)
# Intialize the library (must be called once before other functions).
#strip.begin()

# Server Variables
TCP_IP = '0.0.0.0'
print "Host: ", TCP_IP
TCP_PORT = 5800
BUFFER_SIZE = 20  # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

# Neopixel Variables
LED_COUNT = 12  # Number of LED pixels.
LED_PIN = 18  # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5  # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 255  # Set to 0 for darkest and 255 for brightest

#Color Variables
color = ""
Red = "0"
Green = "255"
Blue = "0"

conn, addr = s.accept()
print 'Connection address:', addr
while 1:
    data = conn.recv(BUFFER_SIZE)
    if not data: break
    color = data
    print "Color Received:", data
    conn.send(data)  # echo
conn.close()

print "Color: ", color

#Color Choosing
Red, Green, Blue = color.split(",")
print "Red: ", Red
print "Green: ", Green
print "Blue: ", Blue
#for i in range(0,LED_COUNT):
    #strip.setPixelColor(i, color(Red, Green, Blue))
#strip.show()
Client
Code:
#!/usr/bin/env python

import socket
import sys


TCP_IP = '192.168.1.125'
TCP_PORT = 5800
BUFFER_SIZE = 1024
nodata = 0
tryip = 1
good = False

while True:

    if tryip == 255:
        print "Can't Find Server..."
        TCP_IP = raw_input("Enter a Custom IP: ")
        if TCP_IP == "":
            print "No Data Sent. Server Should Default Green."
            break
        print "Trying Ip: ", TCP_IP, "\r"

    if tryip == 256:
        print "Custom IP FAILED. No Data Sent. Server SHOULD Default Green."
        break

    if tryip >= 2 and tryip < 255:
        TCP_IP = '10.17.92.'
        TCP_IP += str(tryip)
        print "Trying Ip: ", TCP_IP, "\r"

    while not good:
        MESSAGE = raw_input("Please enter a color: ")
        if MESSAGE == "":
            MESSAGE = "0,255,0"

        if "," in MESSAGE:
            print "you entered", MESSAGE
            good = True
            break
        else:
            print "Not a Color."

    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind((TCP_IP, 5801))
        s.connect((TCP_IP, TCP_PORT))
        s.send(MESSAGE)
        data = s.recv(BUFFER_SIZE)
        s.close()
    except Exception:
        nodata = 1
        if tryip == 1:
            print "Server not on Localhost. Probably Connected to FMS."
        tryip += 1
    else:
        nodata = 0

    if nodata != 1:
        print "sent data:", data
        break
Reply With Quote