Xiaoai classmates control ESP8266 lighting

Xiaoai classmates have been eating ash for a long time, and have always wanted to access Xiaoai to control some smart hardware, but Xiaomi ’s IOT open platform is only open to enterprise users. Recently, I studied the Internet of Things platform of lighting technology. The software and hardware support is very rich. The key is that there is a free version for lovers. Is there any wood for love? In this article, I will share how to access Xiao Ai through the Blinker platform to control the lighting of the connected ESP8266.

1. Hardware

I used the ESP8266 development board as shown in the figure below. In fact, it is the 8266 minimum system and USB to serial port onboard. There are many on Taobao, the big difference may be that the serial chip is divided into CH340 and CP2102.

ESP8266 development board

The ESP8266 module has an onboard LED light, which is connected to GPIO2. The GPIO2 output high-level light turns off, and the output low-level light turns on. Let's control it.

2. Platform section

Smart home devices need to be added from the Mijia app in the Xiaoai classmates app. Therefore, the two apps are downloaded and registered in advance. Here I will focus on the Blinker APP.

  • Download Blinker from the official website, support Android and Apple versions, select the corresponding download you like , and register.
  • Click Add Device, select the Arduino in the DIY device, and select WiFi access. You will get a unique key, which is very important and needs to be added in the code later.

Add device

  • Return to the device list page, click the added device icon to enter the device control panel. You can click on the upper right corner to edit. The device name here is best in Chinese. When you use Xiaoai to control it later, if you speak English to it, it may not be understood.

3. Software part

  1. Install ESP8266 development board extension
  • Start the Arduino IDE, click "File"-"Preferences", enter the following URL in the additional development board management URL:
http://arduino.esp8266.com/stable/package_esp8266com_index.json

Add development board URL

  • Click "Tools"-"Development Board"-"Development Board Manager", enter "esp8266" in the search bar, find the development board to install, the installation is completed as shown below.

Install 8266 development board extension

If your network is not scientific enough, this process may not be completed. The official provides an offline installation method, you can click to view .

  1. Install blinker Arduino library

Click Download Library and extract the downloaded blinker library to My Computer> Documents> Arduino> libraries folder.

  1. Program download

Blinker officially provides the access process of Xiaoai classmates. Taking the control of RGB lights as an example, each operation interface of Xiaoai classmates is introduced . For details, please refer to the official introduction .

I have simplified here, only used to control the light on and off, and only used the power operation interface and device query interface. In fact, it is to implement two callback functions. When receiving the command from Xiao Ai, it will enter the callback function to operate the hardware.

The code is as follows, the actual values ​​of auth, ssid, and pswd in the code should be filled in.

#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";

bool ledState;

void dataRead(const String & data)
{
  BLINKER_LOG("Blinker readString: ", data);

  uint32_t BlinkerTime = millis();

  Blinker.print("millis", BlinkerTime);
}

void miotPowerState(const String & state)
{
  BLINKER_LOG("need set power state: ", state);

  if (state == BLINKER_CMD_ON) {
    digitalWrite(LED_BUILTIN, LOW);
    BlinkerMIOT.powerState("on");
    BlinkerMIOT.print();
    ledState = true;
  }
  else if (state == BLINKER_CMD_OFF) {
    digitalWrite(LED_BUILTIN, HIGH);
    BlinkerMIOT.powerState("off");
    BlinkerMIOT.print();
    ledState = false;
  }
}

void miotQuery(int32_t queryCode)
{
  BLINKER_LOG("MIOT Query codes: ", queryCode);

  switch (queryCode)
  {
    case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
      BLINKER_LOG("MIOT Query Power State");
      BlinkerMIOT.powerState(ledState ? "on" : "off");
      BlinkerMIOT.print();
      break;

    default :
      BlinkerMIOT.powerState(ledState ? "on" : "off");
      BlinkerMIOT.print();
      break;
  }
}
void setup()
{
  Serial.begin(115200);
  BLINKER_DEBUG.stream(Serial);
  BLINKER_DEBUG.debugAll();

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  Blinker.begin(auth, ssid, pswd);

  Blinker.attachData(dataRead);
  BlinkerMIOT.attachPowerState(miotPowerState);
  BlinkerMIOT.attachQuery(miotQuery);
}

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

Select the development board type and actual port number to download the program.

Select 8266 development board

4. Bind little love

  • After downloading the program, reset the development board, and then you will see in the Blinker APP that the added device is now online.

Online device

  • Enter Mijia APP, click "My"-"Other Platform Devices", add lighting technology, after entering, click Sync Device, you will see the device created in Blinker.

Add sync device

  • After the previous binding is successful, the bound device will appear in Xiao Ai's classmates.

Bind successfully

Try to say to Xiao Ai: "turn on the light", "turn off the light", "state of the light", there may be some delays when the network is not good, but the desired control has been achieved. You can also try to control some other devices.


Pay attention to the public number "TonyCode" and reply "Xiaoai" in the background to get the code and documents in the text.

TonyCode

Published 73 original articles · praised 275 · 270,000 views +

Guess you like

Origin blog.csdn.net/TonyIOT/article/details/105076487