學(xué)習(xí)筆記——2、 Arduino+BLinker APP 來控制ESP8266

開發(fā)前期準(zhǔn)備

1、arduinoIDE 寫ESP8266 的開發(fā)環(huán)境的搭建+esp8266的插件庫(kù),

2、esp8266-01芯片的程序下載的電路接法(GPIO0的觸發(fā)參考下圖)
GPIO0的引腳接法

ESP8266 WIFI接入Blinker + BLinker APP 和小愛同學(xué)控制ESP8266

A、esp8266接入WiFi 并創(chuàng)建BIinker的APP的組件

詳細(xì)的操作步驟可以參考點(diǎn)燈科技文檔中心-使用esp8266 & WiFi接入

1、下載并安裝blinker APP
Android下載:
點(diǎn)擊下載
或 在android應(yīng)用商店搜索“blinker”下載安裝
IOS下載:
點(diǎn)擊下載
或 在app store中搜索“blinker”下載

2、下載并安裝blinker Arduino庫(kù)
點(diǎn)擊下載
Windows:將下載好的blinker庫(kù)解壓到 我的電腦>文檔>Arduino>libraries 文件夾中
Mac OS:將下載好的blinker庫(kù)解壓到 文稿>Arduino>libraries 文件夾中
3、在app中添加設(shè)備,獲取Secret Key并DIY界面,添加自己所需的插件

4、編譯并上傳示例程序
打開Arduino IDE,通過 文件>示例>Blinker>Blinker_Hello/Hello_WiFi 打開例程
在程序中找到如下變量,填入你申請(qǐng)到的Secret Key(auth)和要連接的WiFi熱點(diǎn)名(ssid)、密碼(pswd),如:

char auth[] = "abcdefghijkl"; //上一步中在app中獲取到的Secret Key
char ssid[] = "abcdefg"; //你的WiFi熱點(diǎn)名稱
char pswd[] = "123456789"; //你的WiFi密碼

例程中宏LED_BUILTIN為開發(fā)板廠家定義的連接板載LED的引腳,如果你選擇的開發(fā)板沒有定義LED_BUILTIN,可以自行修改為你要使用的引腳
下示例 int buttonPin = 0; // 按鍵的管腳定義 int lightPin = 12; // 大燈

編譯并上傳程序到esp8266開發(fā)板,打開串口調(diào)試器
當(dāng)看到提示“MQTT Connected!”,說明設(shè)備已經(jīng)成功連接到MQTT服務(wù)器
5、下面是esp8266 定義了串口庫(kù)和blinker的wifi庫(kù) 添加了BlinkerButton和BlinkerNumber 組件 其他blinker app組件的使用方法可參考 開發(fā)文檔中>硬件開發(fā)>Arduino支持>APP組件中 可以初始化, 創(chuàng)建對(duì)象;處理收到數(shù)據(jù)的回調(diào)函數(shù);注冊(cè)回調(diào)函數(shù)

#define BUTTON_1 "ButtonKey"
BlinkerButton Button1(BUTTON_1);
#define NUM_1 "NUMKey"
BlinkerNumber NUM1(NUM_1);

下面是實(shí)列代碼

#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;  // 大燈

// 新建組件對(duì)象
BlinkerButton Button1("btn1");
BlinkerNumber Number1("num-abc");

int counter = 0;

// 按下按鍵即會(huì)執(zhí)行該函數(shù)
void button1_callback(const String & state) {
    BLINKER_LOG("get button state: ", state);
    digitalWrite(lightPin, !digitalRead(lightPin));  //按鍵控制燈的 亮和滅
}

// 如果未綁定的組件被觸發(fā),則會(huì)執(zhí)行其中內(nèi)容
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);//沒有綁定的控件 函數(shù)注冊(cè)
    Button1.attach(button1_callback);//按鈕處理 函數(shù)注冊(cè)
}
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); //有信號(hào)輸入時(shí)用

    blinkerStart();//初始化blinker 配置WiFi 遠(yuǎn)程按鈕等信息
}

void loop() {
    Blinker.run();
}

B、小愛同學(xué)接入Blinker APP中 實(shí)現(xiàn)通訊

綁定小愛同學(xué)
1.打開米家App。通過 我的>其他平臺(tái)設(shè)備>點(diǎn)擊添加>點(diǎn)燈科技>綁定賬號(hào) ,綁定blinker賬號(hào)
2.綁定成功后,支持小愛控制的blinker設(shè)備會(huì)出現(xiàn)在 我的>其他平臺(tái)設(shè)備>點(diǎn)燈科技 設(shè)備列表中
3.現(xiàn)在可以使用小愛控制該設(shè)備了
其他說明
1.如果綁定blinker賬號(hào)后,點(diǎn)燈科技列表中沒有設(shè)備,可能是你設(shè)備中沒有燒寫小愛支持程序,或者設(shè)備沒有成功上線。
2.小愛音響App中也可以進(jìn)行添加操作,添加后可看到對(duì)應(yīng)設(shè)備
3.綁定blinker賬號(hào)后,通過手機(jī)上的小愛同學(xué)也可以控制設(shè)備
4.blinker App中對(duì)設(shè)備進(jìn)行修改后(如修改設(shè)備名),可在米家App或小愛音響App中,通過 我的>其他平臺(tái)設(shè)備>點(diǎn)燈科技>同步設(shè)備 更新設(shè)備信息

打開打開例程,通過 文件>示例>Blinker_MIOT>參考小愛同學(xué)的接入 ,具體參考點(diǎn)燈科技開發(fā)文檔>語音助手接入>小米小愛

下面是接入小米小愛的電源開關(guān)實(shí)列

#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();
}

進(jìn)階的是下面是接入小米小愛的燈開關(guān)的實(shí)列

#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();
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容