ThingsPanel通过ESP8266遥控风扇转速
发布日期:
这个教程的目的是ThingsPanel通过ESP 8266遥控风扇转速 , 可通过电脑和App控制 。
硬件准备
1、ESP8266
2、12V风扇
3、220V转12V电源
接线指南
红线 (电源线): 连接到+12V的电源。
黑线 (地线): 连接到ESP8266的GND和电源的GND。
黄线 (TACH线): 连接到ESP8266的一个GPIO引脚,这里我们使用D2(GPIO4)。
蓝线 (PWM控制线): 连接到ESP8266的D1(GPIO5)引脚。
源代码
如下的配置修改
- WiFi用户名和密码
- MQTT用户名和密码
- 主题【见如下图片】
- 设备ID(不能重复)
以上配置在设备接入-设备管理-连接参数中获得。如下图
刷写前注意选择板子和端口
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
const char* ssid = "Redmi";
const char* password = "123456789";
const char* mqtt_server = "dev.thingspanel.cn";
const int mqtt_port = 1883;
const char* mqtt_user = "cd17794c-2cf3-0faf-4613-92234fa636ab";
const char* mqtt_password = "";
WiFiClient espClient;
PubSubClient client(espClient);
const int pwmPin = 5; // D1
const int tachPin = 4; // D2
const int fanSpeedPin = A0;
volatile unsigned long lastTachTime;
volatile unsigned long tachPulses;
void ICACHE_RAM_ATTR tachInterrupt();
volatile unsigned long lastTachInterruptTime = 0;
int currentPWMValue = 0; // 用于存储当前PWM值的全局变量
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
pinMode(pwmPin, OUTPUT);
pinMode(fanSpeedPin, INPUT);
pinMode(tachPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(tachPin), tachInterrupt, FALLING);
analogWriteRange(255); // 设置PWM范围
analogWriteFreq(25000); // 设置PWM频率为25kHz
}
void setup_wifi() {
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (unsigned int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// 根据消息来设置PWM值
if (String(topic) == "device/attributes/cd17794c-2cf3-0faf-4613-92234fa636ab") {
DynamicJsonDocument doc(1024);
deserializeJson(doc, messageTemp);
if (doc.containsKey("setpwm")) {
int pwmValue = doc["setpwm"];
pwmValue = constrain(pwmValue, 0, 255); // 确保PWM值在0到255的范围内
analogWrite(pwmPin, pwmValue);
currentPWMValue = pwmValue; // 更新当前的PWM值
}
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("b8cdff45", mqtt_user, mqtt_password)) {
Serial.println("connected");
client.subscribe("device/attributes/cd17794c-2cf3-0faf-4613-92234fa636ab");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void ICACHE_RAM_ATTR tachInterrupt() {
unsigned long now = micros();
if (now - lastTachInterruptTime > 1000) { // 1000微秒的去抖时间
tachPulses++;
lastTachInterruptTime = now;
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
static unsigned long lastReport = 0;
if (millis() - lastReport >= 1000) { // 每秒上报一次
lastReport = millis();
}
// RPM 计算和上报
static unsigned long lastRpmCalculation = 0;
if (millis() - lastRpmCalculation >= 2000) {
noInterrupts();
unsigned long duration = millis() - lastRpmCalculation;
unsigned long pulses = tachPulses;
tachPulses = 0;
interrupts();
unsigned long rpm = (pulses / 2) * (60.0 / (duration / 1000.0));
Serial.print("PWM value: ");
Serial.print(currentPWMValue);
Serial.print(", RPM: ");
Serial.println(rpm);
// 创建一个JSON文档来存储和发送PWM值和RPM值
StaticJsonDocument<100> doc;
doc["pwmvalue"] = currentPWMValue;
doc["rpm"] = rpm;
// 创建一个字符数组来存储JSON字符串
char buffer[100];
// 将JSON文档序列化为一个字符串
serializeJson(doc, buffer);
// 将包含PWM值和RPM值的JSON字符串发送到MQTT服务器
client.publish("device/attributes", buffer);
// 将要发布的消息打印到串口监视器
Serial.print("Published message: ");
Serial.println(buffer);
lastRpmCalculation = millis();
}
delay(100);
}