nodemcu 連接domoticz上傳自定義的傳感器采集交流電壓

首先準(zhǔn)備一下的東西:1、一個(gè)nodemcu。2、一個(gè)domoticz,可以自己搭建一個(gè),樹(shù)莓派或者vps等等。3、一個(gè)交流電壓傳感器,220v輸入,輸出5v以內(nèi)的交流信號(hào)。

實(shí)現(xiàn)方式,用nodemcu的ADC采集交流傳感器的模擬量,每ms讀取一個(gè)值,一共讀取500個(gè)值存儲(chǔ)在數(shù)組中。采用最大值,經(jīng)過(guò)比例換算出220V交流對(duì)應(yīng)的電壓,自己要有一個(gè)萬(wàn)用表用來(lái)調(diào)測(cè)。在domoticz中添加一個(gè)虛擬的電壓傳感器
虛擬傳感器.jpg

通過(guò)domoticz的http API來(lái)上傳數(shù)據(jù),具體可以看
https://www.domoticz.cn/wiki/Domoticz_API%E5%8F%8AJSON%E7%BD%91%E5%9D%80#.E7.94.B5.E5.8E.8B
nodemcu上面可以用自帶的http模塊,因?yàn)榭梢宰远x頭文件,在上傳數(shù)據(jù)時(shí)需要認(rèn)證。
以下是源碼:

led1 = 4
stat=1
blink=2000
gpio.mode(led1, gpio.OUTPUT)
gpio.write(led1, gpio.HIGH);
local parameter1,parameter2,parameter3,parameter4,parameter5=nil,nil,nil,nil,nil
print('Setting up WIFI...')
wifi.setmode(wifi.STATIONAP )
wifi.sta.autoconnect(1)

tmr.alarm(1, 2000, tmr.ALARM_AUTO, function()
    if wifi.sta.getip() == nil then
        print('Waiting for IP ...')

    else
        print('IP is ' .. wifi.sta.getip())
    tmr.stop(1)
    end
end)
tmr.alarm(4,blink, tmr.ALARM_AUTO, function()
gpio.write(led1, gpio.LOW);
tmr.delay(1000)
gpio.write(led1, gpio.HIGH);
end)

function stringgsub(data)
--分割參數(shù)
aa=string.find(data,",")
ss=string.find(data,",",aa+1)

parameter1=string.sub(data,1,aa-1)
parameter2=string.sub(data,aa+1,ss-1)
parameter3=string.sub(data,ss+1,-1)
return parameter1,parameter2,parameter3
end

if adc.force_init_mode(adc.INIT_ADC)
then
  node.restart()
  return -- don't bother continuing, the restart is scheduled
end
val={}
i=1
--a=0
vall=0
max=0
tmr.alarm(2, 1, tmr.ALARM_AUTO, function()--每毫秒采集數(shù)據(jù)一次500個(gè)
val[i] = adc.read(0)
i=i+1
if i>500 then 
i=0 
tmr.stop(2)
end
end)

tmr.alarm(3,2000, tmr.ALARM_AUTO, function()--每?jī)擅氩杉淮纬槿∽畲蟮臄?shù)據(jù)

for j=1,500 do
if val[j]~=nil then
if max < val[j] then 
max=val[j]
end
end
end
print("adc is:"..max)
AC=(max-765)*1.06--比例換算
print("ADC voltage (V):"..AC)
local testjson={}
testjson={key=AC,curr="30",temp="38"}
local ok, json = pcall(sjson.encode,testjson)
if ok then
  print(json)
else
  print("failed to encode!")
end
max=0
tmr.start(2)
end)
---------------send AV voltage to domoticz

tmr.alarm(5, 15000, tmr.ALARM_AUTO,function() 
conn=net.createConnection(net.TCP, 0) 
conn:connect(8080,"xxxx。com")
conn:on("receive", function(conn, payload)
print("The result is"..payload) 

end )
conn:on("connection", function(conn, payload)
local get1="/json.htm?type=command&param=udevice&idx=7&nvalue=0&svalue="..AC--idx也要寫(xiě)自己傳感器的編碼我的是7

http.get("http://xxx自己domoticz服務(wù)器地址"..get1, "Authorization: Basic 這里寫(xiě)自己的用戶名密碼base64編碼", function(code, data)
    if (code < 0) then
      print("HTTP request failed")
    else
      print(code, data)
    end
  end)
print("The AC voltage is send:"..AC)
if blink~=5000 then
blink=5000
tmr.unregister(4)
tmr.register(4,blink,tmr.ALARM_AUTO,function()
gpio.write(led1, gpio.LOW);
tmr.delay(3000)
gpio.write(led1, gpio.HIGH);end)
tmr.start(4)
end

end
)

end)
udpSocket = net.createUDPSocket() ---設(shè)置nodemcu-esp8266需要連接wifi網(wǎng)絡(luò)
---通過(guò)UDP連接ESP開(kāi)頭的AP,向端口5000發(fā)送命令,命令格式:“AP,wifi名字,wifi密碼”中間用英文逗號(hào)隔開(kāi)。
udpSocket:listen(5000)
udpSocket:on("receive", function(s, data, port, ip)
local parameter1,parameter2,parameter3=stringgsub(data)
if blink~=500 then
blink=500
tmr.unregister(4)
tmr.register(4,blink,tmr.ALARM_AUTO,function()
gpio.write(led1, gpio.LOW);
tmr.delay(1000)
gpio.write(led1, gpio.HIGH);end)
tmr.start(4)
end

if parameter1 == "AP" then
print(string.format("APP: %s:%d",ip, port))
--IP AND PORT 是發(fā)送端的
print("ap is:"..parameter2.."pass is:"..parameter3.."reconnected")
station_cfg={}
station_cfg.ssid=parameter2
station_cfg.pwd=parameter3
wifi.sta.config(station_cfg)

s:send(port,ip,"Will be reconnecting,wait a moument.")

end
end)

修改wifi的連接SSID,需要在手機(jī)下載一個(gè)網(wǎng)絡(luò)調(diào)試軟件,比如有人網(wǎng)絡(luò)助手,或者Socket X。都可以默認(rèn)連接的IP是192.168.4.1。端口是5000
源碼傳到nodemcu ,只需修改自己的domoticz的一些數(shù)據(jù)就可以用了。
不懂的可以問(wèn)我。

最后上圖:
電壓.jpg

歷史數(shù)據(jù).jpg
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容