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/11 08:00] – rim.puks | en:iot:examples:dimlight [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 6: | Line 6: | ||
{{: | {{: | ||
- | 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 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(" | ||
- | iot.subscribe(" | + | |
+ | | ||
+ | // Send message to MQTT server to show that connection is established | ||
iot.log(" | iot.log(" | ||
} | } | ||
Line 87: | 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. | 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 142: | 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 | + | |
- | long newPosition = myEnc.read(); | + | |
- | 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 " | ||
+ | if(analogRead(A0) < 100) | ||
+ | { | ||
+ | | ||
{ | { | ||
- | iot.publishMsg(" | + | iot.publishMsg(" |
- | | + | |
} | } | ||
- | | + | } |
+ | | ||
+ | { | ||
+ | | ||
{ | { | ||
- | | + | |
- | 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, | ||
+ | |||
+ | // 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) | ||
{ | { | ||
- | | + | iot.publishMsg(" |
- | // 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); | + | |
- | | + | |
- | switchState = true; | + | |
- | } | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | if(switchState == true) | + | |
- | { | + | |
- | switchState = false; | + | |
- | } | + | |
- | } | + | |
} | } | ||
+ | else if (newPosition < oldPosition) | ||
+ | { | ||
+ | iot.publishMsg(" | ||
+ | } | ||
+ | oldPosition = newPosition; | ||
} | } | ||
+ | |||
</ | </ |