2020年12月20日 星期日

ESP32 WS2812 , MQTT , Node-Red

 ESP32  WS2812 , MQTT , Node-Red


https://www.youtube.com/watch?v=hMOj_n55m2k








//Arduino程式  Multi thread多執行緒

#include <Adafruit_NeoPixel.h>

//========================================

#include <WiFi.h>

#include <PubSubClient.h>

#include <SPI.h>

#include <AutoConnect.h>

#include <WebServer.h>

WebServer server;

AutoConnect  Portal(server);

//========================================

TaskHandle_t Task0;

TaskHandle_t Task1;

TaskHandle_t Task2;

TaskHandle_t Task3;

TaskHandle_t Task4;

TaskHandle_t Task5;

TaskHandle_t Task6;

TaskHandle_t Task7;

TaskHandle_t Task8;

//========================================

#define PIN 15


// Parameter 1 = number of pixels in strip

// Parameter 2 = pin number (most are valid)

// Parameter 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)

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

// Update these with values suitable for your network.

#define MQTTid              ""                            //id of this mqtt client

#define MQTTip              "broker.mqtt-dashboard.com"   //ip address or hostname of the mqtt broker

#define MQTTport            1883                          //port of the mqtt broker

#define MQTTuser            "alex9ufo"                    //username of this mqtt client

#define MQTTpsw             "alex1234"                    //password of this mqtt client

//#define MQTTuser          "your_username"               //username of this mqtt client

//#define MQTTpsw           "your_password"               //password of this mqtt client

#define MQTTpubQos          2                             //qos of publish (see README)

#define MQTTsubQos          1                             //qos of subscribe

//Variables

String RGB_flag = "";

boolean F_flag = false;

//=============================================================================

boolean pendingDisconnect = false;

void mqttConnectedCb(); // on connect callback

void mqttDisconnectedCb(); // on disconnect callback

void mqttDataCb(char* topic, byte* payload, unsigned int length); // on new message callback


WiFiClient wclient;

PubSubClient client(MQTTip, MQTTport, mqttDataCb, wclient);

//======================================================


void rootPage() {

  char content[] = "Hello, world";

  server.send(200, "text/plain", content);

}  

//=======================================================

void mqttConnectedCb() {

  Serial.println("connected");

  // ... and resubscribe

  client.subscribe("alex9ufo/inTopic/WS2812/RGB_event", MQTTsubQos);

}


//======================================================= 

void mqttDisconnectedCb() {

  Serial.println("disconnected");

}


//======================================================= 

void mqttDataCb(char* topic, byte* payload, unsigned int length) {

  /*

  you can convert payload to a C string appending a null terminator to it;

  this is possible when the message (including protocol overhead) doesn't

  exceeds the MQTT_MAX_PACKET_SIZE defined in the library header.

  you can consider safe to do so when the length of topic plus the length of

  message doesn't exceeds 115 characters

  */

  char* message = (char *) payload;

  message[length] = 0;

  Serial.print("Message arrived [");

  Serial.print(topic);

  Serial.print("] ");

  Serial.println(message);

  String s = message;

  s.trim();

  RGB_flag=s;


}


//======================================================

void process_mqtt() {

  if (WiFi.status() == WL_CONNECTED) {

    if (client.connected()) {

      client.loop();

    } else {

    // client id, client username, client password, last will topic, last will qos, last will retain, last will message

      if (client.connect(MQTTid, MQTTuser, MQTTpsw, MQTTid "/status", 2, true, "0")) {

          pendingDisconnect = false;

          mqttConnectedCb();

      }

    }

  } else {

    if (client.connected())

      client.disconnect();

  }

  if (!client.connected() && !pendingDisconnect) {

    pendingDisconnect = true;

    mqttDisconnectedCb();

  }

}

//======================================================

void setup() {

  Serial.begin(115200);

  strip.begin();

  strip.show(); // Initialize all pixels to 'off'

  Serial.println("Configuring your Mobile WiFi to esp32ap...");

  Serial.println("Configuring another WiFi SSID,PWD...");

  server.on("/", rootPage);

  if (Portal.begin()) {

    Serial.println("HTTP server:" + WiFi.localIP().toString());

  }

    else {

    Serial.println("Connection failed");

    while(true) {yield();}

}

  //=================================================== 

  Serial.println(F("Ready!"));

  Serial.println(F("Control WS2812 RGB LED Strip"));


}


//=================================================== 

void loop() {

  process_mqtt();

  Serial.print("RGB_flag ==");

  Serial.println(RGB_flag);


//===================================================   

  if (RGB_flag == "0"){

  xTaskCreatePinnedToCore(

                    Task0code,   /* Task function. */

                    "Task0",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task0,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 0 */  

  }

  if (RGB_flag == "0"){

  xTaskCreatePinnedToCore(

                    Task1code,   /* Task function. */

                    "Task1",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task1,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 0 */  

  }

  if (RGB_flag == "0"){

  xTaskCreatePinnedToCore(

                    Task2code,   /* Task function. */

                    "Task2",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task2,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 0 */  

  }

//===================================================   

  if (RGB_flag == "1"){

  xTaskCreatePinnedToCore(

                    Task3code,   /* Task function. */

                    "Task3",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task3,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 0 */  

  }

  if (RGB_flag == "1"){

  xTaskCreatePinnedToCore(

                    Task4code,   /* Task function. */

                    "Task4",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task4,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 0 */  

  }

  if (RGB_flag == "1"){

  xTaskCreatePinnedToCore(

                    Task5code,   /* Task function. */

                    "Task5",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task5,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 0 */  

  }

//===================================================  

  if (RGB_flag == "2"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task6code,   /* Task function. */

                    "Task6",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task6,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 1 *///create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  }

  if (RGB_flag == "2"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task7code,   /* Task function. */

                    "Task7",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task7,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 1 *///create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  }

  if (RGB_flag == "2"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task8code,   /* Task function. */

                    "Task8",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task8,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 1 *///create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  }

//=================================================== 

  if (RGB_flag == "3"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task0code,   /* Task function. */

                    "Task0",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task0,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 1 */

  }

  if (RGB_flag == "3"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task6code,   /* Task function. */

                    "Task6",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task6,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 1 */

  }

  if (RGB_flag == "3"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task7code,   /* Task function. */

                    "Task7",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task7,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 1 */

  }

//=================================================== 

  if (RGB_flag == "4"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task1code,   /* Task function. */

                    "Task1",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task1,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 1 */                                                           

  }

  if (RGB_flag == "4"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task4code,   /* Task function. */

                    "Task4",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task4,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 1 */                                                           

  }

  if (RGB_flag == "4"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task8code,   /* Task function. */

                    "Task8",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task8,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 1 */                                                           

  }

//=================================================== 

  if (RGB_flag == "5"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task0code,   /* Task function. */

                    "Task0",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task0,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 1 */

  }

  if (RGB_flag == "5"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task1code,   /* Task function. */

                    "Task1",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task1,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 1 */

  }

  if (RGB_flag == "5"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task2code,   /* Task function. */

                    "Task2",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task2,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 1 */

  }

  if (RGB_flag == "5"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task3code,   /* Task function. */

                    "Task3",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task3,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 1 */

  }

  if (RGB_flag == "5"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task4code,   /* Task function. */

                    "Task4",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task4,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 1 */

  }

  if (RGB_flag == "5"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task5code,   /* Task function. */

                    "Task5",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task5,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 1 */

  }

  if (RGB_flag == "5"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task6code,   /* Task function. */

                    "Task6",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task6,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 1 */

  }

  if (RGB_flag == "5"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task7code,   /* Task function. */

                    "Task7",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task7,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 1 */

  }

  if (RGB_flag == "5"){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task8code,   /* Task function. */

                    "Task8",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task8,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 1 */

 

  }

//===================================================   

   Portal.handleClient(); 

}

 

//=================================================== 

//Task0code~ : Task8code

//===================================================

void Task0code( void * pvParameters ){

  Serial.println("Task0 running on core ");

  Serial.println(xPortGetCoreID());

  // Some example procedures showing how to display to the pixels:

  colorWipe(strip.Color(255, 0, 0), 50); // Red

  vTaskDelete( NULL );

}


void Task1code( void * pvParameters ){

  Serial.println("Task1 running on core ");

  Serial.println(xPortGetCoreID());

  // Some example procedures showing how to display to the pixels:

  colorWipe(strip.Color(0, 255, 0), 50); // Green

  vTaskDelete( NULL );

}


void Task2code( void * pvParameters ){

  Serial.println("Task2 running on core ");

  Serial.println(xPortGetCoreID());

  // Some example procedures showing how to display to the pixels:

  colorWipe(strip.Color(0, 0, 255), 50); // Blue

  vTaskDelete( NULL );

}


void Task3code( void * pvParameters ){

  Serial.println("Task3 running on core ");

  Serial.println(xPortGetCoreID());

  // Send a theater pixel chase in...

  theaterChase(strip.Color(127, 127, 127), 50); // White

  vTaskDelete( NULL );

}


void Task4code( void * pvParameters ){

  Serial.println("Task4 running on core ");

  Serial.println(xPortGetCoreID());

  // Send a theater pixel chase in...

  theaterChase(strip.Color(127,   0,   0), 50); // Red

  vTaskDelete( NULL );

}


void Task5code( void * pvParameters ){

  Serial.println("Task5 running on core ");

  Serial.println(xPortGetCoreID());

  // Send a theater pixel chase in...

  theaterChase(strip.Color(  0,   0, 127), 50); // Blue

  vTaskDelete( NULL );

}


void Task6code( void * pvParameters ){

  Serial.println("Task6 running on core ");

  Serial.println(xPortGetCoreID());

  // Some example procedures showing how to display to the pixels:

  rainbow(20);

  vTaskDelete( NULL );

}


void Task7code( void * pvParameters ){

  Serial.println("Task7 running on core ");

  Serial.println(xPortGetCoreID());

  rainbowCycle(20);

  vTaskDelete( NULL );

}

void Task8code( void * pvParameters ){

  Serial.println("Task8 running on core ");

  Serial.println(xPortGetCoreID());

  theaterChaseRainbow(50);

  vTaskDelete( NULL );

}

//=================================================== 

// Fill the dots one after the other with a color

void colorWipe(uint32_t c, uint8_t wait) {

  for(uint16_t i=0; i<strip.numPixels(); i++) {

      strip.setPixelColor(i, c);

      strip.show();

      delay(wait);

  }

  for(uint16_t i=strip.numPixels(); i<=0; i--) {

      strip.setPixelColor(i, c);

      strip.show();

      delay(wait);

  }

}

//=================================================== 

void rainbow(uint8_t wait) {

  uint16_t i, j;


  for(j=0; j<256; j++) {

    for(i=0; i<strip.numPixels(); i++) {

      strip.setPixelColor(i, Wheel((i+j) & 255));

    }

      strip.show();

      delay(wait);

  }  

  for(j=255; j<=0; j--) {

    for(i=strip.numPixels(); i<=0; i--) {

      strip.setPixelColor(i, Wheel((i+j) & 255));

      }

      strip.show();

      delay(wait);

  }

}

//=================================================== 

// Slightly different, this makes the rainbow equally distributed throughout

void rainbowCycle(uint8_t wait) {

  uint16_t i, j;


  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel

    for(i=0; i< strip.numPixels(); i++) {

      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));

    }

    strip.show();

    delay(wait);

  }

}

//=================================================== 

//Theatre-style crawling lights.

void theaterChase(uint32_t c, uint8_t wait) {

  for (int j=0; j<10; j++) {  //do 10 cycles of chasing

      for (int q=0; q < 3; q++) {

      for (int i=0; i < strip.numPixels(); i=i+3) {

        strip.setPixelColor(i+q, c);    //turn every third pixel on

      }

      strip.show();


      delay(wait);


      for (int i=0; i < strip.numPixels(); i=i+3) {

        strip.setPixelColor(i+q, 0);        //turn every third pixel off

      }

    }

  }

}

//=================================================== 

//Theatre-style crawling lights with rainbow effect

void theaterChaseRainbow(uint8_t wait) {

  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel

    for (int q=0; q < 3; q++) {

        for (int i=0; i < strip.numPixels(); i=i+3) {

          strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on

        }

        strip.show();


        delay(wait);


        for (int i=0; i < strip.numPixels(); i=i+3) {

          strip.setPixelColor(i+q, 0);        //turn every third pixel off

        }

    }

  }

}

//=================================================== 

// Input a value 0 to 255 to get a color value.

// The colours are a transition r - g - b - back to r.

uint32_t Wheel(byte WheelPos) {

  if(WheelPos < 85) {

   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);

  } else if(WheelPos < 170) {

   WheelPos -= 85;

   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);

  } else {

   WheelPos -= 170;

   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);

  }

}


//=================================================== 


#include <Adafruit_NeoPixel.h>

//========================================

#include <WiFi.h>

#include <PubSubClient.h>

#include <SPI.h>

#include <AutoConnect.h>

#include <WebServer.h>

WebServer server;

AutoConnect  Portal(server);

//========================================

TaskHandle_t Task0;

TaskHandle_t Task1;

TaskHandle_t Task2;

TaskHandle_t Task3;

TaskHandle_t Task4;

TaskHandle_t Task5;

//========================================

#define PIN 15


// Parameter 1 = number of pixels in strip

// Parameter 2 = pin number (most are valid)

// Parameter 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)

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);



// Update these with values suitable for your network.


#define MQTTid              ""                            //id of this mqtt client

#define MQTTip              "broker.mqtt-dashboard.com"   //ip address or hostname of the mqtt broker

#define MQTTport            1883                          //port of the mqtt broker

#define MQTTuser            "alex9ufo"                    //username of this mqtt client

#define MQTTpsw             "alex1234"                    //password of this mqtt client

//#define MQTTuser          "your_username"               //username of this mqtt client

//#define MQTTpsw           "your_password"               //password of this mqtt client

#define MQTTpubQos          2                             //qos of publish (see README)

#define MQTTsubQos          1                             //qos of subscribe


//Variables

char RGB_flag = 'a';

//=============================================================================

boolean pendingDisconnect = false;

void mqttConnectedCb(); // on connect callback

void mqttDisconnectedCb(); // on disconnect callback

void mqttDataCb(char* topic, byte* payload, unsigned int length); // on new message callback


WiFiClient wclient;

PubSubClient client(MQTTip, MQTTport, mqttDataCb, wclient);

//======================================================

void rootPage() {

  char content[] = "Hello, world";

  server.send(200, "text/plain", content);

}  

//=======================================================

void mqttConnectedCb() {

  Serial.println("connected");

  

  // ... and resubscribe

  client.subscribe("alex9ufo/inTopic/WS2812/RGB_event", MQTTsubQos);

}

//======================================================= 

void mqttDisconnectedCb() {

  Serial.println("disconnected");

}

//======================================================= 

void mqttDataCb(char* topic, byte* payload, unsigned int length) {


  /*

  you can convert payload to a C string appending a null terminator to it;

  this is possible when the message (including protocol overhead) doesn't

  exceeds the MQTT_MAX_PACKET_SIZE defined in the library header.

  you can consider safe to do so when the length of topic plus the length of

  message doesn't exceeds 115 characters

  */

  char* message = (char *) payload;

  message[length] = 0;


  Serial.print("Message arrived [");

  Serial.print(topic);

  Serial.print("] ");

  Serial.println(message);

  String s = message;


  s.trim();

  // Switch on the LED if an 1 was received as first character

  if (s == "0") {

      RGB_flag='0';

    } // if (s == "0")

  if (s == "1") {

      RGB_flag='1';

    } // if (s == "1") 

  if (s == "2") {

      RGB_flag='2';

    } // if (s == "2") 

  if (s == "3") {

      RGB_flag='3';

    } // if (s == "3") 

  if (s == "4") {

      RGB_flag='4';

    } // if (s == "43") 

  if (s == "5") {

      RGB_flag='5';

    } // if (s == "5") 


  if (s == "6") {

      RGB_flag='6';

    } // if (s == "6") 


               

}

//======================================================

void process_mqtt() {

  if (WiFi.status() == WL_CONNECTED) {

    if (client.connected()) {

      client.loop();

    } else {

    // client id, client username, client password, last will topic, last will qos, last will retain, last will message

      if (client.connect(MQTTid, MQTTuser, MQTTpsw, MQTTid "/status", 2, true, "0")) {

          pendingDisconnect = false;

          mqttConnectedCb();

      }

    }

  } else {

    if (client.connected())

      client.disconnect();

  }

  if (!client.connected() && !pendingDisconnect) {

    pendingDisconnect = true;

    mqttDisconnectedCb();

  }

}


//======================================================

void setup() {

  Serial.begin(115200);

  strip.begin();

  strip.show(); // Initialize all pixels to 'off'

  

  Serial.println("Configuring your Mobile WiFi to esp32ap...");

  Serial.println("Configuring another WiFi SSID,PWD...");

  

  server.on("/", rootPage);

  if (Portal.begin()) {

    Serial.println("HTTP server:" + WiFi.localIP().toString());

  }

    else {

    Serial.println("Connection failed");

    while(true) {yield();}

  }

  //=================================================== 

  Serial.println(F("Ready!"));

  Serial.println(F("Control WS2812 RGB LED Strip"));

  

}

//=================================================== 

void loop() {

  process_mqtt();

  if (RGB_flag == '0'){

  //======================================================

  xTaskCreatePinnedToCore(

                    Task0code,   /* Task function. */

                    "Task0",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task0,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 0 */  


  }

  

  if (RGB_flag == '1'){

  //======================================================   

  xTaskCreatePinnedToCore(

                    Task1code,   /* Task function. */

                    "Task1",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task1,      /* Task handle to keep track of created task */

                    0);          /* pin task to core 0 */  


  }

  

  if (RGB_flag == '2'){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task2code,   /* Task function. */

                    "Task2",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task2,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 1 *///create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

 

  }

  

  if (RGB_flag == '3'){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task3code,   /* Task function. */

                    "Task3",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task3,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 1 */

                    

  }

  

  if (RGB_flag == '4'){

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task4code,   /* Task function. */

                    "Task4",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task4,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 1 */                                                           


  }


  if (RGB_flag == '5'){

    

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1

  xTaskCreatePinnedToCore(

                    Task5code,   /* Task function. */

                    "Task5",     /* name of task. */

                    10000,       /* Stack size of task */

                    NULL,        /* parameter of the task */

                    1,           /* priority of the task */

                    &Task5,      /* Task handle to keep track of created task */

                    1);          /* pin task to core 1 */

  }


   Portal.handleClient(); 

}

//=================================================== 

//Task0code:  

void Task0code( void * pvParameters ){

  Serial.print("Task0 running on core ");

  Serial.println(xPortGetCoreID());


  for(;;){

  // Some example procedures showing how to display to the pixels:

  colorWipe(strip.Color(255, 0, 0), 50); // Red

  colorWipe(strip.Color(0, 255, 0), 50); // Green

  colorWipe(strip.Color(0, 0, 255), 50); // Blue

  } 

}

//=================================================== 

//Task1code:  

void Task1code( void * pvParameters ){

  Serial.print("Task1 running on core ");

  Serial.println(xPortGetCoreID());


  for(;;){

  // Send a theater pixel chase in...

  theaterChase(strip.Color(127, 127, 127), 50); // White

  theaterChase(strip.Color(127,   0,   0), 50); // Red

  theaterChase(strip.Color(  0,   0, 127), 50); // Blue

  } 

}

//=================================================== 

//Task2code:  

void Task2code( void * pvParameters ){

  Serial.print("Task2 running on core ");

  Serial.println(xPortGetCoreID());


  for(;;){

  // Send a theater pixel chase in...

  rainbow(20);

  rainbowCycle(20);

  theaterChaseRainbow(50);

  } 

}

//=================================================== 

//Task3code:  

void Task3code( void * pvParameters ){

  Serial.print("Task3 running on core ");

  Serial.println(xPortGetCoreID());


  for(;;){

  // Send a theater pixel chase in...

  rainbow(20);

  theaterChaseRainbow(50);

  theaterChase(strip.Color(  0,   0, 127), 50); // Blue

  } 

}

//=================================================== 

//Task4code:  

void Task4code( void * pvParameters ){

  Serial.print("Task4 running on core ");

  Serial.println(xPortGetCoreID());


  for(;;){

  // Send a theater pixel chase in...

  theaterChase(strip.Color(127, 127, 127), 50); // White

  theaterChaseRainbow(50);

  theaterChase(strip.Color(127,   0,   0), 50); // Red

  } 

}

//=================================================== 

//Task5code:  

void Task5code( void * pvParameters ){

  Serial.print("Task5 running on core ");

  Serial.println(xPortGetCoreID());


  for(;;){

  // Some example procedures showing how to display to the pixels:

  colorWipe(strip.Color(255, 0, 0), 50); // Red

  colorWipe(strip.Color(0, 255, 0), 50); // Green

  colorWipe(strip.Color(0, 0, 255), 50); // Blue


  // Send a theater pixel chase in...

  theaterChase(strip.Color(127, 127, 127), 50); // White

  theaterChase(strip.Color(127,   0,   0), 50); // Red

  theaterChase(strip.Color(  0,   0, 127), 50); // Blue

 


  rainbow(20);

  rainbowCycle(20);

  theaterChaseRainbow(50);


  } 

}

//=================================================== 

// Fill the dots one after the other with a color

void colorWipe(uint32_t c, uint8_t wait) {

  for(uint16_t i=0; i<strip.numPixels(); i++) {

      strip.setPixelColor(i, c);

      strip.show();

      delay(wait);

  }

}


void rainbow(uint8_t wait) {

  uint16_t i, j;


  for(j=0; j<256; j++) {

    for(i=0; i<strip.numPixels(); i++) {

      strip.setPixelColor(i, Wheel((i+j) & 255));

    }

    strip.show();

    delay(wait);

  }

}


// Slightly different, this makes the rainbow equally distributed throughout

void rainbowCycle(uint8_t wait) {

  uint16_t i, j;


  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel

    for(i=0; i< strip.numPixels(); i++) {

      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));

    }

    strip.show();

    delay(wait);

  }

}


//Theatre-style crawling lights.

void theaterChase(uint32_t c, uint8_t wait) {

  for (int j=0; j<10; j++) {  //do 10 cycles of chasing

    for (int q=0; q < 3; q++) {

      for (int i=0; i < strip.numPixels(); i=i+3) {

        strip.setPixelColor(i+q, c);    //turn every third pixel on

      }

      strip.show();


      delay(wait);


      for (int i=0; i < strip.numPixels(); i=i+3) {

        strip.setPixelColor(i+q, 0);        //turn every third pixel off

      }

    }

  }

}


//Theatre-style crawling lights with rainbow effect

void theaterChaseRainbow(uint8_t wait) {

  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel

    for (int q=0; q < 3; q++) {

        for (int i=0; i < strip.numPixels(); i=i+3) {

          strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on

        }

        strip.show();


        delay(wait);


        for (int i=0; i < strip.numPixels(); i=i+3) {

          strip.setPixelColor(i+q, 0);        //turn every third pixel off

        }

    }

  }

}


// Input a value 0 to 255 to get a color value.

// The colours are a transition r - g - b - back to r.

uint32_t Wheel(byte WheelPos) {

  if(WheelPos < 85) {

   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);

  } else if(WheelPos < 170) {

   WheelPos -= 85;

   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);

  } else {

   WheelPos -= 170;

   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);

  }

}

N0de-Red程式

[{"id":"f0ee5d9a.dee92","type":"ui_button","z":"850801e.b2b88","name":"","group":"ac0f1141.eb50e","order":34,"width":"5","height":"3","passthru":false,"label":"button 0","tooltip":"","color":"","bgcolor":"","icon":"","payload":"0","payloadType":"str","topic":"","x":350,"y":100,"wires":[["fc0d1bf7.8218c8"]]},{"id":"fb5bacea.fd46d","type":"ui_button","z":"850801e.b2b88","name":"","group":"ac0f1141.eb50e","order":35,"width":"5","height":"3","passthru":false,"label":"button 1","tooltip":"","color":"","bgcolor":"","icon":"","payload":"1","payloadType":"str","topic":"","x":350,"y":160,"wires":[["fc0d1bf7.8218c8"]]},{"id":"4d28de8d.ed2b5","type":"ui_button","z":"850801e.b2b88","name":"","group":"ac0f1141.eb50e","order":36,"width":"5","height":"3","passthru":false,"label":"button 2","tooltip":"","color":"","bgcolor":"","icon":"","payload":"2","payloadType":"str","topic":"","x":350,"y":220,"wires":[["fc0d1bf7.8218c8"]]},{"id":"f4e7da85.34ffb8","type":"ui_button","z":"850801e.b2b88","name":"","group":"ac0f1141.eb50e","order":37,"width":"5","height":"3","passthru":false,"label":"button 3","tooltip":"","color":"","bgcolor":"","icon":"","payload":"3","payloadType":"str","topic":"","x":350,"y":300,"wires":[["fc0d1bf7.8218c8"]]},{"id":"c0914ef2.fb9af","type":"ui_button","z":"850801e.b2b88","name":"","group":"ac0f1141.eb50e","order":38,"width":"5","height":"3","passthru":false,"label":"button 4","tooltip":"","color":"","bgcolor":"","icon":"","payload":"4","payloadType":"str","topic":"","x":350,"y":380,"wires":[["fc0d1bf7.8218c8"]]},{"id":"8ff51d77.7730f","type":"ui_button","z":"850801e.b2b88","name":"","group":"ac0f1141.eb50e","order":39,"width":"5","height":"3","passthru":false,"label":"button 5","tooltip":"","color":"","bgcolor":"","icon":"","payload":"5","payloadType":"str","topic":"","x":360,"y":440,"wires":[["fc0d1bf7.8218c8"]]},{"id":"fc0d1bf7.8218c8","type":"mqtt out","z":"850801e.b2b88","name":"WS2812","topic":"alex9ufo/inTopic/WS2812/RGB_event","qos":"1","retain":"","broker":"e4d9b72d.d14398","x":690,"y":280,"wires":[]},{"id":"8ad1ebfd.a92058","type":"mqtt in","z":"850801e.b2b88","name":"WS2812","topic":"alex9ufo/inTopic/WS2812/RGB_event","qos":"1","datatype":"auto","broker":"e4d9b72d.d14398","x":600,"y":460,"wires":[["5a446e3c.bf51c","fb4df492.c59eb8"]]},{"id":"5a446e3c.bf51c","type":"debug","z":"850801e.b2b88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":840,"y":460,"wires":[]},{"id":"fb4df492.c59eb8","type":"ui_text","z":"850801e.b2b88","group":"ac0f1141.eb50e","order":40,"width":0,"height":0,"name":"","label":"按鈕","format":"{{msg.payload}}","layout":"row-center","x":840,"y":540,"wires":[]},{"id":"ac0f1141.eb50e","type":"ui_group","name":"LED讀取","tab":"e03ab9d0.1a8f08","order":1,"disp":true,"width":"15","collapse":false},{"id":"e4d9b72d.d14398","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"e03ab9d0.1a8f08","type":"ui_tab","name":"LED","icon":"dashboard","order":1,"disabled":false,"hidden":false}]


沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...