Can you try 16 rather than 10?
With all bases other than 10 it assumes unsigned values but with 10 it assumes signed.
It could be that it is sending as a delimiter for example.
Can you try 16 rather than 10?
With all bases other than 10 it assumes unsigned values but with 10 it assumes signed.
It could be that it is sending as a delimiter for example.
Okay i will try now and report back.

Can you try uncommenting those 2 lines so we can see how many characters are being returned?
At present only the first returned character is read, it might help if we know how many have been sent though. I'm not sure if the value being returned makese sense or not
Thats a response for that excact command:
And actual code so we talk about the same thing ![]()
#include <stdio.h>
#include <stdlib.h>
#include <SPI.h>
#include <UIPEthernet.h>
//#include "packet.h"
String serial_data;
char incomingByte = 0;
char s = 0;
int choice = 0;
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, 106);
EthernetClient client;
void setup() {
// 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("Wcisnij a aby odczytywac pakiety");
Serial.println("Wcisnij b aby wysylac pakiety");
}
void loop() {
Ethernet.maintain();
// Read serial input:
if (Serial.available() )
{
incomingByte = Serial.read();
}
switch (incomingByte) {
case 'a':
//Serial.print("Logi");
if (client.available()) {
char c = client.read();
Serial.print(c);
Serial.print("");
}
if (!client.connected()) {
client.connect(server, 9876);
}
break;
case 'b':
//Serial.print("Wpisz komendę");
//if (client.available()) {
// char c = client.read();
// Serial.print(c);
// }
// if (client.available() > 0) {
// Serial.print("Klient available");
// }
// char inChar = Serial.read();
if (client.available()) {
Serial.print("Klient available - chars:");
}
delay(100);
// client.flush();
client.print("SCENE$0D");
client.write("SCENE$0D");
Serial.print("SENT");
delay(2000);
int camretLen = client.available();
Serial.println(camretLen);
const int rsLen = 5;
char retString[rsLen];
retString[5] = '\0';
char c = client.read();
int tmp;
tmp = (int) c;
itoa (tmp, retString, 16);
Serial.print("Returned char val: ");
Serial.println (retString);
Serial.print("READ BACK");
// client.flush();
if (!client.connected()) {
client.connect(server, 9876);
}
break;
}
}
AND $0D is the "CR" delimiter and it means "ENTER"
Maybe i am sending the command in a bad way?
OK, the value returned by client.available is 0 meaning no characters to be read. client.read will therefore return -1 also meaning there are no characters to read. I did not expect the value of the string returned to also be -1 but now at least we know.
I think we need to go back and find out why there is nothing being returned again.
if (client.connected()) {
client.print("SCENE$0D");
client.write("SCENE$0D");
The client.write in this section probably is not sending what what you expect. Client.write sends a single character unless you specify a buffer length. I think you need to try:
client.write("SCENE$0D", 8);
though it may be 6 depending on how the $0D is handled.
Do you need to send this twice once with print and once with write or can you send it just once?
Will try in a minute, just checking what it will say in the case "a"
Well i don't know which one should be used, write or print?
Okay tried 6 and 8 with client print. Same result ![]()
But when i put 6 or 8 it is going in a loop.
Before i had to press b to get one more "send"
client.print only takes a null terminated string.
client.write takes either a single byte/char or a buffer and length.
Which did you try?
i tried
client.write("SCENE$0D",6);
client.write("SCENE$0D",7);
client.write("SCENE$0D",8);
client.write("SCENE$0D",10);
client.print uncomented.
Just to check the camera is still on 10.5.5.10, and the ESP on 10.5.5.106 ?
Only asking if things have been switched off and may now have a new IP address for any reason.
Wondering if the string SCENE$0D is interpreted by Hercules as meaning SCENE followed by - ie character 13.
With C we may be sending the literal string SCENE$0D ie SCENE followed by a $ sign then a 0 then a D.
May be worth trying "SCENE\n" as the string to send with client.print
In Hercules i can write SCENE$0D or SCENE#13 and the result is the same.
So client.write or client.print ? ![]()
Tried with \n same result.
Yes camera's ip is 10.5.5.10
ESP 10.5.5.106
I start to think that we need to send this CR in some way:
I tried:
case 'b':
if (client.available()) {
Serial.print("Klient available - chars:");
}
delay(100);
// client.flush();
client.print('S\r');
client.write('S\r');
client.print("SCENE\r");
client.write("SCENE\r");
client.write("SCENE\r,6");
Serial.print("SENT");
delay(2000);
int camretLen = client.available();
Serial.println(camretLen);
const int rsLen = 5;
char retString[rsLen];
retString[5] = '\0';
char c = client.read();
int tmp;
tmp = (int) c;
itoa (tmp, retString, 16);
Serial.print("Returned char val: ");
Serial.println (retString);
Serial.println(c);
Serial.print("READ BACK");
// client.flush();
if (!client.connected()) {
client.connect(server, 9876);
}
break;
}
}
Result:
You were right to replace /n (newline) with /r (carriage return) I have no idea why I though /n was suitable - my mistake.
It still appears as though the camera is not getting the correct string from the ESP and therefore not responding.
I think it might be easier to stick with client.print for the tests.
What happens if you send an invalid string to the camera from Hercules?
Can you set Hercules as a server and then send the message from the ESP to Hercules on the PC and see what arrives?
@countrypaul
Yeah for me the /r should work also but i have no idea why it doesn't
I do not know why in hercules i type the "CR" like
a) $0D
b) #13 (or #14 don't remember now) )
and it works but do you think that i should do it by "/r" in arudino? I also think that maybe the better way but still it should just work then. But okay
With setting server on Hercules I will try and write back when i succes.
Based on what you have just said, I think the camera requires a carriage return termination on a command in order to accept it. In Hercules this appears to be done by putting $0D in the string from which I presume the $ sign acts as an escape character and indicates what follows is a value given in hex - 0D being 13. In C this would normally be done by putting in an escape character followed by an indication of what is required that should be /r for a carriage return.
If you have Wireshark installed on your PC, then you should be able to see what the PC transmits to the Camera and verify that it is what we expect - and confirm what the Camera sends back 0OK or 0OK for example, and in case of error ER or ER.
If you can get the ESP to send things to Hercules on the PC you could also check with Wireshark to see whether what it sends to the PC is what we expect and the same as what Hercules sends to the camera. You could reply from Hercules to the ESP and check whether it handles the reply correctly before trying with ESP to Camera.
Okay we have a HUGE STEP in this topic ![]()
I got a reply from the server !
#include <stdio.h>
#include <stdlib.h>
#include <SPI.h>
#include <UIPEthernet.h>
//#include "packet.h"
String serial_data;
char incomingByte = 0;
char s = 0;
int choice = 0;
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, 106);
EthernetClient client;
void setup() {
// 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("Wcisnij a aby odczytywac pakiety");
Serial.println("Wcisnij b aby wysylac pakiety");
}
void loop() {
Ethernet.maintain();
// Read serial input:
if (Serial.available() )
{
incomingByte = Serial.read();
}
switch (incomingByte) {
case 'a':
//Serial.print("Logi");
if (client.available()) {
char c = client.read();
int camretLen = client.available();
Serial.print(camretLen);
Serial.print("WYNIKI ");
Serial.print(c);
}
if (!client.connected()) {
client.connect(server, 9876);
}
break;
case 'b':
if (client.available()) {
Serial.print("Klient available ");
}
delay(100);
// client.flush();
// client.println('S');
// client.println("SCENE");
// client.write('S\r');
// client.print("SCENE");
// client.write("SCENE\r");
// client.write("SCENE\r,6");
client.print("SCENE\x0d");
client.write("SCENE\x0d");
Serial.print("SENT");
delay(2000);
// int camretLen = client.available();
// Serial.println(camretLen);
// const int rsLen = 5;
// char retString[rsLen];
// retString[5] = '\0';
char c = client.read();
// int tmp;
// tmp = (int) c;
// itoa (tmp, retString, 16);
//
//
//
// Serial.print("Returned char val: ");
// Serial.print (retString);
Serial.println("RESULT");
Serial.println(c);
Serial.println("READ BACK");
/
You see it is "0OK" ![]()
But it works for No line Ending best and it is in a loop.
So now there are new problems ![]()
The final project will work with
Great, well done.
At present you code only reads one character at a time from the client, so it will get one character each time round the loop. You will have al alter you code so that it reads all characters in the buffer until client.available() indicates there are no more.
Note - I think you prematurely curtailed the code you were copying, as it appears to stop before the end on your last post.
@countrypaul
Not sure how can i change it to print it like i want to ![]()
Like i want to type/send command and receive just one result in one line.
and after i press another command i get another result etc.
If you uncomment the first line below, that should indicate how many characters are waiting to be read - I would expect this to be 3 (unless there is a ).
You could then alter the following:
char c = client.read();
// int tmp;
// tmp = (int) c;
// itoa (tmp, retString, 16);
//
//
//
// Serial.print("Returned char val: ");
// Serial.print (retString);
Serial.println("RESULT");
Serial.println(c);
Serial.println("READ BACK");
to :
Serial.println("RESULT");
for (i=0 ; i<camRetLen; i++)
{
char c = client.read();
Serial.print(c);
}
Serial.println();
Serial.println("READ BACK");
That should print out the whole string returned by the camera. If there is a at the end of the returned string you will see a blank line before "READ BACK".
@countrypaul
Okay another problem here, connected everything just like with UNO but to Arduino Due, changed the baud rate to 115200 and when I load the program it is stuck at "Connecting "