This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot:examples:dimlight [2020/07/20 11:26] – external edit 127.0.0.1 | en:iot:examples:dimlight [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
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 | ||
#include < | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
- | // Change it according to the real name of the red IoT module where | + | #define WIFI_NAME " |
- | // DHT shield | + | #define WIFI_PASSWORD " |
- | # | + | |
+ | // Change it according to the real name of the ESP microcontroler | ||
+ | # | ||
// RGB LED pin conficuration | // RGB LED pin conficuration | ||
- | #define PIN D2 | + | #define PIN D2 |
// Create an object for RGB LED | // Create an object for RGB LED | ||
Line 29: | Line 32: | ||
// Variable to store led brightness | // Variable to store led brightness | ||
- | int light = 0; | + | 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. | ||
Line 37: | Line 40: | ||
if(msg==" | if(msg==" | ||
{ | { | ||
- | light+=1; | + | light+=1; |
} | } | ||
else if (msg==" | else if (msg==" | ||
{ | { | ||
- | light-=1; | + | light-=1; |
} | } | ||
if(msg==" | if(msg==" | ||
{ | { | ||
if(light) | if(light) | ||
- | light=0; | + | light=0; |
} | } | ||
if(light> | if(light> | ||
{ | { | ||
- | light=250; | + | light=250; |
} | } | ||
else if(light< | else if(light< | ||
{ | { | ||
- | light=0; | + | light=0; |
} | } | ||
} | } | ||
- | // Message received | + | // Message received |
void iot_received(String topic, String msg) | void iot_received(String topic, String msg) | ||
{ | { | ||
- | // Calculate brightness | + | |
- | lightIntensity(msg); | + | { |
+ | | ||
+ | lightIntensity(msg); | ||
- | | + | |
- | pixels.setPixelColor(0, | + | pixels.setPixelColor(0, |
- | // This sends the updated pixel color to the hardware. | + | // This sends the updated pixel color to the hardware. |
- | pixels.show(); | + | pixels.show(); |
+ | } | ||
} | } | ||
Line 77: | Line 83: | ||
Serial.println(" | Serial.println(" | ||
// Subscribe to get enc message | // Subscribe to get enc message | ||
- | iot.subscribe(DHT_TOPIC"/ | + | iot.subscribe(ENC_TOPIC"/ |
// Send message to MQTT server to show that connection is established | // Send message to MQTT server to show that connection is established | ||
iot.log(" | iot.log(" | ||
Line 85: | Line 91: | ||
{ | { | ||
// Initialize serial port and send message | // Initialize serial port and send message | ||
- | Serial.begin(115200); | + | Serial.begin(115200); |
Serial.println(" | Serial.println(" | ||
- | // print IoT json config to serial | + | |
- | iot.printConfig(); | + | // |
+ | iot.printConfig(); | ||
+ | iot.setup(); // Initialize IoT library | ||
- | | + | |
- | iot.setup(); | + | |
- | + | ||
- | | + | |
- | pixels.begin(); | + | |
// Turn all LED-s off | // Turn all LED-s off | ||
Line 104: | Line 108: | ||
void loop() | void loop() | ||
{ | { | ||
- | // IoT behind the plan work, it should be periodically called | + | |
- | iot.handle(); | + | delay(10); |
- | delay(10); | + | |
} | } | ||
</ | </ | ||
- | |||
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 uses | ||
#include < | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
+ | |||
+ | #define WIFI_NAME " | ||
+ | #define WIFI_PASSWORD " | ||
// Encoder conficuration | // Encoder conficuration | ||
Line 129: | Line 135: | ||
// Variable to store switch state | // Variable to store switch state | ||
bool switchState; | bool switchState; | ||
- | |||
- | // Message received | ||
- | void iot_received(String topic, String msg) | ||
- | { | ||
- | |||
- | } | ||
// Function started after the connection to the server is established. | // Function started after the connection to the server is established. | ||
Line 148: | Line 148: | ||
{ | { | ||
// Initialize serial port and send message | // Initialize serial port and send message | ||
- | Serial.begin(115200); | + | Serial.begin(115200); |
Serial.println(" | Serial.println(" | ||
- | // print IoT json config to serial | + | //iot.setConfig(" |
- | iot.printConfig(); | + | |
- | + | | |
- | // Initialize | + | iot.setup(); |
- | iot.setup(); | + | |
} | } | ||
void loop() | void loop() | ||
{ | { | ||
- | // IoT behind the plan work, it should be periodically called | ||
- | iot.handle(); | ||
- | delay(5); | + | |
+ | |||
+ | | ||
// Handle button. The coder button is connected to analog input. | // Handle button. The coder button is connected to analog input. | ||
- | // If the encoder button is pressed, it will send " | + | // If the encoder button is pressed, it will send " |
if(analogRead(A0) < 100) | if(analogRead(A0) < 100) | ||
{ | { | ||
if(switchState == false) | if(switchState == false) | ||
{ | { | ||
- | String msg = String(1); | ||
iot.publishMsg(" | iot.publishMsg(" | ||
switchState = true; | switchState = true; | ||
} | } | ||
- | | + | } |
+ | | ||
+ | { | ||
+ | if(switchState == true) | ||
{ | { | ||
- | | + | switchState = false; |
- | { | + | |
- | | + | |
- | } | + | |
} | } | ||
} | } | ||
Line 186: | Line 184: | ||
// Encoder behind the plan work, it should be periodically called | // Encoder behind the plan work, it should be periodically called | ||
encoder.service(); | encoder.service(); | ||
- | | + | |
static int16_t oldPosition, | static int16_t oldPosition, | ||
Line 192: | Line 190: | ||
newPosition += encoder.getValue(); | newPosition += encoder.getValue(); | ||
- | // If the encoder is rotated clockwise, | + | // If the encoder is rotated clockwise, it will send " |
- | // | + | // If it the encoder |
if(newPosition > oldPosition) | if(newPosition > oldPosition) | ||
{ | { | ||
- | iot.publishMsg(" | + | iot.publishMsg(" |
} | } | ||
else if (newPosition < oldPosition) | else if (newPosition < oldPosition) | ||
{ | { | ||
- | iot.publishMsg(" | + | iot.publishMsg(" |
} | } | ||
- | oldPosition = newPosition; | + | oldPosition = newPosition; |
} | } | ||
+ | |||
</ | </ |