开发前期准备
1、arduinoIDE 写ESP8266 的开发环境的搭建+esp8266的插件库,
ESP8266 WIFI接入Blinker + BLinker APP 和小爱同学控制ESP8266
A、esp8266接入WiFi 并创建BIinker的APP的组件
详细的操作步骤可以参考点灯科技文档中心-使用esp8266 & WiFi接入
1、下载并安装blinker APP
Android下载:
点击下载
或 在android应用商店搜索“blinker”下载安装
IOS下载:
点击下载
或 在app store中搜索“blinker”下载
2、下载并安装blinker Arduino库
点击下载
Windows:将下载好的blinker库解压到 我的电脑>文档>Arduino>libraries 文件夹中
Mac OS:将下载好的blinker库解压到 文稿>Arduino>libraries 文件夹中
3、在app中添加设备,获取Secret Key并DIY界面,添加自己所需的插件
4、编译并上传示例程序
打开Arduino IDE,通过 文件>示例>Blinker>Blinker_Hello/Hello_WiFi 打开例程
在程序中找到如下变量,填入你申请到的Secret Key(auth)和要连接的WiFi热点名(ssid)、密码(pswd),如:
char auth[] = "abcdefghijkl"; //上一步中在app中获取到的Secret Key
char ssid[] = "abcdefg"; //你的WiFi热点名称
char pswd[] = "123456789"; //你的WiFi密码
例程中宏LED_BUILTIN为开发板厂家定义的连接板载LED的引脚,如果你选择的开发板没有定义LED_BUILTIN,可以自行修改为你要使用的引脚
下示例 int buttonPin = 0; // 按键的管脚定义 int lightPin = 12; // 大灯
编译并上传程序到esp8266开发板,打开串口调试器
当看到提示“MQTT Connected!”,说明设备已经成功连接到MQTT服务器
5、下面是esp8266 定义了串口库和blinker的wifi库 添加了BlinkerButton和BlinkerNumber 组件 其他blinker app组件的使用方法可参考 开发文档中>硬件开发>Arduino支持>APP组件中 可以初始化, 创建对象;处理收到数据的回调函数;注册回调函数
#define BUTTON_1 "ButtonKey"
BlinkerButton Button1(BUTTON_1);
#define NUM_1 "NUMKey"
BlinkerNumber NUM1(NUM_1);
下面是实列代码
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#include <Blinker.h>
char blinker_auth[] = "1e78d64ea06a";//输入blinker密钥//
char blinker_ssid[] = "bad123";
char blinker_pswd[] = "1234567890";
int buttonPin = 0; // 按键的管脚定义
int lightPin = 12; // 大灯
// 新建组件对象
BlinkerButton Button1("btn1");
BlinkerNumber Number1("num-abc");
int counter = 0;
// 按下按键即会执行该函数
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(lightPin, !digitalRead(lightPin)); //按键控制灯的 亮和灭
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
Number1.print(counter);
}
// 初始化blinker
void blinkerStart()
{
// 初始化blinker
Blinker.begin(blinker_auth, blinker_ssid, blinker_pswd);
Blinker.attachData(dataRead);//没有绑定的控件 函数注册
Button1.attach(button1_callback);//按钮处理 函数注册
}
void setup() {
// 初始化串口
Serial.begin(115200);
#if defined(BLINKER_PRINT)
BLINKER_DEBUG.stream(BLINKER_PRINT);
#endif
// 初始化有LED的IO
pinMode(lightPin, OUTPUT);
digitalWrite(lightPin, HIGH);
pinMode(buttonPin,INPUT); //有信号输入时用
blinkerStart();//初始化blinker 配置WiFi 远程按钮等信息
}
void loop() {
Blinker.run();
}
B、小爱同学接入Blinker APP中 实现通讯
绑定小爱同学
1.打开米家App。通过 我的>其他平台设备>点击添加>点灯科技>绑定账号 ,绑定blinker账号
2.绑定成功后,支持小爱控制的blinker设备会出现在 我的>其他平台设备>点灯科技 设备列表中
3.现在可以使用小爱控制该设备了
其他说明
1.如果绑定blinker账号后,点灯科技列表中没有设备,可能是你设备中没有烧写小爱支持程序,或者设备没有成功上线。
2.小爱音响App中也可以进行添加操作,添加后可看到对应设备
3.绑定blinker账号后,通过手机上的小爱同学也可以控制设备
4.blinker App中对设备进行修改后(如修改设备名),可在米家App或小爱音响App中,通过 我的>其他平台设备>点灯科技>同步设备 更新设备信息
打开打开例程,通过 文件>示例>Blinker_MIOT>参考小爱同学的接入 ,具体参考点灯科技开发文档>语音助手接入>小米小爱
下面是接入小米小爱的电源开关实列
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Blinker.h>
char auth[] = "Your Device Secret Key";
char ssid[] = "Your WiFi network SSID or name";
char pswd[] = "Your WiFi network WPA password or WEP key";
bool oState = false;
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
oState = false;
}
}
void miotQuery(int32_t queryCode)
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
}
void loop()
{
Blinker.run();
}
进阶的是下面是接入小米小爱的灯开关的实列
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>
char auth[] = "Your Device Secret Key";
char ssid[] = "Your WiFi network SSID or name";
char pswd[] = "Your WiFi network WPA password or WEP key";
// Download Adafruit_NeoPixel library here:
// https://github.com/adafruit/Adafruit_NeoPixel
#include <Adafruit_NeoPixel.h>
#define PIN 2
#define NUMPIXELS 24
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define RGB_1 "RGBKey"
BlinkerRGB WS2812(RGB_1);
uint8_t colorR, colorG, colorB, colorW;
bool wsState;
uint8_t wsMode = BLINKER_CMD_MIOT_DAY;
void pixelShow()
{
pixels.setBrightness(colorW);
for(int i = 0; i < NUMPIXELS; i++){
pixels.setPixelColor(i, colorR, colorG, colorB);
}
pixels.show();
}
void ws2812_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
BLINKER_LOG("R value: ", r_value);
BLINKER_LOG("G value: ", g_value);
BLINKER_LOG("B value: ", b_value);
BLINKER_LOG("Rrightness value: ", bright_value);
colorR = r_value;
colorG = g_value;
colorB = b_value;
colorW = bright_value;
pixelShow();
}
uint32_t getColor()
{
uint32_t color = colorR << 16 | colorG << 8 | colorB;
return color;
}
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
wsState = true;
if (colorW == 0) colorW = 255;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
wsState = false;
}
pixelShow();
}
void miotColor(int32_t color)
{
BLINKER_LOG("need set color: ", color);
colorR = color >> 16 & 0xFF;
colorG = color >> 8 & 0xFF;
colorB = color & 0xFF;
BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);
pixelShow();
BlinkerMIOT.color(color);
BlinkerMIOT.print();
}
void miotMode(uint8_t mode)
{
BLINKER_LOG("need set mode: ", mode);
if (mode == BLINKER_CMD_MIOT_DAY) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_NIGHT) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_COLOR) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_WARMTH) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_TV) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_READING) {
// Your mode function
}
else if (mode == BLINKER_CMD_MIOT_COMPUTER) {
// Your mode function
}
wsMode = mode;
BlinkerMIOT.mode(mode);
BlinkerMIOT.print();
}
void miotBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);
colorW = bright.toInt();
BLINKER_LOG("now set brightness: ", colorW);
pixelShow();
BlinkerMIOT.brightness(colorW);
BlinkerMIOT.print();
}
void miotColoTemp(int32_t colorTemp)
{
BLINKER_LOG("need set colorTemperature: ", colorTemp);
BlinkerMIOT.colorTemp(colorTemp);
BlinkerMIOT.print();
}
void miotQuery(int32_t queryCode)
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(wsState ? "on" : "off");
BlinkerMIOT.color(0);
BlinkerMIOT.mode(0);
BlinkerMIOT.colorTemp(1000);
BlinkerMIOT.brightness(1);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(wsState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_COLOR_NUMBER :
BLINKER_LOG("MIOT Query Color");
BlinkerMIOT.color(0);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_MODE_NUMBER :
BLINKER_LOG("MIOT Query Mode");
BlinkerMIOT.mode(0);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_COLORTEMP_NUMBER :
BLINKER_LOG("MIOT Query ColorTemperature");
BlinkerMIOT.colorTemp(1000);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_BRIGHTNESS_NUMBER :
BLINKER_LOG("MIOT Query Brightness");
BlinkerMIOT.brightness(1);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(wsState ? "on" : "off");
BlinkerMIOT.color(0);
BlinkerMIOT.mode(0);
BlinkerMIOT.colorTemp(1000);
BlinkerMIOT.brightness(1);
BlinkerMIOT.print();
break;
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachColor(miotColor);
BlinkerMIOT.attachMode(miotMode);
BlinkerMIOT.attachBrightness(miotBright);
BlinkerMIOT.attachColorTemperature(miotColoTemp);
BlinkerMIOT.attachQuery(miotQuery);
pinMode(14, OUTPUT);
digitalWrite(14, HIGH);
pinMode(15, OUTPUT);
digitalWrite(15, HIGH);
colorR = 255;
colorG = 255;
colorB = 255;
colorW = 0;
wsState = true;
pixels.begin();
pixels.setBrightness(colorW);
WS2812.attach(ws2812_callback);
pixelShow();
}
void loop()
{
Blinker.run();
for(int i = 0; i < NUMPIXELS; i++){
pixels.setPixelColor(i, colorR, colorG, colorB);
}
pixels.show();
}