我有一个在终端中运行的脚本,它每 2 秒轮询一次服务器以获取 PHP 脚本中的 0 或 1,然后根据结果执行命令。
不重要的说明:它在树莓派上运行。
所以我的问题是:有没有更好的方式与服务器通信?理想的情况是像 socket.io 或 html5 websocket 类型的连接,但这些在终端上是不可行的。
这是我所拥有的,以便您了解要点
while :
do
gpio mode 0 out
gpio mode 7 out
var1=$(curl http://url.com/index.php)
L1=$(echo $var1 | cut -c1-1)
L7=$(echo $var1 | cut -c7-7)
if [ $L1 == 0 ]; then
gpio write 0 0
elif [ $L1 == 1 ]; then
gpio write 0 1
fi
if [ $L7 == 0 ]; then
gpio write 7 0
elif [ $L7 == 1 ]; then
gpio write 7 1
fi
gpio readall
sleep 2
done
编辑:我需要在同一台服务器上使用它运行大约 1000 个树莓派单元,我只是在寻找最有效的方法。