基于 ESP8266 HomeKit ,我们能够实现对多路继电器的控制。
根据不同的硬件电路,软件实现可能会有些不同,但基本原理不会相差太大。
下面我们用在淘宝上购买的 ESP8266 2路继电器模块,讲述其控制方法。
一、硬件
硬件除2路继电器外,其余与《ubuntu 上 ESP8266 HomeKit 实战(二)1路继电器》所述的硬件相同。
下图是在陶宝上购的双继电器模块。
二、软件
软件与《ubuntu 上 ESP8266 HomeKit 实战(二)1路继电器 》中的基本相同,只是根据双继电器模块的电路不同对代码进行了相应的调整。主要修改了 main.c 中的代码:
/*
* Example of using esp-homekit library to control
* a simple $5 Sonoff Basic using HomeKit.
* The esp-wifi-config library is also used in this
* example. This means you don't have to specify
* your network's SSID and password before building.
*
* In order to flash the sonoff basic you will have to
* have a 3,3v (logic level) FTDI adapter.
*
* To flash this example connect 3,3v, TX, RX, GND
* in this order, beginning in the (square) pin header
* next to the button.
* Next hold down the button and connect the FTDI adapter
* to your computer. The sonoff is now in flash mode and
* you can flash the custom firmware.
*
* WARNING: Do not connect the sonoff to AC while it's
* connected to the FTDI adapter! This may fry your
* computer and sonoff.
*
*/
#include <stdio.h>
#include <string.h>
#include <espressif/esp_wifi.h>
#include <espressif/esp_sta.h>
#include <espressif/esp_common.h>
#include <esp/uart.h>
#include <esp8266.h>
#include <FreeRTOS.h>
#include <task.h>
#include <homekit/homekit.h>
#include <homekit/characteristics.h>
#include <wifi_config.h>
#include <httpd/httpd.h>
#include "button.h"
#include "poweronstate.h"
#define NO_CONNECTION_WATCHDOG_TIMEOUT 600000
const int relay_number = 2;
// The GPIO pin that is connected to last pin of the programming strip of the Sonoff Basic.
const int pin_gpio = 14;
// The GPIO pin that is connected to the relay on the Sonoff Basic.
const int relay_gpio = 12;
// The GPIO pin that is connected to the LED on the Sonoff Basic.
const int led_gpio = 13;
// The GPIO pin that is oconnected to the button on the Sonoff Basic.
const int button_gpio = 0