Difference between revisions of "Implementing Arduino using Generic Serial Device"
Daballiemo (Talk | contribs) (→Example 1 Arduino, LinuxMCE & RGB LED) |
Daballiemo (Talk | contribs) |
||
Line 111: | Line 111: | ||
* Breadboard and some wires | * Breadboard and some wires | ||
* 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 6 for Red, 7 for Green and 8 for Blue. | ||
+ | |||
+ | |||
+ | ---------------- | ||
+ | - - GND ----------RGBLED | ||
+ | - - ||| | ||
+ | - - 6 ---- 300 Ohm--||| - | ||
+ | - - 7 ---- 300 Ohm --|| | ||
+ | - Arduino - 8 ---- 300 Ohm ---| | ||
+ | - - | ||
+ | - - | ||
+ | - - | ||
+ | - - | ||
+ | ---------------- | ||
Revision as of 07:50, 15 September 2012
Version | Status | Date Updated | Updated By |
---|---|---|---|
710 | Unknown | N/A | N/A |
810 | Unknown | N/A | N/A |
1004 | Working, but In Progress | 3th September 2012 | Daballiem0 |
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).
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
Change #384 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 1, it will also take up port 2 & 3 on the Arduino. In this case the ports # 2 & 3 should not be defined within LMCE # # DaBalliemo september 2012 ################################################################################################################## cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL cmdTo = cmd.devidto_ # Device ID in LinuxMCE devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] command = [0,0,0,0,0,0,0,0,0] # define command array log ('logging from #384'); log ('Device ID'); log cmdTo; log ('Port used'); log command[2]; 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%. case cmdId when 192 # 192 is the 'ON' command command [0] = 'P' # Start indication is P (Power) command [1] = 'on-' # '1' indicating 'ON' command [2] = deviceID # Port number defined in LMCE when 193 # 193 is the 'OFF' command command [0] = 'P' # Start indication is P (Power) command [1] = 'off-' # '1' indicating 'OFF' command [2] = deviceID # Port number defined in LMCE when 184 # 184 is Level of dimmer command [0] = 'D' # indicate level command command [1] = deviceID # Port number defined in LMCE command [2] = '-' command [3] = cmd.params_[76] # is level coming from LMCE when 980 # 980 is color scenario command (template 1993) 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 [3] = '-' # indicate RGB command 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 reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data
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 6 for Red, 7 for Green and 8 for Blue.
- - GND ----------RGBLED - - ||| - - 6 ---- 300 Ohm--||| - - - 7 ---- 300 Ohm --|| - Arduino - 8 ---- 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 * //***************************************************************** #include <math.h> #include <SPI.h> // needed for Arduino versions later than 0018 #include <Ethernet.h> boolean debug = true; //debug over serial? // 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 (8, OUTPUT); pinMode (7, OUTPUT); pinMode (6, OUTPUT); pinMode (5, OUTPUT); } // 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.print("Command given: "); if (debug) Serial.println(packetBuffer); if (debug) Serial.print("Buffersize: "); if (debug) Serial.println(packetSize); //if you receive a command that start with 'P' it means that you received a boolean value: //LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin //(in this case: pin x), or make "true" a variable. The second just turn off the pin x. if (packetBuffer[0] == 'P' ) { 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] != '-') { led_state_char[inCount] = packetBuffer[i]; inCount++; inCount_led_state_char = inCount; } if (var == 1 && packetBuffer[i] != '-') { pin_char[inCount] = packetBuffer[i]; inCount++; inCount_pin_char = inCount; } led_state_char[inCount_led_state_char] = '\0'; pin_char[inCount_pin_char] = '\0'; } if (debug) Serial.println(pin_char); int pin = atoi(pin_char); //our pin number if (debug) Serial.print("pin: "); 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); if (debug) Serial.println(led_state_send); client.print(led_state_send); //send a string to LMCE: "Pon-x" digitalWrite(pin, HIGH); //turn on the pin x } if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x' if (debug) Serial.println(" Status is OFF"); char led_state_send[10] = "Poff-"; strcat(led_state_send, pin_char); if (debug) Serial.println(led_state_send); client.print( led_state_send); //send a string to LMCE: "Poff-x"; digitalWrite(pin, LOW); } send_cmd = true; //we have read a command } //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, //or to do something else. 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.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] = "D"; 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; analogWrite(dim_pin, dim); //here we change the dim of the selected pin } //############################## 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.print("Redpin: "); if (debug) Serial.println(Red_pin_char); if (debug)Serial.print("Red:"); if (debug)Serial.print(Red_char); if (debug)Serial.print(" Green:"); if (debug)Serial.print(Green_char); if (debug)Serial.print(" Blue:"); if (debug)Serial.println(Blue_char); //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 analogWrite(dim_pin, dim); if (debug) Serial.print("Red pin: "); 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 = dim_pin + 1; analogWrite(dim_pin, dim); if (debug) Serial.print("Green pin: "); 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 = dim_pin + 2; analogWrite(dim_pin, 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"; 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); if (debug) Serial.println(dim_state_send); client.print( dim_state_send); //here return the made action. send_cmd = true; //here we change the dim of the selected pin }