谷动谷力

 找回密码
 立即注册
查看: 577|回复: 0
打印 上一主题 下一主题
收起左侧

基于手机app的树莓派远程监控(非常全)

[复制链接]
跳转到指定楼层
楼主
发表于 2024-1-17 21:02:15 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
基于手机app的树莓派远程监控(非常全)
一.准备器件:树莓派系统板,树莓派自带摄像头

二. 树莓派系统环境:Raspberry Debian 9 手机app运行环境:android studio
三.CSI摄像头配置的话自己弄,摄像头配置完成后可以用 raspistill -o 1.jpg 来测试一下摄像头的拍照功能

四、网络视频监控配置与实现
1、利用mjpg-streamer框架实现,基本配置方案看链接:(链接私聊)

五、树莓派环境配置
1.基本上按照四中的链接配置 都可以直接将树莓派的摄像头运行起来(如图即为运行成功)


2.但基本配置起来只能在当前目录下运行,因此要添加一下环境,使得全路径运行,方便后面代码的编写。

将该脚本给它一个可执行权限:chmod 777 shart.sh

修改工作目录下的 .bashrc 隐藏文件, 配置命令终端的

Vi /home/pi/.bashrc(这比较靠谱点)

直接在文件末尾的最后一行加入路径就行:/home/pjf/shell


配置完成后保存 并在命令行输入:source ~/.bashrc
重启一下树莓派 ,命令行输入:echo $PATH。显示环境变量添加成功。

3.这时可以在其它路径运行 start.sh 可执行文件了,但要注意start.sh中还有流转发的程序./mjpg_streamer。

直接在其它路径下运行start.sh会提示 ./mjpg_streamer: not found(除开执行权限的问题外,文件的格式同样也需要去注意)

因此 vim start.sh 进入到脚本文件中 将三个文件 ./mjpg_streamer,./input_raspicam.so,./output_http.so 添加至全路径模式(如下图)


保存退出,start.sh 便可以在其它路径下运行起来了


然后打开浏览器,网址输入http://ip:8080/?action=stream,即可看到监控视频效果

注意注意!!!!
上面的环境变量的配置在linux系统普通用户目录(.bashrc)如果在sudo 下运行代码便不可以了,当用户执行sudo时,系统会主动寻找/etc/sudoers文件,判断该用户是否有执行sudo的权限。

因此要想在sudo 下运行还得visudo使用vi打开/etc/sudoers文件来添加环境变量

sudo 命令,为非根用户授予根用户的权限

配置文件是/etc/sudoers,此文件有权限限制,而且有格式要求,万一改错会比较麻烦。使用visudo可以容易配置。

1.输入命令
  1. sudo visudo
复制代码

添加对应路径


ctrl x退出按Y保存
重新运行便可以在sudo 下运行该脚本了

六、树莓派代码的实现(上面步骤配置好代码就简单了)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. void* ptintf_hello_world(void* tid);
  6. void* ptintf_hello_world1(void* tid);

  7. int main(void){
  8.         pthread_t thread;
  9.         pthread_t thread1;
  10.         int status,i=10,i1=10;
  11.         printf("Main here. Creating thread %d\n",i);
  12.         status=pthread_create(&thread,NULL,ptintf_hello_world,(void*)i);
  13.         status=pthread_create(&thread1,NULL,ptintf_hello_world1,(void*)i1);

  14.         pthread_join(thread,NULL);  //pthread_join函数以阻塞的方式等待指定的线程结束;如果线程已经结束,函数会立即返回,并且指定的线程>
  15. 必须是joinable的

  16.         exit(0);
  17. }
  18. void* ptintf_hello_world(void* tid){
  19.         sleep(10);
  20. //      system("wget  http://pi:8080/?action=snapshot -O ./1.jpg ");
  21. //      printf("Hello world0 %d.\n",tid);
  22.         exit(0);
  23. }
  24. void* ptintf_hello_world1(void* tid){
  25.         system("start.sh");
  26.         printf("Hello world1 %d.\n",tid);
  27.         exit(0);
  28. }
复制代码



七、安卓部分代码(注意网络权限的配置)
1.网络权限配置
找到路径app->src->-main->res->AndroidManifest.xml文件
打开 AndroidManifest.xml文件
在application节点之前增加以下代码
  1. <!--允许程序打开网络套接字-->
  2. <uses-permission android:name="android.permission.INTERNET" />
  3. <br/><!--允许程序获取网络状态-->
  4. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
复制代码


打开使用明文网络流量(加密流量)
  1. android:usesCleartextTraffic="true"
复制代码


2.java代码
  1. package com.example.mysuju;

  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.os.PersistableBundle;
  5. import android.view.View;
  6. import android.webkit.WebView;
  7. import android.widget.Button;
  8. import android.widget.MediaController;
  9. import android.widget.VideoView;

  10. import androidx.annotation.Nullable;
  11. import androidx.appcompat.app.AppCompatActivity;

  12. public class MessageActivity extends AppCompatActivity {
  13.     private Button playBtn, stopBtn;
  14.     MediaController mMediaController;
  15.     private WebView webview1;

  16.     @Override
  17.     public void onCreate(@Nullable Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_message);
  20.         VideoView mVideoView = new VideoView(this);
  21.         //mVideoView = (VideoView) findViewById(R.id.video);
  22.         mMediaController = new MediaController(this);
  23.         webview1 = (WebView) findViewById(R.id.webview1);
  24.         playBtn = (Button) findViewById(R.id.login1);
  25.         //stopBtn = (Button) findViewById(R.id.stopbutton);
  26.         // playBtn.setOnClickListener(new mClick());
  27.         //stopBtn.setOnClickListener(new mClick());
  28.         setview();
  29.       

  30.     }
  31.     private void setview() {
  32.         String url="http://pi:8080/?action=stream";//视频链接
  33.         webview1.loadUrl(url);//打开指定URL的html文件
  34.     }
  35. }
复制代码

3.  .xml布局代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="match_parent"
  4.     android:layout_height="match_parent">
  5.     <WebView
  6.         android:id="@+id/webview1"
  7.         android:layout_width="match_parent"
  8.         android:layout_height="300dp"/>
  9.     <LinearLayout
  10.         android:background="@drawable/pj"
  11.         android:layout_width="match_parent"
  12.         android:layout_height="match_parent">

  13.         <Button
  14.             android:id="@+id/login1"
  15.             android:layout_width="100dp"
  16.             android:layout_height="50dp"
  17.             android:text="返回"
  18.             android:textColor="@color/colorAccent"
  19.             android:textSize="20dp"

  20.             >

  21.         </Button>

  22.     </LinearLayout>
  23. </LinearLayout>
复制代码

八、成果展示

————————————————
版权声明:本文为CSDN博主「来杯开水」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/HHHSSD/article/details/122360742



+10
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-5-8 10:10 , Processed in 0.081288 second(s), 44 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表