ubuntu 上 ESP8266 HomeKit 实战(五)2路继电器

本文介绍了如何使用ESP8266通过HomeKit控制2路继电器。硬件部分采用淘宝购买的双继电器模块,软件部分基于先前1路继电器的实现进行调整。提供了针对该硬件的烧写包下载链接,并展示了测试效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于 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
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值