内网实现树莓派控制Arduino


preface

参考博客:https://blog.csdn.net/weixin_42534940/article/details/81416578

1.基础

python模块

httpserver: https://docs.python.org/zh-cn/3.8/library/http.server.html

树莓派

系统配置–>网络配置–>环境搭建–>开发

ifconfig ——查看IP地址 eth0 lo wlan0

连接方式:

  • ssh user@192.168.1.102 ,可远程连接树莓派,进行命令行操作
  • VNC server 进行连接,IP,username,password同ssh

ls /dev/tty* 查看端口

内网外网

内网是从路由器以下开始的,ip都以192开头。
外网不经过路由器和交换机就可以上网的网络。

2.代码

server.py

# --coding:utf-8--
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import socket
import urllib
import serial
#from car_controler import FourWheelDriveCar
#from camera_controler import Camera
 
port='/dev/ttyUSB0'
rate=9600
ser=serial.Serial(port,rate,timeout=1)

class CarServer(BaseHTTPRequestHandler):
    
#    carControler = FourWheelDriveCar()
#    cameraControler = Camera()
 
    def get_host_ip(self):
        '''
        This method is used for getting local ip address
        The car server will deploy on this ip
        '''
        try:
            serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            serverSocket.connect(("8.8.8.8", 80))
            localIP = serverSocket.getsockname()[0]
        finally:
            return localIP
 
    def do_GET(self):
        '''
        Define the car control GUI for client
        For the first edition, it will only return direction contol GUI
        '''
        localIP = CarServer.get_host_ip(self)
 
        # When this GET method is called, then should init the car
        # self.carControler.reset()
 
        # Read control page html file from control.html
        controlPageFile = open("index.html")
        controlPageGUI = controlPageFile.read()
        controlPageFile.close()
        controlPageGUI = controlPageGUI.replace(
            "requestAddress", "http://" + localIP + ":9090/")
        controlPageGUI = controlPageGUI.replace(
            "cameraAddress", "http://" + localIP + ":8080/")
 
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(controlPageGUI.encode())
 
    def do_POST(self):
        length = int(self.headers['Content-Length'])
        qs = self.rfile.read(length)
        direction = qs.decode()
        print(direction)
        ser.write(''.encode())

        self.send_response(200)
 
 
if __name__ == "__main__":
    raspCarServer = CarServer
    hostIP = raspCarServer.get_host_ip(raspCarServer)
    hostPort = 9090
    myServer = HTTPServer((hostIP, hostPort), raspCarServer)
 
    print(time.asctime(), "Server Starts - %s:%s" % (hostIP, hostPort))
 
    try:
        myServer.serve_forever()
    except KeyboardInterrupt:
        pass

index.html

<html>
<script>
    function directionBtnDown(direction) {
        var url = "requestAddress"
        var request = new XMLHttpRequest();
        request.open("POST", url);
 
        request.send(direction)
    }
 
    function directionBtnUp() {
        var url = "requestAddress"
        var request = new XMLHttpRequest();
        request.open("POST", url);
        request.send("S")
    }
</script>
<style type="text/css">
    span.car {
        position: absolute;
        margin-top: 30%;
        height: 480px;   
    }
 
    span.camera {
        position: absolute;
        margin-top: 5%;
        margin-left: 290px;
        height: 480px;
        width: 640px;
        background-color: blue
    }
 
    span.camera_control {
        position: absolute;
        margin-top: 30%;
        margin-left: 950px;
        height: 480px;
        background-color: blue
    }
 
 
    button.top {
        position: absolute;
        height: 50px;
        width: 90px;
        margin-left: 90px
    }
 
    button.left {
        position: absolute;
        height: 50px;
        width: 90px;
        margin-top: 50px;
    }
 
    button.right {
        position: absolute;
        height: 50px;
        width: 90px;
        margin-top: 50px;
        margin-left: 180px
    }
 
    button.bottom {
        position: absolute;
        height: 50px;
        width: 90px;
        margin-top: 100px;
        margin-left: 90px
    }
</style>
 
<head>
    <title>control page</title>
</head>
 
<body>
    <span id="car_control" class="car">
        <button class="top drectionBtn" id="F" onmousedown="directionBtnDown('w')" onmouseup="directionBtnUp()">F</button>
        <button class="left drectionBtn" id="L" onmousedown="directionBtnDown('a')" onmouseup="directionBtnUp()">L</button>
        <button class="right drectionBtn" id="R" onmousedown="directionBtnDown('d')" onmouseup="directionBtnUp()">R</button>
        <button class="bottom drectionBtn" id="B" onmousedown="directionBtnDown('x')" onmouseup="directionBtnUp()">B</button>
    </span>

</body>
 
</html>

然后在树莓派上:sudo python3 server.py

去网页http://192.168.1.102:9090/,实现内网控制小车。


文章作者: ╯晓~
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 ╯晓~ !
评论
  目录