Difference between revisions of "Implementing Arduino using Generic Serial Device"

From LinuxMCE
Jump to: navigation, search
(Example 1 Arduino, LinuxMCE & RGB LED)
(Added category: GSD)
 
(27 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
[[Category:Hardware]]
 
[[Category:Hardware]]
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}
+
[[Category: GSD]]
 +
{{versioninfo|810Status=Working|810UpdatedDate=14th November 2012|810UpdatedBy=Daballiemo|1004Status=Working|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiemo}}
  
 
== General Info ==
 
== General Info ==
Line 27: Line 28:
 
== Example 1 Arduino, LinuxMCE & RGB LED ==
 
== Example 1 Arduino, LinuxMCE & RGB LED ==
  
Change #384 under ruby code of your Arduino GSD to the following:
+
In this example we will use the Arduino Uno with an Ethernet shield to be able to control a RGB LED from within LinuxMCE. The RGB switch (Device Template #: 1993) has to be made a child of the Arduino GSD. Make sure in the auto generated scenario for this device that the "ON" command is really "ON" and not level "100".
 +
 
 +
Note: RGB setting can only be done via a scenario!!
 +
 
 +
One nice feature is that whatever color code the LED (string) has been set to, when using the dim function or switched on or off, it will hold the same color schema. All dim levels are being treated as relative to the orginal value. This means 50% is 50% of the current value and not 50% of the maximum !!!
 +
 
 +
 
 +
Change the following processes under ruby code of your Arduino GSD to the following:
  
 
'''#384 Process Receive Command For Child'''
 
'''#384 Process Receive Command For Child'''
  
 
<pre>
 
<pre>
 +
 
##################################################################################################################
 
##################################################################################################################
 
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE
 
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE
 
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections
 
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections
 
# are in line with eachother.
 
# are in line with eachother.
# One example: if you define an RGB switch with port 1, it will also take up port 2 & 3 on the Arduino. In this case the ports
+
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino.  
# 2 & 3 should not be defined within LMCE
+
#
 +
#
 +
# Commandstructure
 +
#
 +
#      A          -          BBB      -        CCC      -      DDD    -      EEE
 +
#                             
 +
#      P(ower)            pin number (8 or 9)  0 (OFF            0              0
 +
#                                                1 (ON)
 +
#      D(im)              pin number (3 or 9)  0-100 (Level)      0              0
 +
#
 +
#      C(olor)            pin number (3)        0-255 (Red)      0-255 (Green)  0-255 (Blue)
 +
#   
 +
#      R(elative)          pin number (3)        0-100 (Level)
 +
#
 +
#  RedPin = 3;        ------> LinuxMCE RGB switch port number has to be 3 !!!!
 +
#  GreenPin = 5;     
 +
#  BluePin = 6;     
 +
#  OnOffPin = 8;      ------> LinuxMCE On/Off switch port number has to be 8 !!!!
 +
#  WhitePin = 9;      ------> LinuxMCE Dimmer switch port number has to be 9 !!!!
 +
#
 
#
 
#
 
# DaBalliemo september 2012
 
# DaBalliemo september 2012
Line 43: Line 71:
 
cmdId = cmd.id_                                                          # Command ID: ON, OFF, SET LEVEL
 
cmdId = cmd.id_                                                          # Command ID: ON, OFF, SET LEVEL
 
cmdTo = cmd.devidto_ # Device ID in LinuxMCE
 
cmdTo = cmd.devidto_ # Device ID in LinuxMCE
devPort = device_.childdevices_[cmdTo].devdata_[12]                     # 12 contains a port/channel
+
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]             # 12 contains a port/channel
 
childType = device_.childdevices_[cmdTo].devtemplid_                    # Template ID to know type of device: switch or dimmer
 
childType = device_.childdevices_[cmdTo].devtemplid_                    # Template ID to know type of device: switch or dimmer
  
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]
+
command = ['','','','','','','','',''] # define command array
 
+
command = [0,0,0,0,0,0,0,0,0] # define command array
+
  
 
log ('logging from #384');
 
log ('logging from #384');
 
log ('Device ID');
 
log ('Device ID');
log cmdTo;
+
log deviceID;
log ('Port used');
+
log command[2];
+
 
log ('Command given');                            # orginal command from LMCE
 
log ('Command given');                            # orginal command from LMCE
 
log cmdId;
 
log cmdId;
Line 62: Line 86:
 
log cmd.params_[76];                              # The level to set, as a value between 0 (off) and 100 (full).
 
log cmd.params_[76];                              # The level to set, as a value between 0 (off) and 100 (full).
 
                                                   # It can be preceeded with a - or + indicating a relative value.  +20 means up 20%.
 
                                                   # It can be preceeded with a - or + indicating a relative value.  +20 means up 20%.
 +
log ('RGB');
 +
log cmd.params_[279];
 +
log cmd.params_[280];
 +
log cmd.params_[281];
 +
 +
command [1] = deviceID                            # Port number defined in LMCE and pin number in Arduino Uno
 +
command [2] = '-'
 +
 
 
case cmdId
 
case cmdId
  
 
when 192                                          # 192 is the 'ON' command
 
when 192                                          # 192 is the 'ON' command
command [0] = 'P'                                 # Start indication is P (Power)
+
  case childType
command [1] = 'on-'                               # '1' indicating 'ON'   
+
  when 37, 38                                    # device type is "on/off switch" template 37 or dimmable switch template 38
command [2] = deviceID                            # Port number defined in LMCE
+
    command [0] = 'P'                             # Start indication is P (Power)
 
+
    command [3] = '1'                             # '1' indicating 'ON'   
 +
  when 1993                                      # device type is "RGB switch" template 1993
 +
                                                  # by using a dim level of "100" we make sure the Arduino can use the previous levels
 +
    command [0] = 'R'                              # start indicator is relative dimming
 +
    command [3] = 100                              # relative dim level
 +
  end
 +
 
 
when 193                                          # 193 is the 'OFF' command
 
when 193                                          # 193 is the 'OFF' command
command [0] = 'P'                                 # Start indication is P (Power)
+
  command [3] = '0'
command [1] = 'off-'                               # '1' indicating 'OFF' 
+
  case childType
command [2] = deviceID                            # Port number defined in LMCE
+
  when 37, 38                                    # device type is "on/off switch" template 37 or dimmable switch template 38
 +
    command [0] = 'P'                             # Start indication is P (Power)
 +
  when 1993                                      # device type is "RGB switch" template 1993
 +
                                                  # by using a dim level of "0" we make sure the Arduino will remember the settings
 +
    command [0] = 'R'                             # start indicator is relative dimming
 +
  end
  
 
when 184                                          # 184 is Level of dimmer
 
when 184                                          # 184 is Level of dimmer
command [0] = 'D'                                 # indicate level command
+
  case childType
command [1] = deviceID                            # Port number defined in LMCE
+
  when 38
command [2] = '-'
+
    command [0] = 'D'
command [3] = cmd.params_[76]                     # is level coming from LMCE
+
    command [3] = cmd.params_[76]                  # dim level
 
+
  when 1993
 +
    command [0] = 'R'
 +
    command [3] = cmd.params_[76]                 # relative dim level
 +
  end
 +
   
 
when 980                                          # 980 is color scenario command (template 1993)
 
when 980                                          # 980 is color scenario command (template 1993)
command [0] = 'C'                                 # start indicator is C (Color)
+
command [0] = 'C'                                 # start indicator is C (Color)
command [1] = deviceID                            # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue
+
command [4] = cmd.params_[279]                   # red level
command [3] = '-'                                  # indicate RGB command
+
command [5] = '-'
command [4] = cmd.params_[279]                     # red level
+
command [6] = cmd.params_[280]                   # green level
command [5] = '/'
+
command [7] = '-'
command [6] = cmd.params_[280]                     # green level
+
command [8] = cmd.params_[281]                   # blue level
command [7] = '/'
+
command [8] = cmd.params_[281]                     # blue level
+
 
end
 
end
  
Line 97: Line 142:
 
conn_.Send commandstring                          # send if off
 
conn_.Send commandstring                          # send if off
  
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data
+
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data
 +
 
  
 
</pre>
 
</pre>
 +
 +
'''#350 Process Incoming Data'''
 +
 +
<pre>
 +
 +
buffer = conn_.Recv(100,500);
 +
log ('Receiving');
 +
log buffer;
 +
 +
</pre>
 +
 +
'''#373 Private Method Listing '''
 +
 +
<pre>
 +
 +
def log(line)
 +
  # This function logs a line to the log file of the device
 +
  log = File.open("/var/log/pluto/" + device_.devid_.to_s + "_Generic_Serial_Device.log", "a")
 +
  log.puts Time.now.to_s + " (Ruby script):" + line.to_s
 +
  log.close
 +
end
 +
 +
</pre>
 +
  
 
'''Arduino Uno shopping list'''
 
'''Arduino Uno shopping list'''
Line 112: Line 182:
 
* IDE installed on computer
 
* IDE installed on computer
  
 +
There are numerous examples on the internet how to hook up a LED to the Arduino. Because there is no way of knowing what LED exactly is being used please find your own setup between all the examples.
  
 +
The following sketch assumes you are using pin 3 for Red, 5 for Green and 6 for Blue. (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.).
 +
 +
<pre>
 +
----------------               
 +
-              - GND ----------RGBLED (This can be a RGB string when using MOSFET's)
 +
-              -                |||
 +
-              - 3 ---- 300 Ohm--|||              -
 +
-              - 5 ---- 300 Ohm --||
 +
-  Arduino    - 6 ---- 300 Ohm ---|
 +
-              -
 +
-              - 8 ----- On/Off
 +
-              - 9 --------------LED (Dimmable)
 +
-              -                  |
 +
---------------- GND --- 300 Ohm --
 +
 +
</pre>
  
 
'''Sketch for Arduino'''
 
'''Sketch for Arduino'''
Line 127: Line 214:
 
//*****************************************************************
 
//*****************************************************************
  
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *
+
// * with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *
 +
 
 +
// * (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function *
  
 
//*****************************************************************
 
//*****************************************************************
Line 134: Line 223:
 
#include <SPI.h>        // needed for Arduino versions later than 0018
 
#include <SPI.h>        // needed for Arduino versions later than 0018
 
#include <Ethernet.h>
 
#include <Ethernet.h>
 
  
 
boolean debug = true; //debug over serial?
 
boolean debug = true; //debug over serial?
 +
int RedMemory = 255;
 +
int GreenMemory = 255;
 +
int BlueMemory = 255;
  
  
Line 170: Line 261:
  
 
   //port setting:
 
   //port setting:
 +
  pinMode (9, OUTPUT);
 
   pinMode (8, OUTPUT);
 
   pinMode (8, OUTPUT);
  pinMode (7, OUTPUT);
 
 
   pinMode (6, OUTPUT);
 
   pinMode (6, OUTPUT);
 
   pinMode (5, OUTPUT);
 
   pinMode (5, OUTPUT);
 +
  pinMode (3, OUTPUT);
 +
 
 +
 
}
 
}
 +
 +
 +
//********************************************************************************************************************************
 +
//
 +
// Commandstructure
 +
//
 +
//      A          -          BBB      -        CCC      -      DDD    -      EEE
 +
//                             
 +
//      P(ower)            pin number (8 or 9)  0 (OFF            0              0
 +
//                                                1 (ON)
 +
//      D(im)              pin number (3 or 9)  0-100 (Level)      0              0
 +
//
 +
//      C(olor)            pin number (3)        0-255 (Red)      0-255 (Green)  0-255 (Blue)
 +
//
 +
//      R(elative)          pin number (3)        0-100
 +
//
 +
//  RedPin = 3;        ------> LinuxMCE RGB switch port number has to be 3 !!!!
 +
//  GreenPin = 5;     
 +
//  BluePin = 6;     
 +
//  OnOffPin = 8;      ------> LinuxMCE On/Off switch port number has to be 8 !!!!
 +
//  WhitePin = 9;      ------> LinuxMCE Dimmer switch port number has to be 9 !!!!
 +
//
 +
// DaBalliemo september 2012
 +
//
 +
//*********************************************************************************************************************************
  
  
Line 202: Line 321:
 
       }
 
       }
  
       if (debug) Serial.print("Command given: ");
+
       if (debug) {
      if (debug) Serial.println(packetBuffer);
+
        Serial.println("***************************************");
      if (debug) Serial.print("Buffersize: ");
+
        Serial.print("Command given: ");
      if (debug) Serial.println(packetSize);
+
        Serial.println(packetBuffer);
 
+
        Serial.print("Buffersize: ");
      //if you receive a command that start with 'P' it means that you received a boolean value:
+
        Serial.println(packetSize);
      //LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin
+
        Serial.println("***************************************");
       //(in this case: pin x), or make "true" a variable. The second just turn off the pin x.
+
       }
  
 +
//############################## Power Section #################################################################################
 +
//if you receive a command that start with 'P' it means that you received a boolean value:
 +
//LinuxMCE sends "Px-0" or "Px-1". The first turn on a pin
 +
//(in this case: pin x). The second just turn off the pin x.
  
       if (packetBuffer[0] == 'P' ) {
+
       if (packetBuffer[0] == 'P' ) {                                                                 // Px-1 or Px-0
 
         char led_state_char[4];                                                                      //contains 'on' or 'off'
 
         char led_state_char[4];                                                                      //contains 'on' or 'off'
 
         char pin_char[4];                                                                            // the pin number
 
         char pin_char[4];                                                                            // the pin number
Line 228: Line 351:
  
 
           if (var == 0 && packetBuffer[i] != '-') {
 
           if (var == 0 && packetBuffer[i] != '-') {
             led_state_char[inCount] = packetBuffer[i];
+
             pin_char[inCount] = packetBuffer[i];
 
             inCount++;
 
             inCount++;
             inCount_led_state_char = inCount;
+
             inCount_pin_char = inCount;
 
           }
 
           }
  
 
           if (var == 1 && packetBuffer[i] != '-') {
 
           if (var == 1 && packetBuffer[i] != '-') {
             pin_char[inCount] = packetBuffer[i];
+
             led_state_char[inCount] = packetBuffer[i];
 
             inCount++;
 
             inCount++;
             inCount_pin_char = inCount;
+
             inCount_led_state_char = inCount;
 
           }
 
           }
  
Line 242: Line 365:
 
           pin_char[inCount_pin_char] = '\0';
 
           pin_char[inCount_pin_char] = '\0';
 
         }
 
         }
         if (debug) Serial.println(pin_char);
+
       
 +
         if (debug) {
 +
          Serial.println("***************************************");
 +
          Serial.println("Power command given");
 +
          Serial.println("");
 +
          Serial.print("Pin number is: ");
 +
          Serial.println(pin_char);
 +
          Serial.print("  Command given is: ");
 +
          Serial.println(led_state_char);
 +
          Serial.println("***************************************");
 +
        }
  
 +
        int pin = atoi(pin_char);                                                                  //our pin number
  
        int pin = atoi(pin_char);                                                                    //our pin number
+
         if (atoi(led_state_char) == 1) {                                                           //if we send 'Px-1'
         if (debug) Serial.print("pin: ");
+
           char led_state_send[10] = "P";
        if (debug) Serial.print(pin);
+
 
+
 
+
        if (strcmp(led_state_char,"on") == 0) {                                                     //if we send 'Pon-x'
+
          if (debug) Serial.println("    Status is ON");
+
           char led_state_send[10] = "Pon-";
+
 
           strcat(led_state_send, pin_char);
 
           strcat(led_state_send, pin_char);
           if (debug) Serial.println(led_state_send);
+
           strcat(led_state_send, "-");
           client.print(led_state_send);                                                            //send a string to LMCE: "Pon-x"
+
          strcat(led_state_send, led_state_char);
 +
           client.print(led_state_send);                                                            //send a string to LMCE: "Px-1"
 
           digitalWrite(pin, HIGH);                                                                //turn on the pin x
 
           digitalWrite(pin, HIGH);                                                                //turn on the pin x
 
         }
 
         }
  
         if (strcmp(led_state_char,"off") == 0) {                                                   //if we send 'Poff-x'
+
         if (atoi(led_state_char) == 0) {                                                           //if we send 'Px-0'
          if (debug) Serial.println("    Status is OFF");
+
           char led_state_send[10] = "P";
           char led_state_send[10] = "Poff-";
+
 
           strcat(led_state_send, pin_char);
 
           strcat(led_state_send, pin_char);
           if (debug) Serial.println(led_state_send);
+
           strcat(led_state_send, "-");
           client.print( led_state_send);                                                           //send a string to LMCE: "Poff-x";
+
          strcat(led_state_send, led_state_char);
 +
           client.print(led_state_send);                                                           //send a string to LMCE: "Px-0";
 
           digitalWrite(pin, LOW);
 
           digitalWrite(pin, LOW);
 
         }
 
         }
Line 271: Line 400:
  
  
 
+
//############################## Dim Section #################################################################################
      //if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE
+
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE
      //the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that
+
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that
      //we want change dim, "55" is the dim value. LMCE range is from "0" to "100". The conversion 0-100 into 0-255 is done in this sketch
+
//we want change dim, "55" is the dim value. LMCE range is from "0" to "100". The conversion 0-100 into 0-255 is done in this sketch
      //we can use this dim to change directly the value to use in AnalogWrite,
+
//we can use this dim to change directly the value to use in AnalogWrite,
      //or to do something else.
+
  
 
       if (packetBuffer[0] == 'D') {
 
       if (packetBuffer[0] == 'D') {
Line 315: Line 444:
 
         dim = atoi(dim_char);                                                              //here we have the dim number: 0-100
 
         dim = atoi(dim_char);                                                              //here we have the dim number: 0-100
 
         dim = dim * 2.55;                                                                  // Translate 0-100 into 0-255, gets trunked
 
         dim = dim * 2.55;                                                                  // Translate 0-100 into 0-255, gets trunked
 +
        dim_pin = atoi(dim_pin_char);                                                      //and here the pin number
 +
       
 +
        if (debug) {
 +
          Serial.println("***************************************");
 +
          Serial.println("Dim command given");
 +
          Serial.print("dim pin: ");
 +
          Serial.println(dim_pin);
 +
          Serial.print("dim level: ");
 +
          Serial.println(dim);
 +
          Serial.println("***************************************");
 +
        }
 +
 +
        char dim_state_send[10] = "D";
 +
        strcat(dim_state_send, dim_pin_char);
 +
        strcat(dim_state_send, "-");
 +
        strcat(dim_state_send, dim_char);
 +
        client.print( dim_state_send);                                                    //here return the made action.
 +
        send_cmd = true;
 +
        analogWrite(dim_pin, dim);                                                        //here we change the dim of the selected pin
 +
      }
 +
 +
//############################## Relative Dim Section #################################################################################
 +
//if we receive a command that start with 'R' it means that you have chosen a relative dim level in LMCE for a RGB LED or String
 +
//the string that we send is like : "R3-55" and it's means: 'R' is use for "relative dim", "3" is the first pin that
 +
//we want change dim, "55" is the dim value. LMCE range is from "0" to "100". The conversion 0-100 into 0-255 is done in this sketch
 +
//we can use this dim to change directly the value to use in AnalogWrite,
 +
 +
      if (packetBuffer[0] == 'R') {
 +
        char dim_char[10];
 +
        char dim_pin_char[10];
 +
        int inCount = 0;
 +
        int var = 0;
 +
        int inCount_dim_pin_char;
 +
        int inCount_dim_char;
 +
        int DimRed;
 +
        int DimGreen;
 +
        int DimBlue;
 +
        float f;
 +
 +
        for (int i = 1; i <  packetSize; i++)
 +
        {
 +
          //Serial.println(packetBuffer[i]);
 +
          if (packetBuffer[i] == '-') {
 +
            var++;
 +
            inCount = 0;     
 +
          }
 +
 +
          if (var == 0 && packetBuffer[i] != '-') {
 +
            dim_pin_char[inCount] = packetBuffer[i];
 +
            inCount++;
 +
            inCount_dim_pin_char = inCount;
 +
          }
 +
 +
          if (var == 1 && packetBuffer[i] != '-') {
 +
            dim_char[inCount] = packetBuffer[i];
 +
            inCount++;
 +
            inCount_dim_char = inCount;
 +
          }
 +
 +
          dim_pin_char[inCount_dim_pin_char] = '\0';
 +
          dim_char[inCount_dim_char] = '\0';
 +
        }
 +
 +
 +
        //Serial.println(dim_char);
 +
 +
        dim = atoi(dim_char);                                                              //here we have the dim number: 0-100
 
         dim_pin = atoi(dim_pin_char);                                                      //and here the pin number
 
         dim_pin = atoi(dim_pin_char);                                                      //and here the pin number
  
Line 322: Line 518:
 
         if (debug) Serial.println(dim);
 
         if (debug) Serial.println(dim);
  
         char dim_state_send[10] = "D";
+
         char dim_state_send[10] = "R";
 
         strcat(dim_state_send, dim_pin_char);
 
         strcat(dim_state_send, dim_pin_char);
 
         strcat(dim_state_send, "-");
 
         strcat(dim_state_send, "-");
Line 331: Line 527:
 
         send_cmd = true;
 
         send_cmd = true;
  
        analogWrite(dim_pin, dim);                                                        //here we change the dim of the selected pin
 
      }
 
  
 +
        DimRed = int(RedMemory*float(dim)/100);                                          //here we have the dim number:
 +
        dim_pin = atoi(dim_pin_char);                                                    //and here the pin number
 +
        dim_pin = dim_pin;
 +
        analogWrite(dim_pin, DimRed);
 +
 +
        if (debug) {
 +
          Serial.println("***************************************");
 +
          Serial.println("Relative Dim command given");
 +
          Serial.print("Red pin: ");
 +
          Serial.print(dim_pin);
 +
          Serial.print("  Memory level: ");
 +
          Serial.print(RedMemory);
 +
          Serial.print(" New level: ");
 +
          Serial.println(DimRed);
 +
        }         
 +
 +
        DimGreen = int(GreenMemory*float(dim)/100);                                                              //here we have the dim number: 0-255
 +
        dim_pin = atoi(dim_pin_char);                                                            //and here the pin number
 +
        dim_pin = dim_pin + 2;
 +
        analogWrite(dim_pin, DimGreen);
 +
 
 +
        if (debug) {
 +
          Serial.print("Green pin: ");
 +
          Serial.print(dim_pin);
 +
          Serial.print("  Memory level: ");
 +
          Serial.print(GreenMemory);
 +
          Serial.print(" New level: ");
 +
          Serial.println(DimGreen);
 +
        }
 +
 +
        DimBlue = int(BlueMemory*float(dim)/100);                                                            //here we have the dim number: 0-255
 +
        dim_pin = atoi(dim_pin_char);                                                            //and here the pin number
 +
        dim_pin = dim_pin + 3;
 +
        analogWrite(dim_pin, DimBlue);
 +
 +
        if (debug) {
 +
          Serial.print("Blue pin: ");
 +
          Serial.print(dim_pin);
 +
          Serial.print("  Memory level: ");
 +
          Serial.print(BlueMemory);
 +
          Serial.print(" New level: ");
 +
          Serial.println(DimBlue);
 +
          Serial.println("***************************************");
 +
        }         
 +
       
 +
      }
  
  
Line 392: Line 632:
 
           Blue_char[inCount_Blue_char] = '\0';           
 
           Blue_char[inCount_Blue_char] = '\0';           
 
            
 
            
          if (debug) Serial.print("Redpin: ");  
+
        if (debug) {
          if (debug) Serial.println(Red_pin_char);
+
            Serial.println("***************************************");
          if (debug)Serial.print("Red:");  
+
            Serial.println("RGB command given");
          if (debug)Serial.print(Red_char);  
+
            Serial.print("Redpin: ");  
          if (debug)Serial.print(" Green:");  
+
            Serial.println(Red_pin_char);
          if (debug)Serial.print(Green_char);  
+
            Serial.print("Red:");  
          if (debug)Serial.print(" Blue:");  
+
            Serial.print(Red_char);  
          if (debug)Serial.println(Blue_char);
+
            Serial.print(" Green:");  
 +
            Serial.print(Green_char);  
 +
            Serial.print(" Blue:");  
 +
            Serial.println(Blue_char);
 +
          } 
  
       
+
         dim = atoi(Red_char);                                                              //here we have the dim number: 0-255
 
+
 
+
        //Serial.println(dim_char);
+
       
+
     
+
       
+
         dim = atoi(Red_char);                                                              //here we have the dim number: 0-100
+
 
         dim_pin = atoi(Red_pin_char);                                                      //and here the pin number
 
         dim_pin = atoi(Red_pin_char);                                                      //and here the pin number
 
         analogWrite(dim_pin, dim);
 
         analogWrite(dim_pin, dim);
 +
        RedMemory = dim;                                                                  // Make sure we remember
 
          
 
          
        if (debug) Serial.print("Red pin: ");
+
         dim = atoi(Green_char);                                                              //here we have the dim number: 0-255
        if (debug) Serial.print(dim_pin);
+
        if (debug) Serial.print(" level: ");
+
        if (debug) Serial.println(dim);
+
       
+
         dim = atoi(Green_char);                                                              //here we have the dim number: 0-100
+
 
         dim_pin = atoi(Red_pin_char);                                                            //and here the pin number
 
         dim_pin = atoi(Red_pin_char);                                                            //and here the pin number
         dim_pin = dim_pin + 1;
+
         dim_pin = dim_pin + 2;
 
         analogWrite(dim_pin, dim);
 
         analogWrite(dim_pin, dim);
 +
        GreenMemory = dim;
 
   
 
   
        if (debug) Serial.print("Green pin: ");
+
         dim = atoi(Blue_char);                                                              //here we have the dim number: 0-255
        if (debug) Serial.print(dim_pin);
+
        if (debug) Serial.print(" level: ");
+
        if (debug) Serial.println(dim);       
+
       
+
         dim = atoi(Blue_char);                                                              //here we have the dim number: 0-100
+
 
         dim_pin = atoi(Red_pin_char);                                                      //and here the pin number
 
         dim_pin = atoi(Red_pin_char);                                                      //and here the pin number
         dim_pin = dim_pin + 2;
+
         dim_pin = dim_pin + 3;
 
         analogWrite(dim_pin, dim);
 
         analogWrite(dim_pin, dim);
 +
        BlueMemory = dim;
 
        
 
        
        if (debug) Serial.print("Blue pin: ");
 
        if (debug) Serial.print(dim_pin);
 
        if (debug) Serial.print(" level: ");
 
        if (debug) Serial.println(dim);       
 
 
 
 
         char dim_state_send[16] = "C";
 
         char dim_state_send[16] = "C";
 
         strcat(dim_state_send, Red_pin_char);
 
         strcat(dim_state_send, Red_pin_char);
Line 446: Line 670:
 
         strcat(dim_state_send, "-");
 
         strcat(dim_state_send, "-");
 
         strcat(dim_state_send, Blue_char);
 
         strcat(dim_state_send, Blue_char);
       
 
        if (debug) Serial.println(dim_state_send);
 
 
         client.print( dim_state_send);                                                    //here return the made action.
 
         client.print( dim_state_send);                                                    //here return the made action.
 
         send_cmd = true;
 
         send_cmd = true;
Line 453: Line 675:
 
                                                                 //here we change the dim of the selected pin
 
                                                                 //here we change the dim of the selected pin
 
       }
 
       }
 +
    }
 +
  }
  
 
+
}
 
+
 
+
 
+
 
</pre>
 
</pre>

Latest revision as of 11:40, 3 July 2016

Version Status Date Updated Updated By
710 Unknown N/A N/A
810 Working 14th November 2012 Daballiemo
1004 Working 3th September 2012 Daballiemo
1204 Unknown N/A N/A
1404 Unknown N/A N/A
Usage Information

General Info

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.

Arduino can sense the environment by receiving input from a variety of sensors and can affect its surroundings by controlling lights, motors, and other actuators. The microcontroller on the board is programmed using the Arduino programming language (based on Wiring) and the Arduino development environment (based on Processing). Arduino projects can be stand-alone or they can communicate with software running on a computer (e.g. Flash, Processing, MaxMSP).

Arduino uno test.jpg

In the following I want to give an overview what needs to be done to have LinuxMCE control a RGB LED (string) through an Arduino. The string is within brackets as the only added to the Arduino to be able to control a ledstring is some mosfets and a powerblock.

Setting up Linuxmce

For now define Arduino as a vendor via Advanced -> Device Templates. Select the "Add Manufacturer" button and provide "Arduino" as manufacturer followed by save and close.

Define a new template for the Arduino Uno (which I am using atm)

Advanced -> Device Templates, selected "Add device template". Name the template "Arduino Uno", select Implements DCE, Device Category is "Interfaces - Specialized #97". Specify that your device is controlled via category "Device Category:Core" ("Computers - Core"). Otherwise the driver will not be started on the core. As I am using an Ethernet shield select "Is IP based", set "Comm Method" to Ethernet and under "Device data" add #69 TCP Port (int). Give the TCP Port the value "69", set it to required and allowed to modify.

Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.


Example 1 Arduino, LinuxMCE & RGB LED

In this example we will use the Arduino Uno with an Ethernet shield to be able to control a RGB LED from within LinuxMCE. The RGB switch (Device Template #: 1993) has to be made a child of the Arduino GSD. Make sure in the auto generated scenario for this device that the "ON" command is really "ON" and not level "100".

Note: RGB setting can only be done via a scenario!!

One nice feature is that whatever color code the LED (string) has been set to, when using the dim function or switched on or off, it will hold the same color schema. All dim levels are being treated as relative to the orginal value. This means 50% is 50% of the current value and not 50% of the maximum !!!


Change the following processes under ruby code of your Arduino GSD to the following:

#384 Process Receive Command For Child


##################################################################################################################
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections
# are in line with eachother.
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. 
#
#
# Commandstructure
#
#       A           -          BBB      -         CCC       -      DDD     -      EEE
#                              
#       P(ower)             pin number (8 or 9)   0 (OFF             0              0
#                                                 1 (ON)
#       D(im)               pin number (3 or 9)   0-100 (Level)      0              0
#
#       C(olor)             pin number (3)        0-255 (Red)       0-255 (Green)  0-255 (Blue)
#     
#       R(elative)          pin number (3)        0-100 (Level)
#
#  RedPin = 3;        ------> LinuxMCE RGB switch port number has to be 3 !!!!
#  GreenPin = 5;      
#  BluePin = 6;       
#  OnOffPin = 8;      ------> LinuxMCE On/Off switch port number has to be 8 !!!!
#  WhitePin = 9;      ------> LinuxMCE Dimmer switch port number has to be 9 !!!!
#
#
# DaBalliemo september 2012
##################################################################################################################
cmdId = cmd.id_                                                          # Command ID: ON, OFF, SET LEVEL
cmdTo = cmd.devidto_ # Device ID in LinuxMCE
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]              # 12 contains a port/channel
childType = device_.childdevices_[cmdTo].devtemplid_                     # Template ID to know type of device: switch or dimmer

command = ['','','','','','','','',''] # define command array

log ('logging from #384');
log ('Device ID');
log deviceID;
log ('Command given');                             # orginal command from LMCE
log cmdId;
log ('Device Template');                           # Template ID ie switch, dimmer or RGB
log childType;
log ('Level');
log cmd.params_[76];                               # The level to set, as a value between 0 (off) and 100 (full).
                                                   # It can be preceeded with a - or + indicating a relative value.  +20 means up 20%.
log ('RGB');
log cmd.params_[279];
log cmd.params_[280];
log cmd.params_[281];

command [1] = deviceID                             # Port number defined in LMCE and pin number in Arduino Uno
command [2] = '-'
   
case cmdId

when 192                                           # 192 is the 'ON' command
  case childType
   when 37, 38                                     # device type is "on/off switch" template 37 or dimmable switch template 38
    command [0] = 'P'                              # Start indication is P (Power)
    command [3] = '1'                              # '1' indicating 'ON'  
   when 1993                                       # device type is "RGB switch" template 1993
                                                   # by using a dim level of "100" we make sure the Arduino can use the previous levels
    command [0] = 'R'                              # start indicator is relative dimming
    command [3] = 100                              # relative dim level
  end
   
when 193                                           # 193 is the 'OFF' command
  command [3] = '0'
  case childType
   when 37, 38                                     # device type is "on/off switch" template 37 or dimmable switch template 38
    command [0] = 'P'                              # Start indication is P (Power)
   when 1993                                       # device type is "RGB switch" template 1993
                                                   # by using a dim level of "0" we make sure the Arduino will remember the settings
    command [0] = 'R'                              # start indicator is relative dimming
  end

when 184                                           # 184 is Level of dimmer
  case childType
   when 38
    command [0] = 'D'
    command [3] = cmd.params_[76]                  # dim level
   when 1993
    command [0] = 'R'
    command [3] = cmd.params_[76]                  # relative dim level
  end 
     
when 980                                           # 980 is color scenario command (template 1993)
 command [0] = 'C'                                 # start indicator is C (Color)
 command [4] = cmd.params_[279]                    # red level
 command [5] = '-'
 command [6] = cmd.params_[280]                    # green level
 command [7] = '-'
 command [8] = cmd.params_[281]                    # blue level
end


commandstring = command.to_s                       # wrap the command array into a string
log ('Sending');                                   # log that we are sending
log commandstring;                                 # put in logfile what we send
conn_.Send commandstring                           # send if off

# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data


#350 Process Incoming Data


buffer = conn_.Recv(100,500);
log ('Receiving');
log buffer;

#373 Private Method Listing


def log(line)
  # This function logs a line to the log file of the device
  log = File.open("/var/log/pluto/" + device_.devid_.to_s + "_Generic_Serial_Device.log", "a")
  log.puts Time.now.to_s + " (Ruby script):" + line.to_s
  log.close
end


Arduino Uno shopping list

Shopping list:

  • Arduino Uno
  • Ethernet Shield
  • LED
  • Resistors
  • Breadboard and some wires
  • IDE installed on computer

There are numerous examples on the internet how to hook up a LED to the Arduino. Because there is no way of knowing what LED exactly is being used please find your own setup between all the examples.

The following sketch assumes you are using pin 3 for Red, 5 for Green and 6 for Blue. (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.).

----------------                 
-              - GND ----------RGBLED (This can be a RGB string when using MOSFET's)
-              -                 |||
-              - 3 ---- 300 Ohm--|||               -
-              - 5 ---- 300 Ohm --||
-   Arduino    - 6 ---- 300 Ohm ---|
-              -
-              - 8 ----- On/Off
-              - 9 --------------LED (Dimmable)
-              -                  |
---------------- GND --- 300 Ohm --

Sketch for Arduino

/*

  arduino_control_by_lmce.ino:

This sketch sends and receive TCP message strings to LinuxMCE.

*/

//*****************************************************************

// * with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *

// * (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function *

//*****************************************************************

#include <math.h>
#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>

boolean debug = true; //debug over serial?
int RedMemory = 255;
int GreenMemory = 255;
int BlueMemory = 255;


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:

byte mac[] = {
  0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };
byte ip[] = {
  192,168,80,129};
byte gateway[] = {
  192,168,80,1 };
byte subnet[] = {
  255,255,255,0 };

unsigned int localPort = 69;      // local port to listen on

// buffers for receiving and sending data

char packetBuffer[1024]; //buffer to hold incoming commandpacket,
EthernetServer server(69);


void setup() {

  if (debug) Serial.begin(9600);
  if (debug) Serial.println("Starting LinuxMCE sketch...");

  // start the Ethernet

  Ethernet.begin(mac, ip,gateway, subnet);
  server.begin();

  //port setting:
  pinMode (9, OUTPUT);
  pinMode (8, OUTPUT);
  pinMode (6, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (3, OUTPUT);
  

}


//********************************************************************************************************************************
//
// Commandstructure
//
//       A           -          BBB      -         CCC       -      DDD     -      EEE
//                              
//       P(ower)             pin number (8 or 9)   0 (OFF             0              0
//                                                 1 (ON)
//       D(im)               pin number (3 or 9)   0-100 (Level)      0              0
//
//       C(olor)             pin number (3)        0-255 (Red)       0-255 (Green)  0-255 (Blue)
//
//       R(elative)          pin number (3)        0-100
//
//  RedPin = 3;        ------> LinuxMCE RGB switch port number has to be 3 !!!!
//  GreenPin = 5;      
//  BluePin = 6;       
//  OnOffPin = 8;      ------> LinuxMCE On/Off switch port number has to be 8 !!!!
//  WhitePin = 9;      ------> LinuxMCE Dimmer switch port number has to be 9 !!!!
//
// DaBalliemo september 2012
//
//*********************************************************************************************************************************


// variables uses to change the value of analogWrite function.

int dim = 0;
int dim_pin = 0;


void loop() {


  bool send_cmd = false;                                                                           //is true when i send a command.

  // if there's commanddata available, read a packet

  EthernetClient client = server.available();
  int packetSize = 0;
  if (client)
  {

    if (client.connected()) {
      while (client.available()) {
        packetBuffer[packetSize] = client.read();
        packetSize++;
        packetBuffer[packetSize] = '\0';
      }

      if (debug) {
        Serial.println("***************************************");
        Serial.print("Command given: ");
        Serial.println(packetBuffer);
        Serial.print("Buffersize: ");
        Serial.println(packetSize);
        Serial.println("***************************************");
      }

//############################## Power Section #################################################################################
//if you receive a command that start with 'P' it means that you received a boolean value:
//LinuxMCE sends "Px-0" or "Px-1". The first turn on a pin
//(in this case: pin x). The second just turn off the pin x.

      if (packetBuffer[0] == 'P' ) {                                                                 // Px-1 or Px-0
        char led_state_char[4];                                                                      //contains 'on' or 'off'
        char pin_char[4];                                                                            // the pin number
        int inCount = 0;
        int var = 0;
        int inCount_led_state_char;
        int inCount_pin_char;

        for (int i = 1; i <  packetSize; i++)
        {
          if (packetBuffer[i] == '-') {
            var++;
            inCount = 0;       
          }

          if (var == 0 && packetBuffer[i] != '-') {
            pin_char[inCount] = packetBuffer[i];
            inCount++;
            inCount_pin_char = inCount;
          }

          if (var == 1 && packetBuffer[i] != '-') {
            led_state_char[inCount] = packetBuffer[i];
            inCount++;
            inCount_led_state_char = inCount;
          }

          led_state_char[inCount_led_state_char] = '\0';
          pin_char[inCount_pin_char] = '\0';
        }
        
        if (debug) {
          Serial.println("***************************************");
          Serial.println("Power command given");
          Serial.println("");
          Serial.print("Pin number is: ");
          Serial.println(pin_char);
          Serial.print("   Command given is: ");
          Serial.println(led_state_char);
          Serial.println("***************************************");
        } 

        int pin = atoi(pin_char);                                                                  //our pin number

        if (atoi(led_state_char) == 1) {                                                           //if we send 'Px-1'
          char led_state_send[10] = "P";
          strcat(led_state_send, pin_char);
          strcat(led_state_send, "-");
          strcat(led_state_send, led_state_char);
          client.print(led_state_send);                                                            //send a string to LMCE: "Px-1"
          digitalWrite(pin, HIGH);                                                                 //turn on the pin x
        }

        if (atoi(led_state_char) == 0) {                                                           //if we send 'Px-0'
          char led_state_send[10] = "P";
          strcat(led_state_send, pin_char);
          strcat(led_state_send, "-");
          strcat(led_state_send, led_state_char);
          client.print(led_state_send);                                                            //send a string to LMCE: "Px-0";
          digitalWrite(pin, LOW);
        }
        send_cmd = true;                                                                           //we have read a command
      }


//############################## Dim Section #################################################################################
 //if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE
 //the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that
 //we want change dim, "55" is the dim value. LMCE range is from "0" to "100". The conversion 0-100 into 0-255 is done in this sketch
 //we can use this dim to change directly the value to use in AnalogWrite,
 

      if (packetBuffer[0] == 'D') {
        char dim_char[10];
        char dim_pin_char[10];
        int inCount = 0;
        int var = 0;
        int inCount_dim_pin_char;
        int inCount_dim_char;

        for (int i = 1; i <  packetSize; i++)
        {
          //Serial.println(packetBuffer[i]);
          if (packetBuffer[i] == '-') {
            var++;
            inCount = 0;       
          }

          if (var == 0 && packetBuffer[i] != '-') {
            dim_pin_char[inCount] = packetBuffer[i];
            inCount++;
            inCount_dim_pin_char = inCount;
          }

          if (var == 1 && packetBuffer[i] != '-') {
            dim_char[inCount] = packetBuffer[i];
            inCount++;
            inCount_dim_char = inCount;
          }

          dim_pin_char[inCount_dim_pin_char] = '\0';
          dim_char[inCount_dim_char] = '\0';
        }


        //Serial.println(dim_char);

        dim = atoi(dim_char);                                                              //here we have the dim number: 0-100
        dim = dim * 2.55;                                                                  // Translate 0-100 into 0-255, gets trunked
        dim_pin = atoi(dim_pin_char);                                                      //and here the pin number
        
        if (debug) {
          Serial.println("***************************************");
          Serial.println("Dim command given");
          Serial.print("dim pin: ");
          Serial.println(dim_pin);
          Serial.print("dim level: ");
          Serial.println(dim);
          Serial.println("***************************************");
        }

        char dim_state_send[10] = "D";
        strcat(dim_state_send, dim_pin_char);
        strcat(dim_state_send, "-");
        strcat(dim_state_send, dim_char);
        client.print( dim_state_send);                                                     //here return the made action.
        send_cmd = true;
        analogWrite(dim_pin, dim);                                                         //here we change the dim of the selected pin
      }

//############################## Relative Dim Section #################################################################################
//if we receive a command that start with 'R' it means that you have chosen a relative dim level in LMCE for a RGB LED or String
//the string that we send is like : "R3-55" and it's means: 'R' is use for "relative dim", "3" is the first pin that
//we want change dim, "55" is the dim value. LMCE range is from "0" to "100". The conversion 0-100 into 0-255 is done in this sketch
//we can use this dim to change directly the value to use in AnalogWrite,

      if (packetBuffer[0] == 'R') {
        char dim_char[10];
        char dim_pin_char[10];
        int inCount = 0;
        int var = 0;
        int inCount_dim_pin_char;
        int inCount_dim_char;
        int DimRed;
        int DimGreen;
        int DimBlue;
        float f;

        for (int i = 1; i <  packetSize; i++)
        {
          //Serial.println(packetBuffer[i]);
          if (packetBuffer[i] == '-') {
            var++;
            inCount = 0;       
          }

          if (var == 0 && packetBuffer[i] != '-') {
            dim_pin_char[inCount] = packetBuffer[i];
            inCount++;
            inCount_dim_pin_char = inCount;
          }

          if (var == 1 && packetBuffer[i] != '-') {
            dim_char[inCount] = packetBuffer[i];
            inCount++;
            inCount_dim_char = inCount;
          }

          dim_pin_char[inCount_dim_pin_char] = '\0';
          dim_char[inCount_dim_char] = '\0';
        }


        //Serial.println(dim_char);

        dim = atoi(dim_char);                                                              //here we have the dim number: 0-100
        dim_pin = atoi(dim_pin_char);                                                      //and here the pin number

        if (debug) Serial.print("dim pin: ");
        if (debug) Serial.println(dim_pin);
        if (debug) Serial.print("dim level: ");
        if (debug) Serial.println(dim);

        char dim_state_send[10] = "R";
        strcat(dim_state_send, dim_pin_char);
        strcat(dim_state_send, "-");
        strcat(dim_state_send, dim_char);

        if (debug) Serial.println(dim_state_send);
        client.print( dim_state_send);                                                     //here return the made action.
        send_cmd = true;


        DimRed = int(RedMemory*float(dim)/100);                                           //here we have the dim number: 
        dim_pin = atoi(dim_pin_char);                                                     //and here the pin number
        dim_pin = dim_pin;
        analogWrite(dim_pin, DimRed);
 
        if (debug) {
          Serial.println("***************************************");
          Serial.println("Relative Dim command given");
          Serial.print("Red pin: ");
          Serial.print(dim_pin);
          Serial.print("  Memory level: ");
          Serial.print(RedMemory);
          Serial.print(" New level: ");
          Serial.println(DimRed);
         }          
 
        DimGreen = int(GreenMemory*float(dim)/100);                                                              //here we have the dim number: 0-255
        dim_pin = atoi(dim_pin_char);                                                             //and here the pin number
        dim_pin = dim_pin + 2;
        analogWrite(dim_pin, DimGreen);
  
         if (debug) {
          Serial.print("Green pin: ");
          Serial.print(dim_pin);
          Serial.print("  Memory level: ");
          Serial.print(GreenMemory);
          Serial.print(" New level: ");
          Serial.println(DimGreen);
         } 

        DimBlue = int(BlueMemory*float(dim)/100);                                                             //here we have the dim number: 0-255
        dim_pin = atoi(dim_pin_char);                                                             //and here the pin number
        dim_pin = dim_pin + 3;
        analogWrite(dim_pin, DimBlue);

        if (debug) {
          Serial.print("Blue pin: ");
          Serial.print(dim_pin);
          Serial.print("  Memory level: ");
          Serial.print(BlueMemory);
          Serial.print(" New level: ");
          Serial.println(DimBlue); 
          Serial.println("***************************************");
        }          
        
      }


//############################## RGB Section #################################################################################
      //if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.
      //the string that we send is like : "C9-55/155/255" and it's means: 'C' is color i.e. "RGB", "9" is the 1st pin of the RGB LED you have
      //connected to the Arduino, pin 10 is Green and pin 11 is Blue.LMCE range is from "0" to "255" for each channel so no adjustment is necessary.

      if (packetBuffer[0] == 'C') {                                                        // Received a command string for RGB
        char Red_char[10];
        char Red_pin_char[10];
        char Green_char[10];
        char Green_pin_char[10];
        char Blue_char[10];
        char Blue_pin_char[10];        
        int inCount = 0;
        int var = 0;
        int inCount_RGB_pin_char;
        int inCount_Red_char;
        int inCount_Green_char;
        int inCount_Blue_char;        

        for (int i = 1; i <  packetSize; i++)
        {
          //Serial.println(packetBuffer[i]);
          if (packetBuffer[i] == '-') {
            var++;
            inCount = 0;       
          }

          if (var == 0 && packetBuffer[i] != '-') {
            Red_pin_char[inCount] = packetBuffer[i];
            inCount++;
            inCount_RGB_pin_char = inCount;
          }
          if (var == 1 &&packetBuffer[i] != '-') {
            Red_char[inCount] = packetBuffer[i];
            inCount++;
            inCount_Red_char = inCount;
          }
           
          if (var == 2 && packetBuffer[i] != '-') {
            Green_char[inCount] = packetBuffer[i];
            inCount++;
            inCount_Green_char = inCount;
          } 

          if (var == 3  && packetBuffer[i] != '-') {                                    // Reading Blue value
             Blue_char[inCount] = packetBuffer[i];
             inCount++;
             inCount_Blue_char = inCount;
          } 
    
         }
          Red_pin_char[inCount_RGB_pin_char] = '\0';
          Red_char[inCount_Red_char] = '\0';
          Green_char[inCount_Green_char] = '\0';
          Blue_char[inCount_Blue_char] = '\0';          
          
         if (debug) {
            Serial.println("***************************************"); 
            Serial.println("RGB command given");
            Serial.print("Redpin: "); 
            Serial.println(Red_pin_char);
            Serial.print("Red:"); 
            Serial.print(Red_char); 
            Serial.print(" Green:"); 
            Serial.print(Green_char); 
            Serial.print(" Blue:"); 
            Serial.println(Blue_char);
          }   

        dim = atoi(Red_char);                                                              //here we have the dim number: 0-255
        dim_pin = atoi(Red_pin_char);                                                      //and here the pin number
        analogWrite(dim_pin, dim);
        RedMemory = dim;                                                                   // Make sure we remember
        
        dim = atoi(Green_char);                                                              //here we have the dim number: 0-255
        dim_pin = atoi(Red_pin_char);                                                             //and here the pin number
        dim_pin = dim_pin + 2;
        analogWrite(dim_pin, dim);
        GreenMemory = dim;
 
        dim = atoi(Blue_char);                                                              //here we have the dim number: 0-255
        dim_pin = atoi(Red_pin_char);                                                      //and here the pin number
        dim_pin = dim_pin + 3;
        analogWrite(dim_pin, dim);
        BlueMemory = dim;
       
        char dim_state_send[16] = "C";
        strcat(dim_state_send, Red_pin_char);
        strcat(dim_state_send, "-");
        strcat(dim_state_send, Red_char);
        strcat(dim_state_send, "-");
        strcat(dim_state_send, Green_char);
        strcat(dim_state_send, "-");
        strcat(dim_state_send, Blue_char);
        client.print( dim_state_send);                                                     //here return the made action.
        send_cmd = true;

                                                                 //here we change the dim of the selected pin
      }
    }
  }

}