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:dimlight [2020/06/10 20:12] heikopikneren:iot:examples:dimlight [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 6: Line 6:
 {{:en:iot:examples:dimmablelight.jpg?300|}} {{:en:iot:examples:dimmablelight.jpg?300|}}
  
-Once the code has been uploaded the encoder controller will send messages about its increments and decrements via MQTT to the RGB module. RGB controller will then adjust it's brightness accordingly. Pressing the encoder button will turn the LED ON and OFF.+Once the code has been uploaded the encoder controller will send messages about its increments and decrements via MQTT to the RGB module. RGB controller will then adjust it's brightness accordingly. Pressing the encoder button will turn the LED OFF.
  
 To upload the code you need to create two projects in PlatformIO environment. To upload the code you need to create two projects in PlatformIO environment.
Line 12: Line 12:
  
 The following is the code for the controller with the RGB LED shield. Needed libaries: The following is the code for the controller with the RGB LED shield. Needed libaries:
-<code>lib_deps = ITTIoT@1.0.5, Adafruit NeoPixel</code>+<code>lib_deps = ITTIoT, Adafruit NeoPixel</code>
 <code c> <code c>
 +// Includes global variables and librarys that the RGB LED uses
 #include <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
 #include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
  
-// Change it according to the real name of the red IoT module where +#define WIFI_NAME "name" 
-// DHT shield is connected +#define WIFI_PASSWORD "password" 
-#define DHT_TOPIC "ESP53"+ 
 +// Change it according to the real name of the ESP microcontroler IoT module where the encoder is connected 
 +#define ENC_TOPIC "ESP30"
  
 // RGB LED pin conficuration // RGB LED pin conficuration
-#define PIN            D2+#define PIN D2
  
 // Create an object for RGB LED // Create an object for RGB LED
Line 29: Line 32:
  
 // Variable to store led brightness // Variable to store led brightness
-int light = 0;+int light;
  
 //Increases or decreases light intensity variable. Keeps it between 0 and 250. //Increases or decreases light intensity variable. Keeps it between 0 and 250.
Line 37: Line 40:
   if(msg=="1")   if(msg=="1")
   {   {
-    light+=1;+    light+=1; // Increases the light intensity
   }   }
   else if (msg=="-1")   else if (msg=="-1")
   {   {
-    light-=1;+    light-=1; // Decreases the light intensity
   }   }
   if(msg=="0")   if(msg=="0")
   {   {
     if(light)     if(light)
-      light=0;+      light=0; // Switches the light off
   }   }
   if(light>250)   if(light>250)
   {   {
-    light=250;+    light=250; // Upper limit of the light intensity to 250
   }   }
   else if(light<0)   else if(light<0)
   {   {
-    light=0;+    light=0; // Lower limit of the light intensity to 250
   }   }
 } }
  
-// Message received+// Message received from encoder
 void iot_received(String topic, String msg) void iot_received(String topic, String msg)
 { {
-  // Calculate brightness +  if(topic == ENC_TOPIC"/enc"
-  lightIntensity(msg);+  { 
 +    // Calculate brightness by using subfunction defined above 
 +    lightIntensity(msg);
  
-  // Set all led at same brightness +    // Set all led at same brightness 
-  pixels.setPixelColor(0, light, light, light); +    pixels.setPixelColor(0, light, light, light); 
-  // This sends the updated pixel color to the hardware. +    // This sends the updated pixel color to the hardware. 
-  pixels.show();+    pixels.show(); 
 +  }
  
 } }
Line 77: Line 83:
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
   // Subscribe to get enc message   // Subscribe to get enc message
-  iot.subscribe(DHT_TOPIC"/enc");+  iot.subscribe(ENC_TOPIC"/enc");
   // Send message to MQTT server to show that connection is established   // Send message to MQTT server to show that connection is established
   iot.log("IoT Light Dimmer example");   iot.log("IoT Light Dimmer example");
Line 85: Line 91:
 { {
   // Initialize serial port and send message   // Initialize serial port and send message
-  Serial.begin(115200);+  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
-  // print IoT json config to serial +  //iot.setConfig("wname", WIFI_NAME); 
-  iot.printConfig();+  //iot.setConfig("wpass", WIFI_PASSWORD); 
 +  iot.printConfig(); // print IoT json config to serial 
 +  iot.setup(); // Initialize IoT library
  
-  // Initialize IoT library +  pixels.begin();// Initialize RGB LED
-  iot.setup(); +
- +
-  // Initialize RGB LED +
-  pixels.begin();+
  
   // Turn all LED-s off   // Turn all LED-s off
Line 104: Line 108:
 void loop() void loop()
 { {
-  // IoT behind the plan work, it should be periodically called +  iot.handle(); // IoT behind the plan work, it should be periodically called 
-  iot.handle(); +  delay(10); // Wait for 0.01 second
-  delay(10);+
 } }
  
 </code> </code>
- 
 The following is the code for the controller module with the sensor (encoder) shield. The following is the code for the controller module with the sensor (encoder) shield.
-<code>lib_deps = ITTIoT@1.0.5, ClickEncoder</code>+<code>lib_deps = ITTIoT, ClickEncoder</code>
 <code c> <code c>
 +// Includes global variables and librarys that the encoder uses
 #include <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
 #include <ClickEncoder.h> #include <ClickEncoder.h>
 +
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "password"
  
 // Encoder conficuration // Encoder conficuration
Line 129: Line 135:
 // Variable to store switch state // Variable to store switch state
 bool switchState; bool switchState;
- 
-// Message received 
-void iot_received(String topic, String msg) 
-{ 
- 
-} 
  
 // Function started after the connection to the server is established. // Function started after the connection to the server is established.
Line 148: Line 148:
 { {
   // Initialize serial port and send message   // Initialize serial port and send message
-  Serial.begin(115200);+  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
-  // print IoT json config to serial +  //iot.setConfig("wname", WIFI_NAME); 
-  iot.printConfig(); +  //iot.setConfig("wpass", WIFI_PASSWORD); 
- +  iot.printConfig(); // print IoT json config to serial 
-  // Initialize IoT library +  iot.setup(); // Initialize IoT library
-  iot.setup();+
 } }
  
 void loop() void loop()
 { {
-  // IoT behind the plan work, it should be periodically called 
-  iot.handle(); 
  
-  delay(5);+  iot.handle(); // IoT behind the plan work, it should be periodically called 
 + 
 +  delay(5); // Wait 5 msec
  
   // Handle button. The coder button is connected to analog input.   // Handle button. The coder button is connected to analog input.
-  // If the encoder button is pressed, it will send "0".+  // If the encoder button is pressed, it will send "0"Switches the LED off
   if(analogRead(A0) < 100)   if(analogRead(A0) < 100)
   {   {
     if(switchState == false)     if(switchState == false)
     {     {
-      String msg = String(1); 
       iot.publishMsg("enc", "0");       iot.publishMsg("enc", "0");
       switchState = true;       switchState = true;
     }     }
-    else+  } 
 +  else 
 +  { 
 +    if(switchState == true)
     {     {
-      if(switchState == true) +      switchState = false;
-      { +
-        switchState = false; +
-      }+
     }     }
   }   }
Line 186: Line 184:
   // Encoder behind the plan work, it should be periodically called   // Encoder behind the plan work, it should be periodically called
   encoder.service();   encoder.service();
-  +
   static int16_t oldPosition, newPosition;   static int16_t oldPosition, newPosition;
  
Line 192: Line 190:
   newPosition += encoder.getValue();   newPosition += encoder.getValue();
  
-   // If the encoder is rotated clockwise, +   // If the encoder is rotated clockwise, it will send "1" to the "enc" topic. 
-   // it wil send "1" to the "enc" topic. If it is rotated counterclockwise, it will send "-1".+  // If it the encoder is rotated counterclockwise, it will send "-1".
   if(newPosition > oldPosition)   if(newPosition > oldPosition)
   {   {
-    iot.publishMsg("enc", "1");+    iot.publishMsg("enc", "1"); // CW rotation and increasing the LED intensity
   }   }
   else if (newPosition < oldPosition)   else if (newPosition < oldPosition)
   {   {
-    iot.publishMsg("enc", "-1");+    iot.publishMsg("enc", "-1"); // CCW rotation and decreasing the LED intensity
   }   }
-  oldPosition = newPosition;+  oldPosition = newPosition; // saving the new encoder position value for next cycle
 } }
 +
 </code> </code>
en/iot/examples/dimlight.1591819975.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