使用的是NodeMCU v3
ACS712(30A)
使用NodeMCU上的adc來讀取ACS712的類比訊號,直接使用NodeMCU上的VU腳位來供給ACS712 5V的電,輸出的訊號電壓準位為5V,使用2.4k歐姆與4.7k歐姆來降為3.3V,然後接回adc腳位
來源
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid, passwd)
wifi.sta.connect()
wifi.sleeptype(wifi.NONE_SLEEP)
local refreshInterval = .2
local mVperAmp = 100 --100mV on 1A
local Voltage = 0
local VRMS = 0
local AmpsRMS = 0
local maxErr = 0
local avgErr = 0
local totalMeasurement = 0
local TotalPwr = 0
tmr.alarm(0, refreshInterval*1000, tmr.ALARM_SEMI, function()
totalMeasurement = totalMeasurement + 1
Voltage = getVPP()
VRMS = (Voltage/2.0) * 0.707
AmpsRMS = (VRMS * 1000) / mVperAmp
Pwr = AmpsRMS * 220 /10
TotalPwr = TotalPwr + Pwr
print(totalMeasurement..". AvrPwr: "..string.format("%.2f",TotalPwr * 10/totalMeasurement).."W - "..string.format("%.2f",AmpsRMS).."A - "..string.format("%.2f",Pwr * 10).."W")
print(" ")
tmr.start(0)
end)
function getVPP()
local result = 0
local readValue = 0
local maxValue = 0
local minValue = 1024
local nbrReadings = 0
while (nbrReadings<1000 do="" if="" readvalue=""> maxValue) then
maxValue = readValue
elseif (readValue < minValue) then
minValue = readValue
end
nbrReadings = nbrReadings + 1
end
print(totalMeasurement..". Max: "..maxValue.." - Min: "..minValue)
result = (math.abs(maxValue - minValue - 6) * 5)/1024
if (maxValue - minValue > maxErr) then
maxErr = maxValue - minValue
end
avgErr = avgErr + (maxValue - minValue) / 10
print(totalMeasurement..". Err: "..maxValue - minValue.." maxErr: "..maxErr.. " AvgErr: "..string.format("%.2f", avgErr * 10/totalMeasurement))
return result
end
1000>
這是加了判斷電器是否開啟,並根據條件發送通知的小改版本
以吹風機為例,一打開就發送通知。
local refreshInterval = 2
local mVperAmp = 66 --66mV on 1A
local Voltage = 0
local VRMS = 0
local AmpsRMS = 0
local maxErr = 0
local avgErr = 0
local totalMeasurement = 0
local TotalPwr = 0
airConditionerStatus = false
sendStatus = false
tmr.alarm(0, refreshInterval*1000, tmr.ALARM_AUTO, function()
totalMeasurement = totalMeasurement + 1
Voltage = getVPP()
VRMS = (Voltage/2.0) * 0.707
AmpsRMS = (VRMS * 1000) / mVperAmp
if AmpsRMS>3 then
airConditionerStatus = true
while airConditionerStatus do
if airConditionerStatus and sendStatus~=true then
sendHttpRequest()
sendStatus = true
print(airConditionerStatus)
else break
end
end
else
airConditionerStatus = false
sendStatus = false
end
Pwr = AmpsRMS * 220 /10
TotalPwr = TotalPwr + Pwr
print(totalMeasurement..". AvrPwr: "..string.format("%.2f",TotalPwr * 10/totalMeasurement).."W - "..string.format("%.2f",AmpsRMS).."A - "..string.format("%.2f",Pwr * 10).."W")
print(" ")
end)
function sendHttpRequest()
http.post("http://192.168.11.5:8101/ESP8266/Service",
"Content-Type: application/json\r\n",
"{\"Power\":\"on\"}",
function(code, data)
if (code < 0) then
print("HTTP request failed")
else
print(code, data)
end
end)
--http://httpbin.org/ip
end
function getVPP()
local result = 0
local readValue = 0
local maxValue = 0
local minValue = 1024
local nbrReadings = 0
while (nbrReadings<1000) do
readValue = adc.read(0)
if (readValue > maxValue) then
maxValue = readValue
elseif (readValue < minValue) then
minValue = readValue
end
nbrReadings = nbrReadings + 1
end
print(totalMeasurement..". Max: "..maxValue.." - Min: "..minValue)
result = (math.abs(maxValue - minValue - 6) * 5)/1024
if (maxValue - minValue > maxErr) then
maxErr = maxValue - minValue
end
avgErr = avgErr + (maxValue - minValue) / 10
print(totalMeasurement..". Err: "..maxValue - minValue.." maxErr: "..maxErr.. " AvgErr: "..string.format("%.2f", avgErr * 10/totalMeasurement))
return result
end
--http://bit.ly/2x4yyFR