ESP-01S智能灯控源码详解

通过前一篇的ESP-01S实现智能灯控,我们可以快速上手智能灯控并获得不错的交互体验。但是在一些突发情况下(如:客户端在家庭中删除了设备),此时就要从墙中取出esp-01s重新刷入固件,操作就会非常繁琐。本文分析灯控装置的源码,并优化固件,以便在突发情况下对灯控装置配网与重置。

一、项目地址

二、源码

2.1 核心代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <EEPROM.h>
#include<wifi_connect_tool.h> //WiFi配网头文件
#include <Arduino.h>
#include <arduino_homekit_server.h>
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
#define PIN_LED 2 //GPIO2是板载灯
#define PIN_SWITCH 0 //GPIO0是控制继电器的引脚
bool reset_flag = false;

//上电后只执行一次
void setup() {
Serial.begin(115200);
pinMode(PIN_SWITCH, OUTPUT);
digitalWrite(PIN_SWITCH, LOW);//整个装置通电后设置继电器为闭合状态,即电路通。这样就算没有设置装置,也不影响正常使用。
EEPROM.begin(350);
wifi_load(); //WiFi配网
pinMode(PIN_LED, OUTPUT); //设置板载LED灯
if(reset_flag==true){
homekit_storage_reset(); //重置homekit
}
my_homekit_setup();
}

void loop() {
wifi_pant();//查看wifi情况
my_homekit_loop();
}

//==============================
// HomeKit setup and loop
//==============================

// access your HomeKit characteristics defined in my_accessory.c
extern "C" homekit_server_config_t config;
extern "C" homekit_characteristic_t cha_switch_on;

static uint32_t next_heap_millis = 0;


void cha_switch_on_setter(const homekit_value_t value) {
bool on = value.bool_value;
cha_switch_on.value.bool_value = on; //sync the value
LOG_D("Switch: %s", on ? "ON" : "OFF"); //控制台打印日志
digitalWrite(PIN_SWITCH, on ? LOW : HIGH);//如果电路通则断,断则通
digitalWrite(PIN_LED, on ? LOW : HIGH); //这个是控制灯的
}

//接入Homekit后只执行一次
void my_homekit_setup() {
pinMode(PIN_SWITCH, OUTPUT);
digitalWrite(PIN_SWITCH, LOW);

//Add the .setter function to get the switch-event sent from iOS Home APP.
//The .setter should be added before arduino_homekit_setup.
//HomeKit sever uses the .setter_ex internally, see homekit_accessories_init function.
//Maybe this is a legacy design issue in the original esp-homekit library,
//and I have no reason to modify this "feature".
cha_switch_on.setter = cha_switch_on_setter;
arduino_homekit_setup(&config);

//report the switch value to HomeKit if it is changed (e.g. by a physical button)
//bool switch_is_on = true/false;
//cha_switch_on.value.bool_value = switch_is_on;
//homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);
}

void my_homekit_loop() {
arduino_homekit_loop();
const uint32_t t = millis();
if (t > next_heap_millis) {
// show heap info every 5 seconds
next_heap_millis = t + 5 * 1000;
LOG_D("Free heap: %d, HomeKit clients: %d",
ESP.getFreeHeap(), arduino_homekit_connected_clients_count());

}
}

2.2 配置项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <homekit/homekit.h>
#include <homekit/characteristics.h>

void my_accessory_identify(homekit_value_t _value) {
printf("accessory identify\n");
}

// Switch (HAP section 8.38)
// required: ON
// optional: NAME

// format: bool; HAP section 9.70; write the .setter function to get the switch-event sent from iOS Home APP.
homekit_characteristic_t cha_switch_on = HOMEKIT_CHARACTERISTIC_(ON, false);

// format: string; HAP section 9.62; max length 64
homekit_characteristic_t cha_name = HOMEKIT_CHARACTERISTIC_(NAME, "Switch");

homekit_accessory_t *accessories[] = {
HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_switch, .services=(homekit_service_t*[]) {
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
HOMEKIT_CHARACTERISTIC(NAME, "Switch"),
HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"),
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"),
HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"),
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"),
HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
NULL
}),
HOMEKIT_SERVICE(SWITCH, .primary=true, .characteristics=(homekit_characteristic_t*[]){
&cha_switch_on,
&cha_name,
NULL
}),
NULL
}),
NULL
};

homekit_server_config_t config = {
.accessories = accessories,
.password = "111-11-111" //加入家庭时的代码为八个1
};

三、安装步骤

3.1 下载配置Arduino

3.1.1 Arduino官网地址

3.1.2 配置信息

3.1.3 开发板管理器地址

https://arduino.esp8266.com/stable/package_esp8266com_index.json

3.1.4 打开开发板管理器

3.1.5 安装8266

3.2 用Arduino打开下载的项目

3.3 烧录程序

3.3.1 方法一

用esptool烧录:esptool.py write_flash 0x0 【固件路径】

3.3.2 方法二

直接在Arduino烧录上传

3.4 上传静态文件

3.5 配网

3.5.1 待指示灯快速闪烁时,连接(智能灯控配网)WiFi

3.5.2 用浏览器访问ip(6.6.6.6)

3.5.3 注意WiFi名称不能出现中文!!!!

-------------本文结束感谢您的阅读-------------