This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:rgb [2017/09/22 11:40] – Somepub | en:iot:examples:rgb [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | == RGB Example 1 == | + | ====== |
| + | |||
| + | LED RGB module must be connected to controller module or with sensor module. | ||
| + | |||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | Needed libraries: | ||
| + | < | ||
| + | |||
| + | The example code above lights the RGB LED | ||
| <code c> | <code c> | ||
| #include < | #include < | ||
| Line 5: | Line 15: | ||
| #include < | #include < | ||
| + | #define MODULE_TOPIC " | ||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | //Stating, to which PIN the RGB LED has been connected | ||
| #define PIN D2 | #define PIN D2 | ||
| Line 12: | Line 27: | ||
| Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, | Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, | ||
| + | // Splitting string into smaller parts, so that the colour code can be set to the RGB LED | ||
| // https:// | // https:// | ||
| String getValue(String data, char separator, int index) | String getValue(String data, char separator, int index) | ||
| Line 28: | Line 44: | ||
| } | } | ||
| } | } | ||
| - | |||
| return found> | return found> | ||
| } | } | ||
| - | // Change | + | // Changes the RGB LED color and send this info to the computer over COM port |
| // mosquitto_pub -u test -P test -t " | // mosquitto_pub -u test -P test -t " | ||
| void iot_received(String topic, String msg) | void iot_received(String topic, String msg) | ||
| Line 41: | Line 56: | ||
| Serial.println(msg); | Serial.println(msg); | ||
| - | String r = getValue(msg,';', | + | |
| - | String g = getValue(msg,';', | + | { |
| - | String b = getValue(msg,';', | + | // Splitting the RGB values into smaller parts |
| + | | ||
| + | String g = getValue(msg,';', | ||
| + | String b = getValue(msg,';', | ||
| - | | + | |
| - | Serial.println(r); | + | Serial.println(r); |
| - | | + | |
| - | Serial.println(g); | + | Serial.println(g); |
| - | | + | |
| - | Serial.println(b); | + | Serial.println(b); |
| - | | + | // Sending the color code to the RGB LED |
| - | + | | |
| - | | + | pixels.show(); |
| + | } | ||
| } | } | ||
| + | // Subscrining to a MQTT topic, to get the RGB color code for the RGB LED | ||
| void iot_connected() | void iot_connected() | ||
| { | { | ||
| Serial.println(" | Serial.println(" | ||
| - | iot.subscribe(" | + | iot.subscribe(MODULE_TOPIC); |
| iot.log(" | iot.log(" | ||
| } | } | ||
| + | // Setting up some parameters for the ESP microcontroller | ||
| void setup() | void setup() | ||
| { | { | ||
| - | Serial.begin(115200); | + | Serial.begin(115200); |
| Serial.println(" | Serial.println(" | ||
| - | iot.printConfig(); | + | |
| + | // | ||
| + | | ||
| iot.setup(); | iot.setup(); | ||
| Line 77: | Line 100: | ||
| } | } | ||
| + | //Main code, which runs in loop | ||
| void loop() | void loop() | ||
| { | { | ||
| - | iot.handle(); | + | iot.handle(); |
| - | delay(200); | + | delay(200); |
| } | } | ||
| + | |||
| </ | </ | ||