Appreciate the replies.
My esp32-c6 was misbehaving and while troubleshooting via Arduino I managed to get a working example including the RGB LED control I was after (thanks to hmueller01).
/* Evolved from https://github.com/hmueller01/pubsubclient3 (+ a few other examples).
Basic ESP32 MQTT client:
- Connects to configured MQTT server and publishes "hello world" to topic "outTopic" every two seconds
- Subscribes to "inTopic", printing out any messages received; If first char of "inTopic" message is 1, ESP LED is switched on, else it is switched off
- Reconnects if connection drops.
Uses onboard RGB LED to indicate status:
- white initial bootup (solid)
- magenta attempting to connect via wifi
- blue wifi connection established
- red unable to connect to MQTT server
- green connected to MQTT server
- yellow message received (brief flash, dim)
- white message sent (brief flash, dim)
- white user requested LED be turned on (extended BRIGHT flash)
*/
/******************** CONFIG **********************/
constexpr const char* WIFI_SSID = "mywifi";
constexpr const char* WIFI_PASSWORD = "wifipass";
constexpr const char* MQTT_SERVER = "broker.mqtt-dashboard.com";
constexpr const char* MQTT_USER = "muser";
constexpr const char* MQTT_PASSWORD = "mpass;
constexpr uint8_t LED_PIN = 8;
constexpr int MQTT_PORT = 1883;
/**************** CONSTANTS, ET AL ****************/
#ifdef ESP8266
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <esp_random.h>
#define RANDOM_REG32 esp_random()
#else
#error Platform not supported.
#endif
#include <PubSubClient.h>
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel rgbLed(1, LED_PIN, NEO_GRB + NEO_KHZ800);
struct RGB {
uint8_t r, g, b;
};
constexpr RGB COLOR_OFF = { 0, 0, 0};
constexpr RGB RGB_MAGENTA = {255, 0, 255};
constexpr RGB RGB_YELLOW = {255, 255, 0};
constexpr RGB RGB_RED = {255, 0, 0};
constexpr RGB RGB_GREEN = { 0, 255, 0};
constexpr RGB RGB_BLUE = { 0, 0, 255};
constexpr RGB RGB_WHITE10 = { 25, 25, 25};
constexpr RGB RGB_WHITE100 = {255, 255, 255};
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0; // MQTT message counter
/******************* FUNCTIONS ********************/
void LEDsetColor(const RGB& color, uint8_t brightness = 100) {
uint16_t scale = (uint16_t)brightness * 255 / 100;
uint8_t r = (uint8_t)(((uint16_t)color.r * scale) / 255);
uint8_t g = (uint8_t)(((uint16_t)color.g * scale) / 255);
uint8_t b = (uint8_t)(((uint16_t)color.b * scale) / 255);
rgbLed.setPixelColor(0, rgbLed.Color(r, g, b));
rgbLed.show();
}
void setup_wifi() {
LEDsetColor(RGB_MAGENTA, 15);
Serial.print("WiFi: Connecting to ");
Serial.println(WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
LEDsetColor(RGB_BLUE, 15);
randomSeed(RANDOM_REG32);
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, uint8_t* payload, size_t plength) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (size_t i = 0; i < plength; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if ((char)payload[0] == '1') { // Switch on LED if payload first character is '1'
Serial.println("Detected LED trigger");
LEDsetColor(RGB_WHITE100, 100);
delay(1500);
} else {
LEDsetColor(RGB_YELLOW, 1);
delay(1000);
LEDsetColor(COLOR_OFF);
}
}
void reconnect() {
while (!client.connected()) { // Loop until reconnected
Serial.println("Attempting MQTT connection...");
String clientId = "ESP32c6Client";
clientId += String(random(0xffff), HEX);
Serial.print(" Client ID: ");
Serial.println(clientId);
Serial.print(" MQTT username: ");
Serial.println(MQTT_USER);
if (client.connect("ESP32c6Client",MQTT_USER,MQTT_PASSWORD)) {
LEDsetColor(RGB_GREEN, 25);
Serial.println("Connected to MQTT broker");
client.publish("outTopic", "hello world");
client.subscribe("inTopic");
} else {
LEDsetColor(RGB_RED, 25);
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000); // Wait 5 seconds before retrying
LEDsetColor(COLOR_OFF);
}
}
}
/***************** ARDUINO SETUP ******************/
void setup() {
Serial.begin(115200);
Serial.println("---- SYSTEM START ----");
rgbLed.begin(); // Setup RGB LED
rgbLed.show();
LEDsetColor(RGB_WHITE10, 15);
setup_wifi();
client.setServer(MQTT_SERVER, 1883);
client.setCallback(callback);
}
/****************** ARDUINO LOOP ******************/
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
if (now - lastMsg > 2000) {
LEDsetColor(RGB_WHITE10, 5);
lastMsg = now;
++value;
snprintf(msg, MSG_BUFFER_SIZE, "hello world #%d", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
delay(150);
} else {
LEDsetColor(RGB_GREEN, 1);
delay(100);
}
delay(100);
}