实际效果
设备、文件版本
-
树莓派:zero w,系统,镜像版本2018-06-27-raspbian-stretch.img
-
模块:ESP8266 12f
-
模块软件: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) -
RGB灯:WS2812B灯珠,4颗
-
其他零散
树莓派初始化
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.gz
tar -zxvf node-v6.14.3-linux-armv6l.tar.gz
cd node-v6.14.3-linux-armv6l
sudo 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")
end
end)
3、tcp.lua
sv = net.createServer(net.TCP, 30)
ws2812.init()
colorData="000000"
switchStatus=0
function 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
end
function table_leng(t)
local leng=0
for k, v in pairs(t) do
leng=leng+1
end
return leng;
end
function 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
end
sv: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要共地。