sketch for sending IR, controlled by UDP

This sketch only needs help with converting a char* (containing the data for a raw IRcommand) to an unsigned int!

i got this:
char* containing: data = 3950,4050,450,1050,500,2000,450,1050,500,2000,450,1050,450,1100,450,2000,450,2000,500,2000,450,1050,450,2050,450,1050,450,2050,450,1050,450,2050,450,1050,450,2000,500,2000,450,1050,450,1100,450,1050,450,2050,450,1050,450,2000,500

and i want to put in an unsigned int looking like this:
unsigned int cmd[51] = {3950,4050,450,2000,500,1950,500,2000,450,1050,500,1050,450,1050,500,2000,450,2000,500,1950,500,1050,500,1950,500,1050,500,1000,500,1050,450,1050,500,2000,450,2000,500,1950,500,1050,450,1050,500,1050,450,2000,500,1050,450,2000,500};

I’ll post my entire code and hope for the best! (dont be to mean in youre comments! :wink: )

I need help with the last part of it, the funcktion i called “sendir”

/*
First i want to point out that im not a very good programmer! but i hope i get better whilst trying!
And im not english native, im swedish, so sorry for bad my gramar and bad spelling… :slight_smile:

This is my attempt to controll IR devices from anything that can send an UDP package!
I’m trying to make it det all its data from the udp package it self, so i dont have ro reprogram
my arduino every time a want to add a button! i have allso written an app form my Android (nexus 4)
to send the packages for me.

i have put this together from code of other people, modified to match my needs, most of it is rewritten,
but remarks written by those people i left in my code, even if i modefied the code slightly…

how it works:
i send an UDP package li this (this probbably wont work, i just made the numbers upp to have an example:
02* /nec/2060/12/900* /rc5/45479814/16/1500 (the spaces between the * and the / in not pressent in the real packet)
02 means it cantains 2 commands
*means start of a command
the program splits it up like this:
2060 // the actual data that irsend uses
12 // how many bits - irsend needs this
900 // the delay before next command can be proccessed
*/

#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <IRremote.h>
#include <string.h>

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

IRsend irsend;

byte mac = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

char cmd[15];
char data[512];
char bits[15];
char wait[15];
int freq = 38;

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

char buf[3] = “”;
//unsigned long numcommands = 0;
int numcommands = 0;
// buffers for receiving and sending data

const int xUDP_TX_PACKET_MAX_SIZE = 4096;
char packetBuffer[xUDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);

Serial.begin(115200);
}

void loop() {
// if there’s data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{

// Serial.print("Received packet of size ");
// Serial.println(packetSize);
// Serial.print("From ");
IPAddress remote = Udp.remoteIP();
/* for (int i =0; i < 4; i++)
{
Serial.print(remote*, DEC);*

  • if (i < 3)*

  • {*

  • Serial.print(".");*

  • }*

  • }*
    _*/ _
    // Serial.print(", port ");
    // Serial.println(Udp.remotePort());

  • //clear packet from previous data*

  • memset(packetBuffer, 0, xUDP_TX_PACKET_MAX_SIZE);*

  • // read the packet into packetBufffer*

  • Udp.read(packetBuffer,xUDP_TX_PACKET_MAX_SIZE);*

  • for (int x=0; x < 2; x++)*

  • {*

  • buf = packetBuffer;*

  • }*

  • numcommands = atol(buf);*

  • Serial.print("number of commands in packet: as char: ");*

  • Serial.print(buf);*

  • Serial.print(" : as int: ");*

  • Serial.println(numcommands);*

  • Serial.println(“Contents:”);*

  • Serial.println(packetBuffer);*

  • for (int i=1; i <= numcommands; i++)*

  • {*
    _ //Received buffer contains "/cmd/data/bits/wait". Break it up._
    _ char
    slash1;_
    _ char* slash2;_
    _ char* slash3;_
    _ char* slash4; _
    _ char* star;_

  • if (i == 1)*

  • {*

  • slash1 = strstr(packetBuffer, “/”) + 1; // Look for first slash*

  • }*

  • else*

  • {*

  • slash1 = strstr(slash4, “/”) + 1; // Look for first slash on next command*
    // Serial.println(“Contents:”);
    // Serial.println(slash4);

  • }*

  • slash2 = strstr(slash1, “/”) + 1; // second slash*

  • slash3 = strstr(slash2, “/”) + 1; // third slash*

  • slash4 = strstr(slash3, “/”) + 1; // fourth slash*
    _ star = strstr(slash3, “*”) + 1; // star to indicare a new command_

  • Serial.print("processing command: ");*

  • Serial.println(i);*

  • PrintString(“slash1”,slash1);*

  • PrintString(“slash2”,slash2);*

  • PrintString(“slash3”,slash3);*

  • PrintString(“slash4”,slash4);*

  • PrintString(“star”,star);*

  • // strncpy does not automatically add terminating zero, but strncat does! So start with blank string and concatenate.*

  • cmd[0] = 0;*

  • data[0] = 0;*

  • bits[0] = 0;*

  • wait[0] = 0;*

  • strncat(cmd, slash1, slash2-slash1-1);*

  • strncat(data, slash2, slash3-slash2-1);*

  • strncat(bits, slash3, slash4-slash3-1);*

  • strncat(wait, slash4, star-slash4-1);*

  • PrintString(“cmd”,cmd);*

  • PrintString(“data”,data);*

  • PrintString(“bits”,bits);*

  • PrintString(“wait”,wait);*

  • Serial.println(" ");*

  • sendir(cmd, data, bits, wait, freq);*

  • }*

  • }*

  • delay(10);*
    }
    void PrintString(char* label, char* str)
    {

  • Serial.print(label);*

  • Serial.print("=");*

  • Serial.println(str);*
    }
    void PrintNumber(char* label, int number)
    {

  • Serial.print(label);*

  • Serial.print("=");*

  • Serial.println(number, DEC);*
    }
    void sendir(char* cmd, char* data, char* bits, char* wait, int freq)
    {

  • int cmdint = konvertcmd(cmd); *

  • int bitsint = 0;*

  • bitsint = atol(bits);*

  • int waitint = 0;*

  • waitint = atol(wait);*
    // unsigned int* dataintraw = 0; //funkar inte

  • unsigned int dataintraw[51] = {0}; //funkar*

  • int dataint = 0;*

  • dataint = atol(data);*

  • strcat (data,",");*

  • String testtemp = 0;*

  • int i = 0;*

  • for (int x = 0; x <= strlen(data); x++)*

  • {*

  • if (data != ‘,’)*

  • {*

  • testtemp = testtemp + data;*

  • }*

  • else*

  • {*

  • i++;*
    char testtemp_as_char[testtemp.length()+1]; // or: char *test_as_char; … not sure if you have to initialize the buffer

  • testtemp.toCharArray(testtemp_as_char, testtemp.length()+1);*
    dataintraw = atoi(testtemp_as_char);
    // int hej = atoi(testtemp_as_char);

// dataintraw = hej;
*// Serial.print(hej); *
// Serial.print(dataintraw*);*

* if ( x != strlen(data)-1)*
* {*
// Serial.print(",");

* }*
* testtemp = 0;*
* }*
* }*

* Serial.println(“this is what irsend is passed from main loop”);*
* Serial.println("--------------------------------------------");*
* Serial.print(“protokoll :”);*
* Serial.println(cmd);*
* Serial.print(“protokoll int :”); *
* Serial.println(cmdint);*
* Serial.print(“bits :”);*
* Serial.println(bits);*
* Serial.print("bitsint = ");*
* Serial.println(bitsint);*
* Serial.print("data = ");*
* Serial.println(data);*
* Serial.print("dataint = ");*
* Serial.println(dataint);*
* Serial.print("wait = ");*
* Serial.println(wait);*
* Serial.print("waitint = ");*
* Serial.println(waitint);*
* Serial.println(" ");*
* Serial.println(“debug info”);*
* Serial.println("----------");*

* Serial.print("langd pa data : ");*
* Serial.println(strlen(data));*

* Serial.println("");*
* Serial.println(data); *

* Serial.println(“protokoll picker debug”);*
* Serial.println("---------------------");*

* Serial.print("cmd : ");*
* Serial.println(cmd);*
* Serial.print("cmd som int: ");*
* Serial.println(cmdint);*
* if (cmdint == 310)*
* {*
* Serial.print("NEC: ");*
* Serial.print(data);*
* Serial.print(" bitar: ");*
* Serial.print(bitsint);*
* Serial.print(" delay: ");*
* Serial.println(waitint); *
* irsend.sendNEC(dataint, bitsint);*
* delay(waitint); *
* }*
* else if (cmdint == 457)*
* {*
* Serial.print("SONY: ");*
* Serial.print(data);*
* Serial.print(" bitar: ");*
* Serial.print(bitsint);*
* Serial.print(" delay: ");*
* Serial.println(waitint); *
* irsend.sendSony(dataint, bitsint);*
* delay(waitint); *
* }*
* else if (cmdint == 266)*
* {*
* Serial.print("RC5: ");*
* Serial.print(data);*
* Serial.print(" bitar: ");*
* Serial.print(bitsint);*
* Serial.print(" delay: ");*
* Serial.println(waitint); *
* irsend.sendRC5(dataint, bitsint);*
* delay(waitint); *
* }*
* else if (cmdint == 267)*
* {*
* Serial.print("RC6: ");*
* Serial.print(data);*
* Serial.print(" bitar: ");*
* Serial.print(bitsint);*
* Serial.print(" delay: ");*
* Serial.println(waitint); *
* irsend.sendRC6(dataint, bitsint);*
* delay(waitint); *
* }*
* else if (cmdint == 330)*
* {*
* Serial.print(“raw:”);*
* Serial.print(data);*
* Serial.print(" bitar: ");*
* Serial.print(bitsint);*
* Serial.print(" delay: ");*
* Serial.println(waitint); *
* irsend.sendRaw(dataintraw, 51, freq); *
* delay(waitint); *
* }*

* Serial.println("Sent! ");*

}
int konvertcmd(String test)
{
* int total = 0;*
* for (int i = 0 ; i <= test.length() ; i++)*
* {*
_ total = total + int(test*);
}
return total;
}*_

I solved it! it works with my code slightly modified (there was a -1 missing in a loop...)

but i would like to know if theres a better way to do it?

but i would like to know if theres a better way to do it?

I’m pretty sure that if you remove the bulleted lists, smiley faces, and italics from your code, that would be better.

There were two stickies at the top of the forum. Clearly, you just barged in here are started typing, without reading them.

One BIG improvement to your code would be to eliminate ALL occurrences of the keyword String from your code. It is NOT necessary.