You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
def Temperature():
global temperature
enableNotify(0x13)
while True:
if p.waitForNotifications(1.0):
# handleNotification() 함수가 불린다.
disableNotify(0x13)
return temperature
데이터그램소켓 통신으로 안드로이드앱의 요청을 대기합니다.
def server():
global battery,humidity,temperature
#기본포트로설정
port = ECHO_PORT
# 소켓 생성 (UDP = SOCK_DGRAM, TCP = SOCK_STREAM)
s = socket(AF_INET, SOCK_DGRAM)
#포트설정
s.bind(('',port))
#쓰레드 동작 : 블루투스의 데이터를 지속적으로 받아옵니다.
t = threading.Thread(target=notify_thread)
t.start()
while True:
# 클라이언트로 메시지가 도착하면 다음 줄로 넘어가고
# 그렇지 않다면 대기(Blocking)
temp, addr = s.recvfrom(BUFSIZE)
Join_String = humidity +":"+ battery +":" +str(temperature)
Join_as_bytes = str.encode(Join_String)
s.sendto(Join_as_bytes,addr)
2. 라즈베리파이 서버 -> 안드로이드 앱
안드로이드 앱에서 라즈베리파이 서버에 데이터를 5초마다 요청하여 받아옵니다.
class SendData extends Thread{
public void run(){
try{
while(true) {
//UDP 통신용 소켓 생성
DatagramSocket socket = new DatagramSocket();
//서버 주소 변수
InetAddress serverAddr = InetAddress.getByName(sIP);
//보낼 데이터 생성
byte[] buf = ("send me data please ").getBytes();
//패킷으로 변경
DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, sPORT);
//패킷 전송!
socket.send(packet);
//데이터 수신 대기
socket.receive(packet);
//데이터 수신되었다면 문자열로 변환
msg2 = new String(packet.getData()); //습도
separate_msg2 = msg2.split(":");
msg2 = separate_msg2[0];
battery2 = separate_msg2[1];
temperature2 = separate_msg2[2];
if(!battery2.equals("loading") &&battery2.length()>2){
separate_battery2 = battery2.split("d");
battery2 = separate_battery2[0].replaceAll("\\s+","");
}
String temp="";
String temp2= separate_msg2[2].replaceAll("\\s+","");
if(temp2.length()>5) {
for (int i = 0; i < temp2.length(); i++) {
if (Character.isLetter((temp2.charAt(i)))) {
for (int j = 0; j < i; j++) {
temp += temp2.charAt(j);
}
break;
}
}
temperature2=temp;
}
else {
temperature2 = temp2;
}
Thread.sleep(5000); // 1/1000단위 1초 == 1000, 1분 == 60000
}
}catch (Exception e){
Log.v("tag :","오류");
}
}
}