<activity_main.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13dp"
android:text="Raspberry IP Address" />
<EditText
android:id="@+id/ipAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="192.168.0.138:21567" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="2dp"
android:text="On" />
<Button
android:id="@+id/btnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:text="Off" />
</LinearLayout>
</LinearLayout>
<MainActivity>
package com.example.automicapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
Button btnOn, btnOff;
EditText ipAddress;
// Socket myAppSocket = null;
public static String wifiModuleIP = "";
public static int wifiModulePort = 0;
public static String CMD = "0";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOn = findViewById(R.id.btnOn);
btnOff = findViewById(R.id.btnOff);
ipAddress = findViewById(R.id.ipAddress);
btnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getIPandPort();
CMD="O";
Socket_AsyncTask socket_on = new Socket_AsyncTask();
socket_on.execute();
}
});
btnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getIPandPort();
CMD="F";
Socket_AsyncTask socket_on = new Socket_AsyncTask();
socket_on.execute();
}
});
}
public void getIPandPort(){
String iPandPort = ipAddress.getText().toString();
Log.d("MY TEST","IP String: "+ iPandPort);
String temp[] = iPandPort.split(":");
wifiModuleIP = temp[0];
wifiModulePort = Integer.valueOf(temp[1]);
Log.d("MY TEST", "IP:"+wifiModuleIP);
Log.d("MY TEST", "PORT:"+wifiModulePort);
}
public class Socket_AsyncTask extends AsyncTask<Void,Void,Void> {
Socket socket;
@Override
protected Void doInBackground(Void... params) {
try{
InetAddress inetAddress = InetAddress.getByName(MainActivity.wifiModuleIP);
socket = new java.net.Socket(inetAddress, MainActivity.wifiModulePort);
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeBytes(CMD);
dataOutputStream.close();
socket.close();
}
catch (UnknownHostException e){e.printStackTrace();}
catch (IOException e){e.printStackTrace();}
return null;
}
}
}
<AndroidManifest.xml>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.automicapp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AutomicApp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
라즈베리파이 파이썬 파일
LEDControl.py
import RPi.GPIO as GPIO
import time
cur_led = 0
LED = 23
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED,GPIO.OUT)
def LedOn():
GPIO.output(LED, GPIO.HIGH)
cur_led = 1
def LedOff():
GPIO.output(LED, GPIO.LOW)
cur_led = 0
def close():
GPIO.cleanup()
if __name__ == '__main__':
setup()
pi_led_server.py
import LEDControl
from socket import *
from time import ctime
import RPi.GPIO as GPIO
LEDControl.setup()
ctrCmd = ['LedOn','LedOff']
HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
while True:
print('Waiting for connection')
tcpCliSock,addr = tcpSerSock.accept()
print('...connected from :', addr)
try:
while True:
data = ''
data = tcpCliSock.recv(BUFSIZE)
data = data.decode('utf-8')
print(data)
if not data:
break
if data == ctrCmd[0]:
LEDControl.LedOn()
print('LED On: ',LEDControl.cur_led)
if data == ctrCmd[1]:
LEDControl.LedOff()
print('LED Off: ',LEDControl.cur_led)
except KeyboardInterrupt:
GPIO.cleanup()
tcpSerSock.close();
'Android' 카테고리의 다른 글
안드로이드- 조이스틱 (0) | 2020.11.09 |
---|---|
안드로이드 -ReclyerView (0) | 2020.11.06 |
안드로이드- 회전시 데이터 유지하기 (0) | 2020.11.05 |
안드로이드- 회전시 데이터 유지하기 (0) | 2020.11.05 |
안드로이드- 글 적고 저장 누르면 다시 실행시켰을 때 저장됨 (0) | 2020.11.05 |