51单片机因为指令系统、内部结构相对简单,被国内许多高校用来进行单片机的入门教学。在今年年初的疫情防控期间,就有一名应届毕业生使用闲置的树莓派进行远程开发51单片机,以此锻炼自己的敲代码能力。
![学生党进阶指南:用闲置树莓派远程开发51单片机](/img/20211021/Sba17d7a8-6de2-44c2-8be9-e17dd83136f9.jpg)
远程连接上,因为没有公网IP,该同学使用花生壳内网穿透实现了远程开发。值得一提的是,花生壳目前向学生群体免费开放了旗下“青春版”服务,学生可以用来更好地进行远程学习、开发调试等。
下面是该同学使用树莓派搭配花生壳,实现远程开发51单片机的具体步骤分享:
项目原件清单
1.用来远程连接的树莓派3B(已经吃灰很久了….)
2.执行程序的51开发板;
3.显示图像的摄像头(翻出了一个10年前的老USB摄像头…)
4.内网穿透工具花生壳;
5.一些继电器(是不是猜不到这个是干嘛的,先保密)
项目流程
虽然树莓派可以通过vnc进行远程桌面,但喜欢折腾的我怎么能甘心必须安装客户端呢?这绝对不行!!!所以我选择了带有html5解析vnc协议的novnc,远程部分ok。
51单片机的编译软件我选择的是sdcc进行编译,编译后会出现hex文件,但对于很多朋友来说这还不够,hex格式的文件还是很大,我们要用bin!!!大家可以使用Hex2bin来进行转换,okok现在就剩下串口下载了,我们来解决远程下载,这里我选择的是开源的stcgal进行烧写。
最后我们再来看看远程摄像头,远程摄像头的方案就有很多了,这里我选择的是motion。分析完了就让我们撸起袖子加油干吧!
1. 烧录完成系统后,老规矩来给树莓派更换国内源;
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo cp /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.list.bak
修改软件更新源,执行如下命令:
sudo vi /etc/apt/sources.list
按i进入编辑模式,改成清华的软件源地址,按ESC退出,然后 :x 回车保存退出。
deb http://mirror.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
deb-src http://mirror.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
修改系统更新源,执行如下命令:
sudo vi /etc/apt/sources.list.d/raspi.list
将原镜像用#号注释掉,然后插入清华的系统源地址,保存退出:
deb http://mirror.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui
同步更新源及更新软件包,执行如下命令:
sudo apt-get update
sudo apt-get upgrade
2. 更换完成源以后,开启树莓派自带的开启VNC
在终端输入以下命令进入配置界面。
sudo raspi-config
依次操作:Interfacing Options -> VNC -> Yes。之后系统会提示你是否要安装 VNC 服务,输入 y 之后回车,等待系统自动下载安装完成,如果祖师爷还是爱我们的话应该已经开启成功了。
稳妥起见我们还是来查看本地是否安装了vncserver(其实就是看看上面祖师爷有没有显灵)
whereis vncserver
如果没有请手动安装
yum install -y tigervnc-server
如果按照成功了就看这里哦
使用下面的命令来运行vnc
vncserver
此时会要求输入密码。这里需要注意:
The VNC server will run in the background. The port that it runs on is the display number + 5900 (i.e. 5901 in the case above).
意思就是当你执行vncserver :1设置为1时,下面要运行VNC是的端口号应该是5900+1,那就是5901,VNC的默认端口是5900。
3. 安装noVNC
#安装git
sudo apt-get install git
#获取novnc的源码
git clone https://github.com/kanaka/noVNC
进入novnc的目录
cd noVNC
运行novnc(第一次运行的时候会下载一个端口转发的程序可能需要一些时间)
./utils/launch.sh --vnc localhost:5901
4. 安装51开发环境
这里的开发环境依赖python3,需要确保已安装python3, pip3.
如果没有下载pip3请下载安装
安装sdcc
sudo apt-get install sdcc
编译程序例子:
sdcc 1.c
安装hex2bin(如果是不是专业开发者感觉可以不安装)
百度网盘分享地址:https://pan.baidu.com/s/1iky8HsB-nVn1p5J3EzB8NQ
提取码:tg3v
安装pyserial
sudo pip3 install pyserial
安装stcgal
pip3 install stcgal
测试
stcgal -h
#下载程序样例(建议完成安装ch340后在进行下载测试)
stcgal 1.ihx
5. 树莓派安装CH340驱动
驱动下载官方网址(linux版本)
http://www.wch.cn/download/CH341SER_LINUX_ZIP.html
cd 进入文件夹
安装依赖环境
sudo apt-get install linux-headers
修改make文件
打开ch34x.c源码,添加#include <linux/sched/signal.h>,并注释掉waitqueuet wait
编译并加载驱动
make
make load
6. 安装usb摄像头监控界面
sudo apt-get install motion
然后打开 motion daemon 守护进程,让他可以一直在后台运行:
sudo nano /etc/default/motion
#no修改成yes:
start_motion_daemon=yes
修改motion的配置文件:
sudo vim /etc/motion/motion.conf
#deamon off 改成 on
deamon on
#设置分辨率
width 800
height 600
#关闭 localhost 的限制
webcam_localhost off
然后运行 motion:
sudo motion
现在我们的摄像头已经变成了一台网络摄像头。在chrome浏览器下访问 http://[your.domain]:8081 即可看到摄像头当前拍摄的画面。
7. 简单修改noVNC界面
在noVNC源码的app目录下面添加camera文件夹
这里是jq的一个小插件实现了一个可以拖动的小框框
链接:https://pan.baidu.com/s/1VZSh14pUXK36NcxrYTM1-A
提取码:swk9
最后修改vnc.html文件让我们把摄像头和远程界面合二为一
<!DOCTYPE html>
<html lang="en" class="noVNC_loading">
<head>
<!--
noVNC example: simple example using default UI
Copyright (C) 2019 The noVNC Authors
noVNC is licensed under the MPL 2.0 (see LICENSE.txt)
This file is licensed under the 2-Clause BSD license (see LICENSE.txt).
Connect parameters are provided in query string:
http://example.com/?host=HOST&port=PORT&encrypt=1
or the fragment:
http://example.com/#host=HOST&port=PORT&encrypt=1
-->
<title>noVNC</title>
<meta charset="utf-8">
<!-- Icons (see app/images/icons/Makefile for what the sizes are for) -->
<link rel="icon" sizes="16x16" type="image/png" href="app/images/icons/novnc-16x16.png">
<link rel="icon" sizes="24x24" type="image/png" href="app/images/icons/novnc-24x24.png">
<link rel="icon" sizes="32x32" type="image/png" href="app/images/icons/novnc-32x32.png">
<link rel="icon" sizes="48x48" type="image/png" href="app/images/icons/novnc-48x48.png">
<link rel="icon" sizes="60x60" type="image/png" href="app/images/icons/novnc-60x60.png">
<link rel="icon" sizes="64x64" type="image/png" href="app/images/icons/novnc-64x64.png">
<link rel="icon" sizes="72x72" type="image/png" href="app/images/icons/novnc-72x72.png">
<link rel="icon" sizes="76x76" type="image/png" href="app/images/icons/novnc-76x76.png">
<link rel="icon" sizes="96x96" type="image/png" href="app/images/icons/novnc-96x96.png">
<link rel="icon" sizes="120x120" type="image/png" href="app/images/icons/novnc-120x120.png">
<link rel="icon" sizes="144x144" type="image/png" href="app/images/icons/novnc-144x144.png">
<link rel="icon" sizes="152x152" type="image/png" href="app/images/icons/novnc-152x152.png">
<link rel="icon" sizes="192x192" type="image/png" href="app/images/icons/novnc-192x192.png">
<!-- Firefox currently mishandles SVG, see #1419039
<link rel="icon" sizes="any" type="image/svg+xml" href="app/images/icons/novnc-icon.svg">
-->
<!-- Repeated last so that legacy handling will pick this -->
<link rel="icon" sizes="16x16" type="image/png" href="app/images/icons/novnc-16x16.png">
<!-- Apple iOS Safari settings -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<!-- Home Screen Icons (favourites and bookmarks use the normal icons) -->
<link rel="apple-touch-icon" sizes="60x60" type="image/png" href="app/images/icons/novnc-60x60.png">
<link rel="apple-touch-icon" sizes="76x76" type="image/png" href="app/images/icons/novnc-76x76.png">
<link rel="apple-touch-icon" sizes="120x120" type="image/png" href="app/images/icons/novnc-120x120.png">
<link rel="apple-touch-icon" sizes="152x152" type="image/png" href="app/images/icons/novnc-152x152.png">
<!-- Stylesheets -->
<link rel="stylesheet" href="app/styles/base.css">
<script src="app/error-handler.js"></script>
<script type="module" crossorigin="anonymous" src="app/ui.js"></script>
<link rel="stylesheet" type="text/css" href="app/camera/css/normalize.css" />
<link href="app/camera/src/css/interactive.css" rel="stylesheet" />
<style>
html, body {
width: 100%;
height: 100%;
background-color: gainsboro;
}
.content {
margin: 10px;
}
#element {
height: 280px;
width: 340px;
top: 240px;
left: 240px;
background-color: #82ABC0;
}
</style>
</head>
<body>
<!-- 摄像头显示框-->
<div id="element" name="Close false">
<div class="content">
<img style="-webkit-user-select: none;margin: auto;" src="http://192.168.3.208:8081/">
</div>
</div>
<div id="noVNC_fallback_error" class="noVNC_center">
<div>
<div>noVNC encountered an error:</div>
<br>
<div id="noVNC_fallback_errormsg"></div>
</div>
</div>
<!-- noVNC Control Bar -->
<div id="noVNC_control_bar_anchor" class="noVNC_vcenter">
<div id="noVNC_control_bar">
<div id="noVNC_control_bar_handle" title="Hide/Show the control bar"><div></div></div>
<div class="noVNC_scroll">
<h1 class="noVNC_logo" translate="no"><span>no</span><br>VNC</h1>
<!-- Drag/Pan the viewport -->
<input type="image" alt="Drag" src="app/images/drag.svg"
id="noVNC_view_drag_button" class="noVNC_button noVNC_hidden"
title="Move/Drag Viewport">
<!--noVNC Touch Device only buttons-->
<div id="noVNC_mobile_buttons">
<input type="image" alt="Keyboard" src="app/images/keyboard.svg"
id="noVNC_keyboard_button" class="noVNC_button" title="Show Keyboard">
</div>
<!-- Extra manual keys -->
<input type="image" alt="Extra keys" src="app/images/toggleextrakeys.svg"
id="noVNC_toggle_extra_keys_button" class="noVNC_button"
title="Show Extra Keys">
<div class="noVNC_vcenter">
<div id="noVNC_modifiers" class="noVNC_panel">
<input type="image" alt="Ctrl" src="app/images/ctrl.svg"
id="noVNC_toggle_ctrl_button" class="noVNC_button"
title="Toggle Ctrl">
<input type="image" alt="Alt" src="app/images/alt.svg"
id="noVNC_toggle_alt_button" class="noVNC_button"
title="Toggle Alt">
<input type="image" alt="Windows" src="app/images/windows.svg"
id="noVNC_toggle_windows_button" class="noVNC_button"
title="Toggle Windows">
<input type="image" alt="Tab" src="app/images/tab.svg"
id="noVNC_send_tab_button" class="noVNC_button"
title="Send Tab">
<input type="image" alt="Esc" src="app/images/esc.svg"
id="noVNC_send_esc_button" class="noVNC_button"
title="Send Escape">
<input type="image" alt="Ctrl+Alt+Del" src="app/images/ctrlaltdel.svg"
id="noVNC_send_ctrl_alt_del_button" class="noVNC_button"
title="Send Ctrl-Alt-Del">
</div>
</div>
<!-- Shutdown/Reboot -->
<input type="image" alt="Shutdown/Reboot" src="app/images/power.svg"
id="noVNC_power_button" class="noVNC_button"
title="Shutdown/Reboot...">
<div class="noVNC_vcenter">
<div id="noVNC_power" class="noVNC_panel">
<div class="noVNC_heading">
<img alt="" src="app/images/power.svg"> Power
</div>
<input type="button" id="noVNC_shutdown_button" value="Shutdown">
<input type="button" id="noVNC_reboot_button" value="Reboot">
<input type="button" id="noVNC_reset_button" value="Reset">
</div>
</div>
<!-- Clipboard -->
<input type="image" alt="Clipboard" src="app/images/clipboard.svg"
id="noVNC_clipboard_button" class="noVNC_button"
title="Clipboard">
<div class="noVNC_vcenter">
<div id="noVNC_clipboard" class="noVNC_panel">
<div class="noVNC_heading">
<img alt="" src="app/images/clipboard.svg"> Clipboard
</div>
<textarea id="noVNC_clipboard_text" rows=5></textarea>
<br>
<input id="noVNC_clipboard_clear_button" type="button"
value="Clear" class="noVNC_submit">
</div>
</div>
<!-- Toggle fullscreen -->
<input type="image" alt="Fullscreen" src="app/images/fullscreen.svg"
id="noVNC_fullscreen_button" class="noVNC_button noVNC_hidden"
title="Fullscreen">
<!-- Settings -->
<input type="image" alt="Settings" src="app/images/settings.svg"
id="noVNC_settings_button" class="noVNC_button"
title="Settings">
<div class="noVNC_vcenter">
<div id="noVNC_settings" class="noVNC_panel">
<ul>
<li class="noVNC_heading">
<img alt="" src="app/images/settings.svg"> Settings
</li>
<li>
<label><input id="noVNC_setting_shared" type="checkbox"> Shared Mode</label>
</li>
<li>
<label><input id="noVNC_setting_view_only" type="checkbox"> View Only</label>
</li>
<li><hr></li>
<li>
<label><input id="noVNC_setting_view_clip" type="checkbox"> Clip to Window</label>
</li>
<li>
<label for="noVNC_setting_resize">Scaling Mode:</label>
<select id="noVNC_setting_resize" name="vncResize">
<option value="off">None</option>
<option value="scale">Local Scaling</option>
<option value="remote">Remote Resizing</option>
</select>
</li>
<li><hr></li>
<li>
<div class="noVNC_expander">Advanced</div>
<div><ul>
<li>
<label for="noVNC_setting_quality">Quality:</label>
<input id="noVNC_setting_quality" type="range" min="0" max="9" value="6">
</li>
<li>
<label for="noVNC_setting_compression">Compression level:</label>
<input id="noVNC_setting_compression" type="range" min="0" max="9" value="2">
</li>
<li><hr></li>
<li>
<label for="noVNC_setting_repeaterID">Repeater ID:</label>
<input id="noVNC_setting_repeaterID" type="text" value="">
</li>
<li>
<div class="noVNC_expander">WebSocket</div>
<div><ul>
<li>
<label><input id="noVNC_setting_encrypt" type="checkbox"> Encrypt</label>
</li>
<li>
<label for="noVNC_setting_host">Host:</label>
<input id="noVNC_setting_host">
</li>
<li>
<label for="noVNC_setting_port">Port:</label>
<input id="noVNC_setting_port" type="number">
</li>
<li>
<label for="noVNC_setting_path">Path:</label>
<input id="noVNC_setting_path" type="text" value="websockify">
</li>
</ul></div>
</li>
<li><hr></li>
<li>
<label><input id="noVNC_setting_reconnect" type="checkbox"> Automatic Reconnect</label>
</li>
<li>
<label for="noVNC_setting_reconnect_delay">Reconnect Delay (ms):</label>
<input id="noVNC_setting_reconnect_delay" type="number">
</li>
<li><hr></li>
<li>
<label><input id="noVNC_setting_show_dot" type="checkbox"> Show Dot when No Cursor</label>
</li>
<li><hr></li>
<!-- Logging selection dropdown -->
<li>
<label>Logging:
<select id="noVNC_setting_logging" name="vncLogging">
</select>
</label>
</li>
</ul></div>
</li>
<li class="noVNC_version_separator"><hr></li>
<li class="noVNC_version_wrapper">
<span>Version:</span>
<span class="noVNC_version"></span>
</li>
</ul>
</div>
</div>
<!-- Connection Controls -->
<input type="image" alt="Disconnect" src="app/images/disconnect.svg"
id="noVNC_disconnect_button" class="noVNC_button"
title="Disconnect">
</div>
</div>
<div id="noVNC_control_bar_hint"></div>
</div> <!-- End of noVNC_control_bar -->
<!-- Status Dialog -->
<div id="noVNC_status"></div>
<!-- Connect button -->
<div class="noVNC_center">
<div id="noVNC_connect_dlg">
<div class="noVNC_logo" translate="no"><span>no</span>VNC</div>
<div id="noVNC_connect_button"><div>
<img alt="" src="app/images/connect.svg"> Connect
</div></div>
</div>
</div>
<!-- Password Dialog -->
<div class="noVNC_center noVNC_connect_layer">
<div id="noVNC_credentials_dlg" class="noVNC_panel"><form>
<ul>
<li id="noVNC_username_block">
<label>Username:</label>
<input id="noVNC_username_input">
</li>
<li id="noVNC_password_block">
<label>Password:</label>
<input id="noVNC_password_input" type="password">
</li>
<li>
<input id="noVNC_credentials_button" type="submit" value="Send Credentials" class="noVNC_submit">
</li>
</ul>
</form></div>
</div>
<!-- Transition Screens -->
<div id="noVNC_transition">
<div id="noVNC_transition_text"></div>
<div>
<input type="button" id="noVNC_cancel_reconnect_button" value="Cancel" class="noVNC_submit">
</div>
<div class="noVNC_spinner"></div>
</div>
<!-- This is where the RFB elements will attach -->
<div id="noVNC_container">
<!-- Note that Google Chrome on Android doesn't respect any of these,
html attributes which attempt to disable text suggestions on the
on-screen keyboard. Let's hope Chrome implements the ime-mode
style for example -->
<textarea id="noVNC_keyboardinput" autocapitalize="off"
autocomplete="off" spellcheck="false" tabindex="-1"></textarea>
</div>
<audio id="noVNC_bell">
<source src="app/sounds/bell.oga" type="audio/ogg">
<source src="app/sounds/bell.mp3" type="audio/mpeg">
</audio>
<script src="app/camera/src/interactive.js"></script>
<script>
let options = {close: false };
interactive('element', options);
</script>
</body>
</html>
8. 编写继电器控制脚本控制单片机上电
首先我们选用树莓派上的一个引脚来控制继电器,而继电器来控制单片机的通电与断电。脚本代码如下,大家可以选择不同的引脚进行控制,不再做过多的赘述。
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
# BOARD编号方式,基于插座引脚编号
GPIO.setmode(GPIO.BOARD)
# 输出模式
GPIO.setup(0, GPIO.OUT)
GPIO.output(0, GPIO.HIGH)
time.sleep(1)
GPIO.output(0, GPIO.LOW)
time.sleep(1)
这里运行这个代码以后我们会看到一个警告如下
RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
这个警告我们直接忽略就好~
9. 花生壳内网穿透配置
我们可以先到下面的URL中下载花生壳软件,我们这里使用的是树莓派当然要使用树莓派版本的软件了。
https://hsk.oray.com/download/
在下载软件的界面中我们有可以看到帮助选项,这里有很多我们可以参考的设置例子
安装第一步
sudo -s //切换到管理员权限
dpkg -i <安装包名称> //回车,等待安装完成。
我们可以使用下面的命令启动花生壳服务,更多命令可以到帮助网页中查看
phddns start //启动花生壳
phddns enable //设置花生壳开机自启
然后我们可以用过下面的网址远程设置花生壳
http://b.oray.com
首次登录,需先激活。提供扫码激活和密码激活两种方式
激活成功后,进入花生壳管理平台。
![学生党进阶指南:用闲置树莓派远程开发51单片机](/img/20211021/S57685bdd-c407-468d-b879-d826dd1e1b06.png)
添加内网穿透映射时,点击页面上的“增加映射”按钮,根据页面提示填写映射所需的信息:
![学生党进阶指南:用闲置树莓派远程开发51单片机](/img/20211021/S6b698257-ccc3-4aab-bbc1-541efbd345ca.png)
1:自定义;2:选择HTTP(因为我们要映射的部分都是网页);3:选择用作外网访问的域名,直接选择注册帐号时花生壳免费赠送的壳域名即可;4:选择动态端口;5:树莓派设备的内网IP地址;6:映射的服务类型对应端口;7:确认映射内容无误后,点击“确定”。
下面对摄像头和noVNC进行映射,映射添加完成将生成格式为域名+5位数端口号的外网访问地址:
![学生党进阶指南:用闲置树莓派远程开发51单片机](/img/20211021/S7328e50d-d35c-409f-a177-4d97eed201cc.jpg)
然后,在远程外网环境下,直接访问花生壳生成的域名地址就可以实现远程开发51单片机了。好了,本次分享到这里!感谢大家的耐心阅读。
![学生党进阶指南:用闲置树莓派远程开发51单片机](/img/20211021/S842749e5-c987-481d-9fea-df7ee9f97507.png)
值得强调的是,花生壳可以将自己的电脑迅速变成一台云服务器,运行基于http的web服务、基于socket的通讯项目等,如远程访问NAS、Web网站、数据库、Windows RDP远程桌面、游戏联机等,通过内网穿透实现没有公网IP时各种局域网服务的跨网段远程访问。有需要的童鞋,可以直接到花生壳官网“未来之星”页面“白嫖”花生壳“青春版”服务。
|