Dimmable LED example

This example demonstrates how to dim a RGB LED with the ITT IoT framework. For this example you will need two controllers. One with the RGB LED shield and the other with the sensor shield that has the encoder attached. For the guide for attaching the encoder to the sensor shield check out the encoder example.

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. One for the controller with the RGB LED shield and the other for the sensor shield.

The following is the code for the controller with the RGB LED shield. Needed libaries:

lib_deps = ITTIoT, Adafruit NeoPixel
// Includes global variables and librarys that the RGB LED uses
#include <Arduino.h>
#include <ittiot.h>
#include <Adafruit_NeoPixel.h>
 
#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);
 
// Variable to store led brightness
int light;
 
//Increases or decreases light intensity variable. Keeps it between 0 and 250.
//If the encoder button is pressed, it turns the LED OFF.
void lightIntensity(String msg)
{
  if(msg=="1")
  {
    light+=1; // Increases the light intensity
  }
  else if (msg=="-1")
  {
    light-=1; // Decreases the light intensity
  }
  if(msg=="0")
  {
    if(light)
      light=0; // Switches the light off
  }
  if(light>250)
  {
    light=250; // Upper limit of the light intensity to 250
  }
  else if(light<0)
  {
    light=0; // Lower limit of the light intensity to 250
  }
}
 
// Message received from encoder
void iot_received(String topic, String msg)
{
  if(topic == ENC_TOPIC"/enc")
  {
    // Calculate brightness by using subfunction defined above
    lightIntensity(msg);
 
    // Set all led at same brightness
    pixels.setPixelColor(0, light, light, light);
    // This sends the updated pixel color to the hardware.
    pixels.show();
  }
 
}
 
// Function started after the connection to the server is established.
void iot_connected()
{
  // Send message to serial port to show that connection is established
  Serial.println("MQTT connected callback");
  // 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");
}
 
void setup()
{
  // Initialize serial port and send message
  Serial.begin(115200); // setting up serial connection parameter
  Serial.println("Booting");
 
  //iot.setConfig("wname", WIFI_NAME);
  //iot.setConfig("wpass", WIFI_PASSWORD);
  iot.printConfig(); // print IoT json config to serial
  iot.setup(); // Initialize IoT library
 
  pixels.begin();// Initialize RGB LED
 
  // Turn all LED-s off
  pixels.setPixelColor(0, 0, 0, 0);
  pixels.show();
}
 
void loop()
{
  iot.handle(); // IoT behind the plan work, it should be periodically called
  delay(10); // Wait for 0.01 second
}

The following is the code for the controller module with the sensor (encoder) shield.

lib_deps = ITTIoT, ClickEncoder
// Includes global variables and librarys that the encoder uses
#include <Arduino.h>
#include <ittiot.h>
#include <ClickEncoder.h>
 
#define WIFI_NAME "name"
#define WIFI_PASSWORD "password"
 
// Encoder conficuration
#define ENC_PINA 12
#define ENC_PINB 13
#define ENC_BTN   0
#define ENC_STEPS_PER_NOTCH 4
 
// Create an object for encoder
ClickEncoder encoder = ClickEncoder(ENC_PINA, ENC_PINB, ENC_BTN, ENC_STEPS_PER_NOTCH);
 
// Variable to store switch state
bool switchState;
 
// Function started after the connection to the server is established.
void iot_connected()
{
  // Send message to serial port to show that connection is established
  Serial.println("MQTT connected callback");
  // Send message to MQTT server to show that connection is established
  iot.log("IoT encoder example!");
}
 
void setup()
{
  // Initialize serial port and send message
  Serial.begin(115200); // setting up serial connection parameter
  Serial.println("Booting");
 
  //iot.setConfig("wname", WIFI_NAME);
  //iot.setConfig("wpass", WIFI_PASSWORD);
  iot.printConfig(); // print IoT json config to serial
  iot.setup(); // Initialize IoT library
}
 
void loop()
{
 
  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.
  // If the encoder button is pressed, it will send "0". Switches the LED off
  if(analogRead(A0) < 100)
  {
    if(switchState == false)
    {
      iot.publishMsg("enc", "0");
      switchState = true;
    }
  }
  else
  {
    if(switchState == true)
    {
      switchState = false;
    }
  }
 
  // Encoder behind the plan work, it should be periodically called
  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)
  {
    iot.publishMsg("enc", "1"); // CW rotation and increasing the LED intensity
  }
  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
}
en/iot/examples/dimlight.txt · Last modified: 2021/03/05 10: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