2023年12月5日 星期二

DIY Voice controlled ESP32+WS2812 Strip

Purpose:
利用天貓精靈的語音指令去控制ESP32上的WS2812的燈條, 實現改變燈條顏色和燈條的亮度.
Use Tmall Genie's voice commands to control the WS2812 light strip on the ESP32 to change the color and brightness of the light strip
.
Fundamental:
WS2812 RGB LED, it is an LED with a built-in driver chip function, and uses a 5050 LED package to add a driver chip, and the driving method adopts serial input and output.
We will learn how to install WS2812 on an ESP32 board and use it to control a string of addressable LEDs.

Adafruit_NeoPixel.h Library

天貓精靈APP:


Bemfa topic define:
Reference https://cloud.bemfa.com/docs/#/
topic字段值说明:
根据topic字段后三位判断主题类型
当主题名字后三位是001时为插座设备。
When the last three digits of the topic name are 001, it is a socket device.
当主题名字后三位是002时为灯泡设备。
When the last three digits of the topic name are 002, it is the light bulb device.
当主题名字后三位是003时为风扇设备。
当主题名字后三位是004时为传感器设备。
当主题名字后三位是005时为空调设备。
当主题名字后三位是006时为开关设备。
当主题名字后三位是009时为窗帘设备。

Reference the previous article link on blog:
ESP32與WS2812的繽紛世界

Architectures:


YouTube Demo:

ESP32 Code:
#include <WiFi.h>
#include <WiFiClient.h>
#include <Adafruit_NeoPixel.h>

//巴法云服务器地址默认即可
#define TCP_SERVER_ADDR "bemfa.com"
//服务器端口,tcp创客云端口8344
#define TCP_SERVER_PORT "8344"

//********************需要修改的部分*******************//
#define DEFAULT_STASSID  "ras_2.4"     //WIFI名称,区分大小写,不要写错
#define DEFAULT_STAPSW   "181415181415"  //WIFI密码

String UID = "your UID of bemfa";  //用户私钥,可在控制台获取,修改为自己的UID
String TOPIC =   "fan003";         //主题名字,可在控制台新建
String TOPIC1 =   "adapter001";         //主题名字,可在控制台新建
String TOPIC2 =   "led002";         //主题名字,可在控制台新建
//const int LED_Pin = D2;              //单片机引脚
//------------------------------------------------
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    27
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT  180
// NeoPixel brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 200 // Set BRIGHTNESS to about 1/5 (max = 255)
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
int colorIndex = 0;
int endIndex = 0;
String getcolor = "";
int colorvalue = 65280;
int beforecolorBrightness = 20;
int aftercolorBrightness = 20;
int colorflag = 0;
//-----Relay------------------
#define RELAY1       32
#define RELAY2       33
#define RELAY3       25
#define RELAY4       26
#define RELAY5       5
#define RELAY6       17
#define RELAY7       16
#define RELAY8       4
//**************************************************//
//最大字节数
#define MAX_PACKETSIZE 512
//设置心跳值30s
#define KEEPALIVEATIME 30*1000

//tcp客户端相关初始化,默认即可
WiFiClient TCPclient;
String TcpClient_Buff = "";
unsigned int TcpClient_BuffIndex = 0;
unsigned long TcpClient_preTick = 0;
unsigned long preHeartTick = 0;//心跳
unsigned long preTCPStartTick = 0;//连接
bool preTCPConnected = false;

//相关函数初始化
//连接WIFI
void doWiFiTick();
void startSTA();

//TCP初始化连接
void doTCPClientTick();
void startTCPClient();
void sendtoTCPServer(String p);

//风扇控制函数
void turnOn();
void turnOff();
int LEDFlag = 2;
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
TaskHandle_t hled;
void initial()
{
  Serial.println(F("Create Task"));
  //--------------- create task----------------------------------
  xTaskCreatePinnedToCore(
    vLEDTask, "LEDTask" // A name just for humans
    ,
    1024 // This stack size can be checked & adjusted by reading the Stack Highwater
    ,
    NULL, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    ,
    &hled //handle
    ,
    0);
  //----------------------------------------
  //----------------------------------------------------------------------
  //vTaskSuspend(hfunction); //暫停TASK運行
  //----------------------------------------------------------------------
}
/*
  *发送数据到TCP服务器
 */
void sendtoTCPServer(String p){
 
  if (!TCPclient.connected())
  {
    Serial.println("Client is not readly");
    return;
  }
  TCPclient.print(p);
  Serial.println("[Send to TCPServer]:String");
  Serial.println(p);
}
/*
  *初始化和服务器建立连接
*/
void startTCPClient(){
  if(TCPclient.connect(TCP_SERVER_ADDR, atoi(TCP_SERVER_PORT))){
    Serial.print("\nConnected to server:");
    Serial.printf("%s:%d\r\n",TCP_SERVER_ADDR,atoi(TCP_SERVER_PORT));
   
    String tcpTemp="";  //初始化字符串
    String tcpTemp1="";  //初始化字符串
    String tcpTemp2="";  //初始化字符串
    tcpTemp = "cmd=1&uid="+UID+"&topic="+TOPIC+"\r\n"; //构建订阅指令
    sendtoTCPServer(tcpTemp); //发送订阅指令
    tcpTemp="";//清空
    tcpTemp1 = "cmd=1&uid="+UID+"&topic="+TOPIC1+"\r\n"; //构建订阅指令
    sendtoTCPServer(tcpTemp1); //发送订阅指令
    tcpTemp1="";//清空
    tcpTemp2 = "cmd=1&uid="+UID+"&topic="+TOPIC2+"\r\n"; //构建订阅指令
    sendtoTCPServer(tcpTemp2); //发送订阅指令
    tcpTemp2="";//清空

    preTCPConnected = true;
    preHeartTick = millis();
    TCPclient.setNoDelay(true);
  }
  else{
    Serial.print("Failed connected to server:");
    Serial.println(TCP_SERVER_ADDR);
    TCPclient.stop();
    preTCPConnected = false;
  }
  preTCPStartTick = millis();
}


/*
  *检查数据,发送心跳
*/
void doTCPClientTick(){
 //检查是否断开,断开后重连
   if(WiFi.status() != WL_CONNECTED) return;

  if (!TCPclient.connected()) {//断开重连

  if(preTCPConnected == true){

    preTCPConnected = false;
    preTCPStartTick = millis();
    Serial.println();
    Serial.println("TCP Client disconnected.");
    TCPclient.stop();
  }
  else if(millis() - preTCPStartTick > 1*1000)//重新连接
    startTCPClient();
  }
  else
  {
    if (TCPclient.available()) {//收数据
      char c =TCPclient.read();
      TcpClient_Buff +=c;
      TcpClient_BuffIndex++;
      TcpClient_preTick = millis();
     
      if(TcpClient_BuffIndex>=MAX_PACKETSIZE - 1){
        TcpClient_BuffIndex = MAX_PACKETSIZE-2;
        TcpClient_preTick = TcpClient_preTick - 200;
      }
      preHeartTick = millis();
    }
    if(millis() - preHeartTick >= KEEPALIVEATIME){//保持心跳
      preHeartTick = millis();
      Serial.println("--Keep alive:");
      sendtoTCPServer("cmd=0&msg=keep\r\n");
    }
  }
  if((TcpClient_Buff.length() >= 1) && (millis() - TcpClient_preTick>=200))
  {//data ready
    TCPclient.flush();
    Serial.println("Buff");
    Serial.println(TcpClient_Buff);
   
    if((TcpClient_Buff.indexOf("fan003") > 0))
    {
      if((TcpClient_Buff.indexOf("&msg=on") > 0))
      {
        turnOn();
      }
      else if((TcpClient_Buff.indexOf("&msg=off") > 0))
      {
        turnOff();
      }
    }
    //-------------------------------------------------------------
    if((TcpClient_Buff.indexOf("adapter001") > 0))
    {
      if((TcpClient_Buff.indexOf("&msg=on") > 0))
      {
        turnAdapterOn();
      }
      else if((TcpClient_Buff.indexOf("&msg=off") > 0))
      {
        turnAdapterOff();
      }
    }
    //-------------------------------------------------------------
    if((TcpClient_Buff.indexOf("led002") > 0))
    {
      //--------------------------------
      if((TcpClient_Buff.indexOf("&msg=on") > 0))
      {
        turnLedOn();
        if(colorflag == 0)
        {
          for(int i=0;i<LED_COUNT;i++)
          {
            strip.setPixelColor(i, strip.Color(255, 255, 255, beforecolorBrightness)); // Set white
          }
          strip.show();
          colorflag=1;
        }
        //----------------------------------
        //cmd=2&uid=1de98fdcc4554cfa8525aa2e08b74736&topic=led002&msg=on#80
        if(TcpClient_Buff.length() > 65 )
        {
          colorIndex = TcpClient_Buff.indexOf('#');
          endIndex = TcpClient_Buff.lastIndexOf('/r');
          getcolor = "";
          //c语言字符串查找,查找&msg=位置
          //int msgIndex = TcpClient_Buff.indexOf("&msg=");
          getcolor= TcpClient_Buff.substring(colorIndex+1,endIndex);
          beforecolorBrightness= getcolor.toInt();
          Serial.print( " voice colorBrightness--");
          Serial.println(beforecolorBrightness);
          aftercolorBrightness=map(beforecolorBrightness, 0, 100, 0, 255);
          Serial.print( " after colorBrightness--");
          Serial.println(aftercolorBrightness);
          strip.setBrightness(aftercolorBrightness);
          strip.show();
        }
//-- cmd=2&uid=1de98fdcc4554cfa8525aa2e08b74736&topic=led002&msg=on#100#16711680
        if(TcpClient_Buff.length() > 69)
        {
          colorIndex = TcpClient_Buff.lastIndexOf('#');
          endIndex = TcpClient_Buff.lastIndexOf('/r');
          getcolor = "";
          //c语言字符串查找,查找&msg=位置
          //int msgIndex = TcpClient_Buff.indexOf("&msg=");
          getcolor= TcpClient_Buff.substring(colorIndex+1,endIndex);
          colorvalue=getcolor.toInt();
          Serial.print( " colorvalue--");
          Serial.println(colorvalue);
          for(int i=0;i<LED_COUNT;i++)
          {
            strip.setPixelColor(i, colorvalue);
          }
          strip.setBrightness(aftercolorBrightness);
          strip.show();
        }       
      }
      else if((TcpClient_Buff.indexOf("&msg=off") > 0))
      {
        turnLedOff();
      }
    }
    TcpClient_Buff="";
    TcpClient_BuffIndex = 0;
  }
}

void startSTA(){
  WiFi.disconnect();
  WiFi.mode(WIFI_STA);
  WiFi.begin(DEFAULT_STASSID, DEFAULT_STAPSW);
}

/**************************************************************************
                       WIFI
***************************************************************************/
/*
  WiFiTick
  检查是否需要初始化WiFi
  检查WiFi是否连接上,若连接成功启动TCP Client
  控制指示灯
*/
void doWiFiTick()
{
  static bool startSTAFlag = false;
  static bool taskStarted = false;
  static uint32_t lastWiFiCheckTick = 0;

  if (!startSTAFlag) {
    startSTAFlag = true;
    startSTA();
    Serial.printf("Heap size:%d\r\n", ESP.getFreeHeap());
  }

  //未连接1s重连
  if ( WiFi.status() != WL_CONNECTED )
  {
    if (millis() - lastWiFiCheckTick > 1000)
    {
      lastWiFiCheckTick = millis();
    }
  }
  //连接成功建立
  else {
    if (taskStarted == false) {
      taskStarted = true;
      Serial.print("\r\nGet IP Address: ");
      Serial.println(WiFi.localIP());
      startTCPClient();
    }
  }
}
//打開fan
void turnOn(){
  Serial.println("Turn ON");
  //digitalWrite(LED_Pin,LOW);
  digitalWrite(RELAY1,LOW);
}
//关闭灯泡
void turnOff(){
  Serial.println("Turn OFF");
  digitalWrite(RELAY1,HIGH);
}
//打开灯泡
void turnAdapterOn(){
  Serial.println("Turn1 ON");
  //digitalWrite(LED_Pin,LOW);
  digitalWrite(RELAY2,LOW);
}
//关闭灯泡
void turnAdapterOff(){
  Serial.println("Turn1 OFF");
  digitalWrite(RELAY2,HIGH);
}
//打开灯泡
void turnLedOn()
{
  Serial.println("Turn2 ON");
  //digitalWrite(LED_Pin,LOW);
  digitalWrite(RELAY3,LOW);
}
//关闭灯泡
void turnLedOff(){
  Serial.println("Turn2 OFF");
  digitalWrite(RELAY3,HIGH);
 
  strip.clear();
  strip.show();
}
// 初始化,相当于main 函数
void setup() {
  Serial.begin(9600);
  Serial.println(F("init"));
  initial();

  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.setBrightness(beforecolorBrightness);
  strip.clear();          
  strip.show();
  delay(1000);

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(RELAY1,OUTPUT);
  digitalWrite(RELAY1,HIGH);
  pinMode(RELAY2,OUTPUT);
  digitalWrite(RELAY2,HIGH);
  pinMode(RELAY3,OUTPUT);
  digitalWrite(RELAY3,HIGH);
  pinMode(RELAY4,OUTPUT);
  digitalWrite(RELAY4,HIGH);
  pinMode(RELAY5,OUTPUT);
  digitalWrite(RELAY5,HIGH);
  pinMode(RELAY6,OUTPUT);
  digitalWrite(RELAY6,HIGH);
  pinMode(RELAY7,OUTPUT);
  digitalWrite(RELAY7,HIGH);
  pinMode(RELAY8,OUTPUT);
  digitalWrite(RELAY8,HIGH);

}

//循环
void loop() {
  if (WiFi.status() != WL_CONNECTED)
  {
    doWiFiTick();
  }
  doTCPClientTick();  
}
//-------------------------------------------------------------------------
static void vLEDTask(void *pvParameters)
{
  (void)pvParameters;
  int      head          = LED_COUNT - 1;
  int      tail          = 0;

  Serial.println(F("LEDTask at core:"));
  Serial.println(xPortGetCoreID());
  pinMode(LED_BUILTIN, OUTPUT);
  for (;;) // A Task shall never return or exit.
  {
    if(LEDFlag == 0)
    {
      digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
      vTaskDelay(800);
    }
    else if(LEDFlag == 1)
    {
      digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
      vTaskDelay(800);
      digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
      vTaskDelay(800);
    }
    else if(LEDFlag == 2)
    {
      digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
      vTaskDelay(300);
      digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
      vTaskDelay(300);
    }
    else
    {
      vTaskDelay(10);
    }
  }
}



沒有留言:

張貼留言