nodemcu 连接domoticz上传自定义的传感器采集交流电压

首先准备一下的东西:1、一个nodemcu。2、一个domoticz,可以自己搭建一个,树莓派或者vps等等。3、一个交流电压传感器,220v输入,输出5v以内的交流信号。

实现方式,用nodemcu的ADC采集交流传感器的模拟量,每ms读取一个值,一共读取500个值存储在数组中。采用最大值,经过比例换算出220V交流对应的电压,自己要有一个万用表用来调测。在domoticz中添加一个虚拟的电压传感器
虚拟传感器.jpg

通过domoticz的http API来上传数据,具体可以看
https://www.domoticz.cn/wiki/Domoticz_API%E5%8F%8AJSON%E7%BD%91%E5%9D%80#.E7.94.B5.E5.8E.8B
nodemcu上面可以用自带的http模块,因为可以自定义头文件,在上传数据时需要认证。
以下是源码:

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)
--分割参数
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()--每毫秒采集数据一次500个
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()--每两秒采集一次抽取最大的数据

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¶m=udevice&idx=7&nvalue=0&svalue="..AC--idx也要写自己传感器的编码我的是7

http.get("http://xxx自己domoticz服务器地址"..get1, "Authorization: Basic 这里写自己的用户名密码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() ---设置nodemcu-esp8266需要连接wifi网络
---通过UDP连接ESP开头的AP,向端口5000发送命令,命令格式:“AP,wifi名字,wifi密码”中间用英文逗号隔开。
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 是发送端的
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,需要在手机下载一个网络调试软件,比如有人网络助手,或者Socket X。都可以默认连接的IP是192.168.4.1。端口是5000
源码传到nodemcu ,只需修改自己的domoticz的一些数据就可以用了。
不懂的可以问我。

最后上图:
电压.jpg

历史数据.jpg

你可能感兴趣的:(nodemcu 连接domoticz上传自定义的传感器采集交流电压)