博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
树莓派Zero W 通过homebridge ,8266制作siri控制的RGB灯
阅读量:4123 次
发布时间:2019-05-25

本文共 4684 字,大约阅读时间需要 15 分钟。

实际效果

设备、文件版本

  1. 树莓派:zero w,系统,镜像版本2018-06-27-raspbian-stretch.img

  2. 模块:ESP8266 12f

  3. 模块软件:NodeMCU,

    NodeMCU版本及模块:
    modules: crypto,ds18b20,encoder,enduser_setup,file,gpio,http,mqtt,net,node,sjson,tmr,uart,websocket,wifi,ws2812
    build created on 2018-08-01 05:22
    powered by Lua 5.1.4 on SDK 2.2.1(cfd48f3)

  4. RGB灯:WS2812B灯珠,4颗

  5. 其他零散

树莓派初始化

1、安装node.js,先使用如下命令查看cpu信息

uname -a

得到如下

Linux raspberrypi 4.14.50+ #1122 Tue Jun 19 12:21:21 BST 2018 armv6l GNU/Linux

树莓派zero w为armv6l,所以安装armv6l的node.树莓派用pi用户安装老是出问题,所以我切换到root安装

su root  --输入密码wget https://nodejs.org/dist/latest-v6.x/node-v6.14.3-linux-armv6l.tar.gztar -zxvf node-v6.14.3-linux-armv6l.tar.gzcd node-v6.14.3-linux-armv6lsudo cp -R * /usr/local/su pi --切换回去cd ~

2、安装Avahi和其他依赖项

sudo apt-get install libavahi-compat-libdnssd-dev

3、安装homebridge

sudo npm install -g --unsafe-perm homebridge

4、启动一次homebridge

homebridge

5、Ctrl+c停止homebridge,安装homebridge-http-rgb-bulb插件

sudo npm install -g homebridge-http-rgb-bulb

6、编辑homebridge配置文件

nano .homebridge/config.json--输入如下内容:{    "bridge": {        "name": "Homebridge",        "username": "CC:22:3D:E3:CE:30", --填写树莓派mac        "port": 51826,        "pin": "031-45-154"    },    "description": "Bla Bla Bla",    "accessories": [{        "accessory": "HttpRGB",        "name": "RGB Bulb",        "set_url": "http://ip/rgb?color=0x%s",        "get_url": "http://ip/rgb", --ip为nodemcu的ip地址        "http_method": "GET"    }],    "platforms": []}

7、运行homebridge,树莓派上处理完了

iPhone设置

1、打开家庭应用

2、点击添加,添加配件,没有代码或无法扫描,输入代码
3、然后就可以看到一个设备出来了,点击添加,之后回到家庭主界面,应该可以看到一个RGB设备了

NodeMcu配置

1、刷好固件后,要确保有net,ws2812模块,然后编辑两个lua文件,一个init.lua,一个tcp.lua。

2、init.lua文件

cfg={}cfg.ssid="ssid"cfg.pwd="password"print("set up wifi mode")wifi.setmode(wifi.STATION)wifi.sta.config(cfg)wifi.sta.autoconnect(1)tmr.alarm(1, 1000, 1, function()    if wifi.sta.getip()== nil then        print("IP unavaiable, Waiting..")    else        tmr.stop(1)        print("Config done, IP is "..wifi.sta.getip())    dofile("tcp.lua")    endend)

3、tcp.lua

sv = net.createServer(net.TCP, 30)ws2812.init()colorData="000000"switchStatus=0function stringsplit(input, delimiter)      input = tostring(input)      delimiter = tostring(delimiter)      if (delimiter=='') then return false end      local pos,arr = 0, {}      for st,sp in function() return string.find(input, delimiter, pos, true) end do          table.insert(arr, string.sub(input, pos, st - 1))          pos = sp + 1      end      table.insert(arr, string.sub(input, pos))      return arr  endfunction table_leng(t)  local leng=0  for k, v in pairs(t) do    leng=leng+1  end  return leng;endfunction receiver(sck,data)    print (data)    if (data~=nil) then        local _, _, method, path, vars = string.find(data, "([A-Z]+) (.+)?(.+) HTTP");        if(method == nil)then            _, _, method, path = string.find(data, "([A-Z]+) (.+) HTTP");        end        if (path~=nil) then            pathList=stringsplit(path,"/")            len=table_leng(pathList)            print(len)            print(pathList[4])            if (len==4) then                if (pathList[4]=="status") then                    switchStatus=switchStatus                    local html = string.format("HTTP/1.0 200 OK\r\n"                    .."Content-Type: text/html\r\n"                    .."Connection: Close\r\n\r\n"                    ..switchStatus)                    sck:send(html)                    print("switch status is send:"..switchStatus)                elseif (pathList[4]=="on") then                    switchStatus=1                elseif (pathList[4]=="off") then                    switchStatus=0                elseif (pathList[4]=="set") then                    colorData=colorData                    local html = string.format("HTTP/1.0 200 OK\r\n"                    .."Content-Type: text/html\r\n"                    .."Connection: Close\r\n\r\n"                    ..colorData)                    sck:send(html)                    print("color data is send:"..colorData)                end            elseif (len==5) then                print(pathList[5])                if(pathList[4]=="set") then                    r=tonumber(string.sub(pathList[5],1,2),16)                    g=tonumber(string.sub(pathList[5],3,4),16)                    b=tonumber(string.sub(pathList[5],5,6),16)                    colorData=colorData                    ws2812.write(string.char(r,g,b,r,g,b,r,g,b,r,g,b))                end            end        end    end        endsv:listen(80,function(conn)  conn:on("receive",receiver)  conn:on("sent",function(conn) conn:close() end)end)

4、NodeMCU接线

RGB的三个接口,vcc,gnd,IND,只使用in那一边三个接口,IND接口接8266的GPIO 2,也就是nodemcu模块的4号引脚。
注意8266与RGB要共地。

应该就这么多了,有问题欢迎提问!

你可能感兴趣的文章
C 语言-static和extern关键字2-对变量的作用
查看>>
【JavaScript 教程】浏览器—History 对象
查看>>
还不会正则表达式?看这篇!
查看>>
100道+ JavaScript 面试题,助你查漏补缺
查看>>
JavaScript深入理解之闭包
查看>>
这才是学习Vite2的正确姿势!
查看>>
7 个适用于所有前端开发人员的很棒API,你需要了解一下
查看>>
25个构建Web项目的HTML建议,你需要了解一下!
查看>>
【web素材】02-10款大气的购物商城网站模板
查看>>
6种方式实现JavaScript数组扁平化(flat)方法的总结
查看>>
如何实现a===1 && a===2 && a===3返回true?
查看>>
49个在工作中常用且容易遗忘的CSS样式清单整理
查看>>
20种在学习编程的同时也可以在线赚钱的方法
查看>>
隐藏搜索框:CSS 动画正反向序列
查看>>
12 个JavaScript 特性技巧你可能从未使用过
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(上)
查看>>
【视频教程】Javascript ES6 教程27—ES6 构建一个Promise
查看>>
【5分钟代码练习】01—导航栏鼠标悬停效果的实现
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(中)
查看>>
8种ES6中扩展运算符的用法
查看>>