首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >webiopi: html到python再到串行

webiopi: html到python再到串行
EN

Stack Overflow用户
提问于 2018-09-05 02:02:58
回答 1查看 190关注 0票数 0

也许这是一个简单的解决办法,也许我搞砸了。我迷路了。提前感谢您能给我的任何帮助!

所以,我有一个覆盆子pi 2 b+,它必须安装在一个漫游者(RC爬虫)。我决定使用WebIOPi,但我找不到任何有用的东西。

我的项目应该是这样工作的: HTML/JS PWM PYTHON ->串行TX -> ARDUINO我已经有了我需要的一切,而且树莓的PWM屏蔽在这里是相当昂贵的。

我设法在HTML上显示了两个滑块,arduino已经准备好接收命令字符串。但在这两者之间的某个地方,有些东西是不起作用的。

完整的代码在底部,这里只是一些片段:

代码语言:javascript
复制
<div class="slidecontainer">
<input type="range" min="1" max="179" value="90" class="slider" id="myRange" width="500"></div>

这是两个滑块中的一个。

代码语言:javascript
复制
<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("steering");
var slider2 = document.getElementById("myRange2");
var output2 = document.getElementById("throttle");
var vs = slider.value;
var vt = slider2.value;
output.innerHTML = slider.value;
output2.innerHTML = slider2.value;
slider.oninput = function() {
    steering.innerHTML = slider.value - 90;
    webiopi().callMacro("new_steering",vs);
}
slider2.oninput = function() {
    throttle.innerHTML = slider2.value - 90;
    webiopi().callMacro("new_throttle",vt);
}
</script>

在这里,我运行一个脚本,在HTML页面上写入值,格式化为-89到89的范围。我调用一个名为new_steering的WebIOPi宏,传递值vs (vs应该是滑块的值,从1到179)。

代码语言:javascript
复制
@webiopi.macro
def new_steering(nsv):
    f = open("slog.txt", "a+")
    f.write(nsv)
    news_string = "S{}".format(nsv)
    f.write(news_string)
    serial.writeString(news_string)
    f.close()

在python代码中,我有一个宏,它读取传入的值,将其写入文本文档(既不工作也不需要,只是为了检查它是否接收到了变量),它创建了一个以S为前导字符的字符串,并尝试通过串口发送它

有些地方不对劲。我不知道是什么,有多糟糕。请帮帮我!我的小脑袋在沸腾。

我已经这样设置了WebIOPi:

代码语言:javascript
复制
[GPIO]

[~GPIO]

[SCRIPTS]

botler1 = /home/pi/botler1/python/interface.py

[HTTP]

enabled = true
port = 8786

passwd-file = /etc/webiopi/passwd

prompt = "BOTLER1_hello"

doc-root = /home/pi/botler1

welcome-file = index.html

#------------------------------------------------------------------------#

[COAP]
enabled = true
port = 5683
multicast = true

[DEVICES]

usb1 = Serial device:ttyACM0 baudrate:9600

[REST]

gpio-export = 17
gpio-post-value = true
gpio-post-function = false

[ROUTES] 

这是HTML- Web界面

代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WebIOPi | Light Control</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
webiopi().ready(function() {
    // Create a "Light" labeled button for GPIO 17
    var button = webiopi().createGPIOButton(17, "Light");

    // Append button to HTML element with ID="controls" using jQuery
    $("#controls").append(button);

    // Refresh GPIO buttons
    // pass true to refresh repeatedly of false to refresh once
            webiopi().refreshGPIO(true);
});

</script>
<style type="text/css">
    button {
        display: block;
        margin: 5px 5px 5px 5px;
        width: 160px;
        height: 45px;
        font-size: 24pt;
        font-weight: bold;
        color: white;
    }

    #gpio17.LOW {
        background-color: Black;
    }

    #gpio17.HIGH {
        background-color: Blue;
    }
    .slidecontainer {
width: 100%; /* Width of the outside container */
}

/* The slider itself */
.slider {
-webkit-appearance: none;  /* Override default CSS styles */
appearance: none;
width: 100%; /* Full-width */
height: 25px; /* Specified height */
background: #d3d3d3; /* Grey background */
outline: none; /* Remove outline */
opacity: 0.7; /* Set transparency (for mouse-over effects on hover) */
-webkit-transition: .2s; /* 0.2 seconds transition on hover */
transition: opacity .2s;
}

</style>
</head>
<body>

<div class="slidecontainer">
<input type="range" min="1" max="179" value="90" class="slider" id="myRange" width="500"></div>
<div align="center"><p>Steering: <span id="steering"></span></p></div>
<div class="slidecontainer">
<input type="range" min="1" max="179" value="90" class="slider" 
id="myRange2" width="500"></div>
<div align="center"><p>Throttle: <span id="throttle"></span></p></div>

</div>
<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("steering");
var slider2 = document.getElementById("myRange2");
var output2 = document.getElementById("throttle");
var vs = slider.value;
var vt = slider2.value;
output.innerHTML = slider.value;
output2.innerHTML = slider2.value;
slider.oninput = function() {
    steering.innerHTML = slider.value - 90;
    webiopi().callMacro("new_steering",vs);
}
slider2.oninput = function() {
    throttle.innerHTML = slider2.value - 90;
    webiopi().callMacro("new_throttle",vt);
}
</script>

</body>
</html>

这是python文件

代码语言:javascript
复制
import webiopi
import datetime

GPIO = webiopi.GPIO

# setup function is automatically called at WebIOPi startup
def setup():
    nosense = 1

# loop function is repeatedly called by WebIOPi 
def loop():
    # retrieve device named "serial" in the config
    serial = webiopi.deviceInstance("usb1") 
    # write a string
    #serial.writeString("some text")

    webiopi.sleep(1)

@webiopi.macro
def new_steering(nsv):
    f = open("slog.txt", "a+")
    f.write(nsv)
    news_string = "S{}".format(nsv)
    f.write(news_string)
    serial.writeString(news_string)
    f.close()

@webiopi.macro
def new_throttle(ntv):
    d = open("tlog.txt", "a+")
    d.write(ntv)
    newt_string = "T{}".format(ntv)
    d.write(newt_string)
    serial.writeString(newt_string)
    d.close

def destroy():
    nosense = 1
EN

回答 1

Stack Overflow用户

发布于 2018-12-29 03:39:13

你有没有尝试过独立于网络生物的串行通信?我读到串行通信不像以前那样常规,所以,首先尝试一下。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52171778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档