最近想做一個發(fā)熱墊嬉橙,可以用手機控制。
一開始思考過用wifi接入米家進行控制寥假,這樣還能使用語音助手市框。但后來仔細思索一番,發(fā)現(xiàn)使用場景不對糕韧。如果使用wifi連接枫振,那意味著只能在室內使用了。
所以萤彩,最后還是決定直接使用藍牙連接粪滤。
硬件選型
雖然選擇了藍牙連接,但為了以后擴展wifi方便雀扶,所以硬件選用了esp32杖小,同時有wifi和藍牙連接的功能肆汹,代碼又兼容arduino,使用非常方便予权。
藍牙連接方式
- 初步設想是把硬件的mac地址生成二維碼昂勉,手機軟件掃描二維碼獲取mac地址,進行連接及發(fā)送溫度設置等指令扫腺。
- 后來發(fā)現(xiàn)岗照,貌似可以直接用設備名進行藍牙連接,如此一來便可以把所有的硬件設備都設置為相同的設備名笆环,又可以省去二維碼攒至,著實不錯。
- 最后是在查資料時看到一種藍牙廣播的方式躁劣,不過尚未來及做實驗迫吐,日后有機會倒可以試試。
溫控方式
使用溫敏電阻即可讀取溫度习绢。
- 最簡單的溫控可以是直接用繼電器開關進行控制渠抹。設置溫度的上下區(qū)間,加熱到上區(qū)間停止闪萄,低于下區(qū)間則重啟加熱梧却。
- 高階一點的是用pwm的方式調整發(fā)熱電阻的功率,離目標溫度越接近則功率越小败去,如此即可實現(xiàn)平滑溫度曲線放航。甚至于再不行,還可上pid閉環(huán)控制算法圆裕,疊加上之前的誤差广鳍,實時調整。
file
file
file
手機軟件
由于我不會做安卓軟件吓妆,現(xiàn)在只是使用一款“藍牙串口”的app直接發(fā)送指令赊时,控制硬件。
以后還是要學一下安卓行拢,做一套架子出來祖秒。
esp32程序
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
char START_FLAG = '$';
char END_FLAG = '#';
int TEMPERATURE_MIN = 0;
int TEMPERATURE_MAX = 50;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void SerialBT_sendMsg(String msg) {
int i = 0;
for (i = 0; i < msg.length(); i++) {
SerialBT.write(msg[i]);
}
}
int NONE = 0;
int START = 1;
int pre_status = NONE;
int num = 0;
void loop() {
if (SerialBT.available()) {
char msg_char = SerialBT.read();
if (msg_char == START_FLAG) {
num = 0;
pre_status = START;
} else if (msg_char == END_FLAG && pre_status == START) {
if (num >= TEMPERATURE_MIN && num <= TEMPERATURE_MAX) {
String msg = String("set temperature to " + String(num) + "\n");
SerialBT_sendMsg(msg);
}
num = 0;
pre_status = NONE;
} else if (isDigit(msg_char) && pre_status == START) {
num = num * 10 + (msg_char - '0');
} else {
num = 0;
pre_status = NONE;![file](https://upload-images.jianshu.io/upload_images/6302584-ca8f0be6cae98db2.png)
}
// SerialBT_sendMsg(String(String(msg_char) + "\n"));
}
delay(20);
}