### C++ Complete IoT Sensor Example Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt A comprehensive example showcasing a Kniwwelino IoT weather station. It reads an analog sensor, updates an LED matrix display, publishes sensor data via MQTT, controls an RGB LED based on readings, and handles button presses and incoming MQTT commands. Requires Kniwwelino.h and includes setup for MQTT group, message handling, and subscriptions. ```cpp #include void setup() { Kniwwelino.begin("WeatherStation", true, true, false); Kniwwelino.MQTTsetGroup("Classroom"); Kniwwelino.MQTTonMessage(messageReceived); Kniwwelino.MQTTconnectRGB(); Kniwwelino.MQTTsubscribe("commands/#"); pinMode(A0, INPUT); Kniwwelino.MATRIXdrawIcon(ICON_CHECK); Kniwwelino.RGBsetColor(RGB_COLOR_GREEN); } void loop() { // Read analog sensor (e.g., temperature, light) int sensorValue = analogRead(A0); float temperature = map(sensorValue, 0, 1023, 0, 50); // Update display if (Kniwwelino.MATRIXtextDone()) { Kniwwelino.MATRIXwriteOnce(String(temperature, 1) + "C"); } // Publish sensor data Kniwwelino.MQTTpublish(Kniwwelino.getName() + "/temperature", String(temperature)); // Color feedback based on temperature uint8_t hue = map(temperature, 15, 35, 160, 0); // Blue (cold) to Red (hot) Kniwwelino.RGBsetColor(Kniwwelino.RGBhue2int(hue)); // Handle buttons if (Kniwwelino.BUTTONAclicked()) { Kniwwelino.MQTTpublish("events", "Button A pressed on " + Kniwwelino.getName()); } Kniwwelino.sleep(5000); Kniwwelino.loop(); } void messageReceived(String &topic, String &payload) { Serial.println("Received: " + topic + " = " + payload); if (topic == "commands/display") { Kniwwelino.MATRIXwriteOnce(payload); } if (topic == "commands/icon") { Kniwwelino.MATRIXdrawIcon(payload); } } ``` -------------------------------- ### Initialize Kniwwelino Board and Connections Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt The `begin()` function initializes the Kniwwelino board's hardware, WiFi, and MQTT connections. It should be called once in the `setup()` function. Overloaded versions allow customization of WiFi, boot speed, and MQTT logging. ```cpp #include void setup() { // Basic initialization with WiFi enabled and normal boot Kniwwelino.begin(); // Custom initialization: WiFi=true, Fastboot=true Kniwwelino.begin(true, true); // Full initialization with sketch name, WiFi, Fastboot, and MQTT logging Kniwwelino.begin("MySketch", true, true, false); } void loop() { // Must be called in every loop iteration for background processing Kniwwelino.loop(); } ``` -------------------------------- ### Get Kniwwelino Device Information Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Retrieve essential device information such as the unique ID, name, IP address, and MAC address. The `isConnected()` function checks the network and MQTT connection status. ```cpp #include void setup() { Kniwwelino.begin(true, true); // Get unique 6-character device ID (e.g., "A1B2C3") String deviceId = Kniwwelino.getID(); Serial.println("Device ID: " + deviceId); // Get full device name (e.g., "Kniwwelino_A1B2C3") String deviceName = Kniwwelino.getName(); Serial.println("Device Name: " + deviceName); // Get current IP address (returns "0.0.0.0" if not connected) String ip = Kniwwelino.getIP(); Serial.println("IP Address: " + ip); // Get WiFi MAC address String mac = Kniwwelino.getMAC(); Serial.println("MAC Address: " + mac); // Check if connected to WiFi and MQTT if (Kniwwelino.isConnected()) { Serial.println("Connected to network!"); } } void loop() { Kniwwelino.loop(); } ``` -------------------------------- ### Get Synchronized Time with Kniwwelino Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Retrieves the current time, synchronized via NTP servers. The library automatically syncs with Luxembourg NTP servers and handles Central European Timezone conversion. Provides formatted time strings for display or logging. ```cpp #include void setup() { Kniwwelino.begin(true, true); // Wait for NTP sync delay(2000); // Get formatted time string (HH:MM:SS DD.MM.YYYY) String currentTime = Kniwwelino.getTime(); Serial.println("Current time: " + currentTime); Kniwwelino.MATRIXwriteAndWait(currentTime); } void loop() { // Display time every 10 seconds static unsigned long lastUpdate = 0; if (millis() - lastUpdate > 10000) { String time = Kniwwelino.getTime(); Serial.println(time); Kniwwelino.MATRIXwriteOnce(time); lastUpdate = millis(); } Kniwwelino.loop(); } ``` -------------------------------- ### Connect RGB and Matrix to MQTT with Kniwwelino Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt This C++ code demonstrates shortcut functions in the Kniwwelino library to automatically connect the RGB LED and Matrix display to MQTT topics. This allows for remote control of these peripherals by publishing messages to specific topics without needing to write custom message handling code. ```cpp #include void setup() { Kniwwelino.begin(true, true); Kniwwelino.MQTTsetGroup("MyProject"); // Connect RGB LED to MQTT (listens on group/RGB/COLOR) // Send "FF0000" to set red, "00FF00" for green, etc. Kniwwelino.MQTTconnectRGB(); // Connect Matrix to MQTT (listens on group/MATRIX/ICON and group/MATRIX/TEXT) // Send icon string to MATRIX/ICON or text to MATRIX/TEXT Kniwwelino.MQTTconnectMATRIX(); } void loop() { // Press button A to send icon to all boards in group if (Kniwwelino.BUTTONAclicked()) { Kniwwelino.MQTTpublish("MATRIX/ICON", "B0101000000100010111000000"); // Smiley } // Press button B to send text to all boards if (Kniwwelino.BUTTONBclicked()) { Kniwwelino.MQTTpublish("MATRIX/TEXT", "Hello!"); } Kniwwelino.loop(); } ``` -------------------------------- ### Implement MQTT Messaging with Kniwwelino Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt This C++ code illustrates how to use the Kniwwelino library for MQTT communication. It covers setting an MQTT group, subscribing to topics (including wildcards), publishing messages, and handling incoming messages via a callback function. This enables inter-device communication and integration with other IoT platforms. ```cpp #include void setup() { Kniwwelino.begin(true, true); // Set MQTT group (optional, creates isolated channel) Kniwwelino.MQTTsetGroup("MyClassroom"); // Set callback for incoming messages Kniwwelino.MQTTonMessage(messageReceived); // Subscribe to topics (group prefix added automatically) Kniwwelino.MQTTsubscribe("sensors/temperature"); Kniwwelino.MQTTsubscribe("commands/#"); // Wildcard subscription // Subscribe to public topic (visible to all boards) Kniwwelino.MQTTsubscribepublic("announcements"); // Publish a message Kniwwelino.MQTTpublish("sensors/temperature", "25.5"); Kniwwelino.MQTTpublish("status", "online"); // Publish with device name for identification Kniwwelino.MQTTpublish(Kniwwelino.getName() + "/status", "ready"); } void loop() { if (Kniwwelino.BUTTONAclicked()) { Kniwwelino.MQTTpublish("events/button", "A pressed"); } Kniwwelino.MQTTloop(); } // Callback function for received messages void messageReceived(String &topic, String &payload) { Serial.println("Received: " + topic + " = " + payload); if (topic == "sensors/temperature") { float temp = payload.toFloat(); Kniwwelino.MATRIXwriteOnce(String(temp) + "C"); } if (topic == "commands/led") { Kniwwelino.RGBsetColor(payload); } } ``` -------------------------------- ### Connect to Custom MQTT Broker with Kniwwelino Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Establishes a connection to a custom MQTT broker, allowing for custom subscriptions and message handling. Requires broker address, port, username, and password. Handles message reception and publishing. ```cpp #include void setup() { Kniwwelino.begin(true, true); // Connect to custom MQTT broker // Parameters: broker address, port, username, password boolean connected = Kniwwelino.MQTTUserSetup( "mqtt.example.com", // Broker address 1883, // Port "myuser", // Username "mypassword" // Password ); if (connected) { Serial.println("Connected to custom MQTT broker!"); Kniwwelino.MQTTsubscribe("home/sensors/#"); } Kniwwelino.MQTTonMessage(messageReceived); } void loop() { Kniwwelino.MQTTpublish("home/kniwwelino/status", "alive"); Kniwwelino.sleep(10000); Kniwwelino.loop(); } void messageReceived(String &topic, String &payload) { Serial.println(topic + ": " + payload); } ``` -------------------------------- ### C++ Logging with Serial and MQTT Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Demonstrates how to use the Kniwwelino library for debug logging. Messages are sent to the Serial monitor and can optionally be published over MQTT for remote monitoring. Ensure Kniwwelino.h is included and Kniwwelino.begin() is called with the MQTT logging parameter enabled. ```cpp #include void setup() { // Enable MQTT logging with the 4th parameter Kniwwelino.begin("LogDemo", true, true, true); // Log messages (goes to Serial and MQTT if enabled) Kniwwelino.log("Starting up"); Kniwwelino.logln(" - initialization complete"); Kniwwelino.logln("Device ID: " + Kniwwelino.getID()); } void loop() { if (Kniwwelino.BUTTONAclicked()) { Kniwwelino.logln("Button A pressed"); } Kniwwelino.loop(); } ``` -------------------------------- ### RGB Color Conversion Utilities (C++) Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Provides helper functions to convert between hue (0-255), hex strings, and 32-bit color integers. These are essential for creating dynamic color effects and animations on LED matrices. Requires the Kniwwelino library. ```cpp #include void setup() { Kniwwelino.begin(true, true); } void loop() { // Convert hue (0-255) to color integer (creates rainbow effect) for (uint8_t hue = 0; hue < 255; hue++) { unsigned long color = Kniwwelino.RGBhue2int(hue); Kniwwelino.RGBsetColor(color); delay(20); } // Convert hue to hex string String hexColor = Kniwwelino.RGBhue2Hex(128); // Returns something like "00FF80" Serial.println("Hue 128 = " + hexColor); // Convert hex string to integer unsigned long colorInt = Kniwwelino.RGBhex2int("FF5500"); // Convert RGB values to hex string String hex = Kniwwelino.RGBcolor2Hex(255, 128, 0); // Returns "FF8000" // Convert 32-bit color to hex string String hex2 = Kniwwelino.RGBcolor2Hex(0xFF8000); Kniwwelino.loop(); } ``` -------------------------------- ### LED Matrix Icon Display (C++) Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Allows drawing icons on the 5x5 LED matrix using binary strings, hexadecimal format, or predefined constants like `ICON_HEART`. Supports effects like blinking with adjustable speed and duration. Requires the Kniwwelino library. ```cpp #include void setup() { Kniwwelino.begin(true, true); // Draw icon using binary string (B + 25 bits, row by row) // Pattern: Smiley face // 01010 // 00000 // 10001 // 01110 // 00000 Kniwwelino.MATRIXdrawIcon("B0101000000100010111000000"); delay(2000); // Draw icon using hex format (0x + 10 hex digits, 2 per row) Kniwwelino.MATRIXdrawIcon("0x7008E828A0"); delay(2000); // Use predefined icons Kniwwelino.MATRIXdrawIcon(ICON_HEART); delay(2000); Kniwwelino.MATRIXdrawIcon(ICON_SMILE); delay(2000); Kniwwelino.MATRIXdrawIcon(ICON_SAD); delay(2000); Kniwwelino.MATRIXdrawIcon(ICON_CHECK); delay(2000); Kniwwelino.MATRIXdrawIcon(ICON_CROSS); delay(2000); Kniwwelino.MATRIXdrawIcon(ICON_ARROW_UP); delay(2000); Kniwwelino.MATRIXdrawIcon(ICON_ARROW_DOWN); delay(2000); // Draw icon with blink effect and duration // Format: "B<25bits>::" // effect: 0=static, 1=2Hz, 2=1Hz, 3=0.5Hz // duration: seconds * 10 (-1 = forever) Kniwwelino.MATRIXdrawIcon("B0101010101100010101000100:2:50"); // Heart blinking 1Hz for 5 seconds } void loop() { Kniwwelino.loop(); } ``` -------------------------------- ### Handle Onboard Button Presses with Kniwwelino Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt This C++ code demonstrates how to detect clicks and holds on the two onboard buttons (A and B) using the Kniwwelino library. It covers single clicks, simultaneous clicks, and continuous press detection, providing feedback via serial output and matrix icons. ```cpp #include void setup() { Kniwwelino.begin(true, true); Kniwwelino.MATRIXdrawIcon(ICON_HEART); } void loop() { // Detect button click (press and release) // Returns true once per click, resets after checking if (Kniwwelino.BUTTONAclicked()) { Serial.println("Button A was clicked!"); Kniwwelino.MATRIXdrawIcon(ICON_SMILE); } if (Kniwwelino.BUTTONBclicked()) { Serial.println("Button B was clicked!"); Kniwwelino.MATRIXdrawIcon(ICON_SAD); } // Detect both buttons clicked simultaneously if (Kniwwelino.BUTTONABclicked()) { Serial.println("Both buttons clicked!"); Kniwwelino.MATRIXdrawIcon(ICON_HEART); } // Detect button currently held down (continuous) if (Kniwwelino.BUTTONAdown()) { Kniwwelino.RGBsetColor(RGB_COLOR_RED); } else if (Kniwwelino.BUTTONBdown()) { Kniwwelino.RGBsetColor(RGB_COLOR_BLUE); } else { Kniwwelino.RGBclear(); } Kniwwelino.loop(); } ``` -------------------------------- ### LED Matrix Pixel Control (C++) Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Provides low-level control over individual pixels on the 5x5 LED matrix. Allows setting pixel states, clearing the matrix, adjusting brightness, blink rate, scroll speed, and rotation. Requires the Kniwwelino library. ```cpp #include void setup() { Kniwwelino.begin(true, true); // Clear the matrix Kniwwelino.MATRIXclear(); // Set individual pixels (x: 0-4, y: 0-4, on: true/false) Kniwwelino.MATRIXsetPixel(0, 0, true); // Top-left ON Kniwwelino.MATRIXsetPixel(4, 4, true); // Bottom-right ON Kniwwelino.MATRIXsetPixel(2, 2, true); // Center ON delay(2000); // Read pixel state boolean pixelState = Kniwwelino.MATRIXgetPixel(2, 2); Serial.println(pixelState ? "Center is ON" : "Center is OFF"); // Set brightness (0-15, default 10) Kniwwelino.MATRIXsetBrightness(15); // Maximum brightness // Set blink rate: MATRIX_STATIC, MATRIX_BLINK_2HZ, MATRIX_BLINK_1HZ, MATRIX_BLINK_HALFHZ Kniwwelino.MATRIXsetBlinkRate(MATRIX_BLINK_1HZ); delay(3000); Kniwwelino.MATRIXsetBlinkRate(MATRIX_STATIC); // Set scroll speed (1-10, higher = faster) Kniwwelino.MATRIXsetScrollSpeed(5); // Set rotation (0-3, each step = 90 degrees clockwise) Kniwwelino.MATRIXsetRotation(0); // Show device ID as binary pattern on matrix Kniwwelino.MATRIXshowID(); } void loop() { Kniwwelino.loop(); } ``` -------------------------------- ### LED Matrix Text Display (C++) Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Enables displaying scrolling text on a 5x5 LED matrix. Supports infinite scrolling, single scrolls, and blocking waits for text completion. Requires the Kniwwelino library. The `MATRIXwrite` function can take a count and a wait flag for custom behavior. ```cpp #include void setup() { Kniwwelino.begin(true, true); // Display text scrolling forever Kniwwelino.MATRIXwrite("Hello World!"); delay(10000); // Display text scrolling once (non-blocking) Kniwwelino.MATRIXwriteOnce("Welcome!"); delay(5000); // Display text and wait until finished (blocking) Kniwwelino.MATRIXwriteAndWait("Done!"); // Display text with custom count and wait option // count: number of scroll cycles (-1 = MATRIX_FOREVER) // wait: true = blocking, false = non-blocking Kniwwelino.MATRIXwrite("Repeat 3x", 3, false); } void loop() { // Check if text scrolling is complete if (Kniwwelino.MATRIXtextDone()) { Kniwwelino.MATRIXwriteOnce("New text!"); } Kniwwelino.loop(); } ``` -------------------------------- ### Use Sleep and Background Tasks with Kniwwelino Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Implements non-blocking delays using `Kniwwelino.sleep()` which maintains WiFi and MQTT connectivity. Allows registration of custom background tasks that execute periodically during sleep and loop cycles. Keep background tasks brief to avoid blocking. ```cpp #include void setup() { Kniwwelino.begin(true, true); // Register a background task that runs during sleep() and loop() Kniwwelino.setBGTask(myBackgroundTask); } void loop() { // Use Kniwwelino.sleep() instead of delay() // This maintains WiFi/MQTT and calls background tasks Kniwwelino.sleep(1000); // Sleep for 1 second Kniwwelino.MATRIXwriteOnce("Active"); Kniwwelino.sleep(2000); Kniwwelino.loop(); } // Custom background task called regularly void myBackgroundTask() { // Update NeoPixel strip, check sensors, etc. // Keep this function quick to avoid blocking static int counter = 0; counter++; } ``` -------------------------------- ### Generate Tones and Sounds with Kniwwelino Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Plays tones and musical notes through a piezo buzzer connected to a GPIO pin. Supports predefined note frequencies and durations for melodies, as well as continuous tones based on input. Requires a buzzer connected to an output pin. ```cpp #include void setup() { Kniwwelino.begin(true, true); pinMode(D6, OUTPUT); // Buzzer on D6 against GND // Play a melody using predefined notes // playNote(pin, frequency, note_duration) // note_duration: 4=quarter, 8=eighth, 16=sixteenth Kniwwelino.playNote(D6, NOTE_C4, 4); // Quarter note C4 Kniwwelino.playNote(D6, NOTE_D4, 4); Kniwwelino.playNote(D6, NOTE_E4, 4); Kniwwelino.playNote(D6, NOTE_F4, 4); Kniwwelino.playNote(D6, NOTE_G4, 2); // Half note G4 Kniwwelino.playNote(D6, NOTE_G4, 2); // Faster notes Kniwwelino.playNote(D6, NOTE_A4, 8); Kniwwelino.playNote(D6, NOTE_A4, 8); Kniwwelino.playNote(D6, NOTE_A4, 8); Kniwwelino.playNote(D6, NOTE_A4, 8); } void loop() { // Play continuous tone based on distance sensor or other input int distance = 50; // Example sensor value if (distance <= 10) { Kniwwelino.playTone(D6, NOTE_C4); } else if (distance <= 20) { Kniwwelino.playTone(D6, NOTE_E4); } else if (distance <= 30) { Kniwwelino.playTone(D6, NOTE_G4); } else { Kniwwelino.toneOff(D6); // Turn off sound } Kniwwelino.loop(); } ``` -------------------------------- ### Control Kniwwelino RGB LED Color and Effects Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Control the onboard NeoPixel RGB LED using various methods including hex strings, RGB values, and 32-bit integers. Supports effects like blink, flash, spark, and glow with customizable durations and brightness. ```cpp #include void setup() { Kniwwelino.begin(true, true); // Set color using hex string (RRGGBB format) Kniwwelino.RGBsetColor("FF0000"); // Red delay(1000); // Set color using RGB values (0-255) Kniwwelino.RGBsetColor(0, 255, 0); // Green delay(1000); // Set color using 32-bit integer Kniwwelino.RGBsetColor(0x0000FF); // Blue delay(1000); // Set color with effect: RGB_ON, RGB_BLINK, RGB_FLASH, RGB_SPARK, RGB_GLOW // count: number of effect cycles (-1 = forever) Kniwwelino.RGBsetColorEffect("FF00FF", RGB_BLINK, -1); // Blinking magenta forever delay(3000); Kniwwelino.RGBsetColorEffect(255, 165, 0, RGB_FLASH, 10); // Flashing orange 10 times delay(3000); // Use predefined colors Kniwwelino.RGBsetColor(RGB_COLOR_RED); Kniwwelino.RGBsetColor(RGB_COLOR_GREEN); Kniwwelino.RGBsetColor(RGB_COLOR_BLUE); Kniwwelino.RGBsetColor(RGB_COLOR_ORANGE); Kniwwelino.RGBsetColor(RGB_COLOR_CYAN); // Set brightness (1-255) Kniwwelino.RGBsetBrightness(100); // Get current color as 32-bit integer uint32_t currentColor = Kniwwelino.RGBgetColor(); // Turn off RGB LED Kniwwelino.RGBclear(); } void loop() { Kniwwelino.loop(); } ``` -------------------------------- ### File System Operations with Kniwwelino Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt Manages file operations on the device's internal flash memory (SPIFFS) for persistent storage. Supports writing, reading, and deleting files. Useful for storing configuration or data that needs to persist across reboots. ```cpp #include void setup() { Kniwwelino.begin(true, true); // Write data to file (overwrites existing content) Kniwwelino.FILEwrite("/config.txt", "brightness=100\ncolor=FF0000"); Kniwwelino.FILEwrite("/highscore.txt", "42"); // Read data from file String config = Kniwwelino.FILEread("/config.txt"); Serial.println("Config: " + config); String score = Kniwwelino.FILEread("/highscore.txt"); int highscore = score.toInt(); Serial.println("High score: " + String(highscore)); // Delete a file Kniwwelino.FILEdelete("/temp.txt"); } void loop() { // Example: Save game score when button pressed static int currentScore = 0; if (Kniwwelino.BUTTONAclicked()) { currentScore++; Kniwwelino.MATRIXwriteOnce(String(currentScore)); } if (Kniwwelino.BUTTONBclicked()) { // Save score to file Kniwwelino.FILEwrite("/score.txt", String(currentScore)); Kniwwelino.MATRIXwriteOnce("Saved!"); } Kniwwelino.loop(); } ``` -------------------------------- ### Control External Pins and Buttons with Kniwwelino Source: https://context7.com/kniwwelino/kniwwelinolib/llms.txt This C++ code snippet shows how to control external LEDs connected to GPIO pins (D5, D6) with various effects like blinking and flashing, and how to read external buttons connected to a pin (D7) using the Kniwwelino library. It configures pins as outputs or inputs and utilizes built-in pull-up resistors for buttons. ```cpp #include void setup() { Kniwwelino.begin(true, true); // Configure pins as outputs for LEDs pinMode(D5, OUTPUT); pinMode(D6, OUTPUT); // Set pin effects: PIN_ON, PIN_BLINK, PIN_FLASH, PIN_OFF Kniwwelino.PINsetEffect(D5, PIN_BLINK); // Blink LED on D5 Kniwwelino.PINsetEffect(D6, PIN_FLASH); // Flash LED on D6 // Enable external button on D7 (uses internal pullup) Kniwwelino.PINenableButton(D7); } void loop() { // Check if external button on D7 was clicked if (Kniwwelino.PINbuttonClicked(D7)) { Serial.println("External button clicked!"); Kniwwelino.MATRIXdrawIcon(ICON_CHECK); } // Check if external button is currently pressed if (Kniwwelino.PINbuttonDown(D7)) { Kniwwelino.RGBsetColor(RGB_COLOR_GREEN); } else { Kniwwelino.RGBclear(); } // Turn off pin effect and clear // Kniwwelino.PINclear(D5); Kniwwelino.loop(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.