- Home /
Send data from unity to python socket
Hi, i want to send data from unity to RPi 3 with Socket. i created an app with two joystick but i don't know how to send the value of the joystick to the RPi 3 Python Socket.
Code Unity C# socket:
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Net.Sockets;
public class ClientSocket : MonoBehaviour
{
bool socketReady = false;
TcpClient mySocket;
public NetworkStream theStream;
StreamWriter theWriter;
StreamReader theReader;
public String Host = "192.168.1.70";
public Int32 Port = 12345;
void Start()
{
setupSocket();
}
public void setupSocket()
{ // Socket setup here
try
{
mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
theReader = new StreamReader(theStream);
socketReady = true;
}
catch (Exception e)
{
Debug.Log("Socket error:" + e); // catch any exceptions
}
}
}
Code Unity C# Joystick:
using UnityEngine;
using UnityEngine.EventSystems;
public class FixedJoystick : Joystick
{
Vector2 joystickPosition = Vector2.zero;
private Camera cam = new Camera();
void Start()
{
joystickPosition = RectTransformUtility.WorldToScreenPoint(cam, background.position);
}
public override void OnDrag(PointerEventData eventData)
{
Vector2 direction = eventData.position - joystickPosition;
inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ? direction.normalized : direction / (background.sizeDelta.x / 2f);
ClampJoystick();
handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
}
public override void OnPointerDown(PointerEventData eventData)
{
OnDrag(eventData);
}
public override void OnPointerUp(PointerEventData eventData)
{
inputVector = Vector2.zero;
handle.anchoredPosition = Vector2.zero;
}
}
Code Python RPi 3:
import socket
backlog = 1
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('192.20.20.2', 50001))
s.listen(backlog)
try:
print ("is waiting")
client, address = s.accept()
while 1:
data = client.recv(size)
if data:
print (data)
except:
print("closing socket")
client.close()
s.close()
Answer by xxmariofer · Feb 21, 2019 at 12:52 PM
hello, whats your problem? the phyton code not openning? the unity not connecting? the message not being send? you have gave no explanation. but for start i would suggest sending data since you have set up the socket but not used it.
public void TextMessage(string message)
{
if (connected)
{
theWriter.Write(message);
theWriter.Flush();
}
}
are your ip and ports set up correctly? since in the script they are different, did you change them in the editor?
yes i changed them, i use 192.178.1.70 and port 84. (i forgot to change the ip and port on the code i posted here).
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Hand/Finger detection in unity to place AR objects 0 Answers
How to send Slider Information over websocket? 1 Answer
Chat system is not working 1 Answer