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 [2018/05/11 08:00] rim.puksen: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, Encoder</code>+<code>lib_deps = ITTIoT, Adafruit NeoPixel</code>
 <code c> <code c>
-/+// Includes global variables and librarys that the RGB LED uses
-Dimmable LED - RGB shield program +
- +
-This program controls the RGB shield mounted on the controller module. +
-If it receives "1" via MQTT it increases the light intensity of the RGB LED+
-If it receives "-1" it will decrease it. +
-Sending "0" will turn the LED fully ON if it was OFF. And OFF if it was ON. +
-This program is meant to work with the Dimmable LED - Encoder shield program. +
- +
-Author:Rim Puks +
-May 2018 +
-*/ +
 #include <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
 #include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
  
-#define PIN            D2+#define WIFI_NAME "name" 
 +#define WIFI_PASSWORD "password"
  
 +// 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
 +#define PIN D2
 +
 +// Create an object for RGB LED
 Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
-int light = 0;+ 
 +// Variable to store led brightness 
 +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.
-//If the encoder button is pressed, it turns the LED ON or OFF.+//If the encoder button is pressed, it turns the LED OFF.
 void lightIntensity(String msg) void lightIntensity(String msg)
 { {
   if(msg=="1")   if(msg=="1")
   {   {
-    light+=5;+    light+=1// Increases the light intensity
   }   }
   else if (msg=="-1")   else if (msg=="-1")
   {   {
-    light-=5;+    light-=1// Decreases the light intensity
   }   }
   if(msg=="0")   if(msg=="0")
   {   {
     if(light)     if(light)
-      light=0; +      light=0; // Switches the light off
-    else +
-      light = 250;+
   }   }
   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 from encoder
 void iot_received(String topic, String msg) void iot_received(String topic, String msg)
 { {
-  Serial.print("MSG FROM USER callback, topic"); +  if(topic == ENC_TOPIC"/enc") 
-  Serial.print(topic); +  { 
-  Serial.print(" payload: "); +    // Calculate brightness by using subfunction defined above 
-  Serial.println(msg);+    lightIntensity(msg);
  
-  lightIntensity(msg); +    // Set all led at same brightness 
-  pixels.setPixelColor(0, light, light, light); +    pixels.setPixelColor(0, light, light, light); 
-  pixels.show(); // This sends the updated pixel color to the hardware.+    // This sends the updated pixel color to the hardware. 
 +    pixels.show(); 
 +  }
  
 } }
  
 +// Function started after the connection to the server is established.
 void iot_connected() void iot_connected()
 { {
 +  // Send message to serial port to show that connection is established
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
-  iot.subscribe("enc");+  // Subscribe to get enc message 
 +  iot.subscribe(ENC_TOPIC"/enc"); 
 +  // Send message to MQTT server to show that connection is established
   iot.log("IoT Light Dimmer example");   iot.log("IoT Light Dimmer example");
 } }
Line 87: Line 90:
 void setup() void setup()
 { {
-  Serial.begin(115200);+  // Initialize serial port and send message 
 +  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
-  iot.printConfig(); +  //iot.setConfig("wname", WIFI_NAME); 
-  iot.setup();+  //iot.setConfig("wpass", WIFI_PASSWORD); 
 +  iot.printConfig(); // print IoT json config to serial 
 +  iot.setup(); // Initialize IoT library 
 + 
 +  pixels.begin();// Initialize RGB LED
  
-  pixels.begin(); // This initializes the NeoPixel library.+  // Turn all LED-s off 
 +  pixels.setPixelColor(0, 0, 0, 0); 
 +  pixels.show();
 } }
  
 void loop() void loop()
 { {
-  iot.handle(); +  iot.handle(); // IoT behind the plan work, it should be periodically called 
-  delay(200); +  delay(10); // Wait for 0.01 second
-  //Serial.println(light);+
 } }
  
 </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, Adafruit NeoPixel</code>+<code>lib_deps = ITTIoT, ClickEncoder</code>
 <code c> <code c>
-/+// Includes global variables and librarys that the encoder uses
-Dimmable LED - Encoder shield program +
- +
-This program reads the encoder position value. If the encoder is rotated clockwise, +
- it wil send "1" to the "enc" topic. If it is rotated counterclockwise, it will send "-1"+
- If the encoder button is pressed, it will send "0"+
- This program is meant to work with Dimmable LED - RGB shield example. +
- +
- Author:Rim Puks +
- May 2018 +
- */ +
 #include <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
-#include <Ticker.h> +#include <ClickEncoder.h>
-#include <Encoder.h>+
  
-//Pin definition for the encoder +#define WIFI_NAME "name" 
-#define ENC_PIN_1 12 +#define WIFI_PASSWORD "password"
-#define ENC_PIN_2 13 +
-#define SW_PIN    A0+
  
-Encoder myEnc(ENC_PIN_1, ENC_PIN_2); +// Encoder conficuration 
-Ticker encTicker; +#define ENC_PINA 12 
-Ticker swTicker;+#define ENC_PINB 13 
 +#define ENC_BTN   0 
 +#define ENC_STEPS_PER_NOTCH 4
  
-long oldPosition  -999+// Create an object for encoder 
-bool encFlag; +ClickEncoder encoder ClickEncoder(ENC_PINA, ENC_PINB, ENC_BTN, ENC_STEPS_PER_NOTCH)
-bool swFlag;+ 
 +// Variable to store switch state
 bool switchState; bool switchState;
  
Line 142: Line 139:
 void iot_connected() void iot_connected()
 { {
 +  // Send message to serial port to show that connection is established
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
 +  // Send message to MQTT server to show that connection is established
   iot.log("IoT encoder example!");   iot.log("IoT encoder example!");
-} 
- 
-// Ticker library callback 
-void setEncFlag() 
-{ 
-    encFlag=true; 
-} 
- 
-// Ticker library callback 
-void setSwFlag() 
-{ 
-    swFlag=true; 
 } }
  
 void setup() void setup()
 { {
-  Serial.begin(115200);+  // Initialize serial port and send message 
 +  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
  
-  // Initialize Ticker callback and interval to 1 second +  //iot.setConfig("wname"WIFI_NAME); 
-//  encTicker.attach(1setEncFlag); +  //iot.setConfig("wpass", WIFI_PASSWORD); 
-  // Initialize Ticker callback and interval to 0.2 second +  iot.printConfig(); // print IoT json config to serial 
-  swTicker.attach(0.2, setSwFlag); +  iot.setup(); // Initialize IoT library
- +
-  // Print json config to serial +
-  iot.printConfig(); +
-  // Initialize IoT library +
-  iot.setup(); +
 } }
  
 void loop() void loop()
 { {
-  // IoT behind the plan work, it should be periodically called 
-  iot.handle(); 
  
-  // Send encoder reading to the serial portwhen it changes +  iot.handle(); // IoT behind the plan work, it should be periodically called
-  long newPosition = myEnc.read(); +
-  if (newPosition != oldPosition) +
-  {+
  
-    if(newPosition>oldPosition)+  delay(5); // Wait 5 msec 
 + 
 +  // Handle button. The coder button is connected to analog input. 
 +  // If the encoder button is pressed, it will send "0". Switches the LED off 
 +  if(analogRead(A0) < 100) 
 +  { 
 +    if(switchState == false)
     {     {
-      iot.publishMsg("enc", "1"); +      iot.publishMsg("enc", "0"); 
-      delay(200);+      switchState = true;
     }     }
-    else if (newPosition<oldPosition)+  } 
 +  else 
 +  { 
 +    if(switchState == true)
     {     {
-      iot.publishMsg("enc", "-1"); +      switchState = false;
-      delay(200);+
     }     }
-    oldPosition = newPosition; 
   }   }
  
-  // To not read ADC too fast+  // Encoder behind the plan work, it should be periodically called 
-  if(swFlag)+  encoder.service(); 
 + 
 +  static int16_t oldPosition, newPosition; 
 + 
 +  // Read encoder value 
 +  newPosition += encoder.getValue(); 
 + 
 +   // If the encoder is rotated clockwise, it will send "1" to the "enc" topic. 
 +  // If it the encoder is rotated counterclockwise, it will send "-1"
 +  if(newPosition > oldPosition)
   {   {
-    swFlag=false; +    iot.publishMsg("enc", "1"); // CW rotation and increasing the LED intensity
-    // The coder button is connected to analog input. Analog input is pulled up normally. +
-    if(analogRead(SW_PIN) == 0) +
-    { +
-      if(switchState == false) +
-      { +
-        String msg = String(1); +
-        iot.publishMsg("enc", "0"); +
-        switchState = true; +
-      } +
-    } +
-    else +
-    { +
-      if(switchState == true) +
-      { +
-        switchState = false; +
-      } +
-    }+
   }   }
 +  else if (newPosition < oldPosition)
 +  {
 +    iot.publishMsg("enc", "-1"); // CCW rotation and decreasing the LED intensity
 +  }
 +  oldPosition = newPosition; // saving the new encoder position value for next cycle
 } }
 +
 </code> </code>
en/iot/examples/dimlight.1526025611.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