Advertisement
Oleguer

Reto del color

Jan 15th, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <Adafruit_NeoPixel.h>
  4.  
  5. const char* ssid = "raspiNET";
  6. const char* password = "raspiPASS";
  7. const char* mqtt_server = "192.168.8.1";
  8.  
  9. const char* topicRed = "ledsfantasma/red";
  10. const char* topicGreen = "ledsfantasma/green";
  11. const char* topicBlue = "ledsfantasma/blue";
  12. const char* topicBrightness = "ledsfantasma/bright";
  13.  
  14. #define LED_PIN 12
  15. #define NUM_LEDS 5
  16. Adafruit_NeoPixel pixels(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
  17.  
  18. WiFiClient wifiClient;
  19. PubSubClient mqttClient(wifiClient);
  20. int red = 0, green = 0, blue= 0, bright = 255;
  21.  
  22. void setup_wifi() {
  23.   delay(10);
  24.   Serial.println();
  25.   Serial.print("Conectando a ");
  26.   Serial.println(ssid);
  27.  
  28.   WiFi.mode(WIFI_STA);
  29.   WiFi.begin(ssid, password);
  30.  
  31.   while (WiFi.status() != WL_CONNECTED) {
  32.     delay(500);
  33.     Serial.print(".");
  34.   }
  35.  
  36.   Serial.println("\nWiFi conectado!");
  37.   Serial.println("Direccion IP: ");
  38.   Serial.println(WiFi.localIP());
  39. }
  40.  
  41. void reconnect() {
  42.   while (!mqttClient.connected()) {
  43.     Serial.print("Intentando conexion MQTT...");
  44.     String clientId = "ESPClient-";
  45.     clientId += String(random(0xffff), HEX);
  46.  
  47.     if (mqttClient.connect(clientId.c_str())) {
  48.       Serial.println("Conectado");
  49.  
  50.       mqttClient.subscribe(topicRed);
  51.       mqttClient.subscribe(topicGreen);
  52.       mqttClient.subscribe(topicBlue);
  53.       mqttClient.subscribe(topicBrightness);
  54.     } else {
  55.       Serial.print("Error, rc=");
  56.       Serial.print(mqttClient.state());
  57.       Serial.println("; probaremos de nuevo en 5 segundos");
  58.       delay(5000);
  59.     }
  60.   }
  61. }
  62.  
  63. void callback(char* topic, byte* payload, unsigned int length) {
  64.   Serial.print("Mensaje recibido [");
  65.   Serial.print(topic);
  66.   Serial.print("] ");
  67.  
  68.   String payloadSTR;
  69.  
  70.   for (int i = 0; i < length; i++) {
  71.     Serial.print((char)payload[i]);
  72.     payloadSTR.concat(String((char)payload[i]));
  73.   }
  74.   Serial.println();
  75.  
  76.   int value = payloadSTR.toInt();
  77.  
  78.   String topicSTR = String(topic);
  79.   Serial.println(topicSTR);
  80.   if (topicSTR.endsWith("red")) red = value;
  81.   else if (topicSTR.endsWith("green")) green = value;
  82.   else if (topicSTR.endsWith("blue")) blue = value;
  83.   else if (topicSTR.endsWith("bright")) bright = value;
  84.  
  85.   pixels.fill(pixels.Color(red, green, blue), 0);
  86.   pixels.setBrightness(bright);
  87.   pixels.show();
  88. }
  89.  
  90. void setup() {
  91.   Serial.begin(115200);
  92.   setup_wifi();
  93.  
  94.   mqttClient.setServer(mqtt_server, 1883);
  95.   mqttClient.setCallback(callback);
  96.  
  97.   pixels.begin();
  98.   pixels.show();
  99. }
  100.  
  101. void loop() {
  102.   if (!mqttClient.connected()) {
  103.     reconnect();
  104.   }
  105.   mqttClient.loop();
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement