Files
esp8266_study/code/xiaoai/xiaoai.ino
2024-09-27 01:22:07 +08:00

152 lines
4.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* *****************************************************************
*
* Download latest Blinker library here:
* https://github.com/blinker-iot/blinker-library/archive/master.zip
*
*
* Blinker is a cross-hardware, cross-platform solution for the IoT.
* It provides APP, device and server support,
* and uses public cloud services for data transmission and storage.
* It can be used in smart home, data monitoring and other fields
* to help users build Internet of Things projects better and faster.
*
* Make sure installed 2.7.4 or later ESP8266/Arduino package,
* if use ESP8266 with Blinker.
* https://github.com/esp8266/Arduino/releases
*
* Make sure installed 1.0.5 or later ESP32/Arduino package,
* if use ESP32 with Blinker.
* https://github.com/espressif/arduino-esp32/releases
*
* Docs: https://diandeng.tech/doc
*
*
* *****************************************************************
*
* Blinker 库下载地址:
* https://github.com/blinker-iot/blinker-library/archive/master.zip
*
* Blinker 是一套跨硬件、跨平台的物联网解决方案提供APP端、设备端、
* 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、
* 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。
*
* 如果使用 ESP8266 接入 Blinker,
* 请确保安装了 2.7.4 或更新的 ESP8266/Arduino 支持包。
* https://github.com/esp8266/Arduino/releases
*
* 如果使用 ESP32 接入 Blinker,
* 请确保安装了 1.0.5 或更新的 ESP32/Arduino 支持包。
* https://github.com/espressif/arduino-esp32/releases
*
* 文档: https://diandeng.tech/doc
*
*
* *****************************************************************/
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <ESP8266WiFi.h>
#include <Blinker.h>
// key 秘钥
char auth[] = "75cb805d0c29";
// 2.4G wifi名
char ssid[] = "bmy";
// 2.4G wifi密码
char pswd[] = "lb714500";
// 新建组件对象
BlinkerButton Button1("btn-abc");
BlinkerNumber Number1("num-abc");
// 按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("button 状态 ==== : ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker dataRead =====: ", data);
}
// 电源设置
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerMIOT.powerState("off");
// 发送数据
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
}
// 自定义亮度控制
void miotBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);
int ledPwmVal = bright.toInt();
BLINKER_LOG("now set brightness: ", ledPwmVal);
analogWrite(LED_BUILTIN, ledPwmVal);
BlinkerMIOT.brightness(ledPwmVal);
BlinkerMIOT.print();
}
// 色温设置
void miotColoTemp(int32_t colorTemp)
{
BLINKER_LOG("need set colorTemperature: ", colorTemp);
BlinkerMIOT.colorTemp(colorTemp);
BlinkerMIOT.print();
}
// 当日天气
void airData(const String & data) {
BLINKER_LOG("air: ", data);
}
void setup()
{
// 初始化串口
Serial.begin(115200);
// WiFi.softAP(wifiSsid, wifiPassword);
// 开启 DEBUG
BLINKER_DEBUG.stream(Serial);
// 初始化有LED的IO
pinMode(LED_BUILTIN, OUTPUT);
// 关闭LED
digitalWrite(LED_BUILTIN, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachBrightness(miotBright);
Button1.attach(button1_callback);
BlinkerMIOT.attachColorTemperature(miotColoTemp);
Blinker.attachAir(airData);
Blinker.air(340100);
}
void loop() {
Blinker.run();
}