基于ESP8266的家用智能wifi家电控制(附带app设计)
智能浴霸的WiFi连接及APP控制 #生活技巧# #居家生活技巧# #电器维修技巧# #智能电器绑定教程#
一、需求和思路
1.空调不带wifi,遥控总是找不到,手机没有红外线,只好做一个基于esp8266的局域网红外遥控,顺便做个app。
2.使用esp8266开发板,红外模块一个。
3.使用Android studio制作遥控app。
二、esp8266控制程序
#include <ESP8266WiFi.h>
#include <Servo.h>
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
const uint16_t kIrLed = 4;
IRsend irsend(kIrLed);
uint16_t rawData[119] ={1212, 496, 1186, 478, 418, 1270, 1210, 474, 1212, 498, 396, 1268, 416, 1268, 1210, 500, 394, 1294, 416, 1244, 444, 1266, 394, 7880, 1186, 478, 1230, 456, 414, 1294, 1186, 480, 1206, 500, 394, 1292, 394, 1270, 1206, 482, 414, 1268, 416, 1294, 394, 1270, 418, 7856, 1232, 452, 1210, 502, 418, 1268, 1208, 478, 1184, 502, 392, 1274, 414, 1294, 1210, 454, 416, 1292, 392, 1296, 416, 1268, 392, 7884, 1184, 500, 1186, 504, 414, 1270, 1184, 506, 1206, 478, 394, 1294, 392, 1294, 1198, 488, 392, 1294, 392, 1296, 390, 1294, 414, 7860, 1208, 478, 1210, 478, 392, 1294, 1208, 480, 1206, 480, 392, 1294, 394, 1294, 1206, 478, 418, 1270, 394, 1292, 392, 1296, 392};
uint16_t Geli_FanOpen[119] = {1268, 418, 1268, 418, 448, 1238, 1268, 418, 1268, 418, 450, 1238, 448, 1238, 450, 1236, 450, 1238, 448, 1238, 1268, 418, 448, 7828, 1268, 418, 1268, 418, 450, 1236, 1268, 418, 1270, 418, 448, 1238, 450, 1236, 450, 1238, 448, 1238, 448, 1238, 1268, 418, 450, 7828, 1268, 418, 1268, 418, 450, 1236, 1268, 418, 1268, 418, 450, 1236, 448, 1238, 450, 1236, 450, 1236, 450, 1236, 1268, 418, 450, 7826, 1266, 418, 1268, 418, 452, 1234, 1244, 442, 1246, 442, 452, 1234, 452, 1234, 452, 1234, 452, 1234, 452, 1234, 1244, 442, 452, 7824, 1246, 440, 1268, 418, 450, 1236, 1270, 418, 1268, 416, 448, 1238, 446, 1240, 424, 1260, 426, 1260, 430, 1256, 1222, 464, 430};
uint16_t Geli_FanClose[95] = {1268, 418, 1268, 418, 448, 1238, 1268, 418, 1270, 418, 450, 1238, 448, 1240, 448, 1238, 448, 1238, 450, 1236, 448, 1238, 1268, 7008, 1268, 418, 1268, 418, 450, 1238, 1268, 418, 1268, 418, 448, 1238, 448, 1236, 450, 1238, 450, 1236, 450, 1236, 450, 1238, 1268, 7010, 1266, 418, 1268, 418, 450, 1236, 1268, 418, 1268, 418, 450, 1236, 452, 1234, 452, 1236, 452, 1234, 452, 1234, 452, 1234, 1244, 7030, 1244, 440, 1246, 440, 474, 1212, 1270, 416, 1248, 440, 448, 1238, 448, 1238, 448, 1238, 446, 1240, 424, 1260, 426, 1260, 1222};
uint16_t Geli_FanType[95] = {1268, 418, 1268, 418, 448, 1238, 1268, 418, 1268, 418, 448, 1238, 448, 1238, 448, 1238, 448, 1238, 1268, 418, 448, 1238, 448, 7828, 1266, 418, 1268, 418, 448, 1238, 1266, 418, 1268, 418, 448, 1238, 448, 1238, 446, 1238, 472, 1214, 1268, 418, 448, 1238, 448, 7824, 1268, 418, 1268, 418, 448, 1238, 1266, 420, 1268, 418, 450, 1236, 448, 1238, 448, 1238, 448, 1236, 1268, 418, 450, 1236, 448, 7822, 1268, 418, 1268, 418, 450, 1234, 1244, 440, 1268, 418, 472, 1212, 450, 1236, 472, 1214, 446, 1238, 1242, 444, 426, 1260, 430};
uint16_t Geli_FanTimer[71] = {1266, 418, 1268, 418, 448, 1238, 1266, 420, 1268, 418, 448, 1238, 448, 1240, 446, 1238, 1268, 418, 448, 1238, 448, 1238, 448, 7828, 1268, 420, 1268, 418, 448, 1238, 1268, 418, 1268, 418, 448, 1238, 448, 1238, 448, 1238, 1268, 418, 448, 1240, 472, 1214, 448, 7826, 1266, 420, 1268, 418, 448, 1238, 1268, 418, 1266, 420, 448, 1238, 448, 1238, 448, 1236, 1270, 416, 450, 1236, 450, 1236, 450};
#include <ESP8266WiFi.h>
#include <Servo.h>
#ifndef STASSID
#define STASSID "你的wifi"
#define STAPSK "你的wifi密码"
#endif
#define ANGLE_ON 76
#define ANGLE_OFF 270
#define ANGLE_NONE 90
const char* ssid = STASSID;
const char* password = STAPSK;
int angle=ANGLE_NONE;
Servo myservo;
WiFiServer server(80);
void setup() {
Serial.begin(74880);
myservo.attach(4);
myservo.write(angle);
Serial.println();
Serial.println();
Serial.print(F("WiFi start connect "));
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi已连接!"));
server.begin();
Serial.println(F("模块服务端已开启!"));
Serial.println(WiFi.localIP());
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
client.setTimeout(1000);
String req = client.readStringUntil('\r');
Serial.println(F("请求: "));
Serial.println(req);
if (req.indexOf(F("/gpio/0")) != -1) {
angle = ANGLE_ON;
myservo.write(angle);
delay(1000);
myservo.write(ANGLE_NONE);
}
else if (req.indexOf(F("/gpio/1")) != -1) {
irsend.sendRaw(rawData, 119, 38);
}
else if (req.indexOf(F("/gpio/2")) != -1) {
irsend.sendRaw(Geli_FanOpen, 119, 38);
}
else if (req.indexOf(F("/gpio/3")) != -1) {
irsend.sendRaw(Geli_FanTimer, 71, 38);
}
else if (req.indexOf(F("/gpio/4")) != -1) {
irsend.sendRaw(Geli_FanClose, 95, 38);
}
else if (req.indexOf(F("/gpio/5")) != -1) {
irsend.sendRaw(Geli_FanType, 95, 38);
}
else {
Serial.println(F("无效请求"));
}
while (client.available()) {
client.read();
}
client.print(F("<!DOCTYPE html>\r\n<html xmlns='http://www.w3.org/1999/xhtml'>\r\n"));
client.print(F("<head>\r\n<meta charset='utf-8'/>\r\n<title>WiFi智能控制</title>\r\n"));
client.print(F("<style type='text/css'>.state{text-align:center;font-size:10px;}"));
client.print(F(".on{text-align:center;font-size:20px;alink='#1a9e5f'}.off{text-align:center;font-size:20px;alink='#da4f43'}"));
client.print(F("</style><body><div class='state'>灯状态:"));
client.print((angle>ANGLE_NONE) ? F("关</div>") : F("开</div>"));
client.print(F("<br><br><div class='on'><a href='http://"));
client.print(WiFi.localIP());
client.print(F("/gpio/0'>电脑开</a></div><br><br><div class='off'><a href='http://"));
client.print(WiFi.localIP());
client.print(F("/gpio/1'>电扇摇头</a></div><br><br><div class='off'><a href='http://"));
client.print(WiFi.localIP());
client.print(F("/gpio/2'>电扇开机</a></div><br><br><div class='off'><a href='http://"));
client.print(WiFi.localIP());
client.print(F("/gpio/3'>电扇定时</a></div><br><br><div class='off'><a href='http://"));
client.print(WiFi.localIP());
client.print(F("/gpio/4'>电扇关机</a></div><br><br><div class='off'><a href='http://"));
client.print(WiFi.localIP());
client.print(F("/gpio/5'>电扇风类</a></div></body></html>"));
Serial.println(F("客户端已挂起!"));
}
注意:1、程序中的遥控代码来自于esp8266的红外接收模块,详情见另外一篇文章。app设计见另一篇文章。
2、程序下载进芯片之后灯带其连接上家里的wifi,就可以去路由器界面查看其ip地址,然后直接访问其ip地址,就会出现控制页面,点击相应按钮,把红外发射器对准要控制的电器,即可控制。
3、我把红外发射器的数据线接在了D14引脚。
网址:基于ESP8266的家用智能wifi家电控制(附带app设计) https://www.yuejiaxmz.com/news/view/208110
相关内容
深度解析:STM32对接米家平台,打造WiFi智能插座(ESP8266、电流检测)基于STM32单片机的智能家居环境监测与控制系统设计
基于 STM32 的语音识别智能家居控制系统的设计(LD3320语音识别芯片+ESP8266 WIFI模块+DHT11温湿度采集+MQ系列 烟雾及可燃气体+蜂鸣器+步进电机模拟窗帘+OLED液晶显示+
基于STM32智能家电无线控制系统设计
基于STM32的智能家居语音控制系统:集成LD3320、ESP8266设计流程
语音控制:基于ESP8266的DIY助手
基于STM32的智能节能风扇设计
STM32毕业设计——基于STM32+MQTT+WiFi技术的智能家居系统设计与实现(毕业论文+程序源码)——智能家居系统
基于机智云+Shineblink的智能宠物屋,智能投喂、自动清洁及APP远程控制
家用电器远程控制