18/05/2019, 16:56
Problema per inviare un messaggio dal Server al Client.
In pratica ho creato unserver (in python) sul Raspberry, ed il Client (in Java) con Android Studio. Cioe' ho fatto una piccolissima app che mi accende da remoto un LED in raspberry. L'accensione funziona perfettamente ma adesso non riesco a fare in modo che il Server mandi un qualsiasi messaggio al Client in modo di avere conferma che il LED e' stato acceso correttamente.
Per favore qualcuno potrebbe spiegarmi come fare? sia lato server (python) che Client (Java)\
Ecco qui sotto i codi Server / Client
*****************Server (Python)********************************
import socket
import RPi.GPIO as GPIO
from time import sleep
running=True
pp=True
listensocket=socket.socket()
Port=5000
maxCOnnection=999
IP="192.168.1.7"
message=''
def inAscolto():
global running
global clientsocket
global messaggio
print("Sertver started at " + IP + " on port " + str(Port))
listensocket.bind(('',Port))
listensocket.listen(maxCOnnection)
while running:
print("Waiting for input..")
(clientsocket, address) = listensocket.accept()
pp=True
print("New connection made!")
elabora()
def elabora():
global pp
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17,GPIO.OUT)
message=clientsocket.recv(1024).decode()
print(message)
#data = clientsocket.recv(1024)
#clientsocket.sendall(data)
print("Message sent")
if message == "AA":
GPIO.output(17,GPIO.HIGH)
sleep(5)
GPIO.output(17,GPIO.LOW)
message=""
pp=False
clientsocket.close()
else:
print("Niente")
clientsocket.close()
#running=False
if __name__ == '__main__' :
inAscolto()
*****************Client(Java)********************************
public class MainActivity extends AppCompatActivity {
Button accendi;
Button spegni;
TextView testo;
String prova2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
accendi=(Button)findViewById(R.id.btnAccendi);
spegni=(Button)findViewById(R.id.btnSpegni);
testo=(TextView)findViewById(R.id.txt1);
accendi.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
send sendcode=new send();
testo.setText(prova2);
sendcode.execute();
}
});
}
class send extends AsyncTask<Void,Void,Void>{
Socket s;
PrintWriter pw;
@Override
protected Void doInBackground(Void... params) {
try{
s=new Socket("192.168.1.7",5000);
pw=new PrintWriter(s.getOutputStream());
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
prova2 = br.readLine();
pw.write("AA");
pw.flush();
pw.close();
}catch (UnknownHostException e){
System.out.println("Fail_1");
e.printStackTrace();
}
catch (IOException e){
System.out.println("Fail_2");
e.printStackTrace();
}
return null;
}
}
In pratica ho creato unserver (in python) sul Raspberry, ed il Client (in Java) con Android Studio. Cioe' ho fatto una piccolissima app che mi accende da remoto un LED in raspberry. L'accensione funziona perfettamente ma adesso non riesco a fare in modo che il Server mandi un qualsiasi messaggio al Client in modo di avere conferma che il LED e' stato acceso correttamente.
Per favore qualcuno potrebbe spiegarmi come fare? sia lato server (python) che Client (Java)\
Ecco qui sotto i codi Server / Client
*****************Server (Python)********************************
import socket
import RPi.GPIO as GPIO
from time import sleep
running=True
pp=True
listensocket=socket.socket()
Port=5000
maxCOnnection=999
IP="192.168.1.7"
message=''
def inAscolto():
global running
global clientsocket
global messaggio
print("Sertver started at " + IP + " on port " + str(Port))
listensocket.bind(('',Port))
listensocket.listen(maxCOnnection)
while running:
print("Waiting for input..")
(clientsocket, address) = listensocket.accept()
pp=True
print("New connection made!")
elabora()
def elabora():
global pp
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17,GPIO.OUT)
message=clientsocket.recv(1024).decode()
print(message)
#data = clientsocket.recv(1024)
#clientsocket.sendall(data)
print("Message sent")
if message == "AA":
GPIO.output(17,GPIO.HIGH)
sleep(5)
GPIO.output(17,GPIO.LOW)
message=""
pp=False
clientsocket.close()
else:
print("Niente")
clientsocket.close()
#running=False
if __name__ == '__main__' :
inAscolto()
*****************Client(Java)********************************
public class MainActivity extends AppCompatActivity {
Button accendi;
Button spegni;
TextView testo;
String prova2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
accendi=(Button)findViewById(R.id.btnAccendi);
spegni=(Button)findViewById(R.id.btnSpegni);
testo=(TextView)findViewById(R.id.txt1);
accendi.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
send sendcode=new send();
testo.setText(prova2);
sendcode.execute();
}
});
}
class send extends AsyncTask<Void,Void,Void>{
Socket s;
PrintWriter pw;
@Override
protected Void doInBackground(Void... params) {
try{
s=new Socket("192.168.1.7",5000);
pw=new PrintWriter(s.getOutputStream());
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
prova2 = br.readLine();
pw.write("AA");
pw.flush();
pw.close();
}catch (UnknownHostException e){
System.out.println("Fail_1");
e.printStackTrace();
}
catch (IOException e){
System.out.println("Fail_2");
e.printStackTrace();
}
return null;
}
}