『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 146|回复: 0

[技术探讨] esp8285采集max6675温度

[复制链接]

83

主题

564

帖子

3814

积分

论坛元老

Rank: 8Rank: 8

积分
3814
金钱
3250
HASS币
0
发表于 2025-2-12 12:59:29 | 显示全部楼层 |阅读模式
#include <ESP8266WiFi.h>
#include <Adafruit_NeoPixel.h>
#include "max6675.h"
#include <ESP8266WebServer.h>

// WiFi 配置
const char* ssid = "XXXXXXX";
const char* password = "XXXXXXXX";

// MAX6675 配置
#define MAX6675_SCK 5
#define MAX6675_CS  4
#define MAX6675_MISO 16
MAX6675 thermocouple(MAX6675_SCK, MAX6675_CS, MAX6675_MISO);

// NeoPixel 配置
#define LED_PIN 14
#define NUM_LEDS 1
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

// GPIO 配置
const int heaterPin = 10;  // 加热器控制引脚
const int buttonLowPin = 12;  // 低温按钮
const int buttonHighPin = 13; // 高温按钮

// 全局变量
float target_temperature = 80.0;
unsigned long lastUpdate = 0;
const long interval = 1000;  // 1秒更新间隔

// 创建Web服务器,监听80端口
ESP8266WebServer server(80);

void setup() {
  Serial.begin(115200);
  
  // 初始化硬件
  pinMode(heaterPin, OUTPUT);
  pinMode(buttonLowPin, INPUT_PULLUP);
  pinMode(buttonHighPin, INPUT_PULLUP);
  
  // 初始化 MAX6675
  //thermocouple.begin();
  
  // 初始化 NeoPixel
  strip.begin();
  strip.show(); // 初始化时关闭LED

  // 连接 WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected");

  // 打印IP地址
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // 处理根路径请求
  server.on("/", []() {
    float current_temp = thermocouple.readCelsius();
    String heaterStatus = digitalRead(heaterPin) == HIGH ? "开启" : "关闭";
    String page = "<html><head><meta charset='UTF-8'></head><body>"; // 添加字符编码声明
    page += "<h1>温度监控</h1>";
    page += "<p>当前温度: ";
    page += String(current_temp);
    page += " ℃</p>";
    page += "<p>目标温度: ";
    page += String(target_temperature);
    page += " ℃</p>";
    page += "<p>加热器状态: ";
    page += heaterStatus;
    page += "</p>";
    page += "</body></html>";
    server.send(200, "text/html", page);
  });

  // 启动Web服务器
  server.begin();
  Serial.println("Web server started");

  strip.setPixelColor(0, strip.Color(0, 0, 255));   // 蓝色
  strip.show();

  digitalWrite(heaterPin, HIGH);
  delay(5000);
  digitalWrite(heaterPin, LOW);
  delay(5000);

}

void loop() {
  server.handleClient();
  
  // 按钮检测
  if (digitalRead(buttonLowPin) == LOW) {
    delay(100); // 简单防抖
    if (digitalRead(buttonLowPin) == LOW) {
      target_temperature = 75.0;
      Serial.println("Set target to 75℃");
    }
  }
  
  if (digitalRead(buttonHighPin) == LOW) {
    delay(100);
    if (digitalRead(buttonHighPin) == LOW) {
      target_temperature = 230.0;
      Serial.println("Set target to 230℃");
    }
  }

  // 定时更新温度
  if (millis() - lastUpdate >= interval) {
    lastUpdate = millis();
    float temp = thermocouple.readCelsius();
    Serial.print("Temperature: ");
    Serial.print(temp);
    Serial.println(" ℃");

    // 控制 LED 颜色
    if (temp < 50) {
      strip.setPixelColor(0, strip.Color(0, 0, 255));   // 蓝色
    } else if (temp < 80) {
      strip.setPixelColor(0, strip.Color(255, 255, 0)); // 黄色
    } else if (temp < 100) {
      strip.setPixelColor(0, strip.Color(255, 165, 0)); // 橙色
     }  else if (temp < 150) {
      strip.setPixelColor(0, strip.Color(128, 0, 128)); // 紫色
    } else if (temp < 200) {
      strip.setPixelColor(0, strip.Color(255, 0, 0));   // 红色
    } else {
      strip.setPixelColor(0, strip.Color(255, 255, 255)); // 白色
    }
    strip.show();

    // 控制加热器
    if (target_temperature == 75.0) {
      digitalWrite(heaterPin, (temp < 75) ? HIGH : LOW);
    } 
    else if (target_temperature == 230.0) {
      digitalWrite(heaterPin, HIGH);
    }
  }
}
arduino代码如上,实测esp8266或者esp32通过esphome代码采集不到温度值。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2025-3-5 05:00 , Processed in 0.050337 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表