====== Reguleeritava LED-i näidis ======
Antud näidiskood demonstreerib kuidas RGB LED-i valgustugevust muuta läbi ITT IoT raamistiku.
Antud näidise jaoks on vaja kahte kontrollerit. Ühte RGB LED-i jaoks, teist anduri ja enkooderi jaoks. Anduri ja enkooderi omavahel ühendamise juhendi leiab [[et:iot:examples:encoder|enkoodri näidis]].
{{:en:iot:examples:dimmablelight.jpg?300|}}
Kui kood on üles laetud, saadab enkooder RBG moodulisse läbi mqtt sõnumeid enda asendi ja seeläbi väärtuste kohta. RGB kontroller reguleerib valguse heledust vastavalt sellele infole. Enkoodri nupu vajutamine lülitab LED-i välja.
Koodi üles laadimiseks on vaja luua PlatformIO keskkonnas kaks projekti. Üks neist on RGB LED kontrolleri jaoks ning teine anduri ja enkooderi jaoks.
Järgnev kood on mõeldud RGB LED jaoks.
Vajaminevad teegid:
lib_deps = ITTIoT, Adafruit NeoPixel
// Includes global variables and librarys that the RGB LED uses
#include
#include
#include
#define WIFI_NAME "name"
#define WIFI_PASSWORD "password"
// Change it according to the real name of the ESP microcontroler IoT module where the encoder is connected
#define ENC_TOPIC "ESP30"
// RGB LED pin conficuration
#define PIN D2
// Create an object for RGB LED
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
// Variable to store led brightness
int light;
//Increases or decreases light intensity variable. Keeps it between 0 and 250.
//If the encoder button is pressed, it turns the LED OFF.
void lightIntensity(String msg)
{
if(msg=="1")
{
light+=1; // Increases the light intensity
}
else if (msg=="-1")
{
light-=1; // Decreases the light intensity
}
if(msg=="0")
{
if(light)
light=0; // Switches the light off
}
if(light>250)
{
light=250; // Upper limit of the light intensity to 250
}
else if(light<0)
{
light=0; // Lower limit of the light intensity to 250
}
}
// Message received from encoder
void iot_received(String topic, String msg)
{
if(topic == ENC_TOPIC"/enc")
{
// Calculate brightness by using subfunction defined above
lightIntensity(msg);
// Set all led at same brightness
pixels.setPixelColor(0, light, light, light);
// This sends the updated pixel color to the hardware.
pixels.show();
}
}
// Function started after the connection to the server is established.
void iot_connected()
{
// Send message to serial port to show that connection is established
Serial.println("MQTT connected callback");
// Subscribe to get enc message
iot.subscribe(ENC_TOPIC"/enc");
// Send message to MQTT server to show that connection is established
iot.log("IoT Light Dimmer example");
}
void setup()
{
// Initialize serial port and send message
Serial.begin(115200); // setting up serial connection parameter
Serial.println("Booting");
//iot.setConfig("wname", WIFI_NAME);
//iot.setConfig("wpass", WIFI_PASSWORD);
iot.printConfig(); // print IoT json config to serial
iot.setup(); // Initialize IoT library
pixels.begin();// Initialize RGB LED
// Turn all LED-s off
pixels.setPixelColor(0, 0, 0, 0);
pixels.show();
}
void loop()
{
iot.handle(); // IoT behind the plan work, it should be periodically called
delay(10); // Wait for 0.01 second
}
Järgnev kood on anduri ja enkooderi jaoks.
Vajaminevad teegid:
lib_deps = ITTIoT, ClickEncoder
// Includes global variables and librarys that the encoder uses
#include
#include
#include
#define WIFI_NAME "name"
#define WIFI_PASSWORD "password"
// Encoder conficuration
#define ENC_PINA 12
#define ENC_PINB 13
#define ENC_BTN 0
#define ENC_STEPS_PER_NOTCH 4
// Create an object for encoder
ClickEncoder encoder = ClickEncoder(ENC_PINA, ENC_PINB, ENC_BTN, ENC_STEPS_PER_NOTCH);
// Variable to store switch state
bool switchState;
// Function started after the connection to the server is established.
void iot_connected()
{
// Send message to serial port to show that connection is established
Serial.println("MQTT connected callback");
// Send message to MQTT server to show that connection is established
iot.log("IoT encoder example!");
}
void setup()
{
// Initialize serial port and send message
Serial.begin(115200); // setting up serial connection parameter
Serial.println("Booting");
//iot.setConfig("wname", WIFI_NAME);
//iot.setConfig("wpass", WIFI_PASSWORD);
iot.printConfig(); // print IoT json config to serial
iot.setup(); // Initialize IoT library
}
void loop()
{
iot.handle(); // IoT behind the plan work, it should be periodically called
delay(5); // Wait 5 msec
// Handle button. The coder button is connected to analog input.
// If the encoder button is pressed, it will send "0". Switches the LED off
if(analogRead(A0) < 100)
{
if(switchState == false)
{
iot.publishMsg("enc", "0");
switchState = true;
}
}
else
{
if(switchState == true)
{
switchState = false;
}
}
// Encoder behind the plan work, it should be periodically called
encoder.service();
static int16_t oldPosition, newPosition;
// Read encoder value
newPosition += encoder.getValue();
// If the encoder is rotated clockwise, it will send "1" to the "enc" topic.
// If it the encoder is rotated counterclockwise, it will send "-1".
if(newPosition > oldPosition)
{
iot.publishMsg("enc", "1"); // CW rotation and increasing the LED intensity
}
else if (newPosition < oldPosition)
{
iot.publishMsg("enc", "-1"); // CCW rotation and decreasing the LED intensity
}
oldPosition = newPosition; // saving the new encoder position value for next cycle
}