TA的每日心情 | 开心 4 小时前 |
---|
签到天数: 367 天 连续签到: 15 天 [LV.9]以坛为家II
|
本帖最后由 eefocus_3914144 于 2024-2-25 22:55 编辑
这段时间降温好大呀,好冷,为此做一款智能取暖器,是不是很实用呀。
1、材料
(1)报废取暖器一个(加热丝没有坏)
(2)开关电源(220V转5V)
(3)固态继电器1个
(4)DRF0654开发板一个。
(5)Sh30温湿度传感器
2、开发环境
(1)Win10
(2)Arduino
(3)Python11
(4)Pycharm
3、DRF0564工作流程
[size=10.5000pt]
[size=10.5000pt]4、远程桌面工作流程
软件源代码】
DRF0654
- // This example uses an ESP32 Development Board
- // to connect to shiftr.io.
- //
- // You can check on your device after a successful
- // connection here: https://www.shiftr.io/try.
- //
- // by Joël Gähwiler
- // https://github.com/256dpi/arduino-mqtt
-
- #include <WiFi.h>
- #include <MQTT.h>
- #include "Wire.h"
- #include "SHT31.h"
-
- #define HAETPIN D9 //加热开关
- #define SHT31_ADDRESS 0x44
-
-
- #define defultWorkValue 25;
-
- SHT31 sht;
- char buffer[80];
- const char ssid[] = "SSID";
- const char pass[] = "pwd";
-
- int temperUpValue; //最高值
- int temperDownValue; //最低值
- float thisTemperValud;
- int temperSetValue; //设置目标值
-
- int swtickState = 0; //开关状态
- int onoff = 0; //是否打开取暖器
- WiFiClient net;
- MQTTClient client;
-
- unsigned long lastMillis = 0;
-
- void hearContrl(void)
- {
- if(onoff == 1)
- {
- if(thisTemperValud > temperSetValue +1)
- {
- digitalWrite(HAETPIN, LOW);
- }
- else if(thisTemperValud < temperSetValue -1)
- {
- digitalWrite(HAETPIN, HIGH);
- }
- }
- else
- {
- digitalWrite(HAETPIN, LOW);
- }
- }
- void connect() {
- Serial.print("checking wifi...");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(1000);
- }
-
- Serial.print("\nconnecting...");
- while (!client.connect("clietid", "username", "pwd")) {
- Serial.print(".");
- delay(1000);
- //连接后,发送获取状态:
-
- }
-
- Serial.println("\nconnected!");
-
- client.subscribe("/topic/#");
- //连接后发送远程状态
-
- }
-
- void messageReceived(String &topic, String &payload) {
-
- //如果是开机,
- //设置目标温度值
- //如果小于温度值测发出开机信号
-
- //如果是设置温度值测更新温度值
- if(topic == "/topic/hearset")
- {
- Serial.println("incoming: " + topic + " - " + payload);
- temperSetValue = payload.toInt();
- }
- else if(topic == "/topic/heartonoff")
- {
- Serial.println("incoming: " + topic + " - " + payload);
- if(payload == "on")
- {
- //对应的IO高电平
- onoff = 1;
-
- }
- else if(payload == "off")
- {
- //对应的IO低电平
- onoff = 0;
-
- }
- }
- // Note: Do not use the client in the callback to publish, subscribe or
- // unsubscribe as it may cause deadlocks when other things arrive while
- // sending and receiving acknowledgments. Instead, change a global variable,
- // or push to a queue and handle it in the loop after calling `client.loop()`.
- }
-
- void setup() {
- pinMode(HAETPIN,OUTPUT);
- digitalWrite(HAETPIN, LOW);
- onoff = 0;
- Wire.begin();
- Wire.setClock(100000);
- sht.begin();
-
- uint16_t stat = sht.readStatus();
- Serial.print(stat, HEX);
- Serial.println();
-
- Serial.begin(115200);
- WiFi.begin(ssid, pass);
-
- // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
- // by Arduino. You need to set the IP address directly.
- client.begin("域名", net);
- client.onMessage(messageReceived);
-
- connect();
- }
-
- void loop() {
- client.loop();
- delay(10); // <- fixes some issues with WiFi stability
-
- if (!client.connected()) {
- connect();
- }
-
- // publish a message roughly every second.
- if (millis() - lastMillis > 1000) {
- lastMillis = millis();
- sht.read();
- thisTemperValud = sht.getTemperature();
- snprintf(buffer, sizeof(buffer), "{"Temperature":"%.1f", "Humidity":"%.1f%", "WorkState":"%d"}", thisTemperValud , sht.getHumidity(), onoff);
- Serial.println(buffer);
- client.publish("/topic/heart", buffer);
- hearContrl();
- }
-
- }
复制代码 【控制界面】
采用QT来制作
[size=10.5000pt]控制端程序:
- import sys
- import time
- import json
- import re
- from paho.mqtt import client as mqtt
- from PyQt5.QtWidgets import QWidget, QApplication
- from PyQt5.QtCore import QTime, QTimer
- from electriheater import Ui_MainWindow
- from PyQt5.QtWidgets import QApplication, QMainWindow
- class AppGui(QMainWindow,Ui_MainWindow):
- def __init__(self):
- super(AppGui,self).__init__()
- self.setupUi(self)
- self.lcdNumberThishTemp.display(10.233)
- self.pushButton_client_sever.clicked.connect(self.connect_mqtt)
- self.horizontalSliderSetting.valueChanged.connect(self.setLcdShow)
- self.horizontalSliderSetting.sliderReleased.connect(self.temperSet)
- self.pushButtonOnOff.clicked.connect(self.openoffHeart)
- def openoffHeart(self):
- if self.pushButtonOnOff.text() == "开启取暖器":
- self.on_publish("/topic/heartonoff", "on", 1)
- # self.pushButtonOnOff.setText("关闭取暖器")
- else:
- self.on_publish("/topic/heartonoff", "off", 1)
- # self.pushButtonOnOff.setText("开启取暖器")
- def mqttlj(self):
- client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
- self.client = mqtt.Client(client_id)
- self.client.username_pw_set(self.Username,self.Password)
- self.client.on_connect = self.on_connect
- self.client.on_message = self.on_message
- self.client.connect(self.HOST,self.PORT,600)
- self.client.loop_start()
- print(self.Username, self.Password, self.HOST, self.PORT)
- def setLcdShow(self):
- self.lcdNumberSetTemp.display(self.horizontalSliderSetting.value())
- def temperSet(self):
- tempSetValue = self.horizontalSliderSetting.value()
- print("设置温度")
- self.on_publish("/topic/hearset",str(tempSetValue),1)
- # 发送publissh
- def conn_Ck(self):
- self.Username = "username"
- self.Password = "pwd"
- self.HOST = "域名"
- self.PORT = 1883
- self.SubTopic = "/topic/heart"
- self.PubTopic = "/topic/hearset"
- self.mqttlj()
- def on_connect(self,client,userdata,flags,rc):
- if rc == 0:
- print(rc)
- self.pushButton_client_sever.setText("断开服务器")
- client.subscribe(self.SubTopic)
- def on_message(self, client, userdata,msg):
- try:
- mm = json.loads(msg.payload)
- print(mm)
- if re.match(r"^\d+(\.\d+)?[ DISCUZ_CODE_1 ]quot;, mm['Temperature']):
- float_value = float(mm['Temperature'])
- # print(f"转换后的浮点数为: {mm.Temperature}")
- else:
- print("字符串不是有效的数字格式")
- self.lcdNumberThishTemp.display(float_value)
- if mm['WorkState'] == '1':
- self.pushButtonOnOff.setText("关闭取暖器")
- else:
- self.pushButtonOnOff.setText("开启取暖器")
- except Exception as e:
- print("err " +str(e))
- def on_publish(self, topic, payload, qos = 1):
- self.client.publish(topic, payload, qos)
- def connect_mqtt(self):
- if self.pushButton_client_sever.text() == "连接服务器":
- self.conn_Ck();
- elif self.pushButton_client_sever.text() == "断开服务器":
- self.client.loop_stop()
- self.pushButton_client_sever.setText("连接服务器")
- if __name__ =='__main__':
- app = QApplication(sys.argv)
- window = AppGui()
- window.show()
- sys.exit(app.exec_())
复制代码
【实验效果】
[size=10.5000pt]安装好的图片:
【总结】
这次参与【Arrow 有好料】使用DRF0654制作了一个智能取暖器,非常好的一个活动。希望论坛能多举办这样的活动。
【视频介绍】
[size=10.5000pt]
|
|