2017/10/1

mac MQTT(mosquitto)安裝

請先安裝Homebrew,要用Homebrew安裝mosquitto

安裝mosquitto
$ brew intall mosquitto
我安裝過程有出現一些Error訊息,所以安裝完後又把mosquitto刪掉,相依的套件也跟著刪掉再重新安裝一次。
Homebrew會裝在/usr/local/Cellar/mosquitto這個位置

把mosquitto加入.zshrc環境變數,這樣才可以在全域下指令操作它
打開.zshrc
$ vi .zshrc

在上面加入這行
export PATH=/usr/local/Cellar/mosquitto/1.4.14_1/sbin:$PATH

馬上讓.zshrc生效
$ source .zshrc



用brew啟動mosquitto服務
$ brew services start mosquitto

用brew停止mosquitto服務
$ brew services stop mosquitto

用brew重啟mosquitto服務
$ brew services restart mosquitto

用NodeMCU adc讀取ACS712訊號並轉換電壓電流

使用的是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


這是加了判斷電器是否開啟,並根據條件發送通知的小改版本 以吹風機為例,一打開就發送通知。
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