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 ON and 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, Encoder
/*
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 <ittiot.h>
#include <Adafruit_NeoPixel.h>
 
#define PIN            D2
 
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
int light = 0;
 
//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.
void lightIntensity(String msg)
{
  if(msg=="1")
  {
    light+=5;
  }
  else if (msg=="-1")
  {
    light-=5;
  }
  if(msg=="0")
  {
    if(light)
      light=0;
    else
      light = 250;
  }
  if(light>250)
  {
    light=250;
  }
  else if(light<0)
  {
    light=0;
  }
}
 
void iot_received(String topic, String msg)
{
  Serial.print("MSG FROM USER callback, topic: ");
  Serial.print(topic);
  Serial.print(" payload: ");
  Serial.println(msg);
 
  lightIntensity(msg);
  pixels.setPixelColor(0, light, light, light);
  pixels.show(); // This sends the updated pixel color to the hardware.
 
}
 
void iot_connected()
{
  Serial.println("MQTT connected callback");
  iot.subscribe("enc");
  iot.log("IoT Light Dimmer example");
}
 
void setup()
{
  Serial.begin(115200);
  Serial.println("Booting");
 
  iot.printConfig();
  iot.setup();
 
  pixels.begin(); // This initializes the NeoPixel library.
}
 
void loop()
{
  iot.handle();
  delay(200);
  //Serial.println(light);
}

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

lib_deps = ITTIoT, Adafruit NeoPixel
/*
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 <ittiot.h>
#include <Ticker.h>
#include <Encoder.h>
 
//Pin definition for the encoder
#define ENC_PIN_1 12
#define ENC_PIN_2 13
#define SW_PIN    A0
 
Encoder myEnc(ENC_PIN_1, ENC_PIN_2);
Ticker encTicker;
Ticker swTicker;
 
long oldPosition  = -999;
bool encFlag;
bool swFlag;
bool switchState;
 
// Function started after the connection to the server is established.
void iot_connected()
{
  Serial.println("MQTT connected callback");
  iot.log("IoT encoder example!");
}
 
// Ticker library callback
void setEncFlag()
{
    encFlag=true;
}
 
// Ticker library callback
void setSwFlag()
{
    swFlag=true;
}
 
void setup()
{
  Serial.begin(115200);
  Serial.println("Booting");
 
  // Initialize Ticker callback and interval to 1 second
//  encTicker.attach(1, setEncFlag);
  // Initialize Ticker callback and interval to 0.2 second
  swTicker.attach(0.2, setSwFlag);
 
  // Print json config to serial
  iot.printConfig();
  // Initialize IoT library
  iot.setup();
 
}
 
void loop()
{
  // IoT behind the plan work, it should be periodically called
  iot.handle();
 
  // Send encoder reading to the serial port, when it changes
  long newPosition = myEnc.read();
  if (newPosition != oldPosition)
  {
 
    if(newPosition>oldPosition)
    {
      iot.publishMsgTo("ITT/ESP8266/TEST/enc", "1",true); // Topic of the other ESP "Maintopic" + "enc"
      delay(300);
    }
    else if (newPosition<oldPosition)
    {
      iot.publishMsgTo("ITT/ESP8266/TEST/enc", "-1",true);
      delay(300);
    }
    oldPosition = newPosition;
  }
 
  // To not read ADC too fast.
  if(swFlag)
  {
    swFlag=false;
    // 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.publishMsgTo("ITT/ESP8266/TEST/enc", "0",true);
        switchState = true;
        delay(500)
      }
    }
    else
    {
      if(switchState == true)
      {
        switchState = false;
      }
    }
  }
}
en/iot/examples/dimlight.txt · Last modified: 2018/05/11 14:50 by Somepub
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