自制wifi开机卡

最近路由器和电脑分开了,电脑不连有线,导致没法用网络唤醒。
于是乎在淘宝上找了找wifi开机卡,发现都有点小贵

翻了翻,抽屉里有一块闲置的8266板子,遂掏出来折腾

硬件

就是开发板接了个继电器而已,继电器吸合控制开机键连通:

       VCC(5v)
        ▲
        │                        开机键一端
     ┌──┴──────────────┐     ┌────────────
     │               ┌─┴─────┴─┐
     │               │         │继电器
┌────┴────┐          └─┬─────┬─┘
│         │            │     └────────────
│         │   │        │        开机键另一端
│         │   ├────────┘
│    GPIO5├───┤    NPN三极管
│         │   ├─────► ─┐
│ 8266    │   │        │
└────┬────┘            │
     └───────┬─────────┘
             │
           ──┴──
            ───
             ─
            GND

另外供电偷懒,直接连的usb外部供电,同时也能和主板电源隔离,防止烧了

实际焊接效果(及其省钱):

引出来的线,两根插主板,两根插原先的开机键。这样按键开机功能也能正常用

软件

板子刷上nodemcu,几行代码搞定了:

function mqtt_connect()
    local m = mqtt.Client("client id,确保唯一,请自行修改", 120, "xxx", "xxx", 1)
    m:on("connect", function()
                        print("mqtt connected")
                        --订阅的主题请按自己需求来改,如果用的是公共的,请写一个没有重复可能性的
                        m:subscribe("/my/pc/control/on",0,
                          function() print("subscribe success") end)
                    end)
    m:on("message", function(client, topic, data)
                        if data == "turn_on" then--payload数据匹配上再开机
                            local pon = tmr.create()--开一秒继电器即可
                            gpio.write(5, gpio.HIGH)
                            pon:register(1000, tmr.ALARM_SINGLE,
                            function (t)
                                gpio.write(5, gpio.LOW)
                                t:unregister()
                            end)
                            pon:start()
                        end
                    end)
    --掉线直接重启好啦
    m:on("offline", function() node.reset() end)
    m:on("connfail", function() node.reset() end)
    m:connect("broker.emqx.io",1883)--这里演示用的公共mqtt服务器,建议用自己的
end

wifi.setmode(wifi.STATION)
station_cfg = {}
station_cfg.ssid = "Xiaomi_xxxx" --wifi名称
station_cfg.pwd = "12345678" --密码
wifi.sta.config(station_cfg)
wifi.sta.connect()
wifi.sta.autoconnect(1)--自动重连

gpio.mode(5, gpio.OUTPUT)--我用的GPIO5,看情况改着用
gpio.write(5, gpio.LOW)

local wifitime = tmr.create()
wifitime:register(1000,tmr.ALARM_AUTO,
function()
    if not wifi.sta.getip() then
        print("waiting")
    else
        wifitime:unregister()
        mqtt_connect()
    end
end)
wifitime:start()

代码就是连上wifi,去连mqtt服务器,等收到订阅的消息后,打开gpio5一秒以使继电器通一秒

控制页面

这个只要能发送mqtt数据就行,你用个mqtt调试工具来发都可以

我使用的是网页控制php发送mqtt数据,这样方便点

使用了phpMQTT库:https://github.com/bluerhinos/phpMQTT

页面代码(很简陋):

<form action="index.php" method="post">
<input type="text" name="password" value="">
<input type="submit" value="on">
</form>

<?php
if(empty($_POST["password"]))
exit;
if($_POST["password"] != "12345678")//设置了个密码,防止被别人随便开关机
{
    echo "password error";
    exit;
}

require("phpMQTT/phpMQTT.php");

$server = "broker.emqx.io";//要用和模块同样的服务器
$port = 1883;
$username = "";
$password = "";
$client_id = "phpMQTT-publisher-123456"; //确保唯一

$mqtt = new phpMQTT($server, $port, $client_id);

if ($mqtt->connect(true, NULL, $username, $password)) {
    $mqtt->publish("/my/pc/control/on", "turn_on", 0);//发送开机命令
    $mqtt->close();
    echo "ok!\n";
} else {
    echo "Time out!\n";
}

如果支持websocket,其实可以直接用js发数据

EOF

1 Comment

发表评论

您的电子邮箱地址不会被公开。