安卓控制电灯
android关键代码:
packageorg.zhudou.makeabignews.mylamp;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Switch;
importjava.net.DatagramPacket;
importjava.net.DatagramSocket;
importjava.net.Inet6Address;
importjava.net.InetAddress;
importjava.net.NetworkInterface;
importjava.net.SocketException;
importjava.util.Enumeration;
public classMainActivityextendsAppCompatActivity {
DatagramSocketsocket=null;
InetAddressserverAddress=null;
StringbroadcastIP=null;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
finalSwitch switch_lamp=(Switch)findViewById(R.id.switch_lamp);
try{
socket=newDatagramSocket(9999);
}catch(Exception e) {
e.printStackTrace();
}
//
String hostIp =null;
try{
Enumeration nis =NetworkInterface.getNetworkInterfaces();
InetAddress ia =null;
while(nis.hasMoreElements()) {
NetworkInterfaceni = (NetworkInterface) nis.nextElement();
Enumeration ias = ni.getInetAddresses();
while(ias.hasMoreElements()) {
ia = ias.nextElement();
if(iainstanceofInet6Address) {
continue;// skip ipv6
}
String ip = ia.getHostAddress();
if(!"127.0.0.1".equals(ip)) {
hostIp = ia.getHostAddress();
break;
}
}
}
}catch(SocketException e) {
e.printStackTrace();
}
String ip_network_segment="";
String[] sourceIP = hostIp.split("\\.");//ip分段数组
for(inti =0; i < sourceIP.length-1; i++) {
ip_network_segment+=sourceIP[i]+".";
}
broadcastIP=ip_network_segment+"255";
setTitle("MyLamp : "+hostIp);
finalString finalIp_network_segment = ip_network_segment;
switch_lamp.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
String msg="";
if(switch_lamp.isChecked()){
switch_lamp.setText("on");
msg="on";
}else{
switch_lamp.setText("off");
msg="off";
}
//
finalString finalMsg = msg;
newThread(newRunnable() {
@Override
public voidrun() {
try{
String sendData =finalMsg;
bytedata[] = sendData.getBytes();
//
//
for(inti =2; i <254; i++) {
String targetIp=finalIp_network_segment+i;
System.out.print(targetIp);
try{
serverAddress= InetAddress.getByName(targetIp);
DatagramPacket packet =newDatagramPacket(data, data.length,serverAddress,8888);
socket.send(packet);
}catch(Exception e) {
e.printStackTrace();
}
}
//
}catch(Exception e) {
e.printStackTrace();
}
}
}).start();
//
}
});
}
}
chmod +x ledon.sh
#!/bin/bash
echo export pin $1
echo $1 > /sys/class/gpio/export
echo setting direction to output
echo out > /sys/class/gpio/gpio$1/direction
echo setting pi low
echo 0 > /sys/class/gpio/gpio$1/value
#!/bin/bash
echo export pin $1
echo $1 > /sys/class/gpio/export
echo setting direction to output
echo out > /sys/class/gpio/gpio$1/direction
echo setting pi high
echo 1 > /sys/class/gpio/gpio$1/value
改良版: out.sh
#!/bin/bash
echo export pin $1 $2
if [ ! -d /sys/class/gpio/gpio$1 ] ;then
echo $1 > /sys/class/gpio/export
fi
echo out > /sys/class/gpio/gpio$1/direction
echo $2 > /sys/class/gpio/gpio$1/value
udpserver.php