This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:iot:examples:rgb [2021/03/05 01:37] – heiko.pikner | en:iot:examples:rgb [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 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> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define MODULE_TOPIC " | ||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | //Stating, to which PIN the RGB LED has been connected | ||
| + | #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, | ||
| + | |||
| + | // Splitting string into smaller parts, so that the colour code can be set to the RGB LED | ||
| + | // https:// | ||
| + | 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< | ||
| + | { | ||
| + | if(data.charAt(i)==separator || i==maxIndex) | ||
| + | { | ||
| + | found++; | ||
| + | strIndex[0] = strIndex[1]+1; | ||
| + | strIndex[1] = (i == maxIndex) ? i+1 : i; | ||
| + | } | ||
| + | } | ||
| + | return found> | ||
| + | } | ||
| + | |||
| + | // Changes the RGB LED color and send this info to the computer over COM port | ||
| + | // mosquitto_pub -u test -P test -t " | ||
| + | void iot_received(String topic, String msg) | ||
| + | { | ||
| + | Serial.print(" | ||
| + | Serial.print(topic); | ||
| + | Serial.print(" | ||
| + | Serial.println(msg); | ||
| + | |||
| + | if(topic == MODULE_TOPIC) | ||
| + | { | ||
| + | // Splitting the RGB values into smaller parts | ||
| + | String r = getValue(msg,';', | ||
| + | String g = getValue(msg,';', | ||
| + | String b = getValue(msg,';', | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.println(r); | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.println(g); | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.println(b); | ||
| + | |||
| + | // Sending the color code to the RGB LED | ||
| + | pixels.setPixelColor(0, | ||
| + | pixels.show(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Subscrining to a MQTT topic, to get the RGB color code for the RGB LED | ||
| + | void iot_connected() | ||
| + | { | ||
| + | Serial.println(" | ||
| + | iot.subscribe(MODULE_TOPIC); | ||
| + | iot.log(" | ||
| + | } | ||
| + | |||
| + | // Setting up some parameters for the ESP microcontroller | ||
| + | void setup() | ||
| + | { | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | |||
| + | // | ||
| + | // | ||
| + | iot.printConfig(); | ||
| + | iot.setup(); | ||
| + | |||
| + | pixels.begin(); | ||
| + | } | ||
| + | |||
| + | //Main code, which runs in loop | ||
| + | void loop() | ||
| + | { | ||
| + | iot.handle(); | ||
| + | delay(200); // Delay of 200 ms | ||
| + | } | ||
| + | |||
| + | </ | ||