Implementing Arduino using Generic Serial Device

From LinuxMCE
Jump to: navigation, search
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
      }
    }
  }

}