Hello!
I'm new to the forum, so please don't shoot me if I'm not exactly following procedures for posting a problem
But here it is:
I'm trying to control KlikAanKlikUit-reiceivers with an arduino uno talking (over IP, UDP protocol) to a vb6 project on my PC
Here's the sketch, in fact, it is the udpsendreceivestring example, where I added a few snippets found on this forum, plus a few extra ' debug' serial.println commands...
#include <NRT.h>
#include <Wire.h>
#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 <string.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned int localPort = 8888; // local port to listen on
byte group = 0;
byte unit = 0;
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
NRT transmitter(538,5,260,3);
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac);
Udp.begin(localPort);
Serial.begin(9600);
}
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[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
char *Command[10];
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
Serial.println("Split:");
// Split the command
char *p = packetBuffer; //point to *p to the string in inData
char *str; //declaring *str
int counter = 0; //initialise the counter
while (str = strtok_r(p, ";", &p)) // delimiter is the semicolon
{
Command[counter] = str; //use the counter as an index to add each value to the array
Serial.println(Command[counter]);
counter++; //increment the counter
}
char* konijn=Command[0];
Serial.println(konijn);
int sLen = sizeof(konijn);
for(int s=0; s<sLen; s++)
{
Serial.print("konijn[");
Serial.print(s);
Serial.print("] is {");
Serial.print(konijn[s]);
Serial.print("} which has an ascii value of ");
Serial.println(konijn[s], DEC);
}
if (Command[0]=="SWITCH"){
Serial.println("Switching");//This line never shows up in the serial monitor....!
transmitter.sendUnit(atoi(Command[1]),atoi(Command[2]),Command[3]=="1");
}
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}
And this is the serial output:
Received packet of size 13
From 192.168.0.108, port 8888
Contents:
SWITCH;1;3;1
Split:
SWITCH
1
3
1
SWITCH
konijn[0] is {S} which has an ascii value of 83
konijn[1] is {W} which has an ascii value of 87
The fact that it never prints the line Switching tells me the comparison Command[0]=="SWITCH" doesn't work...
The fact that sizeof(konijn) seems to be 1 confirms that.
Am I doing something wrong in the strtok_r routine?