Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <NeoPixelBus.h>
- #include <WiFi.h>
- #include <PubSubClient.h>
- void show_array(byte* Data);
- #define PixelPin 26 // make sure to set this to the correct pin, ignored for Esp8266
- #define colorSaturation 20
- #define LED_PIN 2 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
- const uint16_t PixelCount = 64; // this example assumes 4 pixels, making it smaller will cause a failure
- bool blinkState = false;
- char* ssid = "504";
- char* password = "20142020";
- const char* mqttServer = "m15.cloudmqtt.com";
- const int mqttPort = 15861;
- const char* mqttUser = "llaoendv";
- const char* mqttPassword = "jULqWpRt3vRx";
- NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
- WiFiClient espClient;
- PubSubClient client(espClient);
- void callback(char* topic, byte* payload, unsigned int length) {
- char message[256] = "";
- byte Data[256];
- for (int i = 0; i < 256; i++) {
- if (i < length) {
- Data[i] = payload[i];
- message[i] = (char)payload[i];
- } else {
- Data[i] = 0;
- }
- }
- show_array(Data);
- Serial.println(message);
- Serial.println("-----------------------");
- }
- void setWifiConnect() {
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.println("Connecting to WiFi..");
- delay(1000);
- }
- Serial.println("Connected to the WiFi network");
- }
- void setMqttConnect() {
- client.setServer(mqttServer, mqttPort);
- client.setCallback(callback);
- while (!client.connected()) {
- Serial.println("Connecting to MQTT...");
- if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
- Serial.println("connected");
- } else {
- Serial.print("failed with state ");
- Serial.print(client.state());
- delay(2000);
- }
- }
- client.subscribe("esp/test");
- client.publish("esp/test_hi", "Hello from ESP32");
- }
- void setup() {
- pinMode(LED_PIN, OUTPUT);
- Serial.begin(115200);
- while (!Serial); // wait for Leonardo enumeration, others continue immediately
- setWifiConnect();
- setMqttConnect();
- strip.Begin();
- for (int i = 0; i < PixelCount; i++) {
- strip.SetPixelColor(i, RgbColor(0, 0, 0));
- }
- strip.Show();
- }
- void loop() {
- client.loop();
- //blinkState = !blinkState;
- //digitalWrite(LED_PIN, blinkState);
- //delay(500);
- }
- void show_array(byte* Data) {
- RgbColor Colors[8][8];
- int k = 0;
- if (Data[0] == 0) {
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 8; j++) {
- Colors[i][j] = RgbColor(Data[k + 1], Data[k + 2], Data[k + 3]);
- strip.SetPixelColor(i * 8 + j, Colors[i][j]);
- k += 3;
- }
- }
- } else {}
- strip.Show();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement