树莓派-改造插线板,支持手机平板控制开关

改造原理图:


第1步:搭建Apache+PHP环境

具体步骤可以在网上搜索,此处只描述程序编写部分。

第2步:编写PHP网页端

一共2个页面,1个手机访问页面index.php,1个ajax请求响应页面ajax.php,代码如下:

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="text/javascript" src="jquery-3.2.1.min.js"></script>
    <title>树莓派网页开关</title>
    <style type="text/css">
        .main { text-align: center; }
        #btnSwitch { padding: 2em; width: 80%; background: gray; color: white; border: 0; font-weight: bold; font-size: 1em; border-radius: 1em; display: block; margin: 2em auto; }
        .on { background: red !important; }
    </style>
    <script type="text/javascript">
        var _status = false;
        var url = "ajax.php";
        $(function () {
            initStatus();
            $.ajax({
                type: "POST",
                url: url,
                data: {
                    action: "get-status"
                },
                success: function (result) {
                    _status = (result == "true");
                    initStatus();
                }
            });

            $("#btnSwitch").click(function () {
                _status = (!_status);
                $.ajax({
                    type: "POST",
                    url: url,
                    data: {
                        action: "set-status",
                        status: _status
                    },
                    success: function (result) {
                        initStatus();
                    }
                });
            });
        })
        function initStatus() {
            if (_status) {
                $("#btnSwitch").text("点击关闭").addClass("on");
            } else {
                $("#btnSwitch").text("点击开启").removeClass("on")
            }
        }
    </script>
</head>
<body>
    <div class="main">
        <h3>
            树莓派网页开关</h3>
        <span id="btnSwitch"></span>
    </div>
</body>
</html>
ajax.php

<?php
$action=$_POST[action];
$status=$_POST[status];
if($action=="get-status"){
	$str=file_get_contents("flag.txt");
	echo($str);	
}else{
	$myfile=fopen("flag.txt","w") or die("unable to open file!");
	fwrite($myfile,$status); 
	$str=file_get_contents("flag.txt");
	echo($str);	
}
?>
第3步:Python读取配置文件

import RPi.GPIO as GPIO
from gpiozero import Buzzer
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)  
GPIO.setup(17,GPIO.OUT)
GPIO.setup(25,GPIO.OUT)

#for i in range(1,101):
while True:
    file=open("/var/www/html/discuz/pi/flag.txt")
    text=file.read()
    file.close()
    if(text=="true"):
        #print(text)
        GPIO.output(17,GPIO.HIGH)  
        GPIO.output(25,GPIO.HIGH)      
        
    else:
        GPIO.output(17,GPIO.LOW)
        GPIO.output(25,GPIO.LOW)
        #print(text)
    #time.sleep(.2)

这里接了一个二极管灯和继电器,所以用了2个GPIO接口,可以根据实际情况自行调整。

第4步:连接电路

可以参考原理示意图,注意继电器的入口电压接口和出口电压接口。

第5步:测试电路

上一张完成了的图,继电器被放在了插座内部。


由于涉及220V高压,操作前请务必检查电路并做好安全防护,注意用电安全!!!




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值