Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <NeoPixelBus.h>
  2. #include <WiFi.h>
  3. #include <PubSubClient.h>
  4.  
  5. void show_array(byte* Data);
  6.  
  7. #define PixelPin  26  // make sure to set this to the correct pin, ignored for Esp8266
  8. #define colorSaturation 20
  9. #define LED_PIN 2 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
  10.  
  11. const uint16_t PixelCount = 64; // this example assumes 4 pixels, making it smaller will cause a failure
  12. bool blinkState = false;
  13. char* ssid = "504";
  14. char* password =  "20142020";
  15. const char* mqttServer = "m15.cloudmqtt.com";
  16. const int mqttPort = 15861;
  17. const char* mqttUser = "llaoendv";
  18. const char* mqttPassword = "jULqWpRt3vRx";
  19.  
  20. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  21. WiFiClient espClient;
  22. PubSubClient client(espClient);
  23.  
  24. void callback(char* topic, byte* payload, unsigned int length) {
  25.   char message[256] = "";
  26.   byte Data[256];
  27.  
  28.   for (int i = 0; i < 256; i++) {
  29.     if (i < length) {
  30.       Data[i] = payload[i];
  31.       message[i] = (char)payload[i];
  32.     } else {
  33.       Data[i] = 0;
  34.     }
  35.   }
  36.   show_array(Data);
  37.   Serial.println(message);
  38.   Serial.println("-----------------------");
  39. }
  40.  
  41.  
  42. void setWifiConnect() {
  43.   WiFi.begin(ssid, password);
  44.   while (WiFi.status() != WL_CONNECTED) {
  45.     Serial.println("Connecting to WiFi..");
  46.     delay(1000);
  47.   }
  48.   Serial.println("Connected to the WiFi network");
  49. }
  50.  
  51.  
  52. void setMqttConnect() {
  53.   client.setServer(mqttServer, mqttPort);
  54.   client.setCallback(callback);
  55.  
  56.   while (!client.connected()) {
  57.     Serial.println("Connecting to MQTT...");
  58.     if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
  59.       Serial.println("connected");
  60.     } else {
  61.       Serial.print("failed with state ");
  62.       Serial.print(client.state());
  63.       delay(2000);
  64.     }
  65.   }
  66.   client.subscribe("esp/test");
  67.   client.publish("esp/test_hi", "Hello from ESP32");
  68. }
  69.  
  70.  
  71.  
  72. void setup() {
  73.   pinMode(LED_PIN, OUTPUT);
  74.   Serial.begin(115200);
  75.   while (!Serial); // wait for Leonardo enumeration, others continue immediately
  76.  
  77.   setWifiConnect();
  78.   setMqttConnect();
  79.  
  80.   strip.Begin();
  81.   for (int i = 0; i < PixelCount; i++) {
  82.     strip.SetPixelColor(i, RgbColor(0, 0, 0));
  83.   }
  84.   strip.Show();
  85. }
  86.  
  87. void loop() {
  88.   client.loop();
  89.   //blinkState = !blinkState;
  90.   //digitalWrite(LED_PIN, blinkState);
  91.   //delay(500);
  92. }
  93.  
  94. void show_array(byte* Data) {
  95.   RgbColor Colors[8][8];
  96.   int k = 0;
  97.  
  98.   if (Data[0] == 0) {
  99.     for (int i = 0; i < 8; i++) {
  100.       for (int j = 0; j < 8; j++) {
  101.         Colors[i][j] = RgbColor(Data[k + 1], Data[k + 2], Data[k + 3]);
  102.         strip.SetPixelColor(i * 8 + j, Colors[i][j]);
  103.         k += 3;
  104.       }
  105.     }
  106.   } else {}
  107.   strip.Show();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement