查看: 414|回复: 0

[评测分享] 【Arrow 有好料】DFR0654智能取暖器之成品篇

[复制链接]
  • TA的每日心情
    开心
    昨天 09:27
  • 签到天数: 99 天

    连续签到: 3 天

    [LV.6]常住居民II

    发表于 2024-2-25 22:44:27 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 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] 图片1.png
    [size=10.5000pt]4、远程桌面工作流程
    图片2.png
    软件源代码】
    DRF0654
    1. // This example uses an ESP32 Development Board
    2. // to connect to shiftr.io.
    3. //
    4. // You can check on your device after a successful
    5. // connection here: https://www.shiftr.io/try.
    6. //
    7. // by Joël Gähwiler
    8. // https://github.com/256dpi/arduino-mqtt

    9. #include <WiFi.h>
    10. #include <MQTT.h>
    11. #include "Wire.h"
    12. #include "SHT31.h"

    13. #define HAETPIN  D9  //加热开关
    14. #define SHT31_ADDRESS   0x44


    15. #define defultWorkValue 25;

    16. SHT31 sht;
    17. char buffer[80];
    18. const char ssid[] = "SSID";
    19. const char pass[] = "pwd";

    20. int temperUpValue;   //最高值
    21. int temperDownValue; //最低值
    22. float thisTemperValud;
    23. int temperSetValue;   //设置目标值

    24. int swtickState = 0;  //开关状态
    25. int onoff = 0;     //是否打开取暖器
    26. WiFiClient net;
    27. MQTTClient client;

    28. unsigned long lastMillis = 0;

    29. void hearContrl(void)
    30. {
    31.   if(onoff == 1)
    32.   {
    33.     if(thisTemperValud > temperSetValue +1)
    34.     {
    35.       digitalWrite(HAETPIN, LOW);
    36.     }
    37.     else if(thisTemperValud < temperSetValue -1)
    38.     {
    39.       digitalWrite(HAETPIN, HIGH);
    40.     }
    41.   }
    42.   else
    43.   {
    44.     digitalWrite(HAETPIN, LOW);
    45.   }
    46. }
    47. void connect() {
    48.   Serial.print("checking wifi...");
    49.   while (WiFi.status() != WL_CONNECTED) {
    50.     Serial.print(".");
    51.     delay(1000);
    52.   }

    53.   Serial.print("\nconnecting...");
    54.   while (!client.connect("clietid", "username", "pwd")) {
    55.     Serial.print(".");
    56.     delay(1000);
    57.     //连接后,发送获取状态:

    58.   }

    59.   Serial.println("\nconnected!");

    60.   client.subscribe("/topic/#");
    61.   //连接后发送远程状态

    62. }

    63. void messageReceived(String &topic, String &payload) {

    64.   //如果是开机,
    65.   //设置目标温度值
    66.   //如果小于温度值测发出开机信号

    67.   //如果是设置温度值测更新温度值
    68.   if(topic == "/topic/hearset")
    69.   {
    70.     Serial.println("incoming: " + topic + " - " + payload);
    71.     temperSetValue = payload.toInt();
    72.   }
    73.   else if(topic == "/topic/heartonoff")
    74.   {
    75.     Serial.println("incoming: " + topic + " - " + payload);
    76.     if(payload == "on")
    77.     {
    78.       //对应的IO高电平
    79.       onoff = 1;

    80.     }
    81.     else if(payload == "off")
    82.     {
    83.         //对应的IO低电平
    84.         onoff = 0;

    85.     }
    86.   }
    87.   // Note: Do not use the client in the callback to publish, subscribe or
    88.   // unsubscribe as it may cause deadlocks when other things arrive while
    89.   // sending and receiving acknowledgments. Instead, change a global variable,
    90.   // or push to a queue and handle it in the loop after calling `client.loop()`.
    91. }

    92. void setup() {
    93.   pinMode(HAETPIN,OUTPUT);
    94.   digitalWrite(HAETPIN, LOW);
    95.   onoff = 0;
    96.   Wire.begin();
    97.   Wire.setClock(100000);
    98.   sht.begin();

    99.   uint16_t stat = sht.readStatus();
    100.   Serial.print(stat, HEX);
    101.   Serial.println();

    102.   Serial.begin(115200);
    103.   WiFi.begin(ssid, pass);

    104.   // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
    105.   // by Arduino. You need to set the IP address directly.
    106.   client.begin("域名", net);
    107.   client.onMessage(messageReceived);

    108.   connect();
    109. }

    110. void loop() {
    111.   client.loop();
    112.   delay(10);  // <- fixes some issues with WiFi stability

    113.   if (!client.connected()) {
    114.     connect();
    115.   }

    116.   // publish a message roughly every second.
    117.   if (millis() - lastMillis > 1000) {
    118.     lastMillis = millis();
    119.     sht.read();   
    120.     thisTemperValud = sht.getTemperature();
    121.     snprintf(buffer, sizeof(buffer), "{"Temperature":"%.1f", "Humidity":"%.1f%", "WorkState":"%d"}", thisTemperValud , sht.getHumidity(), onoff);
    122.     Serial.println(buffer);
    123.     client.publish("/topic/heart", buffer);
    124.     hearContrl();
    125.   }

    126. }
    复制代码
    【控制界面】
    采用QT来制作
    图片3.png
    [size=10.5000pt]控制端程序:
    1. import sys
    2. import time
    3. import json
    4. import re
    5. from paho.mqtt import client as mqtt
    6. from PyQt5.QtWidgets import QWidget, QApplication
    7. from PyQt5.QtCore import QTime, QTimer
    8. from electriheater import Ui_MainWindow
    9. from PyQt5.QtWidgets import QApplication, QMainWindow
    10. class AppGui(QMainWindow,Ui_MainWindow):
    11.     def __init__(self):
    12.         super(AppGui,self).__init__()
    13.         self.setupUi(self)

    14.         self.lcdNumberThishTemp.display(10.233)
    15.         self.pushButton_client_sever.clicked.connect(self.connect_mqtt)
    16.         self.horizontalSliderSetting.valueChanged.connect(self.setLcdShow)
    17.         self.horizontalSliderSetting.sliderReleased.connect(self.temperSet)
    18.         self.pushButtonOnOff.clicked.connect(self.openoffHeart)

    19.     def openoffHeart(self):
    20.         if self.pushButtonOnOff.text() == "开启取暖器":
    21.             self.on_publish("/topic/heartonoff", "on", 1)
    22.             # self.pushButtonOnOff.setText("关闭取暖器")
    23.         else:
    24.             self.on_publish("/topic/heartonoff", "off", 1)
    25.             # self.pushButtonOnOff.setText("开启取暖器")

    26.     def mqttlj(self):
    27.         client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
    28.         self.client = mqtt.Client(client_id)
    29.         self.client.username_pw_set(self.Username,self.Password)
    30.         self.client.on_connect = self.on_connect
    31.         self.client.on_message = self.on_message
    32.         self.client.connect(self.HOST,self.PORT,600)
    33.         self.client.loop_start()
    34.         print(self.Username, self.Password, self.HOST, self.PORT)

    35.     def setLcdShow(self):
    36.         self.lcdNumberSetTemp.display(self.horizontalSliderSetting.value())
    37.     def temperSet(self):
    38.         tempSetValue = self.horizontalSliderSetting.value()
    39.         print("设置温度")
    40.         self.on_publish("/topic/hearset",str(tempSetValue),1)
    41.         # 发送publissh
    42.     def conn_Ck(self):
    43.         self.Username = "username"
    44.         self.Password = "pwd"
    45.         self.HOST = "域名"
    46.         self.PORT = 1883
    47.         self.SubTopic = "/topic/heart"
    48.         self.PubTopic = "/topic/hearset"
    49.         self.mqttlj()
    50.     def on_connect(self,client,userdata,flags,rc):
    51.         if rc == 0:
    52.             print(rc)
    53.             self.pushButton_client_sever.setText("断开服务器")
    54.         client.subscribe(self.SubTopic)
    55.     def on_message(self, client, userdata,msg):
    56.         try:
    57.             mm = json.loads(msg.payload)
    58.             print(mm)
    59.             if re.match(r"^\d+(\.\d+)?[        DISCUZ_CODE_1        ]quot;, mm['Temperature']):
    60.                 float_value = float(mm['Temperature'])
    61.                 # print(f"转换后的浮点数为: {mm.Temperature}")
    62.             else:
    63.                 print("字符串不是有效的数字格式")
    64.             self.lcdNumberThishTemp.display(float_value)
    65.             if mm['WorkState'] == '1':
    66.                 self.pushButtonOnOff.setText("关闭取暖器")
    67.             else:
    68.                 self.pushButtonOnOff.setText("开启取暖器")
    69.         except Exception as e:
    70.             print("err " +str(e))

    71.     def on_publish(self, topic, payload, qos = 1):
    72.         self.client.publish(topic, payload, qos)

    73.     def connect_mqtt(self):
    74.         if self.pushButton_client_sever.text() == "连接服务器":
    75.             self.conn_Ck();
    76.         elif self.pushButton_client_sever.text() == "断开服务器":
    77.             self.client.loop_stop()
    78.             self.pushButton_client_sever.setText("连接服务器")
    79. if __name__ =='__main__':
    80.      app = QApplication(sys.argv)
    81.      window = AppGui()
    82.      window.show()
    83.      sys.exit(app.exec_())
    复制代码

    【实验效果】
    图片4.png
    [size=10.5000pt]安装好的图片:
    图片5.png
    【总结】
    这次参与【Arrow 有好料】使用DRF0654制作了一个智能取暖器,非常好的一个活动。希望论坛能多举办这样的活动。
    【视频介绍】

    [size=10.5000pt]

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-28 08:05 , Processed in 0.125203 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.