Advertisement
cymplecy

ESP32 Cheerlights and LED blink

Dec 25th, 2022
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4.  
  5. // Update these with values suitable for your network.
  6. const char* ssid = "";
  7. const char* password = "";
  8. const char* mqtt_server = "mqtt.cheerlights.com";
  9. #define mqtt_port 1883
  10. #define MQTT_USER ""
  11. #define MQTT_PASSWORD ""
  12.  
  13. #define MQTT_SERIAL_RECEIVER_CH "cheerlightsRGB"
  14.  
  15. WiFiClient wifiClient;
  16.  
  17. PubSubClient client(wifiClient);
  18.  
  19. void setup_wifi() {
  20.     delay(10);
  21.     // We start by connecting to a WiFi network
  22.     Serial.println();
  23.     Serial.print("Connecting to ");
  24.     Serial.println(ssid);
  25.     WiFi.begin(ssid, password);
  26.     while (WiFi.status() != WL_CONNECTED) {
  27.       delay(500);
  28.       Serial.print(".");
  29.     }
  30.     randomSeed(micros());
  31.     Serial.println("");
  32.     Serial.println("WiFi connected");
  33.     Serial.println("IP address: ");
  34.     Serial.println(WiFi.localIP());
  35. }
  36.  
  37. void reconnect() {
  38.   // Loop until we're reconnected
  39.   while (!client.connected()) {
  40.     Serial.print("Attempting MQTT connection...");
  41.     // Create a random client ID
  42.     String clientId = "ESP32Client-";
  43.     clientId += String(random(0xffff), HEX);
  44.     // Attempt to connect
  45.     if (client.connect(clientId.c_str(),MQTT_USER,MQTT_PASSWORD)) {
  46.       Serial.println("connected");
  47.       //Once connected,...
  48.       // ... and resubscribe
  49.       client.subscribe(MQTT_SERIAL_RECEIVER_CH);
  50.     } else {
  51.       Serial.print("failed, rc=");
  52.       Serial.print(client.state());
  53.       Serial.println(" try again in 5 seconds");
  54.       // Wait 5 seconds before retrying
  55.       delay(5000);
  56.     }
  57.   }
  58. }
  59.  
  60. void callback(char* topic, byte *payload, unsigned int length) {
  61.     Serial.println("-------new message from broker-----");
  62.     Serial.print("channel:");
  63.     Serial.println(topic);
  64.     Serial.print("data:");  
  65.     Serial.write(payload, length);
  66.     Serial.println();
  67. }
  68.  
  69. void setup() {
  70.   pinMode(10, OUTPUT);
  71.   Serial.begin(115200);
  72.   Serial.println("starting....");
  73.   Serial.setTimeout(500);// Set time out for
  74.   setup_wifi();
  75.   client.setServer(mqtt_server, mqtt_port);
  76.   client.setCallback(callback);
  77.   reconnect();
  78. }
  79.  
  80.  
  81. void loop() {
  82.   client.loop();
  83.   digitalWrite(10, HIGH);   // turn the LED on (HIGH is the voltage level)
  84.   delay(1000);                       // wait for a second
  85.   digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
  86.   delay(1000);                       // wait for a second
  87.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement