This is an old revision of the document!


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.

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.

/*
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 Dimmer Light - Encoder 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("DHT/enc");
  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);
}
en/iot/examples/dimlight.1525778459.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