Hi guys,
I’ve been working on this for a while now and can now send a char array from one core to the other and print it out on the other end. The spec for the software I’m writing requires there to be a header with a Unique ID (UUID) and a zigbee type, which will eventually tell the server what type of controller iit is recieving data from then a variable sized data packet.
As I previously mentioned I have managed to send one char array and I now want to send two which would be inputted seperately on the clientside and split up into two seperate strings or arrays on the server side. My current issue is that in wiring certain things can’t be done. I can’t send a String using the “Client.write()” command, only bytes and chars. I can’t do the string.length() command either, which I will need later, because it doesn’t seem to output a varible but it’s alot easier to add the char arrays together if they are strings. On the server side I have a similar problem as the char arrays refuse to split or print. Basically I have been writing incredibly “hacky” ways around using the wiring experience I have and I have hit a dead end. I hope someone can help. Sorry for how messy the code is.
Clientside code:
byte serv[] = { 172, 16, 0, 222 }; //dos
//SYSTEM_MODE(MANUAL);<
//TCPServer server = TCPServer(23);
TCPClient client;
#include <string.h>
#include <stdio.h>
void setup()
{
Serial.begin(9600);
// start listening for clients
while(!Serial.available()) SPARK_WLAN_Loop();
// client.connect(serv,23);
if (client.connect(serv, 80))
{
Serial.println("connected");
client.println("We are all connected");
}
else
{
Serial.println("connection failed");
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void loop()
{
if (client.connected())
{
delay(1000);
Serial.print("Here");
int length = client.read();
Serial.println(length);
client.flush();
String s4 = "";
for (int i=1; i <= length; i++){
char be = client.read();
s4 += be;
Serial.println(s4);
delay(1000);
}
Serial.println("Post for loop");
String word1 = getValue(s4, ' ', 0);
String word2 = getValue(s4, ' ', 1);
Serial.println(word1);
Serial.println(word2);
/*char packet[13];
char UUID[7];
char ZigBeeType[6];
//int z;
//String UUID = "";
//String ZigBeeType = "";
s4.toCharArray(packet, 13);
char* z = packet;
strncpy(UUID, z, 7);
z += 7;
strncpy(ZigBeeType, z, 6);
z += 6;
/* char * pch;
//printf ("Splitting string \"%s\" into tokens:\n",str);
pchar str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
/*strncpy(UUID, UUIDC, 7);
UUID[7] = '\0';
strncpy(ZigBeeType, ZigBeeTypeC, 6);
ZigBeeType[6] = '\0';
Serial.println("");
Serial.println("The Unique ID of this packet is: ", UUID);
Serial.println("");
Serial.println("The ZigBeeType of this packet is: ", ZigBeeType);
*/
}else{
Serial.println();
Serial.println("disconnecting.");
}
}
Server side code:
#include <string.h>
TCPServer server = TCPServer(80);
TCPClient client;
void setup()
{
Serial.begin(9600);
// start listening for clients
server.begin();
while(!Serial.available()) SPARK_WLAN_Loop();
// Make sure your Serial Terminal app is closed before powering your Core
// Now open your Serial Terminal, and hit any key to continue!
}
void loop()
{
// char packet[10] = "packet";
char UUID[8] = {'1', '2', '3', '4', '5', '6', '7', ' '}; // int length;
char ZigBeeType[6] = {'0', 'x', '0', '6', '0', '0'};
char packet [14];
memcpy(packet, UUID, 8);
memcpy(&packet[20], ZigBeeType, 6);
//String UUIDs = "1234567";
//String ZigBeeTypes = "0x0600";
//String packets = "";
//packetS += UUIDS;
//packetS += ZigBeeTypeS;
//int length = 20;
int length = arraySize(packet);
client.write(length);
if (client.connected()) {
while (client.available()) {
Serial.print("It's working, promise: ");
client.write(packet);
Serial.println(packet);
client.flush();
delay(30000);
}
} else {
// if no client is yet connected, check for a new connection
Serial.print(" No connection here ");
client = server.available();
}
//Serial.print(c);
delay (2000);
}
Client side output:
Here14
▒
▒1
▒12
▒123
▒1234
▒12345
▒123456
▒1234567
▒1234567
▒1234567 ▒
▒1234567 ▒O
▒1234567 ▒O▒
▒1234567 ▒O▒▒
▒1234567 ▒O▒▒▒
Post for loop
▒1234567
▒O▒▒▒
Here-1
Post for loop
Server side output:
No connection here No connection here It's working, promise: 1234567 ▒O