My home is in the Northeast countryside , It's cold in winter , Bought a boiler , Need circulating pump . Simply put, the circulating pump starts automatically after the boiler water is hot , Then the hot water is conveyed away , To heating , The hot water is pumped away , Cold water enters the boiler , The temperature drops , The circulating pump is shut down , Wait for the next time the water is heated . Because the house that needs heating is far away from the place where the boiler is burning , So we need circulating pump , If the distance is close, the circulation will be automatically completed by using the principle of hot water upflow and cold water backflow after the water is heated . Of course, there are such devices on the market that use the temperature to automatically control the opening and closing of the circulating pump :
The principle is that there is a thermistor probe ( With magnet adsorption , It can be adsorbed on the boiler wall ), And then a relay controls . When the temperature reaches the set value , The relay is on , The circulating pump starts , The boiler wall temperature drops after circulation , Relay off , The circulating pump is shut down .
Circulating pump :
Because the circulating pump in my home has a large power , The small relay burned out after being started several times , So a layer of AC contactor is connected in the middle ( My old father added ).
This kind of equipment on the market can roughly solve the problem of water circulation , But there are also some details that cannot be solved , for example : When the water temperature of the return pipe also reaches the set temperature value , The circulating pump will always be on , At this time, you need to manually adjust the knob temperature ( Raise height ), Stop the circulating pump ( The high-power circulating pump consumes a lot of electricity ). And so on When the coal inside the furnace is gradually burned ( It is called waterlogging in Northeast dialect ), The boiler temperature does not reach the set value , At this time, it is necessary to lower the knob , Start the circulating pump , Let the residual temperature produced by these coals not be wasted .
Due to the above shortcomings , Someone needs to adjust the knob every few hours . This part of the work is done by my old father , So I want to make an automatic thing to reduce the workload of my old father .
My original idea was that I could adjust the temperature remotely through my mobile phone , At least you don't have to adjust the knob manually , I get up in the middle of the night and use my mobile phone to adjust it . A structure designed based on the original idea :
esp01 modular + Relay module ,220v turn 5v modular + Insert = Networking socket
esp8226 + Temperature sensor + Nixie tube = Real time temperature detection and display networking module
My control system is written in esp8226 in .
Temperature sensor probe fabrication : To drive ds18b20 sensor , Need to be on the data bus (DQ) And VDD Add a... Between the pins 4.7k Ohm resistance ( Pull up resistance ), This is a must , Wrap it with an iron button , There are two small neodymium magnets inside , Finally, fill it with good glue , This forms a temperature sensor probe , Can be adsorbed on the boiler wall :
I made a mistake in this step , take ds18b20 Both the sensor and the pull-up resistor are put into the probe , The temperature sensor fails to read the temperature when the temperature rises , It is because the resistance value increases with the increase of temperature . Later, I put the resistor in the tail to solve this problem .
Packaged probe :
esp8226 + Nixie tube module :
The shell is for me 3D Printed by the printer , There's... In it esp8226 modular .
The firmware
esp8226 and esp01 I burn them all micropython The firmware .
signal communication
At the beginning of communication, I want to use socket Realization , Make it simple HTTP Protocol to communicate , But such control on mobile phones is troublesome , There is no easy-to-use software , I have to do a web Interface , in addition socket Blocking threads .
Then I found out MQTT This kind of agreement , This is a hardware network communication protocol for the Internet of things , Can cope with high latency network environment . A brief introduction MQTT agreement , First, you need a server ( There are also generations { too }{ filter } Manage the server , Running on a computer , Here I run on raspberry pie ), Then all your IOT hardware devices ( Such as esp8226) It's all clients , IOT hardware equipment ( client ) There will be no direct communication between , They all communicate directly with the server , If you need to communicate between hardware devices, you need to publish or subscribe to topics . for example : If the hardware A And hardware B We need to communicate with each other , So first they all need to be connected to Server side , then A Publish a project called topic The theme of , If the hardware B Accept A You need to subscribe topic This theme , That's it A ---> B Communication for , So reverse communication is also a principle ,B Release theme ,A Subscribe to . This agreement has another advantage , You can subscribe to multiple clients of a topic , In this way, multi terminal communication can be realized .mqtt In protocol communication, each client does not know the existence of other clients , They all communicate directly with the server .
mqtt The agreement micropython The firmware ( I don't know if the default firmware of Anxin also exists ) There is a big problem with communication :LmacRxBlk:1
Report errors . for example , If you subscribe to a topic at the beginning , Then you use a non blocking method check_msg()
Handle through the callback function Subscribed topic messages , At this time, if you subscribe to more topics than you check_msg()
The number of times , Then it will be in micropython
An error is reported at the bottom of the firmware LmacRxBlk:1
, Then the communication is interrupted , Simply put, you subscribe to a topic and enter the event loop , There is no time to process the message of the subscription topic , Will lead to this error . The official explanation for this mistake is tcp buffer Resources are not released , because esp8226 Available resources are very limited , But in mqtt In communication, I can't release the connection after every cycle , Then re - establish the connection each time , And the disgusting thing about reporting errors is ,try except
It can't be captured , So the problem can only be solved by design , That is, to ensure that the published interval of the subscribed topic is far less than the cyclic interval , Ensure that the subscribed messages can be processed in time every time . I have solved this problem for a long time , because try except
Can't capture , And not often , Unable to locate , I have no idea why the communication was interrupted .
Code core logic writing
The core control logic I wrote in esp8226
Module , Cycle acquisition temperature , Then publish the temperature as a theme , The mobile phone can subscribe to the theme to view the boiler temperature in real time , There is one in the loop setting temperature
The variable of , If the collected temperature is higher than setting temperature
Then release a Switch theme
Open theme , If the temperature is lower than setting temperature
, Then release a Switch theme
close , And every time you subscribe Set the theme , For modification setting temperature
Variable .
esp01 Control relay , Every time you subscribe Switch theme
That's all right. , And then every time The current state of the switch
Publish as a theme .
I wrote this code logic at the beginning , You only need to publish and set the temperature theme on the mobile phone , But it also needs people to use their mobile phones to adjust for a period of time , Still can't realize automation , Later I revised it esp8226 Code logic in , It's a little complicated .
esp8226 The core logic is the same as above , The switch will be turned on after each detection temperature is higher than the set temperature , However, the switch will not be closed immediately if the temperature is detected to be lower than the set temperature after startup , Instead, wait for the detection temperature to be lower than the set temperature minus a variable, and then issue a switch off topic . This is to solve The detection temperature jumps repeatedly at the set temperature threshold , This will cause the switch to open and close repeatedly in a short time , A subtraction variable is introduced here , I call it Temperature step
. There's a problem here , If the boiler keeps heating up , Then even if the circulating pump is always on , The boiler temperature will not be lower than the set temperature ( The temperature of the return pipe is higher than the set temperature ), Then the circulating pump will be always on , So here I introduce the second variable : Maximum startup time
, I start a timer after each startup , If the timer time exceeds Maximum startup time
, No matter whether the temperature is lower than setting temperature
, Will shut down the circulating pump , And will setting temperature
Improve , Increase the temperature to Temperature step
Add the current temperature , Here we achieve Automatic adjustment setting temperature
Of ( Lift direction ). There's another problem : When the coal in the furnace is burnt out , The temperature gradually decreases , The detected temperature will certainly be much lower than the set temperature , The circulating pump will never start . So here I introduce the third variable ( Callback detection time
), When esp8226 When powered on , Start a timer , If the timing time be equal to Callback detection time
, It will be setting temperature
Adjust to the current temperature , be used for setting temperature
Automatic callback , hypothesis Callback detection time
by 30 minute , So even setting temperature
higher than Check the temperature , It's going to happen 30 Start the circulating pump in one minute , And residual logic to realize a closed loop , In this way... Can be achieved setting temperature
, Up and down automatic adjustment . I also added a variable here Minimum temperature
, If the current temperature is below Minimum temperature
, So even if The current temperature is higher than setting temperature
It will not start the circulating pump , Used when coal is not burned , This will prevent you from 30 Start the circulating pump every minutes , Minimum temperature
Set it higher than the ambient temperature .
here setting temperature
, Temperature step
, Maximum startup time
, Callback detection time
, Minimum temperature
, Can be set through the mobile terminal , I let esp8226 Subscribe to these topics to set these variables .
from machine import Pin,reset
import onewire
from ds18x20 import DS18X20
import time
import tm1637
from umqtt.simple import MQTTClient
def main():
client_id = "esp_temperature"
mserver = '192.168.0.99'
#mserver = '192.168.3.200'
#mserver = 'mq.tongxinmao.com'
tm = tm1637.TM1637(clk=Pin(14), dio=Pin(12))
ow=onewire.OneWire(Pin(4))
d = DS18X20(ow)
rom = d.scan()
def sub_callback(topic, msg):
# print((topic, msg))
nonlocal setTemperature
nonlocal lowTemperature
nonlocal startTime
nonlocal scanTime
nonlocal step
nonlocal startTimeTemp
nonlocal scanTimeTemp
nonlocal switchStatus
data = int(msg.decode())
if topic == b'setTemperature':
setTemperature = data
elif topic == b"setLowTemperature":
lowTemperature = data
elif topic == b"setStartTime":
startTime = data
startTimeTemp = startTime
elif topic == b"setScanTime":
scanTime = data*60
scanTimeTemp = scanTime
elif topic == b"setStep":
step = data
elif topic == b"switchWell":
switchStatus = data
else:
print(" error ")
def publishInfo():
nonlocal client
client.publish("setTemperatureR",str(setTemperature),retain=True)
client.publish("setLowTemperatureR",str(lowTemperature),retain=True)
client.publish("setStartTimeR",str(startTime),retain=True)
client.publish("setScanTimeR",str(int(scanTime/60)),retain=True)
client.publish("setStepR",str(step),retain=True)
client = MQTTClient(client_id, mserver, 0)
client.set_callback(sub_callback)
client.connect()
client.subscribe(b'setTemperature')
client.subscribe(b'setLowTemperature')
client.subscribe(b'setStartTime')
client.subscribe(b'setScanTime')
client.subscribe(b"setStep")
client.subscribe(b"switchWell")
showSet = True
setTemperature = 40
lowTemperature = 40
startTime = 120 # Starting time
scanTime = 1800 #30 minute
step = 7
switchStatus = 0 # 0 Means closing ,1 Open for indication
startTimeTemp = startTime
scanTimeTemp = scanTime
while True:
try:
d.convert_temp()
# Display temperature and set temperature
nowTemperature = d.read_temp(rom[0])
if showSet:
tm.temperature(int(setTemperature))
else:
tm.number(int(nowTemperature*10))
if int(nowTemperature) >= setTemperature and (not switchStatus) and int(nowTemperature) > lowTemperature:# Start above the set temperature
client.publish("switch",'1',retain=True)
startTimeTemp = startTime
if (int(nowTemperature) <= setTemperature - step) and switchStatus:
client.publish("switch",'0',retain=True)
if startTimeTemp <= 0: # If the time exceeds 3 minute , Auto stop , And increase the set temperature
client.publish("switch",'0',retain=True)
setTemperature = int(nowTemperature + step)
startTimeTemp = startTime
if switchStatus:
startTimeTemp -= 1
client.check_msg()
client.publish("temperature",str(round(nowTemperature,2)),retain=True)
publishInfo()
if scanTimeTemp <= 0:
setTemperature = int(nowTemperature - step) # Callback cooling
scanTimeTemp = scanTime
scanTimeTemp -= 1
except Exception as e:
reset()
time.sleep(1)
showSet = not showSet
esp01 In the code :
from machine import Pin
from umqtt.simple import MQTTClient
import time
def main():
def sub_callback(topic, msg):
nonlocal client
nonlocal pin
"""
Receive subscription message callback
"""
if msg == b'0':
pin.off()
else:
pin.on()
client.publish("switchWell",str(pin.value()),retain=True)
client_id = "switch_id"
mserver = '192.168.0.99'
#mserver = 'mq.tongxinmao.com'
pin = Pin(0,Pin.OUT)
client = MQTTClient(client_id, mserver, 0)
client.set_callback(sub_callback)
client.connect()
client.subscribe(b'switch')
while True:
client.check_msg()
client.publish("switchStatus",str(pin.value()),retain=True)
1. Mobile terminal :
MQTT Dash:
IoTMQTTPanel:
On the mobile phone, I still recommend IoTMQTTPanel
, Because it is very convenient to publish the configuration of one panel to another mobile phone , Just publish a topic , Then the receiving end can publish the whole panel by subscribing to a topic with the same name .MQTT Dash
It also has this function , But it will get stuck after the release , do not know why .
2. Computer terminal :
I don't know what software is available on the computer , So I use it PyQt5 Simply write a monitoring software , No adjustment function , Because the equipment was not very stable at that time , So I have been monitoring the running status .
at present , The whole thing has been running steadily for two weeks , There is no need for people to adjust . I've been doing it over and over for a long time , To solve all the problems , Especially the LmacRxBlk:1
Report errors , It took me a long time . It's still very difficult to make something useful from scratch , Because some problems can only be exposed through the actual environment , For example, when the pull-up resistance is heated up, the resistance value changes . There are many details I haven't mentioned , For example, burning firmware ,3d Modeling shell ,mosquitto Service deployed on raspberry pie , Port forwarding , There are too many contents to repeat , Only the main content and logic can be briefly described .
Official account :Python Source code
Get full code