This is an old revision of the document!


Table of Contents

RGB

Theory

A light-emitting diode (LED) is a two-lead semiconductor light source. It is a p–n junction diode that emits light when activated. When a suitable voltage is applied to the leads, electrons are able to recombine with electron holes within the device, releasing energy in the form of photons. This effect is called electroluminescence, and the color of the light (corresponding to the energy of the photon) is determined by the energy band gap of the semiconductor. LEDs are typically small (less than 1 mm2) and integrated optical components may be used to shape the radiation pattern.

RGB LEDs consist of one red, one green, and one blue LED. By independently adjusting each of the three, RGB LEDs are capable of producing a wide color gamut. Unlike dedicated-color LEDs, however, these obviously do not produce pure wavelengths. Moreover, such modules as commercially available are often not optimized for smooth color mixing.

Practical

#include <Arduino.h>
#include <ittiot.h>
#include <Adafruit_NeoPixel.h>
 
#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);
 
  pixels.setPixelColor(0, r.toInt(), g.toInt(), b.toInt()); // Moderately bright green color.
 
  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 //Peale Serial.begin ja enne iot.setup
  iot.setup();
 
  pixels.begin(); // This initializes the NeoPixel library.
}
 
void loop()
{
  iot.handle();
  delay(200);
}
en/iot/examples/rgb.1506677129.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0