本帖最后由 jedmeng 于 2018-10-19 20:16 编辑
第四部分:代码
代码写的比较匆忙比较乱,就不全放上来了,只把控制相关的部分贴上来。
1. 水泵和电磁阀
local isPumoOn = false
-- 初始化
gpio.mode(GPIO_BUTTON_PIN, gpio.INT, gpio.PULLUP)
gpio.mode(GPIO_PUMP_PIN, gpio.OUTPUT)
gpio.mode(GPIO_VALVE_PIN, gpio.OUTPUT)
gpio.write(GPIO_PUMP_PIN, GPIO_PUMP_INIT)
gpio.write(GPIO_VALVE_PIN, GPIO_VALVE_INIT)
-- 订阅mqtt消息
MQTT.subscribe(MQTT_TOPIC_PUMP, function(payload)
if payload == MQTT_PAYLOAD_ON and not isPumoOn then
turnPumpOn()
elseif payload == MQTT_PAYLOAD_OFF and isPumoOn then
turnPumpOff()
end
end)
-- 按键防抖
local buttonTrigger = false
buttonTimer = tmr.create()
buttonTimer:register(100, tmr.ALARM_SEMI, function()
buttonTrigger = false
end)
-- 响应按键动作
gpio.trig(GPIO_BUTTON_PIN, 'low', function()
if gpio.read(GPIO_BUTTON_PIN) == gpio.HIGH then
return
end
buttonTimer:stop()
buttonTimer:start()
if buttonTrigger == true then
return
end
buttonTrigger = true
debug("pump button trigger")
if isPumoOn then
turnPumpOff()
else
turnPumpOn()
end
end)
-- 间隔100ms查询水温,到达阈值后关闭水泵
timer = tmr.create()
timer:register(100, tmr.ALARM_AUTO, function()
if THERM.temperature() >= TEMPERATURE_THRESHOULD then
turnPumpOff()
end
end)
-- 开启水泵和电磁阀
function turnPumpOn()
isPumoOn = true
gpio.write(GPIO_VALVE_PIN, GPIO_VALVE_ON)
gpio.write(GPIO_PUMP_PIN, GPIO_PUMP_ON)
MQTT.publish(MQTT_TOPIC_PUMP, MQTT_PAYLOAD_ON)
THERM.start()
timer:start()
debug("pump on")
end
-- 关闭水泵和电磁阀
function turnPumpOff()
isPumoOn = false
gpio.write(GPIO_PUMP_PIN, GPIO_PUMP_OFF)
gpio.write(GPIO_VALVE_PIN, GPIO_VALVE_OFF)
MQTT.publish(MQTT_TOPIC_PUMP, MQTT_PAYLOAD_OFF)
THERM.stop()
timer:stop()
debug("pump off")
end
2. 温度传感器
-- 计算温度用到的两个常数
local RTD_A = 3.9083e-3
local RTD_B = -5.775e-7
-- 这个是MAX31865模块中参照电阻的阻值,一般在400左右
local R_REF = 430
THERM = {}
-- 初始化温度传感器
function THERM.init()
spi.setup(SPI_ID, spi.MASTER, spi.CPOL_LOW, spi.CPHA_HIGH, 8, 40)
gpio.mode(SPI_PIN_CS, gpio.OUTPUT)
gpio.write(SPI_PIN_CS, gpio.LOW)
-- 设置使用三线电阻
local config = THERM.read(0x00)
config = bit.bor(config, 0x10)
config = 0xD2
THERM.write(0x00, config)
end
-- 从SPI读入
function THERM.read(add)
gpio.write(SPI_PIN_CS, gpio.LOW)
spi.send(SPI_ID, add)
local data = spi.recv(SPI_ID, 1)
gpio.write(SPI_PIN_CS, gpio.HIGH)
return string.byte(data)
end
-- 写入到SPI
function THERM.write(add, data)
gpio.write(SPI_PIN_CS, gpio.LOW)
spi.send(SPI_ID, bit.bor(add, 0x80), data)
gpio.write(SPI_PIN_CS, gpio.HIGH)
end
-- 开始测温
function THERM.start()
-- 使能BIAS、开启自动转换
local config = THERM.read(0x00)
config = bit.bor(config, 0xC0)
THERM.write(0x00, config)
debug("thermonmeter start")
end
-- 结束测温
function THERM.stop()
-- 去使能BIAS、关闭自动转换
local config = THERM.read(0x00)
config = bit.band(config, 0x3)
THERM.write(0x00, config)
debug("thermonmeter stop")
end
-- 读取并计算温度
function THERM.temperature()
local r1 = THERM.read(0x01)
local r2 = THERM.read(0x02)
local adc = bit.lshift(r1, 7) + bit.rshift(r2, 1)
local rtd = adc * R_REF / 32768
local z1 = -RTD_A
local z2 = RTD_A * RTD_A - (4 * RTD_B)
local z3 = (4 * RTD_B) / 100
local z4 = 2 * RTD_B
local temp = (math.sqrt(z2 + (z3 * rtd)) + z1) / z4;
debug("temperature: " .. temp)
return temp
end
THERM.init()
|