http://wiki.linuxmce.org/api.php?action=feedcontributions&user=Daballiemo&feedformat=atomLinuxMCE - User contributions [en]2024-03-29T00:08:39ZUser contributionsMediaWiki 1.24.1http://wiki.linuxmce.org/index.php?title=User:Daballiemo&diff=37056User:Daballiemo2023-04-16T14:28:02Z<p>Daballiemo: update</p>
<hr />
<div>[[Category:User Setups]]<br />
'''Who is user123456789'''<br />
<br />
user123456789 is:<br />
<br />
*busy with linux (and espec. Ubuntu) since 2005 as a hobby,<br />
*still trying to combine his 2 hobbies (Motorcycle driving and LMCE) which btw is not a good idea ;-)<br />
<br />
Regards,<br />
<br />
'''Setup'''<br />
<br />
''Kantoor''<br />
<br />
MD:<br />
<br />
Foxconn nT330i<br />
<br />
Audio: 0d8c:0103 C-Media Electronics, Inc. Turtle Beach Audio Advantage Micro (USB audio the following /root/.asoundrc setup):<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "cards.pcm.default"<br />
}<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
# Then define the new device as the default<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
} <br />
<br />
<br />
<br />
Here I will put my setup shortly<br />
<br />
'''Things I want to research in the future'''</div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=34336Implementing Arduino using Generic Serial Device2013-08-11T12:03:11Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|810Status=Working|810UpdatedDate=14th November 2012|810UpdatedBy=Daballiemo|1004Status=Working|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiemo}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'R' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'R' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [3] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [3] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
</pre><br />
<br />
'''#373 Private Method Listing '''<br />
<br />
<pre><br />
<br />
def log(line)<br />
# This function logs a line to the log file of the device<br />
log = File.open("/var/log/pluto/" + device_.devid_.to_s + "_Generic_Serial_Device.log", "a")<br />
log.puts Time.now.to_s + " (Ruby script):" + line.to_s<br />
log.close<br />
end<br />
<br />
</pre><br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
// * with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
// * (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
boolean debug = true; //debug over serial?<br />
int RedMemory = 255;<br />
int GreenMemory = 255;<br />
int BlueMemory = 255;<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (9, OUTPUT);<br />
pinMode (8, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
pinMode (3, OUTPUT);<br />
<br />
<br />
}<br />
<br />
<br />
//********************************************************************************************************************************<br />
//<br />
// Commandstructure<br />
//<br />
// A - BBB - CCC - DDD - EEE<br />
// <br />
// P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
// 1 (ON)<br />
// D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
//<br />
// C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
//<br />
// R(elative) pin number (3) 0-100<br />
//<br />
// RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
// GreenPin = 5; <br />
// BluePin = 6; <br />
// OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
// WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
//<br />
// DaBalliemo september 2012<br />
//<br />
//*********************************************************************************************************************************<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.print("Command given: ");<br />
Serial.println(packetBuffer);<br />
Serial.print("Buffersize: ");<br />
Serial.println(packetSize);<br />
Serial.println("***************************************");<br />
}<br />
<br />
//############################## Power Section #################################################################################<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Px-0" or "Px-1". The first turn on a pin<br />
//(in this case: pin x). The second just turn off the pin x.<br />
<br />
if (packetBuffer[0] == 'P' ) { // Px-1 or Px-0<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Power command given");<br />
Serial.println("");<br />
Serial.print("Pin number is: ");<br />
Serial.println(pin_char);<br />
Serial.print(" Command given is: ");<br />
Serial.println(led_state_char);<br />
Serial.println("***************************************");<br />
} <br />
<br />
int pin = atoi(pin_char); //our pin number<br />
<br />
if (atoi(led_state_char) == 1) { //if we send 'Px-1'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-1"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (atoi(led_state_char) == 0) { //if we send 'Px-0'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-0";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
//############################## Dim Section #################################################################################<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Dim command given");<br />
Serial.print("dim pin: ");<br />
Serial.println(dim_pin);<br />
Serial.print("dim level: ");<br />
Serial.println(dim);<br />
Serial.println("***************************************");<br />
}<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
//############################## Relative Dim Section #################################################################################<br />
//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<br />
//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<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
if (packetBuffer[0] == 'R') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
int DimRed;<br />
int DimGreen;<br />
int DimBlue;<br />
float f;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "R";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
<br />
DimRed = int(RedMemory*float(dim)/100); //here we have the dim number: <br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin;<br />
analogWrite(dim_pin, DimRed);<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Relative Dim command given");<br />
Serial.print("Red pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(RedMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimRed);<br />
} <br />
<br />
DimGreen = int(GreenMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, DimGreen);<br />
<br />
if (debug) {<br />
Serial.print("Green pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(GreenMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimGreen);<br />
} <br />
<br />
DimBlue = int(BlueMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, DimBlue);<br />
<br />
if (debug) {<br />
Serial.print("Blue pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(BlueMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimBlue); <br />
Serial.println("***************************************");<br />
} <br />
<br />
}<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) {<br />
Serial.println("***************************************"); <br />
Serial.println("RGB command given");<br />
Serial.print("Redpin: "); <br />
Serial.println(Red_pin_char);<br />
Serial.print("Red:"); <br />
Serial.print(Red_char); <br />
Serial.print(" Green:"); <br />
Serial.print(Green_char); <br />
Serial.print(" Blue:"); <br />
Serial.println(Blue_char);<br />
} <br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
RedMemory = dim; // Make sure we remember<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
GreenMemory = dim;<br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, dim);<br />
BlueMemory = dim;<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
}<br />
}<br />
<br />
}<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=34335Implementing Arduino using Generic Serial Device2013-08-11T11:59:20Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|810Status=Working|810UpdatedDate=14th November 2012|810UpdatedBy=Daballiemo|1004Status=Working|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiemo}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'R' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'R' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [3] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [3] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
</pre><br />
<br />
'''#373 Private Method Listing '''<br />
<br />
<pre><br />
<br />
def log(line)<br />
# This function logs a line to the log file of the device<br />
log = File.open("/var/log/pluto/" + device_.devid_.to_s + "_Generic_Serial_Device.log", "a")<br />
log.puts Time.now.to_s + " (Ruby script):" + line.to_s<br />
log.close<br />
end<br />
<br />
</pre><br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
[[File:Rgb-led-strips-mosfets.png]]<br />
<br />
or<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
// * with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
// * (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
boolean debug = true; //debug over serial?<br />
int RedMemory = 255;<br />
int GreenMemory = 255;<br />
int BlueMemory = 255;<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (9, OUTPUT);<br />
pinMode (8, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
pinMode (3, OUTPUT);<br />
<br />
<br />
}<br />
<br />
<br />
//********************************************************************************************************************************<br />
//<br />
// Commandstructure<br />
//<br />
// A - BBB - CCC - DDD - EEE<br />
// <br />
// P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
// 1 (ON)<br />
// D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
//<br />
// C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
//<br />
// R(elative) pin number (3) 0-100<br />
//<br />
// RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
// GreenPin = 5; <br />
// BluePin = 6; <br />
// OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
// WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
//<br />
// DaBalliemo september 2012<br />
//<br />
//*********************************************************************************************************************************<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.print("Command given: ");<br />
Serial.println(packetBuffer);<br />
Serial.print("Buffersize: ");<br />
Serial.println(packetSize);<br />
Serial.println("***************************************");<br />
}<br />
<br />
//############################## Power Section #################################################################################<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Px-0" or "Px-1". The first turn on a pin<br />
//(in this case: pin x). The second just turn off the pin x.<br />
<br />
if (packetBuffer[0] == 'P' ) { // Px-1 or Px-0<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Power command given");<br />
Serial.println("");<br />
Serial.print("Pin number is: ");<br />
Serial.println(pin_char);<br />
Serial.print(" Command given is: ");<br />
Serial.println(led_state_char);<br />
Serial.println("***************************************");<br />
} <br />
<br />
int pin = atoi(pin_char); //our pin number<br />
<br />
if (atoi(led_state_char) == 1) { //if we send 'Px-1'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-1"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (atoi(led_state_char) == 0) { //if we send 'Px-0'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-0";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
//############################## Dim Section #################################################################################<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Dim command given");<br />
Serial.print("dim pin: ");<br />
Serial.println(dim_pin);<br />
Serial.print("dim level: ");<br />
Serial.println(dim);<br />
Serial.println("***************************************");<br />
}<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
//############################## Relative Dim Section #################################################################################<br />
//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<br />
//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<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
if (packetBuffer[0] == 'R') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
int DimRed;<br />
int DimGreen;<br />
int DimBlue;<br />
float f;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "R";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
<br />
DimRed = int(RedMemory*float(dim)/100); //here we have the dim number: <br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin;<br />
analogWrite(dim_pin, DimRed);<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Relative Dim command given");<br />
Serial.print("Red pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(RedMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimRed);<br />
} <br />
<br />
DimGreen = int(GreenMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, DimGreen);<br />
<br />
if (debug) {<br />
Serial.print("Green pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(GreenMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimGreen);<br />
} <br />
<br />
DimBlue = int(BlueMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, DimBlue);<br />
<br />
if (debug) {<br />
Serial.print("Blue pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(BlueMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimBlue); <br />
Serial.println("***************************************");<br />
} <br />
<br />
}<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) {<br />
Serial.println("***************************************"); <br />
Serial.println("RGB command given");<br />
Serial.print("Redpin: "); <br />
Serial.println(Red_pin_char);<br />
Serial.print("Red:"); <br />
Serial.print(Red_char); <br />
Serial.print(" Green:"); <br />
Serial.print(Green_char); <br />
Serial.print(" Blue:"); <br />
Serial.println(Blue_char);<br />
} <br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
RedMemory = dim; // Make sure we remember<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
GreenMemory = dim;<br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, dim);<br />
BlueMemory = dim;<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
}<br />
}<br />
<br />
}<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=34334Implementing Arduino using Generic Serial Device2013-08-11T11:56:25Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|810Status=Working|810UpdatedDate=14th November 2012|810UpdatedBy=Daballiemo|1004Status=Working|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiemo}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'R' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'R' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [3] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [3] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
</pre><br />
<br />
'''#373 Private Method Listing '''<br />
<br />
<pre><br />
<br />
def log(line)<br />
# This function logs a line to the log file of the device<br />
log = File.open("/var/log/pluto/" + device_.devid_.to_s + "_Generic_Serial_Device.log", "a")<br />
log.puts Time.now.to_s + " (Ruby script):" + line.to_s<br />
log.close<br />
end<br />
<br />
</pre><br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
or<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
// * with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
// * (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
boolean debug = true; //debug over serial?<br />
int RedMemory = 255;<br />
int GreenMemory = 255;<br />
int BlueMemory = 255;<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (9, OUTPUT);<br />
pinMode (8, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
pinMode (3, OUTPUT);<br />
<br />
<br />
}<br />
<br />
<br />
//********************************************************************************************************************************<br />
//<br />
// Commandstructure<br />
//<br />
// A - BBB - CCC - DDD - EEE<br />
// <br />
// P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
// 1 (ON)<br />
// D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
//<br />
// C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
//<br />
// R(elative) pin number (3) 0-100<br />
//<br />
// RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
// GreenPin = 5; <br />
// BluePin = 6; <br />
// OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
// WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
//<br />
// DaBalliemo september 2012<br />
//<br />
//*********************************************************************************************************************************<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.print("Command given: ");<br />
Serial.println(packetBuffer);<br />
Serial.print("Buffersize: ");<br />
Serial.println(packetSize);<br />
Serial.println("***************************************");<br />
}<br />
<br />
//############################## Power Section #################################################################################<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Px-0" or "Px-1". The first turn on a pin<br />
//(in this case: pin x). The second just turn off the pin x.<br />
<br />
if (packetBuffer[0] == 'P' ) { // Px-1 or Px-0<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Power command given");<br />
Serial.println("");<br />
Serial.print("Pin number is: ");<br />
Serial.println(pin_char);<br />
Serial.print(" Command given is: ");<br />
Serial.println(led_state_char);<br />
Serial.println("***************************************");<br />
} <br />
<br />
int pin = atoi(pin_char); //our pin number<br />
<br />
if (atoi(led_state_char) == 1) { //if we send 'Px-1'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-1"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (atoi(led_state_char) == 0) { //if we send 'Px-0'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-0";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
//############################## Dim Section #################################################################################<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Dim command given");<br />
Serial.print("dim pin: ");<br />
Serial.println(dim_pin);<br />
Serial.print("dim level: ");<br />
Serial.println(dim);<br />
Serial.println("***************************************");<br />
}<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
//############################## Relative Dim Section #################################################################################<br />
//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<br />
//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<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
if (packetBuffer[0] == 'R') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
int DimRed;<br />
int DimGreen;<br />
int DimBlue;<br />
float f;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "R";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
<br />
DimRed = int(RedMemory*float(dim)/100); //here we have the dim number: <br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin;<br />
analogWrite(dim_pin, DimRed);<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Relative Dim command given");<br />
Serial.print("Red pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(RedMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimRed);<br />
} <br />
<br />
DimGreen = int(GreenMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, DimGreen);<br />
<br />
if (debug) {<br />
Serial.print("Green pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(GreenMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimGreen);<br />
} <br />
<br />
DimBlue = int(BlueMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, DimBlue);<br />
<br />
if (debug) {<br />
Serial.print("Blue pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(BlueMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimBlue); <br />
Serial.println("***************************************");<br />
} <br />
<br />
}<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) {<br />
Serial.println("***************************************"); <br />
Serial.println("RGB command given");<br />
Serial.print("Redpin: "); <br />
Serial.println(Red_pin_char);<br />
Serial.print("Red:"); <br />
Serial.print(Red_char); <br />
Serial.print(" Green:"); <br />
Serial.print(Green_char); <br />
Serial.print(" Blue:"); <br />
Serial.println(Blue_char);<br />
} <br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
RedMemory = dim; // Make sure we remember<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
GreenMemory = dim;<br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, dim);<br />
BlueMemory = dim;<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
}<br />
}<br />
<br />
}<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Express_Controls_EZmotion_(HSM_100)&diff=34213Express Controls EZmotion (HSM 100)2013-06-24T09:22:04Z<p>Daballiemo: /* Setup */</p>
<hr />
<div>[[Category: Hardware]]<br />
{{Versioninfo|810Status=Working,PnP as 3 multilevel sensors|810UpdatedDate=4th May 2010|810UpdatedBy=sambuca}}<br />
[[Category: RF Control]]<br />
[[Category: ZWave]]<br />
[[Category: ZWave_security]]<br />
[[Category: ZWave_motion_sensor]]<br />
[[Category: ZWave_light_sensor]]<br />
[[Category: ZWave_temperature_sensor]]<br />
[[Category: Security]]<br />
[[Category: 0810]]<br />
<br />
= About = <br />
The EZMotion is a 3-in-1 Z-Wave sensor. This is also sold by Innovus and Homeseer (HSM100)<br />
<br />
Manuf ID1: 0x0 ID2: 0x1e, Prod Typ1: 0x0 Typ2: 0x2, Prod ID1: 0x0 ID2: 0x1<br />
<br />
= Features =<br />
* Motion detection<br />
* Light sensor<br />
* Temperature sensor.<br />
<br />
= Setup =<br />
If you use SIS mode in your ZWave setup, this device should automatically be added as three multilevel sensors.<br />
If not, it is possible to set it up manually. If you are running SIS mode, you can delete the main device and re-add it as a motion detector, see below for details.<br />
<br />
You will have one multilevel sensor added for the EZMotion. Find and open this from the device tree.<br />
We will change this to be the thermometer instance. Find the Port/Channel number. This is the node ID.<br />
Change the Port/Channel number to <node id>/3 (for instance 4/3 if your node id is 4), type to Thermometer, insert 49 in Capabilities, and save.<br />
<br />
Add a new device below the ZWave Embedded Climate Interface using the same device template (Multilevel sensor).<br />
Insert <node id>/2 in Port/Channel number, insert 49 in Capabilities and save.<br />
This will be the brightness sensor.<br />
Please notice that the values returned from this sensor ranges from 0 to 100% and is relative to the light measured since X (see the manual).<br />
<br />
Then we add the Motion detector. This will not be added automatically even when running the SIS mode.<br />
Open the ZWave Embedded Security Interface and click Create New Child. Select the Motion Detector template.<br />
Insert <node id> in Port/Channel number, Motion Detector in type, 133,96,49,112,132,128,114,119,134 in Capabilities, and save.<br />
<br />
Reload router and you should receive events from all three sensors.<br />
The motion detector will also report a values(which is not sent to LMCE atm.). This value if the number of minutes since last motion was detected, and 0 if no motion was detected. See the manual for more information.<br />
<br />
Note: when using Homepro ZCU010 USB computer interface you will have to send a "Set Association" command hooking up the controller and the HSM 100. If not done the HSM100 will not report back to LinuxMCE.<br />
Node ID of controller can be found in the Zwave log file; ''Home id: 0x0000e95d, our node id: 239 <0xb6d59b70>''.<br />
<br />
= Configuration =<br />
<br />
To change the interval temperature and light is reported go to Home > My Devices > CORE > ZWave and chose "Send command to device". Select "SetWakeUp" and set parameters as follows:<br />
<br />
48 Value (int) : <the time in minutes can only be 6 or any increment of 6> <br><br />
239 NodeID (int) : <the zwave nodeid> <br><br />
<br />
Send Message<br />
<br />
Then push the button on the EZmotion (HSM 100)<br />
<br />
<br />
<br />
By default it seems that the HSM 100 has a motion timeout of 20 minutes. That means that it will not send any motion detection events for 20 minutes after it is first tripped. This is a useful setting for saving batteries but not a useful setting for an occupancy sensor controlling lights. To set the motion timeout of the HSM 100:<br />
<br />
#Select the ZWave device in the advanced web config devices section. <br />
#Select send command to device. <br />
#Select "Set Config Param" as the command to send. <br />
#48 Value (int) = Your desired timeout in minutes<br />
#239 NodeID (int) = The ZWave node ID of the HSM 100 to configure<br />
#248 Parameter ID (int) = 2<br />
#Press the blue button on the side of the HSM 100 you are trying to configure<br />
#Click "Send Message"<br />
<br />
= Command classes =<br />
0x4 0x21 0x1 <br />
* COMMAND_CLASS_MULTI_INSTANCE <br />
* COMMAND_CLASS_SENSOR_MULTILEVEL <br />
* COMMAND_CLASS_CONFIGURATION <br />
* COMMAND_CLASS_WAKE_UP <br />
* COMMAND_CLASS_ASSOCIATION <br />
* COMMAND_CLASS_BATTERY <br />
* COMMAND_CLASS_MANUFACTURER_SPECIFIC <br />
* COMMAND_CLASS_NODE_NAMING <br />
* COMMAND_CLASS_VERSION<br />
<br />
= Links =<br />
<br />
http://www.expresscontrols.com<br />
<br />
http://www.expresscontrols.com/pdf/EZMotionOwnerManual.pdf<br />
<br />
[http://www.expresscontrols.com/ideas.htm Which type of AAA battery to use in EZMotion?]</div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Express_Controls_EZmotion_(HSM_100)&diff=34212Express Controls EZmotion (HSM 100)2013-06-24T09:20:20Z<p>Daballiemo: /* Setup */</p>
<hr />
<div>[[Category: Hardware]]<br />
{{Versioninfo|810Status=Working,PnP as 3 multilevel sensors|810UpdatedDate=4th May 2010|810UpdatedBy=sambuca}}<br />
[[Category: RF Control]]<br />
[[Category: ZWave]]<br />
[[Category: ZWave_security]]<br />
[[Category: ZWave_motion_sensor]]<br />
[[Category: ZWave_light_sensor]]<br />
[[Category: ZWave_temperature_sensor]]<br />
[[Category: Security]]<br />
[[Category: 0810]]<br />
<br />
= About = <br />
The EZMotion is a 3-in-1 Z-Wave sensor. This is also sold by Innovus and Homeseer (HSM100)<br />
<br />
Manuf ID1: 0x0 ID2: 0x1e, Prod Typ1: 0x0 Typ2: 0x2, Prod ID1: 0x0 ID2: 0x1<br />
<br />
= Features =<br />
* Motion detection<br />
* Light sensor<br />
* Temperature sensor.<br />
<br />
= Setup =<br />
If you use SIS mode in your ZWave setup, this device should automatically be added as three multilevel sensors.<br />
If not, it is possible to set it up manually. If you are running SIS mode, you can delete the main device and re-add it as a motion detector, see below for details.<br />
<br />
You will have one multilevel sensor added for the EZMotion. Find and open this from the device tree.<br />
We will change this to be the thermometer instance. Find the Port/Channel number. This is the node ID.<br />
Change the Port/Channel number to <node id>/3 (for instance 4/3 if your node id is 4), type to Thermometer, insert 49 in Capabilities, and save.<br />
<br />
Add a new device below the ZWave Embedded Climate Interface using the same device template (Multilevel sensor).<br />
Insert <node id>/2 in Port/Channel number, insert 49 in Capabilities and save.<br />
This will be the brightness sensor.<br />
Please notice that the values returned from this sensor ranges from 0 to 100% and is relative to the light measured since X (see the manual).<br />
<br />
Then we add the Motion detector. This will not be added automatically even when running the SIS mode.<br />
Open the ZWave Embedded Security Interface and click Create New Child. Select the Motion Detector template.<br />
Insert <node id> in Port/Channel number, Motion Detector in type, 133,96,49,112,132,128,114,119,134 in Capabilities, and save.<br />
<br />
Reload router and you should receive events from all three sensors.<br />
The motion detector will also report a values(which is not sent to LMCE atm.). This value if the number of minutes since last motion was detected, and 0 if no motion was detected. See the manual for more information.<br />
<br />
Note: when using Homepro ZCU010 USB computer interface you will have to send a "Set Association" command hooking up the controller and the HSM 100.<br />
Node ID of controller can be found in the Zwave log file; ''Home id: 0x0000e95d, our node id: 239 <0xb6d59b70>''<br />
<br />
= Configuration =<br />
<br />
To change the interval temperature and light is reported go to Home > My Devices > CORE > ZWave and chose "Send command to device". Select "SetWakeUp" and set parameters as follows:<br />
<br />
48 Value (int) : <the time in minutes can only be 6 or any increment of 6> <br><br />
239 NodeID (int) : <the zwave nodeid> <br><br />
<br />
Send Message<br />
<br />
Then push the button on the EZmotion (HSM 100)<br />
<br />
<br />
<br />
By default it seems that the HSM 100 has a motion timeout of 20 minutes. That means that it will not send any motion detection events for 20 minutes after it is first tripped. This is a useful setting for saving batteries but not a useful setting for an occupancy sensor controlling lights. To set the motion timeout of the HSM 100:<br />
<br />
#Select the ZWave device in the advanced web config devices section. <br />
#Select send command to device. <br />
#Select "Set Config Param" as the command to send. <br />
#48 Value (int) = Your desired timeout in minutes<br />
#239 NodeID (int) = The ZWave node ID of the HSM 100 to configure<br />
#248 Parameter ID (int) = 2<br />
#Press the blue button on the side of the HSM 100 you are trying to configure<br />
#Click "Send Message"<br />
<br />
= Command classes =<br />
0x4 0x21 0x1 <br />
* COMMAND_CLASS_MULTI_INSTANCE <br />
* COMMAND_CLASS_SENSOR_MULTILEVEL <br />
* COMMAND_CLASS_CONFIGURATION <br />
* COMMAND_CLASS_WAKE_UP <br />
* COMMAND_CLASS_ASSOCIATION <br />
* COMMAND_CLASS_BATTERY <br />
* COMMAND_CLASS_MANUFACTURER_SPECIFIC <br />
* COMMAND_CLASS_NODE_NAMING <br />
* COMMAND_CLASS_VERSION<br />
<br />
= Links =<br />
<br />
http://www.expresscontrols.com<br />
<br />
http://www.expresscontrols.com/pdf/EZMotionOwnerManual.pdf<br />
<br />
[http://www.expresscontrols.com/ideas.htm Which type of AAA battery to use in EZMotion?]</div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Foscam_FI8905W&diff=34148Foscam FI8905W2013-05-26T10:37:20Z<p>Daballiemo: </p>
<hr />
<div>[[Category: Hardware]]<br />
{{versioninfo|810Status=Works|810UpdatedDate=8th Jan 2011|810UpdatedBy=purps|versioninfo|1004Status=Works|1004UpdatedDate=27th May 2013|1004UpdatedBy=Daballiemo}}<br />
[[Category: Cameras]]<br />
[[category: IP Cameras]]<br />
<br />
{| align="right"<br />
| __TOC__<br />
|}<br />
<br />
<br />
<br />
=Features=<br />
<br />
* High image & video quality<br />
* High-sensitivity 1/4" CMOS sensor (300k pixels)<br />
* Auto IR-LED night vision up to 30m (90ft)<br />
* Motion detection alert via email or upload image to a specified FTP server<br />
* Support a wide range of browsers(IE, Firefox, Google)<br />
* Support remote viewing & record<br />
* Support image fullsceen and image snapshot<br />
* Multi-level users management and passwords definition<br />
* Embeded Web Server<br />
* WEP,WPA and WPA2 Encryption<br />
* Optimized MJPEG video compression for transmission<br />
* Support wireless network (WiFi/802.11/b/g)mobile<br />
* Supporting Dynamic IP (DDNS)and UPnP LAN and Internet(ADSL,Cable Modem)<br />
* Multi-Protocol support and Transportation (such as TCP/IP, SMTP and HTTP)<br />
* Support Mobile Phone View such as Iphone & Smart phone (It should support midp2.0 java mobile or pda mobile)<br />
* Simple to setup, Friendly GUI, DIY installation<br />
* Aluminum Alloy Design, Shell Vandal-Proof,IP65 Waterproof and weatherproof for Any Harsh Environment<br />
<br />
''Note: This is the 60 IR LED version; the FI8904W has fewer LEDs (24 to be exact) which means that it is not as good when it comes to night vision. The FI8904W is easily identifiable (apart from the number of LEDs!) by the shape of its cowling. The cowling of the FI8905W is 'U'-shaped in cross-section, whereas the cowling of the FI8904W is what I would call 'Micky Mouse'-shaped. One more thing; be careful of the cheap clones on eBay, you don't want to end up with one of those.''<br />
<br />
=Quick review=<br />
This camera does have quite a mean zoom lens on it (12 mm). Also the colours are a bit odd; greens and browns appear purple for example. That said, it is by far the cheapest outdoor IP camera around, and I think that it is very good for the price.<br />
<br />
Do also bear in mind [http://forum.linuxmce.org/index.php?topic=10134.0 this issue].<br />
<br />
=Installation=<br />
<br />
''Note: This section describes how to install this camera as a [[Motion]] device; I have not tried installing it as a standalone viewer, but I see no reason why it wouldn't work.''<br />
<br />
<span style="color:red">'''''Note: These instructions are for when the camera is plugged into a router on the external network, as opposed to a switch on the internal network. Not ideal, but this was necessary due to [http://forum.linuxmce.org/index.php?topic=10134.0 this aforementioned problem].'''''</span><br />
<br />
# Install motion wrapper (if it is not installed already). In web admin (type "192.168.80.1" in a browser of your choice), click "Show devices tree" (very bottom of left-hand pane), click on "CORE" and then "Create Child Device". Then press the "Pick device template" button, which will open a new window. Under "Device Template", select "Motion Wrapper" and press the "Pick device template" button.<br />
# Plug in your camera (remember I plug my Foscams into the external router). In your router admin page (accessed via 192.168.1.1 in my case) find out what IP was assigned. Type this IP into your browser and have a look through the options. Set the username and password. I also set a static IP just in case it tries to change itself. I also specified the camera's IP on my router so that no other device could take it (belt and braces).<br />
# In LMCE web admin (type "192.168.80.1" into a browser), go to Wizard -> Devices -> Surveillance Cameras. Click the "Add Device" button at the bottom of the page. This will open a new window.<br />
# In the window that just opened, pick "Generic Motion IP Camera" from the dropdown menu in the "Device Template" section. Press the "Pick device template" button.<br />
# Find the new camera in Wizard -> Devices -> Surveillance Cameras. Change the description to something more obvious if you so wish and select a room.<br />
# Change the "Path" field to "http://192.168.1.XXX/videostream.cgi?user=XXXXX&pwd=XXXXXX", not forgetting to replace the Xs with the IP, username and password. Add "80" to the "TCP Port" field. ''Press "Save".''<br />
# Press the "Advanced" button. Check the IP and MAC address in the "Device Info" section. If it's not the same as the MAC and IP you saw in the Panasonic utility, change it. ''Click "Save".''<br />
# Do a Reload & Regen, and that should be it. This camera can now be used as a motion sensor and/or for recording movement (recordings are stored in "/home/cameras/XX" where XX is the device number of the camera, and last for 5 days I believe). A scenario will hopefully be automatically generated.<br />
# Rinse and repeat for any additional cameras.<br />
<br />
''Note: After reloading/restarting the Core, motion dies. It has to be restarted manually by doing a "sudo /etc/init.d/motion restart". This issue has been reported http://svn.linuxmce.org/trac.cgi/ticket/904 & http://svn.linuxmce.org/trac.cgi/ticket/715 (not sure whether these two tickets are reporting the same issue or not).''<br />
<br />
''Note: Very occasionally I have found that the scenarios sometimes require a little bit of tinkering. If there is no image displayed when you click on the scenario, look in Wizard -> Scenarios -> Security scenarios, select the scenario in question, select "Advanced Wizard" in the "Edit scenario using wizard" section, and just check that the number in the "#2 PK_Device (int)" field corresponds with the camera's Device #; if it does not, change it. Reload & Regen.''</div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Foscam_FI8905W&diff=34147Foscam FI8905W2013-05-26T10:34:48Z<p>Daballiemo: </p>
<hr />
<div>[[Category: Hardware]]<br />
{{versioninfo|810Status=Works|810UpdatedDate=8th Jan 2011|810UpdatedBy=purps|versioninfo|1004Status=Works|1004UpdatedDate=27th May 2013|810UpdatedBy=Daballiemo}}<br />
[[Category: Cameras]]<br />
[[category: IP Cameras]]<br />
<br />
{| align="right"<br />
| __TOC__<br />
|}<br />
<br />
<br />
<br />
=Features=<br />
<br />
* High image & video quality<br />
* High-sensitivity 1/4" CMOS sensor (300k pixels)<br />
* Auto IR-LED night vision up to 30m (90ft)<br />
* Motion detection alert via email or upload image to a specified FTP server<br />
* Support a wide range of browsers(IE, Firefox, Google)<br />
* Support remote viewing & record<br />
* Support image fullsceen and image snapshot<br />
* Multi-level users management and passwords definition<br />
* Embeded Web Server<br />
* WEP,WPA and WPA2 Encryption<br />
* Optimized MJPEG video compression for transmission<br />
* Support wireless network (WiFi/802.11/b/g)mobile<br />
* Supporting Dynamic IP (DDNS)and UPnP LAN and Internet(ADSL,Cable Modem)<br />
* Multi-Protocol support and Transportation (such as TCP/IP, SMTP and HTTP)<br />
* Support Mobile Phone View such as Iphone & Smart phone (It should support midp2.0 java mobile or pda mobile)<br />
* Simple to setup, Friendly GUI, DIY installation<br />
* Aluminum Alloy Design, Shell Vandal-Proof,IP65 Waterproof and weatherproof for Any Harsh Environment<br />
<br />
''Note: This is the 60 IR LED version; the FI8904W has fewer LEDs (24 to be exact) which means that it is not as good when it comes to night vision. The FI8904W is easily identifiable (apart from the number of LEDs!) by the shape of its cowling. The cowling of the FI8905W is 'U'-shaped in cross-section, whereas the cowling of the FI8904W is what I would call 'Micky Mouse'-shaped. One more thing; be careful of the cheap clones on eBay, you don't want to end up with one of those.''<br />
<br />
=Quick review=<br />
This camera does have quite a mean zoom lens on it (12 mm). Also the colours are a bit odd; greens and browns appear purple for example. That said, it is by far the cheapest outdoor IP camera around, and I think that it is very good for the price.<br />
<br />
Do also bear in mind [http://forum.linuxmce.org/index.php?topic=10134.0 this issue].<br />
<br />
=Installation=<br />
<br />
''Note: This section describes how to install this camera as a [[Motion]] device; I have not tried installing it as a standalone viewer, but I see no reason why it wouldn't work.''<br />
<br />
<span style="color:red">'''''Note: These instructions are for when the camera is plugged into a router on the external network, as opposed to a switch on the internal network. Not ideal, but this was necessary due to [http://forum.linuxmce.org/index.php?topic=10134.0 this aforementioned problem].'''''</span><br />
<br />
# Install motion wrapper (if it is not installed already). In web admin (type "192.168.80.1" in a browser of your choice), click "Show devices tree" (very bottom of left-hand pane), click on "CORE" and then "Create Child Device". Then press the "Pick device template" button, which will open a new window. Under "Device Template", select "Motion Wrapper" and press the "Pick device template" button.<br />
# Plug in your camera (remember I plug my Foscams into the external router). In your router admin page (accessed via 192.168.1.1 in my case) find out what IP was assigned. Type this IP into your browser and have a look through the options. Set the username and password. I also set a static IP just in case it tries to change itself. I also specified the camera's IP on my router so that no other device could take it (belt and braces).<br />
# In LMCE web admin (type "192.168.80.1" into a browser), go to Wizard -> Devices -> Surveillance Cameras. Click the "Add Device" button at the bottom of the page. This will open a new window.<br />
# In the window that just opened, pick "Generic Motion IP Camera" from the dropdown menu in the "Device Template" section. Press the "Pick device template" button.<br />
# Find the new camera in Wizard -> Devices -> Surveillance Cameras. Change the description to something more obvious if you so wish and select a room.<br />
# Change the "Path" field to "http://192.168.1.XXX/videostream.cgi?user=XXXXX&pwd=XXXXXX", not forgetting to replace the Xs with the IP, username and password. Add "80" to the "TCP Port" field. ''Press "Save".''<br />
# Press the "Advanced" button. Check the IP and MAC address in the "Device Info" section. If it's not the same as the MAC and IP you saw in the Panasonic utility, change it. ''Click "Save".''<br />
# Do a Reload & Regen, and that should be it. This camera can now be used as a motion sensor and/or for recording movement (recordings are stored in "/home/cameras/XX" where XX is the device number of the camera, and last for 5 days I believe). A scenario will hopefully be automatically generated.<br />
# Rinse and repeat for any additional cameras.<br />
<br />
''Note: After reloading/restarting the Core, motion dies. It has to be restarted manually by doing a "sudo /etc/init.d/motion restart". This issue has been reported http://svn.linuxmce.org/trac.cgi/ticket/904 & http://svn.linuxmce.org/trac.cgi/ticket/715 (not sure whether these two tickets are reporting the same issue or not).''<br />
<br />
''Note: Very occasionally I have found that the scenarios sometimes require a little bit of tinkering. If there is no image displayed when you click on the scenario, look in Wizard -> Scenarios -> Security scenarios, select the scenario in question, select "Advanced Wizard" in the "Edit scenario using wizard" section, and just check that the number in the "#2 PK_Device (int)" field corresponds with the camera's Device #; if it does not, change it. Reload & Regen.''</div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Foscam_FI8905W&diff=34146Foscam FI8905W2013-05-26T10:33:50Z<p>Daballiemo: Undo revision 34145 by Daballiemo (talk)</p>
<hr />
<div>[[Category: Hardware]]<br />
{{versioninfo|810Status=Works|810UpdatedDate=8th Jan 2011|810UpdatedBy=purps}{versioninfo|1004Status=Works|1004UpdatedDate=27th May 2013|810UpdatedBy=Daballiemo}}<br />
[[Category: Cameras]]<br />
[[category: IP Cameras]]<br />
<br />
{| align="right"<br />
| __TOC__<br />
|}<br />
<br />
<br />
<br />
=Features=<br />
<br />
* High image & video quality<br />
* High-sensitivity 1/4" CMOS sensor (300k pixels)<br />
* Auto IR-LED night vision up to 30m (90ft)<br />
* Motion detection alert via email or upload image to a specified FTP server<br />
* Support a wide range of browsers(IE, Firefox, Google)<br />
* Support remote viewing & record<br />
* Support image fullsceen and image snapshot<br />
* Multi-level users management and passwords definition<br />
* Embeded Web Server<br />
* WEP,WPA and WPA2 Encryption<br />
* Optimized MJPEG video compression for transmission<br />
* Support wireless network (WiFi/802.11/b/g)mobile<br />
* Supporting Dynamic IP (DDNS)and UPnP LAN and Internet(ADSL,Cable Modem)<br />
* Multi-Protocol support and Transportation (such as TCP/IP, SMTP and HTTP)<br />
* Support Mobile Phone View such as Iphone & Smart phone (It should support midp2.0 java mobile or pda mobile)<br />
* Simple to setup, Friendly GUI, DIY installation<br />
* Aluminum Alloy Design, Shell Vandal-Proof,IP65 Waterproof and weatherproof for Any Harsh Environment<br />
<br />
''Note: This is the 60 IR LED version; the FI8904W has fewer LEDs (24 to be exact) which means that it is not as good when it comes to night vision. The FI8904W is easily identifiable (apart from the number of LEDs!) by the shape of its cowling. The cowling of the FI8905W is 'U'-shaped in cross-section, whereas the cowling of the FI8904W is what I would call 'Micky Mouse'-shaped. One more thing; be careful of the cheap clones on eBay, you don't want to end up with one of those.''<br />
<br />
=Quick review=<br />
This camera does have quite a mean zoom lens on it (12 mm). Also the colours are a bit odd; greens and browns appear purple for example. That said, it is by far the cheapest outdoor IP camera around, and I think that it is very good for the price.<br />
<br />
Do also bear in mind [http://forum.linuxmce.org/index.php?topic=10134.0 this issue].<br />
<br />
=Installation=<br />
<br />
''Note: This section describes how to install this camera as a [[Motion]] device; I have not tried installing it as a standalone viewer, but I see no reason why it wouldn't work.''<br />
<br />
<span style="color:red">'''''Note: These instructions are for when the camera is plugged into a router on the external network, as opposed to a switch on the internal network. Not ideal, but this was necessary due to [http://forum.linuxmce.org/index.php?topic=10134.0 this aforementioned problem].'''''</span><br />
<br />
# Install motion wrapper (if it is not installed already). In web admin (type "192.168.80.1" in a browser of your choice), click "Show devices tree" (very bottom of left-hand pane), click on "CORE" and then "Create Child Device". Then press the "Pick device template" button, which will open a new window. Under "Device Template", select "Motion Wrapper" and press the "Pick device template" button.<br />
# Plug in your camera (remember I plug my Foscams into the external router). In your router admin page (accessed via 192.168.1.1 in my case) find out what IP was assigned. Type this IP into your browser and have a look through the options. Set the username and password. I also set a static IP just in case it tries to change itself. I also specified the camera's IP on my router so that no other device could take it (belt and braces).<br />
# In LMCE web admin (type "192.168.80.1" into a browser), go to Wizard -> Devices -> Surveillance Cameras. Click the "Add Device" button at the bottom of the page. This will open a new window.<br />
# In the window that just opened, pick "Generic Motion IP Camera" from the dropdown menu in the "Device Template" section. Press the "Pick device template" button.<br />
# Find the new camera in Wizard -> Devices -> Surveillance Cameras. Change the description to something more obvious if you so wish and select a room.<br />
# Change the "Path" field to "http://192.168.1.XXX/videostream.cgi?user=XXXXX&pwd=XXXXXX", not forgetting to replace the Xs with the IP, username and password. Add "80" to the "TCP Port" field. ''Press "Save".''<br />
# Press the "Advanced" button. Check the IP and MAC address in the "Device Info" section. If it's not the same as the MAC and IP you saw in the Panasonic utility, change it. ''Click "Save".''<br />
# Do a Reload & Regen, and that should be it. This camera can now be used as a motion sensor and/or for recording movement (recordings are stored in "/home/cameras/XX" where XX is the device number of the camera, and last for 5 days I believe). A scenario will hopefully be automatically generated.<br />
# Rinse and repeat for any additional cameras.<br />
<br />
''Note: After reloading/restarting the Core, motion dies. It has to be restarted manually by doing a "sudo /etc/init.d/motion restart". This issue has been reported http://svn.linuxmce.org/trac.cgi/ticket/904 & http://svn.linuxmce.org/trac.cgi/ticket/715 (not sure whether these two tickets are reporting the same issue or not).''<br />
<br />
''Note: Very occasionally I have found that the scenarios sometimes require a little bit of tinkering. If there is no image displayed when you click on the scenario, look in Wizard -> Scenarios -> Security scenarios, select the scenario in question, select "Advanced Wizard" in the "Edit scenario using wizard" section, and just check that the number in the "#2 PK_Device (int)" field corresponds with the camera's Device #; if it does not, change it. Reload & Regen.''</div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Foscam_FI8905W&diff=34145Foscam FI8905W2013-05-26T10:33:37Z<p>Daballiemo: Undo revision 34144 by Daballiemo (talk)</p>
<hr />
<div>[[Category: Hardware]]<br />
{{versioninfo|810Status=Works|810UpdatedDate=8th Jan 2011|810UpdatedBy=purps}}<br />
{{versioninfo|1004Status=Works|1004UpdatedDate=27th May 2013|810UpdatedBy=Daballiemo}}<br />
[[Category: Cameras]]<br />
[[category: IP Cameras]]<br />
<br />
{| align="right"<br />
| __TOC__<br />
|}<br />
<br />
<br />
<br />
=Features=<br />
<br />
* High image & video quality<br />
* High-sensitivity 1/4" CMOS sensor (300k pixels)<br />
* Auto IR-LED night vision up to 30m (90ft)<br />
* Motion detection alert via email or upload image to a specified FTP server<br />
* Support a wide range of browsers(IE, Firefox, Google)<br />
* Support remote viewing & record<br />
* Support image fullsceen and image snapshot<br />
* Multi-level users management and passwords definition<br />
* Embeded Web Server<br />
* WEP,WPA and WPA2 Encryption<br />
* Optimized MJPEG video compression for transmission<br />
* Support wireless network (WiFi/802.11/b/g)mobile<br />
* Supporting Dynamic IP (DDNS)and UPnP LAN and Internet(ADSL,Cable Modem)<br />
* Multi-Protocol support and Transportation (such as TCP/IP, SMTP and HTTP)<br />
* Support Mobile Phone View such as Iphone & Smart phone (It should support midp2.0 java mobile or pda mobile)<br />
* Simple to setup, Friendly GUI, DIY installation<br />
* Aluminum Alloy Design, Shell Vandal-Proof,IP65 Waterproof and weatherproof for Any Harsh Environment<br />
<br />
''Note: This is the 60 IR LED version; the FI8904W has fewer LEDs (24 to be exact) which means that it is not as good when it comes to night vision. The FI8904W is easily identifiable (apart from the number of LEDs!) by the shape of its cowling. The cowling of the FI8905W is 'U'-shaped in cross-section, whereas the cowling of the FI8904W is what I would call 'Micky Mouse'-shaped. One more thing; be careful of the cheap clones on eBay, you don't want to end up with one of those.''<br />
<br />
=Quick review=<br />
This camera does have quite a mean zoom lens on it (12 mm). Also the colours are a bit odd; greens and browns appear purple for example. That said, it is by far the cheapest outdoor IP camera around, and I think that it is very good for the price.<br />
<br />
Do also bear in mind [http://forum.linuxmce.org/index.php?topic=10134.0 this issue].<br />
<br />
=Installation=<br />
<br />
''Note: This section describes how to install this camera as a [[Motion]] device; I have not tried installing it as a standalone viewer, but I see no reason why it wouldn't work.''<br />
<br />
<span style="color:red">'''''Note: These instructions are for when the camera is plugged into a router on the external network, as opposed to a switch on the internal network. Not ideal, but this was necessary due to [http://forum.linuxmce.org/index.php?topic=10134.0 this aforementioned problem].'''''</span><br />
<br />
# Install motion wrapper (if it is not installed already). In web admin (type "192.168.80.1" in a browser of your choice), click "Show devices tree" (very bottom of left-hand pane), click on "CORE" and then "Create Child Device". Then press the "Pick device template" button, which will open a new window. Under "Device Template", select "Motion Wrapper" and press the "Pick device template" button.<br />
# Plug in your camera (remember I plug my Foscams into the external router). In your router admin page (accessed via 192.168.1.1 in my case) find out what IP was assigned. Type this IP into your browser and have a look through the options. Set the username and password. I also set a static IP just in case it tries to change itself. I also specified the camera's IP on my router so that no other device could take it (belt and braces).<br />
# In LMCE web admin (type "192.168.80.1" into a browser), go to Wizard -> Devices -> Surveillance Cameras. Click the "Add Device" button at the bottom of the page. This will open a new window.<br />
# In the window that just opened, pick "Generic Motion IP Camera" from the dropdown menu in the "Device Template" section. Press the "Pick device template" button.<br />
# Find the new camera in Wizard -> Devices -> Surveillance Cameras. Change the description to something more obvious if you so wish and select a room.<br />
# Change the "Path" field to "http://192.168.1.XXX/videostream.cgi?user=XXXXX&pwd=XXXXXX", not forgetting to replace the Xs with the IP, username and password. Add "80" to the "TCP Port" field. ''Press "Save".''<br />
# Press the "Advanced" button. Check the IP and MAC address in the "Device Info" section. If it's not the same as the MAC and IP you saw in the Panasonic utility, change it. ''Click "Save".''<br />
# Do a Reload & Regen, and that should be it. This camera can now be used as a motion sensor and/or for recording movement (recordings are stored in "/home/cameras/XX" where XX is the device number of the camera, and last for 5 days I believe). A scenario will hopefully be automatically generated.<br />
# Rinse and repeat for any additional cameras.<br />
<br />
''Note: After reloading/restarting the Core, motion dies. It has to be restarted manually by doing a "sudo /etc/init.d/motion restart". This issue has been reported http://svn.linuxmce.org/trac.cgi/ticket/904 & http://svn.linuxmce.org/trac.cgi/ticket/715 (not sure whether these two tickets are reporting the same issue or not).''<br />
<br />
''Note: Very occasionally I have found that the scenarios sometimes require a little bit of tinkering. If there is no image displayed when you click on the scenario, look in Wizard -> Scenarios -> Security scenarios, select the scenario in question, select "Advanced Wizard" in the "Edit scenario using wizard" section, and just check that the number in the "#2 PK_Device (int)" field corresponds with the camera's Device #; if it does not, change it. Reload & Regen.''</div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Foscam_FI8905W&diff=34144Foscam FI8905W2013-05-26T10:32:44Z<p>Daballiemo: </p>
<hr />
<div>[[Category: Hardware]]<br />
{{versioninfo|810Status=Works|810UpdatedDate=8th Jan 2011|810UpdatedBy=purps}{versioninfo|1004Status=Works|1004UpdatedDate=27th May 2013|810UpdatedBy=Daballiemo}}<br />
[[Category: Cameras]]<br />
[[category: IP Cameras]]<br />
<br />
{| align="right"<br />
| __TOC__<br />
|}<br />
<br />
<br />
<br />
=Features=<br />
<br />
* High image & video quality<br />
* High-sensitivity 1/4" CMOS sensor (300k pixels)<br />
* Auto IR-LED night vision up to 30m (90ft)<br />
* Motion detection alert via email or upload image to a specified FTP server<br />
* Support a wide range of browsers(IE, Firefox, Google)<br />
* Support remote viewing & record<br />
* Support image fullsceen and image snapshot<br />
* Multi-level users management and passwords definition<br />
* Embeded Web Server<br />
* WEP,WPA and WPA2 Encryption<br />
* Optimized MJPEG video compression for transmission<br />
* Support wireless network (WiFi/802.11/b/g)mobile<br />
* Supporting Dynamic IP (DDNS)and UPnP LAN and Internet(ADSL,Cable Modem)<br />
* Multi-Protocol support and Transportation (such as TCP/IP, SMTP and HTTP)<br />
* Support Mobile Phone View such as Iphone & Smart phone (It should support midp2.0 java mobile or pda mobile)<br />
* Simple to setup, Friendly GUI, DIY installation<br />
* Aluminum Alloy Design, Shell Vandal-Proof,IP65 Waterproof and weatherproof for Any Harsh Environment<br />
<br />
''Note: This is the 60 IR LED version; the FI8904W has fewer LEDs (24 to be exact) which means that it is not as good when it comes to night vision. The FI8904W is easily identifiable (apart from the number of LEDs!) by the shape of its cowling. The cowling of the FI8905W is 'U'-shaped in cross-section, whereas the cowling of the FI8904W is what I would call 'Micky Mouse'-shaped. One more thing; be careful of the cheap clones on eBay, you don't want to end up with one of those.''<br />
<br />
=Quick review=<br />
This camera does have quite a mean zoom lens on it (12 mm). Also the colours are a bit odd; greens and browns appear purple for example. That said, it is by far the cheapest outdoor IP camera around, and I think that it is very good for the price.<br />
<br />
Do also bear in mind [http://forum.linuxmce.org/index.php?topic=10134.0 this issue].<br />
<br />
=Installation=<br />
<br />
''Note: This section describes how to install this camera as a [[Motion]] device; I have not tried installing it as a standalone viewer, but I see no reason why it wouldn't work.''<br />
<br />
<span style="color:red">'''''Note: These instructions are for when the camera is plugged into a router on the external network, as opposed to a switch on the internal network. Not ideal, but this was necessary due to [http://forum.linuxmce.org/index.php?topic=10134.0 this aforementioned problem].'''''</span><br />
<br />
# Install motion wrapper (if it is not installed already). In web admin (type "192.168.80.1" in a browser of your choice), click "Show devices tree" (very bottom of left-hand pane), click on "CORE" and then "Create Child Device". Then press the "Pick device template" button, which will open a new window. Under "Device Template", select "Motion Wrapper" and press the "Pick device template" button.<br />
# Plug in your camera (remember I plug my Foscams into the external router). In your router admin page (accessed via 192.168.1.1 in my case) find out what IP was assigned. Type this IP into your browser and have a look through the options. Set the username and password. I also set a static IP just in case it tries to change itself. I also specified the camera's IP on my router so that no other device could take it (belt and braces).<br />
# In LMCE web admin (type "192.168.80.1" into a browser), go to Wizard -> Devices -> Surveillance Cameras. Click the "Add Device" button at the bottom of the page. This will open a new window.<br />
# In the window that just opened, pick "Generic Motion IP Camera" from the dropdown menu in the "Device Template" section. Press the "Pick device template" button.<br />
# Find the new camera in Wizard -> Devices -> Surveillance Cameras. Change the description to something more obvious if you so wish and select a room.<br />
# Change the "Path" field to "http://192.168.1.XXX/videostream.cgi?user=XXXXX&pwd=XXXXXX", not forgetting to replace the Xs with the IP, username and password. Add "80" to the "TCP Port" field. ''Press "Save".''<br />
# Press the "Advanced" button. Check the IP and MAC address in the "Device Info" section. If it's not the same as the MAC and IP you saw in the Panasonic utility, change it. ''Click "Save".''<br />
# Do a Reload & Regen, and that should be it. This camera can now be used as a motion sensor and/or for recording movement (recordings are stored in "/home/cameras/XX" where XX is the device number of the camera, and last for 5 days I believe). A scenario will hopefully be automatically generated.<br />
# Rinse and repeat for any additional cameras.<br />
<br />
''Note: After reloading/restarting the Core, motion dies. It has to be restarted manually by doing a "sudo /etc/init.d/motion restart". This issue has been reported http://svn.linuxmce.org/trac.cgi/ticket/904 & http://svn.linuxmce.org/trac.cgi/ticket/715 (not sure whether these two tickets are reporting the same issue or not).''<br />
<br />
''Note: Very occasionally I have found that the scenarios sometimes require a little bit of tinkering. If there is no image displayed when you click on the scenario, look in Wizard -> Scenarios -> Security scenarios, select the scenario in question, select "Advanced Wizard" in the "Edit scenario using wizard" section, and just check that the number in the "#2 PK_Device (int)" field corresponds with the camera's Device #; if it does not, change it. Reload & Regen.''</div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Foscam_FI8905W&diff=34143Foscam FI8905W2013-05-26T10:32:01Z<p>Daballiemo: Works under 1004</p>
<hr />
<div>[[Category: Hardware]]<br />
{{versioninfo|810Status=Works|810UpdatedDate=8th Jan 2011|810UpdatedBy=purps}}<br />
{{versioninfo|1004Status=Works|1004UpdatedDate=27th May 2013|810UpdatedBy=Daballiemo}}<br />
[[Category: Cameras]]<br />
[[category: IP Cameras]]<br />
<br />
{| align="right"<br />
| __TOC__<br />
|}<br />
<br />
<br />
<br />
=Features=<br />
<br />
* High image & video quality<br />
* High-sensitivity 1/4" CMOS sensor (300k pixels)<br />
* Auto IR-LED night vision up to 30m (90ft)<br />
* Motion detection alert via email or upload image to a specified FTP server<br />
* Support a wide range of browsers(IE, Firefox, Google)<br />
* Support remote viewing & record<br />
* Support image fullsceen and image snapshot<br />
* Multi-level users management and passwords definition<br />
* Embeded Web Server<br />
* WEP,WPA and WPA2 Encryption<br />
* Optimized MJPEG video compression for transmission<br />
* Support wireless network (WiFi/802.11/b/g)mobile<br />
* Supporting Dynamic IP (DDNS)and UPnP LAN and Internet(ADSL,Cable Modem)<br />
* Multi-Protocol support and Transportation (such as TCP/IP, SMTP and HTTP)<br />
* Support Mobile Phone View such as Iphone & Smart phone (It should support midp2.0 java mobile or pda mobile)<br />
* Simple to setup, Friendly GUI, DIY installation<br />
* Aluminum Alloy Design, Shell Vandal-Proof,IP65 Waterproof and weatherproof for Any Harsh Environment<br />
<br />
''Note: This is the 60 IR LED version; the FI8904W has fewer LEDs (24 to be exact) which means that it is not as good when it comes to night vision. The FI8904W is easily identifiable (apart from the number of LEDs!) by the shape of its cowling. The cowling of the FI8905W is 'U'-shaped in cross-section, whereas the cowling of the FI8904W is what I would call 'Micky Mouse'-shaped. One more thing; be careful of the cheap clones on eBay, you don't want to end up with one of those.''<br />
<br />
=Quick review=<br />
This camera does have quite a mean zoom lens on it (12 mm). Also the colours are a bit odd; greens and browns appear purple for example. That said, it is by far the cheapest outdoor IP camera around, and I think that it is very good for the price.<br />
<br />
Do also bear in mind [http://forum.linuxmce.org/index.php?topic=10134.0 this issue].<br />
<br />
=Installation=<br />
<br />
''Note: This section describes how to install this camera as a [[Motion]] device; I have not tried installing it as a standalone viewer, but I see no reason why it wouldn't work.''<br />
<br />
<span style="color:red">'''''Note: These instructions are for when the camera is plugged into a router on the external network, as opposed to a switch on the internal network. Not ideal, but this was necessary due to [http://forum.linuxmce.org/index.php?topic=10134.0 this aforementioned problem].'''''</span><br />
<br />
# Install motion wrapper (if it is not installed already). In web admin (type "192.168.80.1" in a browser of your choice), click "Show devices tree" (very bottom of left-hand pane), click on "CORE" and then "Create Child Device". Then press the "Pick device template" button, which will open a new window. Under "Device Template", select "Motion Wrapper" and press the "Pick device template" button.<br />
# Plug in your camera (remember I plug my Foscams into the external router). In your router admin page (accessed via 192.168.1.1 in my case) find out what IP was assigned. Type this IP into your browser and have a look through the options. Set the username and password. I also set a static IP just in case it tries to change itself. I also specified the camera's IP on my router so that no other device could take it (belt and braces).<br />
# In LMCE web admin (type "192.168.80.1" into a browser), go to Wizard -> Devices -> Surveillance Cameras. Click the "Add Device" button at the bottom of the page. This will open a new window.<br />
# In the window that just opened, pick "Generic Motion IP Camera" from the dropdown menu in the "Device Template" section. Press the "Pick device template" button.<br />
# Find the new camera in Wizard -> Devices -> Surveillance Cameras. Change the description to something more obvious if you so wish and select a room.<br />
# Change the "Path" field to "http://192.168.1.XXX/videostream.cgi?user=XXXXX&pwd=XXXXXX", not forgetting to replace the Xs with the IP, username and password. Add "80" to the "TCP Port" field. ''Press "Save".''<br />
# Press the "Advanced" button. Check the IP and MAC address in the "Device Info" section. If it's not the same as the MAC and IP you saw in the Panasonic utility, change it. ''Click "Save".''<br />
# Do a Reload & Regen, and that should be it. This camera can now be used as a motion sensor and/or for recording movement (recordings are stored in "/home/cameras/XX" where XX is the device number of the camera, and last for 5 days I believe). A scenario will hopefully be automatically generated.<br />
# Rinse and repeat for any additional cameras.<br />
<br />
''Note: After reloading/restarting the Core, motion dies. It has to be restarted manually by doing a "sudo /etc/init.d/motion restart". This issue has been reported http://svn.linuxmce.org/trac.cgi/ticket/904 & http://svn.linuxmce.org/trac.cgi/ticket/715 (not sure whether these two tickets are reporting the same issue or not).''<br />
<br />
''Note: Very occasionally I have found that the scenarios sometimes require a little bit of tinkering. If there is no image displayed when you click on the scenario, look in Wizard -> Scenarios -> Security scenarios, select the scenario in question, select "Advanced Wizard" in the "Edit scenario using wizard" section, and just check that the number in the "#2 PK_Device (int)" field corresponds with the camera's Device #; if it does not, change it. Reload & Regen.''</div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=33421Implementing Arduino using Generic Serial Device2012-11-21T20:03:17Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|810Status=Working|810UpdatedDate=14th November 2012|810UpdatedBy=Daballiemo|1004Status=Working|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiemo}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'R' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'R' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [3] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [3] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
</pre><br />
<br />
'''#373 Private Method Listing '''<br />
<br />
<pre><br />
<br />
def log(line)<br />
# This function logs a line to the log file of the device<br />
log = File.open("/var/log/pluto/" + device_.devid_.to_s + "_Generic_Serial_Device.log", "a")<br />
log.puts Time.now.to_s + " (Ruby script):" + line.to_s<br />
log.close<br />
end<br />
<br />
</pre><br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
// * with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
// * (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
boolean debug = true; //debug over serial?<br />
int RedMemory = 255;<br />
int GreenMemory = 255;<br />
int BlueMemory = 255;<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (9, OUTPUT);<br />
pinMode (8, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
pinMode (3, OUTPUT);<br />
<br />
<br />
}<br />
<br />
<br />
//********************************************************************************************************************************<br />
//<br />
// Commandstructure<br />
//<br />
// A - BBB - CCC - DDD - EEE<br />
// <br />
// P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
// 1 (ON)<br />
// D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
//<br />
// C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
//<br />
// R(elative) pin number (3) 0-100<br />
//<br />
// RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
// GreenPin = 5; <br />
// BluePin = 6; <br />
// OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
// WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
//<br />
// DaBalliemo september 2012<br />
//<br />
//*********************************************************************************************************************************<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.print("Command given: ");<br />
Serial.println(packetBuffer);<br />
Serial.print("Buffersize: ");<br />
Serial.println(packetSize);<br />
Serial.println("***************************************");<br />
}<br />
<br />
//############################## Power Section #################################################################################<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Px-0" or "Px-1". The first turn on a pin<br />
//(in this case: pin x). The second just turn off the pin x.<br />
<br />
if (packetBuffer[0] == 'P' ) { // Px-1 or Px-0<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Power command given");<br />
Serial.println("");<br />
Serial.print("Pin number is: ");<br />
Serial.println(pin_char);<br />
Serial.print(" Command given is: ");<br />
Serial.println(led_state_char);<br />
Serial.println("***************************************");<br />
} <br />
<br />
int pin = atoi(pin_char); //our pin number<br />
<br />
if (atoi(led_state_char) == 1) { //if we send 'Px-1'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-1"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (atoi(led_state_char) == 0) { //if we send 'Px-0'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-0";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
//############################## Dim Section #################################################################################<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Dim command given");<br />
Serial.print("dim pin: ");<br />
Serial.println(dim_pin);<br />
Serial.print("dim level: ");<br />
Serial.println(dim);<br />
Serial.println("***************************************");<br />
}<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
//############################## Relative Dim Section #################################################################################<br />
//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<br />
//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<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
if (packetBuffer[0] == 'R') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
int DimRed;<br />
int DimGreen;<br />
int DimBlue;<br />
float f;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "R";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
<br />
DimRed = int(RedMemory*float(dim)/100); //here we have the dim number: <br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin;<br />
analogWrite(dim_pin, DimRed);<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Relative Dim command given");<br />
Serial.print("Red pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(RedMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimRed);<br />
} <br />
<br />
DimGreen = int(GreenMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, DimGreen);<br />
<br />
if (debug) {<br />
Serial.print("Green pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(GreenMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimGreen);<br />
} <br />
<br />
DimBlue = int(BlueMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, DimBlue);<br />
<br />
if (debug) {<br />
Serial.print("Blue pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(BlueMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimBlue); <br />
Serial.println("***************************************");<br />
} <br />
<br />
}<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) {<br />
Serial.println("***************************************"); <br />
Serial.println("RGB command given");<br />
Serial.print("Redpin: "); <br />
Serial.println(Red_pin_char);<br />
Serial.print("Red:"); <br />
Serial.print(Red_char); <br />
Serial.print(" Green:"); <br />
Serial.print(Green_char); <br />
Serial.print(" Blue:"); <br />
Serial.println(Blue_char);<br />
} <br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
RedMemory = dim; // Make sure we remember<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
GreenMemory = dim;<br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, dim);<br />
BlueMemory = dim;<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
}<br />
}<br />
<br />
}<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=33420Implementing Arduino using Generic Serial Device2012-11-21T20:02:13Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|0810Status=Working|0810UpdatedDate=14th November 2012|0810UpdatedBy=Daballiemo|1004Status=Working|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiemo}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'R' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'R' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [3] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [3] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
</pre><br />
<br />
'''#373 Private Method Listing '''<br />
<br />
<pre><br />
<br />
def log(line)<br />
# This function logs a line to the log file of the device<br />
log = File.open("/var/log/pluto/" + device_.devid_.to_s + "_Generic_Serial_Device.log", "a")<br />
log.puts Time.now.to_s + " (Ruby script):" + line.to_s<br />
log.close<br />
end<br />
<br />
</pre><br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
// * with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
// * (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
boolean debug = true; //debug over serial?<br />
int RedMemory = 255;<br />
int GreenMemory = 255;<br />
int BlueMemory = 255;<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (9, OUTPUT);<br />
pinMode (8, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
pinMode (3, OUTPUT);<br />
<br />
<br />
}<br />
<br />
<br />
//********************************************************************************************************************************<br />
//<br />
// Commandstructure<br />
//<br />
// A - BBB - CCC - DDD - EEE<br />
// <br />
// P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
// 1 (ON)<br />
// D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
//<br />
// C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
//<br />
// R(elative) pin number (3) 0-100<br />
//<br />
// RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
// GreenPin = 5; <br />
// BluePin = 6; <br />
// OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
// WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
//<br />
// DaBalliemo september 2012<br />
//<br />
//*********************************************************************************************************************************<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.print("Command given: ");<br />
Serial.println(packetBuffer);<br />
Serial.print("Buffersize: ");<br />
Serial.println(packetSize);<br />
Serial.println("***************************************");<br />
}<br />
<br />
//############################## Power Section #################################################################################<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Px-0" or "Px-1". The first turn on a pin<br />
//(in this case: pin x). The second just turn off the pin x.<br />
<br />
if (packetBuffer[0] == 'P' ) { // Px-1 or Px-0<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Power command given");<br />
Serial.println("");<br />
Serial.print("Pin number is: ");<br />
Serial.println(pin_char);<br />
Serial.print(" Command given is: ");<br />
Serial.println(led_state_char);<br />
Serial.println("***************************************");<br />
} <br />
<br />
int pin = atoi(pin_char); //our pin number<br />
<br />
if (atoi(led_state_char) == 1) { //if we send 'Px-1'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-1"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (atoi(led_state_char) == 0) { //if we send 'Px-0'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-0";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
//############################## Dim Section #################################################################################<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Dim command given");<br />
Serial.print("dim pin: ");<br />
Serial.println(dim_pin);<br />
Serial.print("dim level: ");<br />
Serial.println(dim);<br />
Serial.println("***************************************");<br />
}<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
//############################## Relative Dim Section #################################################################################<br />
//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<br />
//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<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
if (packetBuffer[0] == 'R') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
int DimRed;<br />
int DimGreen;<br />
int DimBlue;<br />
float f;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "R";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
<br />
DimRed = int(RedMemory*float(dim)/100); //here we have the dim number: <br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin;<br />
analogWrite(dim_pin, DimRed);<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Relative Dim command given");<br />
Serial.print("Red pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(RedMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimRed);<br />
} <br />
<br />
DimGreen = int(GreenMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, DimGreen);<br />
<br />
if (debug) {<br />
Serial.print("Green pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(GreenMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimGreen);<br />
} <br />
<br />
DimBlue = int(BlueMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, DimBlue);<br />
<br />
if (debug) {<br />
Serial.print("Blue pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(BlueMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimBlue); <br />
Serial.println("***************************************");<br />
} <br />
<br />
}<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) {<br />
Serial.println("***************************************"); <br />
Serial.println("RGB command given");<br />
Serial.print("Redpin: "); <br />
Serial.println(Red_pin_char);<br />
Serial.print("Red:"); <br />
Serial.print(Red_char); <br />
Serial.print(" Green:"); <br />
Serial.print(Green_char); <br />
Serial.print(" Blue:"); <br />
Serial.println(Blue_char);<br />
} <br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
RedMemory = dim; // Make sure we remember<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
GreenMemory = dim;<br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, dim);<br />
BlueMemory = dim;<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
}<br />
}<br />
<br />
}<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32191Implementing Arduino using Generic Serial Device2012-09-15T18:52:56Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiemo}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'R' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'R' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [3] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [3] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
</pre><br />
<br />
'''#373 Private Method Listing '''<br />
<br />
<pre><br />
<br />
def log(line)<br />
# This function logs a line to the log file of the device<br />
log = File.open("/var/log/pluto/" + device_.devid_.to_s + "_Generic_Serial_Device.log", "a")<br />
log.puts Time.now.to_s + " (Ruby script):" + line.to_s<br />
log.close<br />
end<br />
<br />
</pre><br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
// * with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
// * (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
boolean debug = true; //debug over serial?<br />
int RedMemory = 255;<br />
int GreenMemory = 255;<br />
int BlueMemory = 255;<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (9, OUTPUT);<br />
pinMode (8, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
pinMode (3, OUTPUT);<br />
<br />
<br />
}<br />
<br />
<br />
//********************************************************************************************************************************<br />
//<br />
// Commandstructure<br />
//<br />
// A - BBB - CCC - DDD - EEE<br />
// <br />
// P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
// 1 (ON)<br />
// D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
//<br />
// C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
//<br />
// R(elative) pin number (3) 0-100<br />
//<br />
// RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
// GreenPin = 5; <br />
// BluePin = 6; <br />
// OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
// WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
//<br />
// DaBalliemo september 2012<br />
//<br />
//*********************************************************************************************************************************<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.print("Command given: ");<br />
Serial.println(packetBuffer);<br />
Serial.print("Buffersize: ");<br />
Serial.println(packetSize);<br />
Serial.println("***************************************");<br />
}<br />
<br />
//############################## Power Section #################################################################################<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Px-0" or "Px-1". The first turn on a pin<br />
//(in this case: pin x). The second just turn off the pin x.<br />
<br />
if (packetBuffer[0] == 'P' ) { // Px-1 or Px-0<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Power command given");<br />
Serial.println("");<br />
Serial.print("Pin number is: ");<br />
Serial.println(pin_char);<br />
Serial.print(" Command given is: ");<br />
Serial.println(led_state_char);<br />
Serial.println("***************************************");<br />
} <br />
<br />
int pin = atoi(pin_char); //our pin number<br />
<br />
if (atoi(led_state_char) == 1) { //if we send 'Px-1'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-1"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (atoi(led_state_char) == 0) { //if we send 'Px-0'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-0";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
//############################## Dim Section #################################################################################<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Dim command given");<br />
Serial.print("dim pin: ");<br />
Serial.println(dim_pin);<br />
Serial.print("dim level: ");<br />
Serial.println(dim);<br />
Serial.println("***************************************");<br />
}<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
//############################## Relative Dim Section #################################################################################<br />
//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<br />
//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<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
if (packetBuffer[0] == 'R') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
int DimRed;<br />
int DimGreen;<br />
int DimBlue;<br />
float f;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "R";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
<br />
DimRed = int(RedMemory*float(dim)/100); //here we have the dim number: <br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin;<br />
analogWrite(dim_pin, DimRed);<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Relative Dim command given");<br />
Serial.print("Red pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(RedMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimRed);<br />
} <br />
<br />
DimGreen = int(GreenMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, DimGreen);<br />
<br />
if (debug) {<br />
Serial.print("Green pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(GreenMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimGreen);<br />
} <br />
<br />
DimBlue = int(BlueMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, DimBlue);<br />
<br />
if (debug) {<br />
Serial.print("Blue pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(BlueMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimBlue); <br />
Serial.println("***************************************");<br />
} <br />
<br />
}<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) {<br />
Serial.println("***************************************"); <br />
Serial.println("RGB command given");<br />
Serial.print("Redpin: "); <br />
Serial.println(Red_pin_char);<br />
Serial.print("Red:"); <br />
Serial.print(Red_char); <br />
Serial.print(" Green:"); <br />
Serial.print(Green_char); <br />
Serial.print(" Blue:"); <br />
Serial.println(Blue_char);<br />
} <br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
RedMemory = dim; // Make sure we remember<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
GreenMemory = dim;<br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, dim);<br />
BlueMemory = dim;<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
}<br />
}<br />
<br />
}<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32190Implementing Arduino using Generic Serial Device2012-09-15T17:38:15Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'R' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'R' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [3] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [3] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
</pre><br />
<br />
'''#373 Private Method Listing '''<br />
<br />
<pre><br />
<br />
def log(line)<br />
# This function logs a line to the log file of the device<br />
log = File.open("/var/log/pluto/" + device_.devid_.to_s + "_Generic_Serial_Device.log", "a")<br />
log.puts Time.now.to_s + " (Ruby script):" + line.to_s<br />
log.close<br />
end<br />
<br />
</pre><br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
// * with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
// * (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
boolean debug = true; //debug over serial?<br />
int RedMemory = 255;<br />
int GreenMemory = 255;<br />
int BlueMemory = 255;<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (9, OUTPUT);<br />
pinMode (8, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
pinMode (3, OUTPUT);<br />
<br />
<br />
}<br />
<br />
<br />
//********************************************************************************************************************************<br />
//<br />
// Commandstructure<br />
//<br />
// A - BBB - CCC - DDD - EEE<br />
// <br />
// P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
// 1 (ON)<br />
// D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
//<br />
// C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
//<br />
// R(elative) pin number (3) 0-100<br />
//<br />
// RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
// GreenPin = 5; <br />
// BluePin = 6; <br />
// OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
// WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
//<br />
// DaBalliemo september 2012<br />
//<br />
//*********************************************************************************************************************************<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.print("Command given: ");<br />
Serial.println(packetBuffer);<br />
Serial.print("Buffersize: ");<br />
Serial.println(packetSize);<br />
Serial.println("***************************************");<br />
}<br />
<br />
//############################## Power Section #################################################################################<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Px-0" or "Px-1". The first turn on a pin<br />
//(in this case: pin x). The second just turn off the pin x.<br />
<br />
if (packetBuffer[0] == 'P' ) { // Px-1 or Px-0<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Power command given");<br />
Serial.println("");<br />
Serial.print("Pin number is: ");<br />
Serial.println(pin_char);<br />
Serial.print(" Command given is: ");<br />
Serial.println(led_state_char);<br />
Serial.println("***************************************");<br />
} <br />
<br />
int pin = atoi(pin_char); //our pin number<br />
<br />
if (atoi(led_state_char) == 1) { //if we send 'Px-1'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-1"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (atoi(led_state_char) == 0) { //if we send 'Px-0'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-0";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
//############################## Dim Section #################################################################################<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Dim command given");<br />
Serial.print("dim pin: ");<br />
Serial.println(dim_pin);<br />
Serial.print("dim level: ");<br />
Serial.println(dim);<br />
Serial.println("***************************************");<br />
}<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
//############################## Relative Dim Section #################################################################################<br />
//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<br />
//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<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
if (packetBuffer[0] == 'R') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
int DimRed;<br />
int DimGreen;<br />
int DimBlue;<br />
float f;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "R";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
<br />
DimRed = int(RedMemory*float(dim)/100); //here we have the dim number: <br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin;<br />
analogWrite(dim_pin, DimRed);<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Relative Dim command given");<br />
Serial.print("Red pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(RedMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimRed);<br />
} <br />
<br />
DimGreen = int(GreenMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, DimGreen);<br />
<br />
if (debug) {<br />
Serial.print("Green pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(GreenMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimGreen);<br />
} <br />
<br />
DimBlue = int(BlueMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, DimBlue);<br />
<br />
if (debug) {<br />
Serial.print("Blue pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(BlueMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimBlue); <br />
Serial.println("***************************************");<br />
} <br />
<br />
}<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) {<br />
Serial.println("***************************************"); <br />
Serial.println("RGB command given");<br />
Serial.print("Redpin: "); <br />
Serial.println(Red_pin_char);<br />
Serial.print("Red:"); <br />
Serial.print(Red_char); <br />
Serial.print(" Green:"); <br />
Serial.print(Green_char); <br />
Serial.print(" Blue:"); <br />
Serial.println(Blue_char);<br />
} <br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
RedMemory = dim; // Make sure we remember<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
GreenMemory = dim;<br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, dim);<br />
BlueMemory = dim;<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
}<br />
}<br />
<br />
}<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32189Implementing Arduino using Generic Serial Device2012-09-15T17:37:37Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'R' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'R' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [3] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [3] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
</pre><br />
<br />
'''#373 Private Method Listing '''<br />
<br />
<pre><br />
<br />
def log(line)<br />
# This function logs a line to the log file of the device<br />
log = File.open("/var/log/pluto/" + device_.devid_.to_s + "_Generic_Serial_Device.log", "a")<br />
log.puts Time.now.to_s + " (Ruby script):" + line.to_s<br />
log.close<br />
end<br />
<br />
</pre><br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
// * with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
// * (PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
boolean debug = true; //debug over serial?<br />
int RedMemory = 255;<br />
int GreenMemory = 255;<br />
int BlueMemory = 255;<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (9, OUTPUT);<br />
pinMode (8, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
pinMode (3, OUTPUT);<br />
<br />
<br />
}<br />
<br />
<br />
//********************************************************************************************************************************<br />
//<br />
// Commandstructure<br />
//<br />
// A - BBB - CCC - DDD - EEE<br />
// <br />
// P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
// 1 (ON)<br />
// D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
//<br />
// C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
//<br />
// R(elative) pin number (3) 0-100<br />
//<br />
// RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
// GreenPin = 5; <br />
// BluePin = 6; <br />
// OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
// WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
//<br />
// DaBalliemo september 2012<br />
//<br />
//*********************************************************************************************************************************<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.print("Command given: ");<br />
Serial.println(packetBuffer);<br />
Serial.print("Buffersize: ");<br />
Serial.println(packetSize);<br />
Serial.println("***************************************");<br />
}<br />
<br />
//############################## Power Section #################################################################################<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Px-0" or "Px-1". The first turn on a pin<br />
//(in this case: pin x). The second just turn off the pin x.<br />
<br />
if (packetBuffer[0] == 'P' ) { // Px-1 or Px-0<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Power command given");<br />
Serial.println("");<br />
Serial.print("Pin number is: ");<br />
Serial.println(pin_char);<br />
Serial.print(" Command given is: ");<br />
Serial.println(led_state_char);<br />
Serial.println("***************************************");<br />
} <br />
<br />
int pin = atoi(pin_char); //our pin number<br />
<br />
if (atoi(led_state_char) == 1) { //if we send 'Px-1'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-1"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (atoi(led_state_char) == 0) { //if we send 'Px-0'<br />
char led_state_send[10] = "P";<br />
strcat(led_state_send, pin_char);<br />
strcat(led_state_send, "-");<br />
strcat(led_state_send, led_state_char);<br />
client.print(led_state_send); //send a string to LMCE: "Px-0";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
//############################## Dim Section #################################################################################<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Dim command given");<br />
Serial.print("dim pin: ");<br />
Serial.println(dim_pin);<br />
Serial.print("dim level: ");<br />
Serial.println(dim);<br />
Serial.println("***************************************");<br />
}<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
//############################## Relative Dim Section #################################################################################<br />
//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<br />
//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<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
<br />
if (packetBuffer[0] == 'R') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
int DimRed;<br />
int DimGreen;<br />
int DimBlue;<br />
float f;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "R";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
<br />
DimRed = int(RedMemory*float(dim)/100); //here we have the dim number: <br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin;<br />
analogWrite(dim_pin, DimRed);<br />
<br />
if (debug) {<br />
Serial.println("***************************************");<br />
Serial.println("Relative Dim command given");<br />
Serial.print("Red pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(RedMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimRed);<br />
} <br />
<br />
DimGreen = int(GreenMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, DimGreen);<br />
<br />
if (debug) {<br />
Serial.print("Green pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(GreenMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimGreen);<br />
} <br />
<br />
DimBlue = int(BlueMemory*float(dim)/100); //here we have the dim number: 0-255<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, DimBlue);<br />
<br />
if (debug) {<br />
Serial.print("Blue pin: ");<br />
Serial.print(dim_pin);<br />
Serial.print(" Memory level: ");<br />
Serial.print(BlueMemory);<br />
Serial.print(" New level: ");<br />
Serial.println(DimBlue); <br />
Serial.println("***************************************");<br />
} <br />
<br />
}<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) {<br />
Serial.println("***************************************"); <br />
Serial.println("RGB command given");<br />
Serial.print("Redpin: "); <br />
Serial.println(Red_pin_char);<br />
Serial.print("Red:"); <br />
Serial.print(Red_char); <br />
Serial.print(" Green:"); <br />
Serial.print(Green_char); <br />
Serial.print(" Blue:"); <br />
Serial.println(Blue_char);<br />
} <br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
RedMemory = dim; // Make sure we remember<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
GreenMemory = dim;<br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-255<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 3;<br />
analogWrite(dim_pin, dim);<br />
BlueMemory = dim;<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
}<br />
}<br />
<br />
}<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32188Implementing Arduino using Generic Serial Device2012-09-15T17:32:31Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'R' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'R' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [3] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [3] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
</pre><br />
<br />
'''#373 Private Method Listing '''<br />
<br />
<pre><br />
<br />
def log(line)<br />
# This function logs a line to the log file of the device<br />
log = File.open("/var/log/pluto/" + device_.devid_.to_s + "_Generic_Serial_Device.log", "a")<br />
log.puts Time.now.to_s + " (Ruby script):" + line.to_s<br />
log.close<br />
end<br />
<br />
</pre><br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32187Implementing Arduino using Generic Serial Device2012-09-15T17:30:30Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'R' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'R' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [3] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [3] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32186Implementing Arduino using Generic Serial Device2012-09-15T17:24:56Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'R' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'D' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [3] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [3] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32185Implementing Arduino using Generic Serial Device2012-09-15T17:20:26Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'D' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'D' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [4] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [4] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32184Implementing Arduino using Generic Serial Device2012-09-15T17:18:57Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
One nice feature is that whatever color code the LED (string) has been set to, when using the dim function or on and 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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'D' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'D' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [4] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [4] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED (This can be a RGB string when using MOSFET's)<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32183Implementing Arduino using Generic Serial Device2012-09-15T17:18:00Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
One nice feature is that whatever color code the LED (string) has been set to, when using the dim function or on and 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 !!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'D' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'D' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [4] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [4] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32182Implementing Arduino using Generic Serial Device2012-09-15T17:14:34Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'D' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'D' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [4] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [4] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- - 8 ----- On/Off<br />
- - 9 --------------LED (Dimmable)<br />
- - |<br />
---------------- GND --- 300 Ohm --<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32181Implementing Arduino using Generic Serial Device2012-09-15T17:11:35Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'D' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'D' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [4] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [4] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32180Implementing Arduino using Generic Serial Device2012-09-15T17:09:05Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
<br />
Change the following processes under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'D' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'D' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [4] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [4] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32179Implementing Arduino using Generic Serial Device2012-09-15T17:08:23Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# One example: if you define an RGB switch with port 3, it will also take up port 5 & 6 on the Arduino. <br />
#<br />
#<br />
# Commandstructure<br />
#<br />
# A - BBB - CCC - DDD - EEE<br />
# <br />
# P(ower) pin number (8 or 9) 0 (OFF 0 0<br />
# 1 (ON)<br />
# D(im) pin number (3 or 9) 0-100 (Level) 0 0<br />
#<br />
# C(olor) pin number (3) 0-255 (Red) 0-255 (Green) 0-255 (Blue)<br />
# <br />
# R(elative) pin number (3) 0-100 (Level)<br />
#<br />
# RedPin = 3; ------> LinuxMCE RGB switch port number has to be 3 !!!!<br />
# GreenPin = 5; <br />
# BluePin = 6; <br />
# OnOffPin = 8; ------> LinuxMCE On/Off switch port number has to be 8 !!!!<br />
# WhitePin = 9; ------> LinuxMCE Dimmer switch port number has to be 9 !!!!<br />
#<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
command = ['','','','','','','','',''] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log deviceID;<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
log ('RGB');<br />
log cmd.params_[279];<br />
log cmd.params_[280];<br />
log cmd.params_[281];<br />
<br />
command [1] = deviceID # Port number defined in LMCE and pin number in Arduino Uno<br />
command [2] = '-'<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [3] = '1' # '1' indicating 'ON' <br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "100" we make sure the Arduino can use the previous levels<br />
command [0] = 'D' # start indicator is relative dimming<br />
command [3] = 100 # relative dim level<br />
end<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [3] = '0'<br />
case childType<br />
when 37, 38 # device type is "on/off switch" template 37 or dimmable switch template 38<br />
command [0] = 'P' # Start indication is P (Power)<br />
when 1993 # device type is "RGB switch" template 1993<br />
# by using a dim level of "0" we make sure the Arduino will remember the settings<br />
command [0] = 'D' # start indicator is relative dimming<br />
end<br />
<br />
when 184 # 184 is Level of dimmer<br />
case childType<br />
when 38<br />
command [0] = 'D'<br />
command [4] = cmd.params_[76] # dim level<br />
when 1993<br />
command [0] = 'R'<br />
command [4] = cmd.params_[76] # relative dim level<br />
end <br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '-'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '-'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32178Implementing Arduino using Generic Serial Device2012-09-15T17:07:31Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''#350 Process Incoming Data''' <br />
<br />
<pre><br />
<br />
buffer = conn_.Recv(100,500);<br />
log ('Receiving');<br />
log buffer;<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32177Implementing Arduino using Generic Serial Device2012-09-15T13:54:05Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
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.).<br />
<br />
<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32176Implementing Arduino using Generic Serial Device2012-09-15T13:52:57Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
The following sketch assumes you are using pin 3 for Red, 5 for Green and 6 for Blue. (3, 5 & 6 are PWM ports).<br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 3 ---- 300 Ohm--||| -<br />
- - 5 ---- 300 Ohm --||<br />
- Arduino - 6 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32174Implementing Arduino using Generic Serial Device2012-09-15T08:41:37Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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".<br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
The following sketch assumes you are using pin 6 for Red, 7 for Green and 8 for Blue. <br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 6 ---- 300 Ohm--||| -<br />
- - 7 ---- 300 Ohm --||<br />
- Arduino - 8 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32173Implementing Arduino using Generic Serial Device2012-09-15T06:55:56Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
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. <br />
<br />
Note: RGB setting can only be done via a scenario!!<br />
<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
The following sketch assumes you are using pin 6 for Red, 7 for Green and 8 for Blue. <br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 6 ---- 300 Ohm--||| -<br />
- - 7 ---- 300 Ohm --||<br />
- Arduino - 8 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32172Implementing Arduino using Generic Serial Device2012-09-15T06:53:06Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
In this example we will use the Arduino Uno with an Ethernet shield to be able to control a RGB LED from within LinuxMCE. <br />
<br />
<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
The following sketch assumes you are using pin 6 for Red, 7 for Green and 8 for Blue. <br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 6 ---- 300 Ohm--||| -<br />
- - 7 ---- 300 Ohm --||<br />
- Arduino - 8 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32171Implementing Arduino using Generic Serial Device2012-09-15T06:51:21Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
The following sketch assumes you are using pin 6 for Red, 7 for Green and 8 for Blue. <br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 6 ---- 300 Ohm--||| -<br />
- - 7 ---- 300 Ohm --||<br />
- Arduino - 8 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
</pre><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32170Implementing Arduino using Generic Serial Device2012-09-15T06:50:54Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
The following sketch assumes you are using pin 6 for Red, 7 for Green and 8 for Blue. <br />
<br />
<pre><br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 6 ---- 300 Ohm--||| -<br />
- - 7 ---- 300 Ohm --||<br />
- Arduino - 8 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
</ore><br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32169Implementing Arduino using Generic Serial Device2012-09-15T06:50:13Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
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.<br />
<br />
The following sketch assumes you are using pin 6 for Red, 7 for Green and 8 for Blue. <br />
<br />
<br />
---------------- <br />
- - GND ----------RGBLED<br />
- - |||<br />
- - 6 ---- 300 Ohm--||| -<br />
- - 7 ---- 300 Ohm --||<br />
- Arduino - 8 ---- 300 Ohm ---|<br />
- -<br />
- -<br />
- -<br />
- -<br />
----------------<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32168Implementing Arduino using Generic Serial Device2012-09-15T06:41:29Z<p>Daballiemo: /* Example 1 Arduino, LinuxMCE & RGB LED */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32167Implementing Arduino using Generic Serial Device2012-09-15T06:39:15Z<p>Daballiemo: /* Arduino Uno Setup */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32166Implementing Arduino using Generic Serial Device2012-09-15T06:38:45Z<p>Daballiemo: /* Setting up Linuxmce */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device.<br />
<br />
<br />
<br />
== Example 1 Arduino, LinuxMCE & RGB LED ==<br />
<br />
Change #384 under ruby code of your Arduino GSD to the following:<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32152Implementing Arduino using Generic Serial Device2012-09-10T21:08:50Z<p>Daballiemo: /* Arduino Uno Setup */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device and change #384 to below<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
boolean debug = true; //debug over serial?<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
if (debug) Serial.begin(9600);<br />
if (debug) Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
pinMode (8, OUTPUT);<br />
pinMode (7, OUTPUT);<br />
pinMode (6, OUTPUT);<br />
pinMode (5, OUTPUT);<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
if (debug) Serial.print("Command given: ");<br />
if (debug) Serial.println(packetBuffer);<br />
if (debug) Serial.print("Buffersize: ");<br />
if (debug) Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
if (debug) Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
if (debug) Serial.print("pin: ");<br />
if (debug) Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
if (debug) Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin x<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
if (debug) Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
if (debug) Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
send_cmd = true; //we have read a command<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-100<br />
dim = dim * 2.55; // Translate 0-100 into 0-255, gets trunked<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
if (debug) Serial.print("dim pin: ");<br />
if (debug) Serial.println(dim_pin);<br />
if (debug) Serial.print("dim level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
strcat(dim_state_send, dim_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, dim_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
//############################## RGB Section #################################################################################<br />
//if we receive a command that start with 'C' it means that you have chosen a RGB scenario level in LMCE.<br />
//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<br />
//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.<br />
<br />
if (packetBuffer[0] == 'C') { // Received a command string for RGB<br />
char Red_char[10];<br />
char Red_pin_char[10];<br />
char Green_char[10];<br />
char Green_pin_char[10];<br />
char Blue_char[10];<br />
char Blue_pin_char[10]; <br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_RGB_pin_char;<br />
int inCount_Red_char;<br />
int inCount_Green_char;<br />
int inCount_Blue_char; <br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
Red_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_RGB_pin_char = inCount;<br />
}<br />
if (var == 1 &&packetBuffer[i] != '-') {<br />
Red_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Red_char = inCount;<br />
}<br />
<br />
if (var == 2 && packetBuffer[i] != '-') {<br />
Green_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Green_char = inCount;<br />
} <br />
<br />
if (var == 3 && packetBuffer[i] != '-') { // Reading Blue value<br />
Blue_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_Blue_char = inCount;<br />
} <br />
<br />
}<br />
Red_pin_char[inCount_RGB_pin_char] = '\0';<br />
Red_char[inCount_Red_char] = '\0';<br />
Green_char[inCount_Green_char] = '\0';<br />
Blue_char[inCount_Blue_char] = '\0'; <br />
<br />
if (debug) Serial.print("Redpin: "); <br />
if (debug) Serial.println(Red_pin_char);<br />
if (debug)Serial.print("Red:"); <br />
if (debug)Serial.print(Red_char); <br />
if (debug)Serial.print(" Green:"); <br />
if (debug)Serial.print(Green_char); <br />
if (debug)Serial.print(" Blue:"); <br />
if (debug)Serial.println(Blue_char);<br />
<br />
<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
<br />
<br />
dim = atoi(Red_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Red pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim);<br />
<br />
dim = atoi(Green_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 1;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Green pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
dim = atoi(Blue_char); //here we have the dim number: 0-100<br />
dim_pin = atoi(Red_pin_char); //and here the pin number<br />
dim_pin = dim_pin + 2;<br />
analogWrite(dim_pin, dim);<br />
<br />
if (debug) Serial.print("Blue pin: ");<br />
if (debug) Serial.print(dim_pin);<br />
if (debug) Serial.print(" level: ");<br />
if (debug) Serial.println(dim); <br />
<br />
<br />
char dim_state_send[16] = "C";<br />
strcat(dim_state_send, Red_pin_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Red_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Green_char);<br />
strcat(dim_state_send, "-");<br />
strcat(dim_state_send, Blue_char);<br />
<br />
if (debug) Serial.println(dim_state_send);<br />
client.print( dim_state_send); //here return the made action.<br />
send_cmd = true;<br />
<br />
//here we change the dim of the selected pin<br />
}<br />
<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32145Implementing Arduino using Generic Serial Device2012-09-09T17:55:25Z<p>Daballiemo: /* Setting up Linuxmce */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device and change #384 to below<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
Serial.begin(9600);<br />
Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
<br />
pinMode (5, OUTPUT);<br />
// pinMode (6, OUTPUT);<br />
// pinMode (8, OUTPUT);<br />
<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
Serial.print("Contents: ");<br />
Serial.print(packetBuffer);<br />
Serial.print(" Buffersize: ");<br />
Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
Serial.print("pin: ");<br />
Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin 7<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
send_cmd = true; //we have read a command<br />
<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-255<br />
dim = dim * 2.55;<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
Serial.println("dim pin: ");<br />
<br />
Serial.println(dim_pin);<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
Serial.println("dim: ");<br />
<br />
Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
<br />
strcat(dim_state_send, dim_pin_char);<br />
<br />
strcat(dim_state_send, "-");<br />
<br />
strcat(dim_state_send, dim_char);<br />
<br />
Serial.println(dim_state_send);<br />
<br />
client.print( dim_state_send); //here return the made action.<br />
<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
<br />
}<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32144Implementing Arduino using Generic Serial Device2012-09-09T17:54:23Z<p>Daballiemo: /* Setting up Linuxmce */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will reply with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
Serial.begin(9600);<br />
Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
<br />
pinMode (5, OUTPUT);<br />
// pinMode (6, OUTPUT);<br />
// pinMode (8, OUTPUT);<br />
<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
Serial.print("Contents: ");<br />
Serial.print(packetBuffer);<br />
Serial.print(" Buffersize: ");<br />
Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
Serial.print("pin: ");<br />
Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin 7<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
send_cmd = true; //we have read a command<br />
<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-255<br />
dim = dim * 2.55;<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
Serial.println("dim pin: ");<br />
<br />
Serial.println(dim_pin);<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
Serial.println("dim: ");<br />
<br />
Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
<br />
strcat(dim_state_send, dim_pin_char);<br />
<br />
strcat(dim_state_send, "-");<br />
<br />
strcat(dim_state_send, dim_char);<br />
<br />
Serial.println(dim_state_send);<br />
<br />
client.print( dim_state_send); //here return the made action.<br />
<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
<br />
}<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32142Implementing Arduino using Generic Serial Device2012-09-09T15:42:02Z<p>Daballiemo: /* Setting up Linuxmce */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
##################################################################################################################<br />
# Use below with care. The commands used will interact with the sketch on the Arduino. There is however no way LMCE<br />
# can retrieve what you have connected to the pins, so make sure the combination LMCE, Arduino and physical connections<br />
# are in line with eachother.<br />
# 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<br />
# 2 & 3 should not be defined within LMCE<br />
#<br />
# DaBalliemo september 2012<br />
##################################################################################################################<br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0,0,0,0] # define command array<br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [0] = 'C' # start indicator is C (Color)<br />
command [1] = deviceID # First port number ie Port 1 == Red, Port 2 == Green, Port 3 == Blue<br />
command [3] = '-' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = '/'<br />
command [6] = cmd.params_[280] # green level<br />
command [7] = '/'<br />
command [8] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
# The Arduino will replay with exactly the same command as it has been given. It can be captured via #350 Process Incoming Data<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
Serial.begin(9600);<br />
Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
<br />
pinMode (5, OUTPUT);<br />
// pinMode (6, OUTPUT);<br />
// pinMode (8, OUTPUT);<br />
<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
Serial.print("Contents: ");<br />
Serial.print(packetBuffer);<br />
Serial.print(" Buffersize: ");<br />
Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
Serial.print("pin: ");<br />
Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin 7<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
send_cmd = true; //we have read a command<br />
<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-255<br />
dim = dim * 2.55;<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
Serial.println("dim pin: ");<br />
<br />
Serial.println(dim_pin);<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
Serial.println("dim: ");<br />
<br />
Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
<br />
strcat(dim_state_send, dim_pin_char);<br />
<br />
strcat(dim_state_send, "-");<br />
<br />
strcat(dim_state_send, dim_char);<br />
<br />
Serial.println(dim_state_send);<br />
<br />
client.print( dim_state_send); //here return the made action.<br />
<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
<br />
}<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32141Implementing Arduino using Generic Serial Device2012-09-09T13:59:26Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0] # define command array<br />
<br />
command [0] = '' # Parameter<br />
command [1] = '' # Parameter<br />
command [2] = '' # Parameter<br />
command [3] = '' # Parameter<br />
command [4] = '' # Parameter<br />
command [5] = '' # Parameter<br />
command [6] = '' # Parameter <br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [3] = '3' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = cmd.params_[280] # green level<br />
command [6] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
Serial.begin(9600);<br />
Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
<br />
pinMode (5, OUTPUT);<br />
// pinMode (6, OUTPUT);<br />
// pinMode (8, OUTPUT);<br />
<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
Serial.print("Contents: ");<br />
Serial.print(packetBuffer);<br />
Serial.print(" Buffersize: ");<br />
Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
Serial.print("pin: ");<br />
Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin 7<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
send_cmd = true; //we have read a command<br />
<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-255<br />
dim = dim * 2.55;<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
Serial.println("dim pin: ");<br />
<br />
Serial.println(dim_pin);<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
Serial.println("dim: ");<br />
<br />
Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
<br />
strcat(dim_state_send, dim_pin_char);<br />
<br />
strcat(dim_state_send, "-");<br />
<br />
strcat(dim_state_send, dim_char);<br />
<br />
Serial.println(dim_state_send);<br />
<br />
client.print( dim_state_send); //here return the made action.<br />
<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
<br />
}<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32140Implementing Arduino using Generic Serial Device2012-09-09T13:58:59Z<p>Daballiemo: /* Setting up Arduino Uno and LinuxMCE to be able to control a RGB LED string */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
== Overview ==<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0] # define command array<br />
<br />
command [0] = '' # Parameter<br />
command [1] = '' # Parameter<br />
command [2] = '' # Parameter<br />
command [3] = '' # Parameter<br />
command [4] = '' # Parameter<br />
command [5] = '' # Parameter<br />
command [6] = '' # Parameter <br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [3] = '3' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = cmd.params_[280] # green level<br />
command [6] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
Serial.begin(9600);<br />
Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
<br />
pinMode (5, OUTPUT);<br />
// pinMode (6, OUTPUT);<br />
// pinMode (8, OUTPUT);<br />
<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
Serial.print("Contents: ");<br />
Serial.print(packetBuffer);<br />
Serial.print(" Buffersize: ");<br />
Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
Serial.print("pin: ");<br />
Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin 7<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
send_cmd = true; //we have read a command<br />
<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-255<br />
dim = dim * 2.55;<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
Serial.println("dim pin: ");<br />
<br />
Serial.println(dim_pin);<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
Serial.println("dim: ");<br />
<br />
Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
<br />
strcat(dim_state_send, dim_pin_char);<br />
<br />
strcat(dim_state_send, "-");<br />
<br />
strcat(dim_state_send, dim_char);<br />
<br />
Serial.println(dim_state_send);<br />
<br />
client.print( dim_state_send); //here return the made action.<br />
<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
<br />
}<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32139Implementing Arduino using Generic Serial Device2012-09-09T13:58:20Z<p>Daballiemo: </p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=Working, but In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
== Setting up Arduino Uno and LinuxMCE to be able to control a RGB LED string ==<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0] # define command array<br />
<br />
command [0] = '' # Parameter<br />
command [1] = '' # Parameter<br />
command [2] = '' # Parameter<br />
command [3] = '' # Parameter<br />
command [4] = '' # Parameter<br />
command [5] = '' # Parameter<br />
command [6] = '' # Parameter <br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [3] = '3' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = cmd.params_[280] # green level<br />
command [6] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
Serial.begin(9600);<br />
Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
<br />
pinMode (5, OUTPUT);<br />
// pinMode (6, OUTPUT);<br />
// pinMode (8, OUTPUT);<br />
<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
Serial.print("Contents: ");<br />
Serial.print(packetBuffer);<br />
Serial.print(" Buffersize: ");<br />
Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
Serial.print("pin: ");<br />
Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin 7<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
send_cmd = true; //we have read a command<br />
<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-255<br />
dim = dim * 2.55;<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
Serial.println("dim pin: ");<br />
<br />
Serial.println(dim_pin);<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
Serial.println("dim: ");<br />
<br />
Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
<br />
strcat(dim_state_send, dim_pin_char);<br />
<br />
strcat(dim_state_send, "-");<br />
<br />
strcat(dim_state_send, dim_char);<br />
<br />
Serial.println(dim_state_send);<br />
<br />
client.print( dim_state_send); //here return the made action.<br />
<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
<br />
}<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32138Implementing Arduino using Generic Serial Device2012-09-09T13:57:57Z<p>Daballiemo: /* General Info (Information not valid yet) */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info ==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
== Setting up Arduino Uno and LinuxMCE to be able to control a RGB LED string ==<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0] # define command array<br />
<br />
command [0] = '' # Parameter<br />
command [1] = '' # Parameter<br />
command [2] = '' # Parameter<br />
command [3] = '' # Parameter<br />
command [4] = '' # Parameter<br />
command [5] = '' # Parameter<br />
command [6] = '' # Parameter <br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [3] = '3' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = cmd.params_[280] # green level<br />
command [6] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
Serial.begin(9600);<br />
Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
<br />
pinMode (5, OUTPUT);<br />
// pinMode (6, OUTPUT);<br />
// pinMode (8, OUTPUT);<br />
<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
Serial.print("Contents: ");<br />
Serial.print(packetBuffer);<br />
Serial.print(" Buffersize: ");<br />
Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
Serial.print("pin: ");<br />
Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin 7<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
send_cmd = true; //we have read a command<br />
<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-255<br />
dim = dim * 2.55;<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
Serial.println("dim pin: ");<br />
<br />
Serial.println(dim_pin);<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
Serial.println("dim: ");<br />
<br />
Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
<br />
strcat(dim_state_send, dim_pin_char);<br />
<br />
strcat(dim_state_send, "-");<br />
<br />
strcat(dim_state_send, dim_char);<br />
<br />
Serial.println(dim_state_send);<br />
<br />
client.print( dim_state_send); //here return the made action.<br />
<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
<br />
}<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32137Implementing Arduino using Generic Serial Device2012-09-09T13:57:15Z<p>Daballiemo: /* Arduino Uno Setup */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info (Information not valid yet)==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
== Setting up Arduino Uno and LinuxMCE to be able to control a RGB LED string ==<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0] # define command array<br />
<br />
command [0] = '' # Parameter<br />
command [1] = '' # Parameter<br />
command [2] = '' # Parameter<br />
command [3] = '' # Parameter<br />
command [4] = '' # Parameter<br />
command [5] = '' # Parameter<br />
command [6] = '' # Parameter <br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [3] = '3' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = cmd.params_[280] # green level<br />
command [6] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<pre><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
Serial.begin(9600);<br />
Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
<br />
pinMode (5, OUTPUT);<br />
// pinMode (6, OUTPUT);<br />
// pinMode (8, OUTPUT);<br />
<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
Serial.print("Contents: ");<br />
Serial.print(packetBuffer);<br />
Serial.print(" Buffersize: ");<br />
Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
Serial.print("pin: ");<br />
Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin 7<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
send_cmd = true; //we have read a command<br />
<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-255<br />
dim = dim * 2.55;<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
Serial.println("dim pin: ");<br />
<br />
Serial.println(dim_pin);<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
Serial.println("dim: ");<br />
<br />
Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
<br />
strcat(dim_state_send, dim_pin_char);<br />
<br />
strcat(dim_state_send, "-");<br />
<br />
strcat(dim_state_send, dim_char);<br />
<br />
Serial.println(dim_state_send);<br />
<br />
client.print( dim_state_send); //here return the made action.<br />
<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
<br />
}<br />
<br />
<br />
<br />
<br />
</pre></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32136Implementing Arduino using Generic Serial Device2012-09-09T13:56:25Z<p>Daballiemo: /* Arduino Uno Setup */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info (Information not valid yet)==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
== Setting up Arduino Uno and LinuxMCE to be able to control a RGB LED string ==<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0] # define command array<br />
<br />
command [0] = '' # Parameter<br />
command [1] = '' # Parameter<br />
command [2] = '' # Parameter<br />
command [3] = '' # Parameter<br />
command [4] = '' # Parameter<br />
command [5] = '' # Parameter<br />
command [6] = '' # Parameter <br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [3] = '3' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = cmd.params_[280] # green level<br />
command [6] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
* Arduino Uno<br />
* Ethernet Shield<br />
* LED<br />
* Resistors<br />
* Breadboard and some wires<br />
* IDE installed on computer<br />
<br />
<br />
<br />
'''Sketch for Arduino'''<br />
<br />
<var><br />
/*<br />
<br />
arduino_control_by_lmce.ino:<br />
<br />
This sketch sends and receive TCP message strings to LinuxMCE.<br />
<br />
*/<br />
<br />
//*****************************************************************<br />
<br />
//*with ethernet shield DON'T USE this pin: 10, 11, 12, 13 *<br />
<br />
//*****************************************************************<br />
<br />
#include <math.h><br />
#include <SPI.h> // needed for Arduino versions later than 0018<br />
#include <Ethernet.h><br />
<br />
<br />
double Thermister(int RawADC); //function to read temperature from a thermister.<br />
<br />
<br />
// Enter a MAC address and IP address for your controller below.<br />
// The IP address will be dependent on your local network:<br />
<br />
byte mac[] = {<br />
0x90, 0xA2, 0xDA, 0x00, 0x58, 0x27 };<br />
byte ip[] = {<br />
192,168,80,129};<br />
byte gateway[] = {<br />
192,168,80,1 };<br />
byte subnet[] = {<br />
255,255,255,0 };<br />
<br />
unsigned int localPort = 69; // local port to listen on<br />
<br />
// buffers for receiving and sending data<br />
<br />
char packetBuffer[1024]; //buffer to hold incoming commandpacket,<br />
EthernetServer server(69);<br />
<br />
<br />
void setup() {<br />
<br />
Serial.begin(9600);<br />
Serial.println("Starting LinuxMCE sketch...");<br />
<br />
// start the Ethernet<br />
<br />
Ethernet.begin(mac, ip,gateway, subnet);<br />
server.begin();<br />
<br />
//port setting:<br />
<br />
pinMode (5, OUTPUT);<br />
// pinMode (6, OUTPUT);<br />
// pinMode (8, OUTPUT);<br />
<br />
}<br />
<br />
<br />
// variables uses to change the value of analogWrite function.<br />
<br />
int dim = 0;<br />
int dim_pin = 0;<br />
<br />
<br />
void loop() {<br />
<br />
<br />
bool send_cmd = false; //is true when i send a command.<br />
<br />
// if there's commanddata available, read a packet<br />
<br />
EthernetClient client = server.available();<br />
int packetSize = 0;<br />
if (client)<br />
{<br />
<br />
if (client.connected()) {<br />
while (client.available()) {<br />
packetBuffer[packetSize] = client.read();<br />
packetSize++;<br />
packetBuffer[packetSize] = '\0';<br />
}<br />
<br />
Serial.print("Contents: ");<br />
Serial.print(packetBuffer);<br />
Serial.print(" Buffersize: ");<br />
Serial.println(packetSize);<br />
<br />
//if you receive a command that start with 'P' it means that you received a boolean value:<br />
//LinuxMCE sends "Pon-x" or "Poff-x". The first turn on a pin<br />
//(in this case: pin x), or make "true" a variable. The second just turn off the pin x.<br />
<br />
<br />
if (packetBuffer[0] == 'P' ) {<br />
char led_state_char[4]; //contains 'on' or 'off'<br />
char pin_char[4]; // the pin number<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_led_state_char;<br />
int inCount_pin_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
led_state_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_led_state_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_pin_char = inCount;<br />
}<br />
<br />
led_state_char[inCount_led_state_char] = '\0';<br />
pin_char[inCount_pin_char] = '\0';<br />
}<br />
Serial.println(pin_char);<br />
<br />
<br />
int pin = atoi(pin_char); //our pin number<br />
Serial.print("pin: ");<br />
Serial.print(pin);<br />
<br />
<br />
if (strcmp(led_state_char,"on") == 0) { //if we send 'Pon-x'<br />
Serial.println(" Status is ON");<br />
char led_state_send[10] = "Pon-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print(led_state_send); //send a string to LMCE: "Pon-x"<br />
digitalWrite(pin, HIGH); //turn on the pin 7<br />
}<br />
<br />
if (strcmp(led_state_char,"off") == 0) { //if we send 'Poff-x'<br />
Serial.println(" Status is OFF");<br />
char led_state_send[10] = "Poff-";<br />
strcat(led_state_send, pin_char);<br />
Serial.println(led_state_send);<br />
client.print( led_state_send); //send a string to LMCE: "Poff-x";<br />
digitalWrite(pin, LOW);<br />
}<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
send_cmd = true; //we have read a command<br />
<br />
}<br />
<br />
<br />
<br />
//if we receive a command that start with 'D' it means that you have chosen a dim level in LMCE<br />
//the string that we send is like : "D9-55" and it's means: 'd' is use for "dim", "9" is the pin that<br />
//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<br />
//we can use this dim to change directly the value to use in AnalogWrite,<br />
//or to do something else.<br />
<br />
if (packetBuffer[0] == 'D') {<br />
char dim_char[10];<br />
char dim_pin_char[10];<br />
int inCount = 0;<br />
int var = 0;<br />
int inCount_dim_pin_char;<br />
int inCount_dim_char;<br />
<br />
for (int i = 1; i < packetSize; i++)<br />
{<br />
//Serial.println(packetBuffer[i]);<br />
if (packetBuffer[i] == '-') {<br />
var++;<br />
inCount = 0; <br />
}<br />
<br />
if (var == 0 && packetBuffer[i] != '-') {<br />
dim_pin_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_pin_char = inCount;<br />
}<br />
<br />
if (var == 1 && packetBuffer[i] != '-') {<br />
dim_char[inCount] = packetBuffer[i];<br />
inCount++;<br />
inCount_dim_char = inCount;<br />
}<br />
<br />
dim_pin_char[inCount_dim_pin_char] = '\0';<br />
dim_char[inCount_dim_char] = '\0';<br />
}<br />
<br />
<br />
//Serial.println(dim_char);<br />
<br />
dim = atoi(dim_char); //here we have the dim number: 0-255<br />
dim = dim * 2.55;<br />
dim_pin = atoi(dim_pin_char); //and here the pin number<br />
<br />
Serial.println("dim pin: ");<br />
<br />
Serial.println(dim_pin);<br />
<br />
//int dim = packetBuffer[0];<br />
<br />
Serial.println("dim: ");<br />
<br />
Serial.println(dim);<br />
<br />
char dim_state_send[10] = "D";<br />
<br />
strcat(dim_state_send, dim_pin_char);<br />
<br />
strcat(dim_state_send, "-");<br />
<br />
strcat(dim_state_send, dim_char);<br />
<br />
Serial.println(dim_state_send);<br />
<br />
client.print( dim_state_send); //here return the made action.<br />
<br />
send_cmd = true;<br />
<br />
analogWrite(dim_pin, dim); //here we change the dim of the selected pin<br />
<br />
}<br />
<br />
<br />
<br />
<br />
</var></div>Daballiemohttp://wiki.linuxmce.org/index.php?title=Implementing_Arduino_using_Generic_Serial_Device&diff=32135Implementing Arduino using Generic Serial Device2012-09-09T13:53:43Z<p>Daballiemo: /* Arduino Uno Setup */</p>
<hr />
<div>[[Category:Hardware]]<br />
{{versioninfo|1004Status=In Progress|1004UpdatedDate=3th September 2012|1004UpdatedBy=Daballiem0}}<br />
<br />
== General Info (Information not valid yet)==<br />
<br />
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.<br />
<br />
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). <br />
<br />
[[File:Arduino_uno_test.jpg]]<br />
<br />
== Setting up Arduino Uno and LinuxMCE to be able to control a RGB LED string ==<br />
<br />
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.<br />
<br />
== Setting up Linuxmce ==<br />
<br />
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.<br />
<br />
Define a new template for the Arduino Uno (which I am using atm)<br />
<br />
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.<br />
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.<br />
<br />
Now set up the rest of the template according to http://wiki.linuxmce.org/index.php/How_to_add_your_own_GSD_device<br />
<br />
'''#384 Process Receive Command For Child'''<br />
<br />
<pre><br />
cmdId = cmd.id_ # Command ID: ON, OFF, SET LEVEL<br />
cmdTo = cmd.devidto_ # Device ID in LinuxMCE<br />
devPort = device_.childdevices_[cmdTo].devdata_[12] # 12 contains a port/channel<br />
childType = device_.childdevices_[cmdTo].devtemplid_ # Template ID to know type of device: switch or dimmer<br />
<br />
deviceID = device_.childdevices_[cmd.devidto_].devdata_[12]<br />
<br />
command = [0,0,0,0,0,0] # define command array<br />
<br />
command [0] = '' # Parameter<br />
command [1] = '' # Parameter<br />
command [2] = '' # Parameter<br />
command [3] = '' # Parameter<br />
command [4] = '' # Parameter<br />
command [5] = '' # Parameter<br />
command [6] = '' # Parameter <br />
<br />
log ('logging from #384');<br />
log ('Device ID');<br />
log cmdTo;<br />
log ('Port used');<br />
log command[2];<br />
log ('Command given'); # orginal command from LMCE<br />
log cmdId;<br />
log ('Device Template'); # Template ID ie switch, dimmer or RGB<br />
log childType;<br />
log ('Level');<br />
log cmd.params_[76]; # The level to set, as a value between 0 (off) and 100 (full).<br />
# It can be preceeded with a - or + indicating a relative value. +20 means up 20%.<br />
<br />
<br />
case cmdId<br />
<br />
when 192 # 192 is the 'ON' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'on-' # '1' indicating 'ON' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 193 # 193 is the 'OFF' command<br />
command [0] = 'P' # Start indication is P (Power)<br />
command [1] = 'off-' # '1' indicating 'OFF' <br />
command [2] = deviceID # Port number defined in LMCE<br />
<br />
when 184 # 184 is Level of dimmer<br />
command [0] = 'D' # indicate level command<br />
command [1] = deviceID # Port number defined in LMCE<br />
command [2] = '-'<br />
command [3] = cmd.params_[76] # is level coming from LMCE<br />
<br />
when 980 # 980 is color scenario command (template 1993)<br />
command [3] = '3' # indicate RGB command<br />
command [4] = cmd.params_[279] # red level<br />
command [5] = cmd.params_[280] # green level<br />
command [6] = cmd.params_[281] # blue level<br />
end<br />
<br />
<br />
commandstring = command.to_s # wrap the command array into a string<br />
log ('Sending'); # log that we are sending<br />
log commandstring; # put in logfile what we send<br />
conn_.Send commandstring # send if off<br />
<br />
</pre><br />
<br />
== Arduino Uno Setup ==<br />
<br />
<br />
<br />
'''Arduino Uno shopping list'''<br />
<br />
Shopping list:<br />
<br />
Arduino Uno<br />
<br />
Ethernet Shield<br />
<br />
LED<br />
<br />
Resistors<br />
<br />
IDE installed on computer<br />
<br />
Sketch to upload to Arduino;<br />
<br />
<var><br />
/*<br />
* Arduino LMCE interlink<br />
*<br />
* A basic example displaying the ability of LinuxMCE<br />
* to control an LED on an Arduino with<br />
* an ethernet shield.<br />
* <br />
* Daballiemo<br />
* http://linuxmce.org<br />
*/<br />
#include <SPI.h><br />
#include <Ethernet.h><br />
int switchPin = 2;<br />
int LEDpin = 5; //LED set to pin 5<br />
int x;<br />
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //must give WIZnet a MAC<br />
byte ip[] = { 192, 168, 80, 129 }; //must configure WIZnet IP<br />
EthernetServer server = EthernetServer(69); //TCP port the server is listening on, I'm using port 69, but you could use any<br />
void setup()<br />
{<br />
Ethernet.begin(mac, ip);<br />
server.begin();<br />
Serial.begin(9600); //for troubleshooting purposes (not needed)<br />
pinMode(LEDpin, OUTPUT);<br />
pinMode(switchPin, INPUT); // sets the digital pin as input to read switch<br />
}<br />
void loop () {<br />
EthernetClient client = server.available(); //client connects to server<br />
if (client){ //if connection present<br />
x = client.read(); //read information coming from server<br />
Serial.println(x);} //print to serial (troublshooting only)<br />
if (x == 49){ //if information sent is a zero<br />
digitalWrite(LEDpin, LOW);} //turn of LED<br />
else if (x == 48){ //if information sent is a one<br />
digitalWrite(LEDpin, HIGH);} //turn on LED<br />
else if (x == 2){ //if information sent is a two<br />
digitalWrite(LEDpin, HIGH); //blink the LED<br />
delay(500);<br />
digitalWrite(LEDpin, LOW);<br />
delay(500);}<br />
if (digitalRead(switchPin) == 1){<br />
Serial.println("Button has been pressed " );<br />
server.write("Button has been pressed ");} // Read the pin and display the value<br />
}<br />
<br />
</var></div>Daballiemo