===== ITT RGB LED module ===== This laboratory is located in the office of ITT Group in Tallinn, Estonia. ==== Introduction ==== This laboratory consists of an RGB LED module connected to the ITT IoT controller module. The module contains one RGB LED. This node can be used to create very basic MQTT systems but can also be used as a signalling part of other more complicated systems. {{:en:iot_homelab_kit:hardware:led2.png?200 |}} ==== Prerequisites ==== General knowledge of MQTT protocol: topics, brokers, subscribe, publish. ==== Technical details ==== This laboratory conisists of a ITT IoT controller module connected to a RGB LED module. === Actuators === This laboratory involves a WS2812B RGB LED. RGB LEDs are basically three LEDs (red, green, blue) that are built inside one shell. They include a WS2812 driver that makes it possible to use only one pin to control them. === Specifications === * LED size: 5050 * Colors: 16777216 === Electrical connection === Connected to port GPIO4. === Software, libraries and externals === To control WS2812B RGB LED the following libraries are used: * ITTIoT libary - used to program the controller module. * Adafruit NeoPixel libary - used to control the RGB LED. === Communication === The user can connect to this controller using the Distancelab environment. === Limits === At the same time, one user can program the controller. But all users connected to the Distancelab MQTT broker can control the RGB LED, assuming they use the topic described in the controller program. ==== Hands-on labs ==== === Example code === #include #include #include #define PIN D2 // When we setup the NeoPixel library, we tell it how many pixels, // and which pin to use to send signals. // Note that for older NeoPixel strips you might need to change // the third parameter--see the strandtest // example for more information on possible values. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800); // https://stackoverflow.com/questions/9072320/split-string-into-string-array String getValue(String data, char separator, int index) { int found = 0; int strIndex[] = {0, -1}; int maxIndex = data.length()-1; for(int i=0; i<=maxIndex && found<=index; i++) { if(data.charAt(i)==separator || i==maxIndex) { found++; strIndex[0] = strIndex[1]+1; strIndex[1] = (i == maxIndex) ? i+1 : i; } } return found>index ? data.substring(strIndex[0], strIndex[1]) : ""; } // Change RGB LED color // mosquitto_pub -u test -P test -t "ITT/IOT/3/rgb" -m "51;255;153" void iot_received(String topic, String msg) { Serial.print("MSG FROM USER callback, topic: "); Serial.print(topic); Serial.print(" payload: "); Serial.println(msg); String r = getValue(msg,';',0); String g = getValue(msg,';',1); String b = getValue(msg,';',2); Serial.print("R: "); Serial.println(r); Serial.print("G: "); Serial.println(g); Serial.print("B: "); Serial.println(b); // Moderately bright green color. pixels.setPixelColor(0, r.toInt(), g.toInt(), b.toInt()); pixels.show(); // This sends the updated pixel color to the hardware. } void iot_connected() { Serial.println("MQTT connected callback"); iot.subscribe("rgb"); iot.log("IoT NeoPixel example!"); } void setup() { Serial.begin(115200); Serial.println("Booting"); iot.printConfig(); // print json config to serial iot.setup(); pixels.begin(); // This initializes the NeoPixel library. } void loop() { iot.handle(); delay(200); } ==== Support ==== info@ittgroup.ee