[Arduino物联网开发实战4]天猫精灵控制2
对于照明设备,除了可以查询其状态外,还可以改变其状态。详见:[天猫精灵blinker接口文档](https://doc.blinker.app/?file=007-%E8%AF%AD%E9%9F%B3%E5%8A%A9%E6%89%8B%E6%8E%A5%E5%85%A5/01-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5)
可以修改各回调函数中的内容,以响应不同的指令。完整示例如下:
```
#define BLINKER_WIFI
#define BLINKER_ALIGENIE_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";
#include <Adafruit_NeoPixel.h>
#define PIN 13
#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;
String wsMode = BLINKER_CMD_COMMON;
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();
}
String getColor()
{
uint32_t color = colorR << 16 | colorG << 8 | colorB;
switch (color)
{
case 0xFF0000 :
return "Red";
case 0xFFFF00 :
return "Yellow";
case 0x0000FF :
return "Blue";
case 0x00FF00 :
return "Green";
case 0xFFFFFF :
return "White";
case 0x000000 :
return "Black";
case 0x00FFFF :
return "Cyan";
case 0x800080 :
return "Purple";
case 0xFFA500 :
return "Orange";
default :
return "White";
}
}
void aligeniePowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, HIGH);
BlinkerAliGenie.powerState("on");
BlinkerAliGenie.print();
wsState = true;
if (colorW == 0) colorW = 255;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerAliGenie.powerState("off");
BlinkerAliGenie.print();
wsState = false;
}
pixelShow();
}
void aligenieColor(const String & color)
{
BLINKER_LOG("need set color: ", color);
if (color == "Red") {
colorR = 255;
colorG = 0;
colorB = 0;
}
else if (color == "Yellow") {
colorR = 255;
colorG = 255;
colorB = 0;
}
else if (color == "Blue") {
colorR = 0;
colorG = 0;
colorB = 255;
}
else if (color == "Green") {
colorR = 0;
colorG = 255;
colorB = 0;
}
else if (color == "White") {
colorR = 255;
colorG = 255;
colorB = 255;
}
else if (color == "Black") {
colorR = 0;
colorG = 0;
colorB = 0;
}
else if (color == "Cyan") {
colorR = 0;
colorG = 255;
colorB = 255;
}
else if (color == "Purple") {
colorR = 128;
colorG = 0;
colorB = 128;
}
else if (color == "Orange") {
colorR = 255;
colorG = 165;
colorB = 0;
}
if (wsState == false) {
wsState = true;
colorW = 255;
}
if (colorW == 0) {
colorW = 255;
}
pixelShow();
BlinkerAliGenie.color(color);
BlinkerAliGenie.print();
}
void aligenieMode(const String & mode)
{
BLINKER_LOG("need set mode: ", mode);
if (mode == BLINKER_CMD_ALIGENIE_READING) {
// Your mode function
}
else if (mode == BLINKER_CMD_ALIGENIE_MOVIE) {
// Your mode function
}
else if (mode == BLINKER_CMD_ALIGENIE_SLEEP) {
// Your mode function
}
else if (mode == BLINKER_CMD_ALIGENIE_HOLIDAY) {
// Your mode function
}
else if (mode == BLINKER_CMD_ALIGENIE_MUSIC) {
// Your mode function
}
else if (mode == BLINKER_CMD_ALIGENIE_COMMON) {
// Your mode function
}
wsMode = mode;
BlinkerAliGenie.mode(mode);
BlinkerAliGenie.print();
}
void aligenieCancelMode(const String & cmode)
{
BLINKER_LOG("need cancel mode: ", cmode);
if (cmode == BLINKER_CMD_ALIGENIE_READING) {
// Your mode function
}
else if (cmode == BLINKER_ALIGENIE_CMD_MOVIE) {
// Your mode function
}
else if (cmode == BLINKER_ALIGENIE_CMD_SLEEP) {
// Your mode function
}
else if (cmode == BLINKER_ALIGENIE_CMD_HOLIDAY) {
// Your mode function
}
else if (cmode == BLINKER_ALIGENIE_CMD_MUSIC) {
// Your mode function
}
else if (cmode == BLINKER_ALIGENIE_CMD_COMMON) {
// Your mode function
}
wsMode = BLINKER_CMD_COMMON; // new mode
BlinkerAliGenie.mode(wsMode); // must response
BlinkerAliGenie.print();
}
void aligenieBright(const String & bright)
{
BLINKER_LOG("need set brightness: ", bright);
if (bright == BLINKER_CMD_MAX) {
colorW = 255;
}
else if (bright == BLINKER_CMD_MIN) {
colorW = 0;
}
else {
colorW = bright.toInt();
}
BLINKER_LOG("now set brightness: ", colorW);
pixelShow();
BlinkerAliGenie.brightness(colorW);
BlinkerAliGenie.print();
}
void aligenieRelativeBright(int32_t bright)
{
BLINKER_LOG("need set relative brightness: ", bright);
if (colorW + bright < 255 && colorW + bright >= 0) {
colorW += bright;
}
BLINKER_LOG("now set brightness: ", colorW);
pixelShow();
BlinkerAliGenie.brightness(bright);
BlinkerAliGenie.print();
}
void aligenieColoTemp(int32_t colorTemp)
{
BLINKER_LOG("need set colorTemperature: ", colorTemp);
BlinkerAliGenie.colorTemp(colorTemp);
BlinkerAliGenie.print();
}
void aligenieRelativeColoTemp(int32_t colorTemp)
{
BLINKER_LOG("need set relative colorTemperature: ", colorTemp);
BlinkerAliGenie.colorTemp(colorTemp);
BlinkerAliGenie.print();
}
void aligenieQuery(int32_t queryCode)
{
BLINKER_LOG("AliGenie Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("AliGenie Query All");
BlinkerAliGenie.powerState(wsState ? "on" : "off");
BlinkerAliGenie.color(getColor());
BlinkerAliGenie.mode(wsMode);
BlinkerAliGenie.colorTemp(50);
BlinkerAliGenie.brightness(colorW);
BlinkerAliGenie.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("AliGenie Query Power State");
BlinkerAliGenie.powerState(wsState ? "on" : "off");
BlinkerAliGenie.print();
break;
case BLINKER_CMD_QUERY_COLOR_NUMBER :
BLINKER_LOG("AliGenie Query Color");
BlinkerAliGenie.color(getColor());
BlinkerAliGenie.print();
break;
case BLINKER_CMD_QUERY_MODE_NUMBER :
BLINKER_LOG("AliGenie Query Mode");
BlinkerAliGenie.mode(wsMode);
BlinkerAliGenie.print();
break;
case BLINKER_CMD_QUERY_COLORTEMP_NUMBER :
BLINKER_LOG("AliGenie Query ColorTemperature");
BlinkerAliGenie.colorTemp(50);
BlinkerAliGenie.print();
break;
case BLINKER_CMD_QUERY_BRIGHTNESS_NUMBER :
BLINKER_LOG("AliGenie Query Brightness");
BlinkerAliGenie.brightness(colorW);
BlinkerAliGenie.print();
break;
default :
BlinkerAliGenie.powerState(wsState ? "on" : "off");
BlinkerAliGenie.color(getColor());
BlinkerAliGenie.mode(wsMode);
BlinkerAliGenie.colorTemp(50);
BlinkerAliGenie.brightness(colorW);
BlinkerAliGenie.print();
break;
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print(BlinkerTime);
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);
BlinkerAliGenie.attachPowerState(aligeniePowerState);
BlinkerAliGenie.attachColor(aligenieColor);
BlinkerAliGenie.attachMode(aligenieMode);
BlinkerAliGenie.attachCancelMode(aligenieCancelMode);
BlinkerAliGenie.attachBrightness(aligenieBright);
BlinkerAliGenie.attachRelativeBrightness(aligenieRelativeBright);
BlinkerAliGenie.attachColorTemperature(aligenieColoTemp);
BlinkerAliGenie.attachRelativeColorTemperature(aligenieRelativeColoTemp);
BlinkerAliGenie.attachQuery(aligenieQuery);
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();
}
```
更详细的文档,可见
https://diandeng.tech/doc/tmallgenie
:funk:iiiiiiiiiiiiiiiiiiiiiiiiiiiiii blinker有没有办法和手机进行字符串的通信呢? trip 发表于 2019-11-19 20:09
blinker有没有办法和手机进行字符串的通信呢?
可以 奈何col 发表于 2019-11-19 22:46
可以
应该用哪个函数啊
奈何col 发表于 2019-11-19 22:46
可以
看了看手机端没有可以输入字符的组件啊
我想可以用手机发送一段字符串,8266收到后在再回一段字符串 trip 发表于 2019-11-20 09:34
看了看手机端没有可以输入字符的组件啊
我想可以用手机发送一段字符串,8266收到后在再回一段字符串 ...
调试组件,或者直接用按键自定义方式 RGBKey是什么意思?在哪儿获取?
页:
[1]