Hello
I have a program which is a TCP IP client and I added an option in which I can type in text (commands) from keyboard connected to the 2nd USB hub of Arduino Due.
The Ethernet module is ENC28J60.
The command must be COMMANDCR, where CR is carriage return
When i write for example:
client.write("SCENE\x0d"); ( SCENE=command, \x0d = CR)
I have a response from the server so I am 100% sure that this excact command is OKAY.
Later when I
a) Type in "SCENE\x0d" FROM KEYBOARD CONNECTED TO ARUDINO USB or
b) Type in "SCENE" FROM KEYBOARD CONNECTED TO ARUDINO USB and add "\x0d" to it later
I do not have response from the server, so the command must be in a bad form...
But when I print "the final command" It says "SCENE" so the "\x0d" works as a carriage return because it disapears.
So the problem is in a format of command or in carriage return. I tried everything 100x times so please help me if you have any idea...
The esential part of code:
void keyloop()
{
while (mode == 1) {
String sentence = "";
bool waitforkey = true;
while (waitforkey == true)
{
// Process USB tasks
usb.Task();
// Look for valid ASCII characters
if (inputString >= 32 && inputString <= 127)
{
sentence += char(inputString);
Serial.write(inputString);
}
// Check for ENTER
else if (inputString == '\')
{
Serial.println();
Serial.println("Your command was sent:");
Serial.println();
sortSentence(sentence);
Serial.println("Length of sentence");
Serial.println(sentence.length() + 10);
char komenda[sentence.length()];
sentence.toCharArray(komenda, sentence.length() + 10);
// Serial.println(strlen (komenda));
strcat(komenda, "\x0d");
delay(100);
Serial.println("Final command");
Serial.println(komenda);
Serial.println("Size of final command");
Serial.println(sizeof(komenda));
client.write(komenda);
delay(1000);
if (client.available() > 0) {
int camretLen = client.available();
Serial.println("RESULT");
for (int i = 0 ; i < camretLen; i++) {
char c = client.read();
Serial.print(c);
client.flush();
}
}
Serial.print("CHECK");
waitforkey = false;
}
inputString = 0;
}
}
}
whole code:
//Ethernet
#include <EthernetENC.h>
#include <SPI.h>
char incomingByte = 0;
String CR = "";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // numeric IP for Google (no DNS)
IPAddress ip(10, 5, 5, 110);
EthernetClient client;
//Keyboard
#include <KeyboardController.h>
char inputString = 0;
// Initialize USB Controller
USBHost usb;
// Attach keyboard controller to USB
KeyboardController keyboard(usb);
int mode = 0;
void setup() {
Ethernet.init(10);
// Open serial communications and wait for port to open:
Serial.begin(115200);
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 9876)) {
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
Serial.println("Type a to listen");
Serial.println("Type b to send");
}
void loop() {
Ethernet.maintain();
// Read serial input:
if (Serial.available() )
{
incomingByte = Serial.read();
}
else
{
incomingByte = '0';
}
switch (incomingByte) {
case 'a':
if (client.available()) {
for (int i = 0 ; i < client.available(); i++) {
char d = client.read();
Serial.print(d);
}
}
break;
case 'b':
mode = 1;
keyloop();
// client.write("SCENE\x0d");
// Serial.print("SENT");
// delay(500);
// int camretLen = client.available();
// if (client.available() > 0) {
// Serial.println("RESULT");
// for (int i = 0 ; i < camretLen; i++) {
// char c = client.read();
// Serial.print(c);
// }
// }
// break;
client.flush();
if (!client.connected()) {
client.connect(server, 9876);
}
}
}
// This function intercepts key press
void keyPressed()
{
inputString = keyboard.getKey();
}
// Sort the final sentence
void sortSentence(String sentence)
{
// Sentence logic goes here
Serial.println(sentence);
}
void keyloop()
{
while (mode == 1) {
String sentence = "";
bool waitforkey = true;
while (waitforkey == true)
{
// Process USB tasks
usb.Task();
// Look for valid ASCII characters
if (inputString >= 32 && inputString <= 127)
{
sentence += char(inputString);
Serial.write(inputString);
}
// Check for ENTER
else if (inputString == '\')
{
Serial.println();
Serial.println("Your command was sent:");
Serial.println();
sortSentence(sentence);
Serial.println("Length of sentence");
Serial.println(sentence.length() + 10);
char komenda[sentence.length()];
sentence.toCharArray(komenda, sentence.length() + 10);
// Serial.println(strlen (komenda));
strcat(komenda, "\x0d");
delay(100);
Serial.println("Final command");
Serial.println(komenda);
Serial.println("Size of final command");
Serial.println(sizeof(komenda));
client.write(komenda);
delay(1000);
if (client.available() > 0) {
int camretLen = client.available();
Serial.println("RESULT");
for (int i = 0 ; i < camretLen; i++) {
char c = client.read();
Serial.print(c);
client.flush();
}
}
Serial.print("CHECK");
waitforkey = false;
}
inputString = 0;
}
}
}