[英]Get data from Raspberry PI over Websocket into Web Page?
I am new at web developing and I want to do webpage for remote controlling my Raspberry PI. 我是Web开发的新手,我想创建用于远程控制Raspberry PI的网页。 On raspberry I have few sensors attached and I can get data by sending request on 192.168.1.100:9997.
在覆盆子上,我连接了几个传感器,可以通过在192.168.1.100:9997上发送请求来获取数据。 There is code written in Python.
有一些用Python编写的代码。 Everything works if I try to get data with Putty for example.
例如,如果我尝试通过Putty获取数据,一切都会正常。 Now I want to establish TCP connection for reading data over my webpage.
现在,我想建立TCP连接以通过我的网页读取数据。 I was searching for few days and found that this is possible by creating Websockets.
我搜索了几天,发现可以通过创建Websockets来实现。 There are many tools, most described I found is Node.js.
有很多工具,我发现最多描述的是Node.js。 As I understand with Node.js is possible to create Websockets and it can also serves webpage (instead of Appache for example)?
据我了解,Node.js可以创建Websocket,它还可以为网页提供服务(例如代替Appache)吗?
For example I am running this Websocket server just for reading data from RPi in "server.js". 例如,我运行此Websocket服务器只是为了从“ server.js”中的RPi读取数据。 Now I don't know how can I get this data from "server.js" into my .html?
现在我不知道如何将这些数据从“ server.js”导入到我的.html文件中? I didn't find any very basic examples.
我没有找到任何非常基本的示例。 I can get data via database, but this is not what I want.
我可以通过数据库获取数据,但这不是我想要的。 I also want to send request from my Webpage to Rpi and then read the answer.
我也想将请求从我的网页发送到Rpi,然后阅读答案。
I hope you understand my problem. 希望你能理解我的问题。 If you can point me in some good examples or tell me how it must be done I will be very glad.
如果您能在一些好的例子中指出我或告诉我必须怎么做,我将非常高兴。 I want to do this with Javasrcipt if it's possible.
我想用Javasrcipt做到这一点。
Thank you in advance. 先感谢您。
EDIT: I have now working example with Node.js, but I don't know how to implement this into my Web page that user can trigger this part of codes from .html, and show answered data into .html web page. 编辑:我现在有使用Node.js的示例,但是我不知道如何将其实现到我的Web页面中,用户可以从.html触发这部分代码,并在.html Web页面中显示已回答的数据。 I hope this helps.
我希望这有帮助。
var client = new net.Socket();
client.connect(9997, '192.168.1.100', function() {
console.log('Connected');
//sending request
//THIS SHOULD BE TRIGGERED FROM HTML onclick for example
client.write('$DATA');
});
client.on('data', function(data) {
console.log('Received: ' + data);
//THIS DATA SHOULD BE SHOWN IN HTML for example
//client.destroy(); // kill client after server's response
});
client.on('close', function() {
console.log('Connection closed');
});
For getting data off a Pi and into a Web page, take a look at some examples doing this using WAMP (an open protocol which runs on top of WebSocket) and Crossbar.io (open source router for WAMP) - http://crossbar.io/iotcookbook/Raspberry-Pi/ 为了使数据从Pi进入Web页面,请看一些使用WAMP(在WebSocket之上运行的开放协议)和Crossbar.io(用于WAMP的开源路由器)执行此操作的示例-http:// crossbar .io / iotcookbook / Raspberry-Pi /
Full disclosure: I'm working on these projects - but they are open source, and a great fit for what the OP wants to do. 完全公开:我正在研究这些项目-但它们是开源的,非常适合OP要做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.