1

愿望: 我有一个设备或微控制器,我将其称为 mc,当使用套接字单击网页按钮时,我需要与之通信。

MC1
<button id ='on1' name='mc1'>On</button>
<button id ='off1' name='mc1'>off</button>

详细信息: 我要完成的是单击按钮时将信息传递给 mc。

我尝试了什么: 目前我可以监听一个端口,可以将数据写入 mc 以及接收数据。为此,我通过服务器 php cli 启动了一个文件。该文件包含这些基本的套接字函数。

 $socket = @socket_create_listen("port#");
 $client = socket_accept($socket);
 socket_write($client, $msg);
 socket_read ($client, 256);

然后 mc 在端口连接到服务器#

问题: 我很难理解如何弥合我的 php 网页与按钮之间的差距,并将按钮已被单击的数据传递给 mc。

尝试解决方案: 我可以让侦听端口的文件运行,然后在单独的文件中写入客户端吗?

附加说明: MC LAN 我想避免设置端口转发和外部 ip 有时会发生变化。出于这些原因,我选择让 MC 建立与服务器的连接,从而允许服务器写入 MC 而无需端口转发和不变的 IP 地址。

谢谢 JT

4

2 回答 2

1

最简单的方法是创建一个在 Web 服务器中运行的简单程序(php 很好)(如 apache,但无论如何)。每当它被调用时,它都会同步打开一个到你的 MC 的套接字并发送数据。

您不会编写任何代码来打开侦听套接字,但您的 php 会使用socket_connect打开客户端套接字到 MC ,发送数据,然后关闭套接字..然后您可以将状态返回给 Web 客户端。

于 2013-08-24T02:31:10.300 回答
0

Based on the assumption that your web server can't directly open a connection to your MC, and that your MC can only create an outbound connection to a static IP. You want a loop in your php cli code, see http://www.php.net/manual/en/function.socket-read.php#73509 - change usleep (1000000); to sleep(1); and after the sleep command, check a database (or the existence of a file) for whether any messages should be sent to the MC. Then on your frontend php script (html), you could write to your database or create a file when the user POST's back for the button click.

于 2013-08-24T02:39:29.843 回答