Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot:examples:rgb [2017/09/29 10:35] Somepuben:iot:examples:rgb [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
-======  RGB ======+======  RGB LED example======
  
-==== 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 ==== 
 LED RGB module must be connected to controller module or with sensor module. LED RGB module must be connected to controller module or with sensor module.
  
-{{:en:iot:examples:22117621_1694384017263043_731497034_o.jpg?300|}}  +{{:en:iot:examples:rgbpicture1.jpg?200|}} 
-{{:en:iot:examples:22140418_1694383960596382_968213581_o.jpg?300|}}+{{:en:iot:examples:rgbpicture2.jpg?200|}}
  
 +Needed libraries:
 +<code>lib_deps = ITTIoT, Adafruit NeoPixel</code>
  
 The example code above lights the RGB LED The example code above lights the RGB LED
Line 22: Line 15:
 #include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
  
 +#define MODULE_TOPIC "ESP30/rgb"
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "password"
 +
 +//Stating, to which PIN the RGB LED has been connected
 #define PIN            D2 #define PIN            D2
  
Line 29: Line 27:
 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
  
 +// Splitting string into smaller parts, so that the colour code can be set to the RGB LED
 // https://stackoverflow.com/questions/9072320/split-string-into-string-array // https://stackoverflow.com/questions/9072320/split-string-into-string-array
 String getValue(String data, char separator, int index) String getValue(String data, char separator, int index)
Line 45: Line 44:
     }     }
   }   }
- 
   return found>index ? data.substring(strIndex[0], strIndex[1]) : "";   return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
 } }
  
-// Change RGB LED color+// Changes the RGB LED color and send this info to the computer over COM port
 // mosquitto_pub -u test -P test -t "ITT/IOT/3/rgb" -m "51;255;153" // mosquitto_pub -u test -P test -t "ITT/IOT/3/rgb" -m "51;255;153"
 void iot_received(String topic, String msg) void iot_received(String topic, String msg)
Line 58: Line 56:
   Serial.println(msg);   Serial.println(msg);
  
-  String r = getValue(msg,';',0); +  if(topic == MODULE_TOPIC) 
-  String g = getValue(msg,';',1); +  { 
-  String b = getValue(msg,';',2);+    // Splitting the RGB values into smaller parts 
 +    String r = getValue(msg,';',0); 
 +    String g = getValue(msg,';',1); 
 +    String b = getValue(msg,';',2);
  
-  Serial.print("R: "); +    Serial.print("R: "); 
-  Serial.println(r);+    Serial.println(r);
  
-  Serial.print("G: "); +    Serial.print("G: "); 
-  Serial.println(g);+    Serial.println(g);
  
-  Serial.print("B: "); +    Serial.print("B: "); 
-  Serial.println(b);+    Serial.println(b);
  
-  pixels.setPixelColor(0, r.toInt(), g.toInt(), b.toInt()); // Moderately bright green color. +    // Sending the color code to the RGB LED 
- +    pixels.setPixelColor(0, r.toInt(), g.toInt(), b.toInt()); // Moderately bright green color. The 0 number addresses the first RGB LED (is the address number) 
-  pixels.show(); // This sends the updated pixel color to the hardware.+    pixels.show(); // This sends the updated pixel color to the hardware. 
 +  }
 } }
  
 +// Subscrining to a MQTT topic, to get the RGB color code for the RGB LED
 void iot_connected() void iot_connected()
 { {
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
-  iot.subscribe("rgb");+  iot.subscribe(MODULE_TOPIC);
   iot.log("IoT NeoPixel example!");   iot.log("IoT NeoPixel example!");
 } }
  
 +// Setting up some parameters for the ESP microcontroller
 void setup() void setup()
 { {
-  Serial.begin(115200);+  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
-  iot.printConfig(); // print json config to serial //Peale Serial.begin ja enne iot.setup+  //iot.setConfig("wname", WIFI_NAME); 
 +  //iot.setConfig("wpass", WIFI_PASSWORD); 
 +  iot.printConfig(); // print json config to serial
   iot.setup();   iot.setup();
  
Line 94: Line 100:
 } }
  
 +//Main code, which runs in loop
 void loop() void loop()
 { {
-  iot.handle(); +  iot.handle(); // Calls iot.handle out 
-  delay(200);+  delay(200); // Delay of 200 ms
 } }
 +
 </code> </code>
en/iot/examples/rgb.1506681312.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