整合代码,文档

master
CYZ 2 years ago
parent 8bd735be4a
commit ab6d68936b

@ -0,0 +1,64 @@
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "xiu"; // 请替换成您的WiFi热点名称SSID
const char* password = "12345678"; // 请替换成您的WiFi热点密码
const char* host = "192.168.39.219"; // 请替换成您的服务端IP地址
const uint16_t port = 9975; // 请替换成您的服务端监听端口号
int pirPin = 13; //out引脚接到GPIO_13
bool motionDetected = false; // 初始状态设定为没有检测到人
unsigned long motionDetectedTimestamp = 0; // 记录最近一次检测到人的时间戳
unsigned long motionThreshold = 3000; // 设置人体运动持续时间阈值为3秒
WiFiUDP udp;
void setup() {
pinMode(pirPin, INPUT);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
//连接wifi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP());
udp.begin(8888);
}
void loop() {
bool currentMotionState = digitalRead(pirPin); // 读取HCSR-501输出引脚的状态
if (currentMotionState == HIGH) { // 如果检测到人
//motionDetected = true;
//motionDetectedTimestamp = millis(); // 记录最近一次检测到人的时间戳
// 发送消息到服务端
udp.beginPacket(host, port);
udp.write("Motion detected!");
udp.endPacket();
Serial.println("Motion detected!"); // 在串口打印检测到信息
} else if (motionDetected && (millis() - motionDetectedTimestamp > motionThreshold)) {
// 如果当前没有检测到人,并且已经超过了阈值时间,重置状态
motionDetected = false;
motionDetectedTimestamp = 0;
Serial.println("Motion disapper");
}
// 接收服务端的回复
/*
int packetSize = udp.parsePacket();
if (packetSize) {
String response = "";
while (udp.available()) {
response += (char) udp.read();
}
Serial.println("Response received: " + response);
}*/
// 每隔1秒钟执行一次循环
delay(1000);
}

@ -1,17 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="deploymentTargetDropDown"> <component name="deploymentTargetDropDown">
<targetSelectedWithDropDown> <runningDeviceTargetSelectedWithDropDown>
<Target> <Target>
<type value="QUICK_BOOT_TARGET" /> <type value="RUNNING_DEVICE_TARGET" />
<deviceKey> <deviceKey>
<Key> <Key>
<type value="VIRTUAL_DEVICE_PATH" /> <type value="SERIAL_NUMBER" />
<value value="C:\Users\zqy04\.android\avd\Pixel_XL_API_31.avd" /> <value value="W3VBB21918206146" />
</Key> </Key>
</deviceKey> </deviceKey>
</Target> </Target>
</targetSelectedWithDropDown> </runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-06-25T00:54:24.006491100Z" /> <timeTargetWasSelectedWithDropDown value="2023-06-28T03:24:01.996639400Z" />
</component> </component>
</project> </project>

@ -1,5 +0,0 @@
C
java:S1604"(Make this anonymous inner class a lambda(ìäÔéùÿÿÿÿ
P
java:S1161":Add the "@Override" annotation above this method signature(¼²Á÷

@ -1,5 +0,0 @@
>
java:S1604"(Make this anonymous inner class a lambda(ʆ<C38A>õ
P
java:S1161":Add the "@Override" annotation above this method signature(¼²Á÷

@ -9,11 +9,7 @@ A
gradle.properties,2\a\2afbb999f001938c88fa43fc2ef52abf0f8213e4 gradle.properties,2\a\2afbb999f001938c88fa43fc2ef52abf0f8213e4
k k
;app/src/test/java/com/example/sixaunyi/ExampleUnitTest.java,8\9\892f839083a73d776402535dde27e522288853c9 ;app/src/test/java/com/example/sixaunyi/ExampleUnitTest.java,8\9\892f839083a73d776402535dde27e522288853c9
i
9app/src/main/java/com/example/sixaunyi/FirstFragment.java,0\3\0336633037f72ee431c162e8d6cbc29d1cd6fa5d
? ?
settings.gradle,0\5\05efc8b1657769a27696d478ded1e95f38737233 settings.gradle,0\5\05efc8b1657769a27696d478ded1e95f38737233
j @
:app/src/main/java/com/example/sixaunyi/SecondFragment.java,1\2\12963d86ac5c2888cc2a9ae459de5665bc06b01f app/build.gradle,f\4\f4a01d6a4fcb971362ec00a83903fd3902f52164
j
:app/src/main/java/com/example/sixaunyi/MapApplication.java,7\7\776a4b5f215e52628a4164ac15ef90fa338360ce

@ -18,9 +18,9 @@
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules" android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher" android:icon="@drawable/app"
android:label="@string/app_name" android:label="哨兵"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@drawable/app"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.Sixaunyi.NoActionBar" android:theme="@style/Theme.Sixaunyi.NoActionBar"
tools:targetApi="31"> tools:targetApi="31">

@ -13,6 +13,7 @@ import android.content.Context;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.location.Location; import android.location.Location;
import android.nfc.Tag; import android.nfc.Tag;
@ -108,10 +109,11 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
//private ActivityResultLauncher<Intent> launcher; //private ActivityResultLauncher<Intent> launcher;
////////////////////////////可修改部分///////////////////////////// ////////////////////////////可修改部分/////////////////////////////
private final LatLng Monitorlatlng = new LatLng(28.258348849051746,113.04552182879037); private final LatLng Monitorlatlng = new LatLng(28.257553,113.0417);
private final static String Recieve_IP = "192.168.39.46"; private final static String Recieve_IP = "192.168.39.46";
private final static int RECEIVE_PORT = 9987; private final static int RECEIVE_PORT = 9975;
private final static String SEND_to_IP = "192.168.39.47"; private final static String SEND_to_IP = "192.168.39.195";
private final static int SEND_PORT = 8888; private final static int SEND_PORT = 8888;
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
private InetAddress serverAddr; private InetAddress serverAddr;
@ -126,6 +128,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
if (savedInstanceState != null) { if (savedInstanceState != null) {
// 从保存的状态数据中恢复状态 // 从保存的状态数据中恢复状态
value = savedInstanceState.getString("key"); value = savedInstanceState.getString("key");
@ -145,9 +148,10 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
//检查安卓版本 //检查安卓版本
checkingAndroidVersion(); checkingAndroidVersion();
udpReceiveThread = new UdpReceiveThread();
udpReceiveThread.startListening();
addSensorMarker(Monitorlatlng); addSensorMarker(Monitorlatlng);
} }
/** /**
* Android * Android
@ -248,7 +252,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
// 设置为true表示显示定位层并可触发定位false表示隐藏定位层并不可触发定位默认是false // 设置为true表示显示定位层并可触发定位false表示隐藏定位层并不可触发定位默认是false
aMap.setMyLocationEnabled(true); aMap.setMyLocationEnabled(true);
//设置最小缩放等级为16 ,缩放级别范围为[3, 20] //设置最小缩放等级为16 ,缩放级别范围为[3, 20]
aMap.setMinZoomLevel(18); aMap.setMinZoomLevel(14);
//设置地图标志点击事件 //设置地图标志点击事件
aMap.setOnMarkerClickListener(this); aMap.setOnMarkerClickListener(this);
//设置地图标志拖拽事件 //设置地图标志拖拽事件
@ -302,7 +306,10 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
city = aMapLocation.getCity(); city = aMapLocation.getCity();
//当前位置经纬度坐标 //当前位置经纬度坐标
Current_latlng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude()); Current_latlng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
MonitorDistance = AMapUtils.calculateLineDistance(Current_latlng,Monitorlatlng);
Log.i(TAG, String.valueOf(Current_latlng.longitude));
Log.i(TAG, String.valueOf(Current_latlng.latitude));
//
//获取纬度 //获取纬度
double latitude = aMapLocation.getLatitude(); double latitude = aMapLocation.getLatitude();
//获取经度 //获取经度
@ -310,6 +317,8 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
Log.d("MainActivity", aMapLocation.getCity()); Log.d("MainActivity", aMapLocation.getCity());
showMsg(address); showMsg(address);
udpReceiveThread = new UdpReceiveThread();
udpReceiveThread.startListening();
//停止定位后,本地定位服务并不会被销毁 //停止定位后,本地定位服务并不会被销毁
mLocationClient.stopLocation(); mLocationClient.stopLocation();
if (mListener != null) { if (mListener != null) {
@ -391,7 +400,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
.draggable(true) .draggable(true)
//标点图标 //标点图标
.icon(BitmapDescriptorFactory. .icon(BitmapDescriptorFactory.
fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.img))) fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.weizhi)))
//备注 //备注
.title("路径点") .title("路径点")
.snippet("距离:") .snippet("距离:")
@ -435,7 +444,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.drone))) fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.drone)))
// 备注 // 备注
.title("无人机") .title("无人机")
.snippet("距离:")
); );
droneMarker.showInfoWindow(); droneMarker.showInfoWindow();
} }
@ -631,14 +640,32 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
} }
class ReceiveHandler extends Handler{
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Context context = MainActivity.this;
showAlert(context,"传感器报警");
addDroneMarker();
moveDrone();
}
});
}
}
}
public class UdpReceiveThread extends Thread { public class UdpReceiveThread extends Thread {
private static final String TAG = "UdpReceiveThread"; private static final String TAG = "UdpReceiveThread";
private static final int mRECEIVE_PORT = RECEIVE_PORT; private static final int mRECEIVE_PORT = RECEIVE_PORT;
private boolean mRunning; private boolean mRunning = true;
private String mReceiveIp = Recieve_IP; private String mReceiveIp = Recieve_IP;
private String mExpectedData = "Motion detected!"; private String mExpectedData = "Motion detected!";
private float udpMonitorDistance = MonitorDistance; private float udpMonitorDistance = MonitorDistance;
private String udpMonitorAngle = MonitorAngle; private String udpMonitorAngle = MonitorAngle;
private Handler receiveHandler = new ReceiveHandler();
@Override @Override
public void run() { public void run() {
@ -654,11 +681,9 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
Log.d(TAG, "Received data: " + receivedString); Log.d(TAG, "Received data: " + receivedString);
if (receivedString.equals(mExpectedData)) { if (receivedString.equals(mExpectedData)) {
Log.i(TAG, "run: 222222222222222222222222222"); Log.i(TAG, "run: 222222222222222222222222222");
receiveHandler.sendEmptyMessage(1);
sendCommand("WARNING "+String.valueOf(udpMonitorDistance)+" "+udpMonitorAngle); sendCommand("WARNING "+String.valueOf(udpMonitorDistance)+" "+udpMonitorAngle);
Context context = MainActivity.this;
showAlert(context,"传感器报警");
addDroneMarker();
moveDrone();
mRunning = false; mRunning = false;
@ -672,17 +697,6 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
e.printStackTrace(); e.printStackTrace();
} }
} }
public void showAlert(Context context, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("警告");
builder.setMessage(message);
builder.setIcon(R.drawable.warning);
builder.setPositiveButton("确定", null);
AlertDialog dialog = builder.create();
dialog.show();
}
public void startListening() { public void startListening() {
mRunning = true; mRunning = true;
start(); start();
@ -693,6 +707,17 @@ public class MainActivity extends AppCompatActivity implements AMapLocationList
} }
} }
public void showAlert(Context context, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("警告");
builder.setMessage(message);
builder.setIcon(R.drawable.warning);
builder.setPositiveButton("确定", null);
AlertDialog dialog = builder.create();
dialog.show();
}
//以北为0度 //以北为0度
public static String calcAngle(Double centerLat, Double centerLon, Double anotherLat, Double anotherLon) { public static String calcAngle(Double centerLat, Double centerLon, Double anotherLat, Double anotherLon) {
//差值 //差值

@ -37,13 +37,15 @@ public class SettingActivity extends AppCompatActivity {
private final static String SEND_to_IP = "192.168.39.47"; private final static String SEND_to_IP = "192.168.39.47";
private SeekBar Speed_btn; private SeekBar Speed_btn;
private Switch Battery_btn; private SeekBar Battery_btn;
private ImageButton return_btn; private ImageButton return_btn;
private final static int SEND_PORT = 8888; private final static int SEND_PORT = 8888;
private ExecutorService mThreadPool = Executors.newCachedThreadPool(); private ExecutorService mThreadPool = Executors.newCachedThreadPool();
private Switch photo_btn; private Switch photo_btn;
private Context context; private Context context;
private SharedPreferences sharedPreferences; private SharedPreferences sharedPreferences;
private TextView tvProgress;
private TextView Battery_warning;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -51,8 +53,9 @@ public class SettingActivity extends AppCompatActivity {
Speed_btn= findViewById(R.id.speed); Speed_btn= findViewById(R.id.speed);
return_btn=findViewById(R.id.returns); return_btn=findViewById(R.id.returns);
photo_btn = findViewById(R.id.photo); photo_btn = findViewById(R.id.photo);
Battery_btn = findViewById(R.id.Battery_warning); Battery_btn = findViewById(R.id.warning_num);
TextView tvProgress = (TextView) findViewById(R.id.tvProgress); tvProgress = findViewById(R.id.tvProgress);
Battery_warning=findViewById(R.id.Battery_warning);
sharedPreferences = getSharedPreferences("control_state", MODE_PRIVATE); sharedPreferences = getSharedPreferences("control_state", MODE_PRIVATE);
boolean photo_btn_state= sharedPreferences.getBoolean("photo_btn_state", false); boolean photo_btn_state= sharedPreferences.getBoolean("photo_btn_state", false);
photo_btn.setChecked(photo_btn_state); photo_btn.setChecked(photo_btn_state);
@ -60,6 +63,9 @@ public class SettingActivity extends AppCompatActivity {
int speedProgress = sharedPreferences.getInt("speed_progress", 0); int speedProgress = sharedPreferences.getInt("speed_progress", 0);
Speed_btn.setProgress(speedProgress); Speed_btn.setProgress(speedProgress);
tvProgress.setText(String.valueOf(speedProgress+10)); tvProgress.setText(String.valueOf(speedProgress+10));
int warning_num= sharedPreferences.getInt("battery_num", 0);
Battery_btn.setProgress(warning_num);
Battery_warning.setText(String.valueOf(warning_num));
photo_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { photo_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@ -80,11 +86,9 @@ public class SettingActivity extends AppCompatActivity {
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
e.printStackTrace(); e.printStackTrace();
} }
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("speed_progress", progress); editor.putInt("speed_progress", progress);
editor.apply(); editor.apply();
} }
@Override @Override
public void onStartTrackingTouch(SeekBar seekBar) {} public void onStartTrackingTouch(SeekBar seekBar) {}
@ -93,30 +97,25 @@ public class SettingActivity extends AppCompatActivity {
public void onStopTrackingTouch(SeekBar seekBar) {} public void onStopTrackingTouch(SeekBar seekBar) {}
}); });
Battery_btn.setOnClickListener(new View.OnClickListener() { Battery_btn.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override @Override
public void onClick(View v) { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (Battery_btn.isChecked()) { Battery_warning.setText(String.valueOf(progress));
try { SharedPreferences.Editor editor = sharedPreferences.edit();
sendCommand("battery_warning"); editor.putInt("battery_num", progress);
} catch (UnknownHostException e) { editor.apply();
e.printStackTrace();
}
}else
{
try {
sendCommand("warning_cancel");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
} }
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
}); });
return_btn.setOnClickListener(new View.OnClickListener(){ return_btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(SettingActivity.this, VideoActivity.class);
// 启动目标 Activity // 启动目标 Activity
finish(); passingParameters();
} }
}); });
photo_btn.setOnClickListener(new View.OnClickListener() { photo_btn.setOnClickListener(new View.OnClickListener() {
@ -124,14 +123,14 @@ public class SettingActivity extends AppCompatActivity {
public void onClick(View v) { public void onClick(View v) {
if (photo_btn.isChecked()) { if (photo_btn.isChecked()) {
try { try {
sendCommand("photo_high"); sendCommand("PHOTO_HIGH");
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
e.printStackTrace(); e.printStackTrace();
} }
}else }else
{ {
try { try {
sendCommand("photo_low"); sendCommand("PHOTO_LOW");
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -177,5 +176,19 @@ public class SettingActivity extends AppCompatActivity {
mThreadPool.execute(sendRunnable1); mThreadPool.execute(sendRunnable1);
} }
@Override
public void onBackPressed() {
// 在这里处理返回事件的逻辑
super.onBackPressed(); // 调用父类的方法,执行默认的返回操作
passingParameters();
}
private void passingParameters(){
int warning_num= sharedPreferences.getInt("battery_num", 0);
Log.i(TAG, "111111111111111111111"+String.valueOf(warning_num));
Intent intent = new Intent(SettingActivity.this,VideoActivity.class);
intent.putExtra("BATTERY_NUM", warning_num); // 设置要传递的参数
setResult(RESULT_OK, intent);
finish();
}
} }

@ -64,8 +64,8 @@ import android.widget.ToggleButton;
public class VideoActivity extends AppCompatActivity { public class VideoActivity extends AppCompatActivity {
private final static String SEND_to_IP = "192.168.39.47"; //填写接收方IP private final static String SEND_to_IP = "192.168.39.195"; //填写接收方IP
private final static String Recieve_IP = "192.168.39.47";//填服务器IP与send_to_IP为同一个 private final static String Recieve_IP = "192.168.39.195";//填服务器IP与send_to_IP为同一个
private final static int SEND_PORT = 8888; //发送端口号 private final static int SEND_PORT = 8888; //发送端口号
private final static int RECEIVE_PORT = 9999; //接收端口号 private final static int RECEIVE_PORT = 9999; //接收端口号
private static boolean listenStatus = true; //接收线程的循环标识 private static boolean listenStatus = true; //接收线程的循环标识

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

@ -26,14 +26,37 @@
android:scaleType="centerInside" android:scaleType="centerInside"
app:srcCompat="@drawable/returns" /> app:srcCompat="@drawable/returns" />
<Switch <LinearLayout
android:id="@+id/Battery_warning"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_height="50dp"
android:text="电量预警" android:layout_marginTop="10dp"
android:textColor="@color/white" android:orientation="horizontal"
android:textSize="28dp" android:background="@drawable/txt_radiuborder">
android:background="@drawable/txt_radiuborder"/>
<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:text="电量预警"
android:textColor="@color/white"
android:textSize="28dp" />
<SeekBar
android:id="@+id/warning_num"
android:layout_width="180dp"
android:layout_height="50dp"
android:max="100"
android:progress="0"
android:thumbTint='@color/teal_200' />
<TextView
android:id="@+id/Battery_warning"
android:layout_width="77dp"
android:layout_height="30dp"
android:text="0"
android:textColor="@color/white"
android:textSize="18dp" />
</LinearLayout>
<Switch <Switch
android:id="@+id/photo" android:id="@+id/photo"

Loading…
Cancel
Save