This is an old revision of the document!
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 the guide for attaching the encoder to the sensor shield check out the encoder example.
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. 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:
lib_deps = ITTIoT@1.0.5, Adafruit NeoPixel
#include <Arduino.h> #include <ittiot.h> #include <Adafruit_NeoPixel.h> // Change it according to the real name of the red IoT module where // DHT shield is connected #define DHT_TOPIC "ESP53" // 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 = 0; //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; } else if (msg=="-1") { light-=1; } if(msg=="0") { if(light) light=0; } if(light>250) { light=250; } else if(light<0) { light=0; } } // Message received void iot_received(String topic, String msg) { // Calculate brightness 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(DHT_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); Serial.println("Booting"); // print IoT json config to serial iot.printConfig(); // Initialize IoT library iot.setup(); // Initialize RGB LED pixels.begin(); // Turn all LED-s off pixels.setPixelColor(0, 0, 0, 0); pixels.show(); } void loop() { // IoT behind the plan work, it should be periodically called iot.handle(); delay(10); }
The following is the code for the controller module with the sensor (encoder) shield.
lib_deps = ITTIoT@1.0.5, ClickEncoder
#include <Arduino.h> #include <ittiot.h> #include <ClickEncoder.h> // 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; // Message received void iot_received(String topic, String msg) { } // 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); Serial.println("Booting"); // print IoT json config to serial iot.printConfig(); // Initialize IoT library iot.setup(); } 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. // If the encoder button is pressed, it will send "0". if(analogRead(A0) < 100) { if(switchState == false) { String msg = String(1); 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 wil send "1" to the "enc" topic. If it is rotated counterclockwise, it will send "-1". if(newPosition > oldPosition) { iot.publishMsg("enc", "1"); } else if (newPosition < oldPosition) { iot.publishMsg("enc", "-1"); } oldPosition = newPosition; }