vany5921
Published © LGPL

Atom controlled by XiaoAI Voice

When you don't want to enter the app control device, you can consider using voice assistant to operate atom directly

BeginnerFull instructions provided2 hours44
Atom controlled by XiaoAI Voice

Story

Read more

Code

Arduino code

Arduino
#define BLINKER_WIFI  
#define BLINKER_MIOT_OUTLET  
#define LEDPIN 27 
#define NUMPIXELS 1
#define BUTTON_1 "btn-abc" 

#include <Blinker.h>
#include <Adafruit_NeoPixel.h>

char auth[] = "7d63b741****"; 
char ssid[] = "M5-2.4G"; 
char pswd[] = "Password"; 

bool oState = false; 
const int OUT_PIN = 33; 

BlinkerButton Button1(BUTTON_1);

Adafruit_NeoPixel pixels(NUMPIXELS, LEDPIN, NEO_GRB + NEO_KHZ800);

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

    if (state == BLINKER_CMD_ON) {
        digitalWrite(OUT_PIN, LOW);
        pixels.setPixelColor(0, pixels.Color(0, 0, 0));
        BlinkerMIOT.powerState("on");
        BlinkerMIOT.print();
        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(OUT_PIN, HIGH);
        pixels.setPixelColor(0, pixels.Color(255,255,255));
        BlinkerMIOT.powerState("off");
        BlinkerMIOT.print();
        oState = false;
    }
    pixels.show();
}

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);

    pixels.begin();

    pinMode(OUT_PIN, OUTPUT);
    digitalWrite(OUT_PIN, LOW);

    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);
    Button1.attach(button1_callback);
    BlinkerMIOT.attachPowerState(miotPowerState);
    BlinkerMIOT.attachQuery(miotQuery);
}

void loop()
{
    Blinker.run();
}
void button1_callback(const String & state) 
{
  if (state == BLINKER_CMD_BUTTON_TAP)
  {
    if (oState) {
      digitalWrite(OUT_PIN, LOW);
      pixels.setPixelColor(0, pixels.Color(0, 0, 0));
      oState = false;
    }
    else {
      digitalWrite(OUT_PIN, HIGH);
      pixels.setPixelColor(0, pixels.Color(255, 255, 255));
      oState = true;
    }
    Button1.print();
    pixels.show();
  }
}

Credits

vany5921
30 projects • 6 followers

Comments