Hey
I have a program which is TCP/IP client which can send and receive messages to the industrial smart camera. The program is going to work like that:
1.User turns on the device ( will add screen later, now working on serial)
2. User types in basic specs like IP adress and TCP PORT number
3. User chooses to just listen or send messages and listen for a respond from the server
a) just litening in a loop
b) sending message ( typed in by user) and getting respond from the server in a loop
Hardware:
Arduino DUE
ENC28J60
OMRON FQ2 Smart Camera
Router working as a network switch
For now I have 2 programs.
Main in which I choose 3a or 3b option and it works but the message is in a program (without typing it in )
Second one is a program in which I can type words by USB keyboard and i print these words (commands) in a serial.
Now i want to combine them so add the keyboard functionality into to the main program.
I tried a few options but it did not work ( just pasting the lines in a main program).
I want to type in from keyboard:
Ip and TCP port number
Message (command)
Here is the 1st program:
#include <EthernetENC.h>
#include <SPI.h>
char command = 0;
char incomingByte = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // Server ip
IPAddress ip(10, 5, 5, 109); // Setting arduino uip
EthernetClient client;
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("Press a to listen ");
Serial.println("Press b to send commands");
}
void loop() {
Ethernet.maintain();
// Read serial input:
if (Serial.available() )
{
incomingByte = Serial.read();
}
else
{
incomingByte = 0;
}
switch (incomingByte) {
//CASE a
case 'a':
if (client.available()) {
char c = client.read();
Serial.print(c);
}
break;
//CASE b
case 'b':
delay(100);
//
while (Serial.available()) {
command = Serial.read();
delay(500);
client.write(command);
}
Serial.print("SENT");
delay(500);
int camretLen = client.available();
if (camretLen > 0) {
Serial.println("RESULT");
for (int i = 0 ; i < camretLen; i++) {
char c = client.read();
Serial.print(c);
}
client.flush();
break;
}
if (!client.connected()) {
client.connect(server, 9876);
}
}
}
Here is the 2nd program:
#include <KeyboardController.h>
String inputString = ""; // a string to hold incoming data
String finalString = "";
boolean stringComplete = false; // whether the string is complete
USBHost usb;
// Attach Keyboard to native USB socket
KeyboardController keyboard(usb);
void setup() {
Serial.begin(115200);
Serial.println(" Enter command ");
}
void keyPressed() {
char inChar = keyboard.getOemKey();
char inKey = keyboard.getKey();
Serial.print("OEM key: ");
Serial.println(keyboard.getOemKey());
Serial.write(keyboard.getKey());
Serial.println();
if (inKey == '\') { // If carriage return
stringComplete = true;
}
else
{
inputString += inKey;
}
}
void loop() {
usb.Task();
if (stringComplete) {
Serial.print(" You entered this command: ");
Serial.println(inputString);
delay(1000);
inputString = "";
stringComplete = false;
Serial.println(" Enter command ");
}
}