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 [2020/07/20 11:26] – external edit 127.0.0.1en:iot:examples:rgb [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 7: Line 7:
  
 Needed libraries: Needed libraries:
-<code>lib_deps = ITTIoT@1.0.5, Adafruit NeoPixel</code>+<code>lib_deps = ITTIoT, Adafruit NeoPixel</code>
  
 The example code above lights the RGB LED The example code above lights the RGB LED
Line 15: 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 22: 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 38: 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 51: 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("ESP01/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 87: 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.1595244384.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