This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot:examples:dimlight [2018/05/08 11:28] – rim.puks | en:iot:examples:dimlight [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
This example demonstrates how to dim a RGB LED with the ITT IoT framework. | 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 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 [[en: |
{{: | {{: | ||
- | 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. | ||
One for the controller with the RGB LED shield and the other for the sensor shield. | 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: | ||
+ | < | ||
<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 " | + | |
- | If it receives " | + | |
- | Sending " | + | |
- | This program is meant to work with the Dimmable LED - Encoder shield program. | + | |
- | + | ||
- | Author:Rim Puks | + | |
- | May 2018 | + | |
- | */ | + | |
#include < | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
- | # | + | # |
+ | #define WIFI_PASSWORD " | ||
+ | // Change it according to the real name of the ESP microcontroler IoT module where the encoder is connected | ||
+ | #define ENC_TOPIC " | ||
+ | |||
+ | // RGB LED pin conficuration | ||
+ | #define PIN D2 | ||
+ | |||
+ | // Create an object for RGB LED | ||
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, | Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, | ||
- | 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==" | if(msg==" | ||
{ | { | ||
- | light+=5; | + | light+=1; // Increases the light intensity |
} | } | ||
else if (msg==" | else if (msg==" | ||
{ | { | ||
- | light-=5; | + | light-=1; // Decreases the light intensity |
} | } | ||
if(msg==" | if(msg==" | ||
{ | { | ||
if(light) | if(light) | ||
- | light=0; | + | light=0; |
- | else | + | |
- | | + | |
} | } | ||
if(light> | if(light> | ||
{ | { | ||
- | light=250; | + | light=250; |
} | } | ||
else if(light< | else if(light< | ||
{ | { | ||
- | light=0; | + | light=0; |
} | } | ||
} | } | ||
+ | // Message received from encoder | ||
void iot_received(String topic, String msg) | void iot_received(String topic, String msg) | ||
{ | { | ||
- | | + | |
- | | + | |
- | | + | // Calculate brightness by using subfunction defined above |
- | | + | |
- | lightIntensity(msg); | + | // Set all led at same brightness |
- | pixels.setPixelColor(0, | + | pixels.setPixelColor(0, |
- | | + | // 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(" | Serial.println(" | ||
- | | + | // Subscribe to get enc message |
- | iot.subscribe(" | + | iot.subscribe(ENC_TOPIC"/enc"); |
+ | // Send message to MQTT server to show that connection is established | ||
iot.log(" | iot.log(" | ||
} | } | ||
Line 86: | Line 90: | ||
void setup() | void setup() | ||
{ | { | ||
- | Serial.begin(115200); | + | |
+ | | ||
Serial.println(" | Serial.println(" | ||
- | iot.printConfig(); | + | |
- | iot.setup(); | + | // |
+ | | ||
+ | iot.setup(); | ||
+ | |||
+ | pixels.begin();// | ||
- | pixels.begin(); // This initializes the NeoPixel library. | + | |
+ | | ||
+ | pixels.show(); | ||
} | } | ||
void loop() | void loop() | ||
{ | { | ||
- | iot.handle(); | + | iot.handle(); |
- | delay(200); | + | delay(10); // Wait for 0.01 second |
- | | + | |
} | } | ||
</ | </ | ||
+ | The following is the code for the controller module with the sensor (encoder) shield. | ||
+ | < | ||
<code c> | <code c> | ||
- | /* | + | // Includes global variables and librarys that the encoder |
- | Dimmable LED - Encoder shield program | + | |
- | + | ||
- | This program reads the encoder | + | |
- | it wil send " | + | |
- | If the encoder button is pressed, it will send " | + | |
- | This program is meant to work with Dimmable LED - RGB shield example. | + | |
- | + | ||
- | | + | |
- | May 2018 | + | |
- | */ | + | |
#include < | #include < | ||
#include < | #include < | ||
- | #include <Ticker.h> | + | #include <ClickEncoder.h> |
- | #include <Encoder.h> | + | |
- | //Pin definition for the encoder | + | # |
- | #define ENC_PIN_1 12 | + | # |
- | # | + | |
- | # | + | |
- | Encoder | + | // Encoder |
- | Ticker encTicker; | + | #define ENC_PINA 12 |
- | Ticker swTicker; | + | #define ENC_PINB 13 |
+ | #define ENC_BTN | ||
+ | #define ENC_STEPS_PER_NOTCH 4 | ||
- | long oldPosition | + | // Create an object for encoder |
- | bool encFlag; | + | ClickEncoder encoder |
- | bool swFlag; | + | |
+ | // Variable to store switch state | ||
bool switchState; | bool switchState; | ||
Line 139: | Line 139: | ||
void iot_connected() | void iot_connected() | ||
{ | { | ||
+ | // Send message to serial port to show that connection is established | ||
Serial.println(" | Serial.println(" | ||
+ | // Send message to MQTT server to show that connection is established | ||
iot.log(" | iot.log(" | ||
- | } | ||
- | |||
- | // Ticker library callback | ||
- | void setEncFlag() | ||
- | { | ||
- | encFlag=true; | ||
- | } | ||
- | |||
- | // Ticker library callback | ||
- | void setSwFlag() | ||
- | { | ||
- | swFlag=true; | ||
} | } | ||
void setup() | void setup() | ||
{ | { | ||
- | Serial.begin(115200); | + | |
+ | | ||
Serial.println(" | Serial.println(" | ||
- | // Initialize Ticker callback and interval to 1 second | + | //iot.setConfig(" |
- | // encTicker.attach(1, setEncFlag); | + | //iot.setConfig(" |
- | // Initialize Ticker callback and interval to 0.2 second | + | |
- | | + | iot.setup(); // Initialize IoT library |
- | + | ||
- | | + | |
- | iot.printConfig(); | + | |
- | | + | |
- | iot.setup(); | + | |
} | } | ||
void loop() | void loop() | ||
{ | { | ||
- | // IoT behind the plan work, it should be periodically called | ||
- | iot.handle(); | ||
- | // Send encoder reading to the serial port, when it changes | + | |
- | | + | |
- | if (newPosition != oldPosition) | + | |
+ | |||
+ | // Handle button. The coder button is connected to analog input. | ||
+ | // If the encoder button is pressed, it will send " | ||
+ | if(analogRead(A0) < 100) | ||
{ | { | ||
- | + | | |
- | | + | |
{ | { | ||
- | iot.publishMsg(" | + | iot.publishMsg(" |
- | | + | |
} | } | ||
- | else if (newPosition< | ||
- | { | ||
- | iot.publishMsg(" | ||
- | delay(200); | ||
- | } | ||
- | oldPosition = newPosition; | ||
} | } | ||
- | + | else | |
- | // To not read ADC too fast. | + | |
- | if(swFlag) | + | |
{ | { | ||
- | | + | if(switchState |
- | // The coder button is connected to analog input. Analog input is pulled up normally. | + | |
- | | + | |
{ | { | ||
- | | + | switchState = false; |
- | { | + | |
- | String msg = String(1); | + | |
- | iot.publishMsg(" | + | |
- | switchState = true; | + | |
- | } | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | if(switchState == true) | + | |
- | { | + | |
- | | + | |
- | } | + | |
} | } | ||
} | } | ||
- | // Send encoder | + | // Encoder behind the plan work, it should be periodically called |
- | /* if(encFlag) | + | |
+ | |||
+ | static int16_t oldPosition, | ||
+ | |||
+ | // Read encoder value | ||
+ | newPosition += encoder.getValue(); | ||
+ | |||
+ | // If the encoder is rotated clockwise, it will send " | ||
+ | // If it the encoder is rotated counterclockwise, | ||
+ | if(newPosition > oldPosition) | ||
{ | { | ||
- | encFlag = false; | + | iot.publishMsg(" |
- | | + | } |
- | iot.publishMsg(" | + | else if (newPosition |
- | }*/ | + | { |
+ | | ||
+ | } | ||
+ | oldPosition = newPosition; | ||
} | } | ||
+ | |||
</ | </ |