I just got my Ethernet Shield and started off with the Ethernet TelnetClient example. My goal is to write a program to let the arduino run a telnet command get the answer (but before that delete some parts of it) and then use the value for some 7 Segments and servos etc....
1.My first question is concerning the IP Settings: In out network we use DHCP to distribute the IPs and DNS, Gateway etc... On the Arduino I see I need to use a static IP address as I am using on the Telnet Server HOST. Now do I only need to set the IP and a random MAC-Address (I got the shield from DFROBOT with no default) and have the connection working with the network?
2.Now I connect to the telnet server by setting his IP and port. That works. But now there is question 2: how do i tell the Arduino to run the command "Arn.Preg:123:" ?
3.Now I would store the output with the read command in a variable and pass it on in my program. But first I need to get rid of the Arn.Resp and the ":" and only get the number. is there a way to do so? (probably there is but I didn't find it.)
I hope you can help me with my program, i really "love" my Arduino it is BRILLIANT!!!!
1.My first question is concerning the IP Settings: In out network we use DHCP to distribute the IPs and DNS, Gateway etc... On the Arduino I see I need to use a static IP address as I am using on the Telnet Server HOST. Now do I only need to set the IP and a random MAC-Address (I got the shield from DFROBOT with no default) and have the connection working with the network?
Just start with a static IP address, there are DHCP libs for Arduino that can be added later. Sometimes the MAC address is on a sticker on the shield (or the bag/box it came in), you musttake care that the MAC and IP addresses are unique.
2.Now I connect to the telnet server by setting his IP and port. That works. But now there is question 2: how do i tell the Arduino to run the command "Arn.Preg:123:" ?
Please clarify, is this a string that the Arduino must send to the server, or something the Arduino gets back from the server.
3.Now I would store the output with the read command in a variable and pass it on in my program. But first I need to get rid of the Arn.Resp and the ":" and only get the number. is there a way to do so? (probably there is but I didn't find it.)
Yes, you should parse the string. If it is the same text allways, you can get the substring by copying only the relevant part. This can be done with strcpy, see snippet.
There are zillion examples about parsing, search for substring parsing split fields etc.
Ok that helped a lot^^but I still can't connect to the host. From Terminal (i'm using mac) I can connect easily with " telnet x.x.x.x 8090" and then it works but here it never gets a connection.... I also added some delay to it after Ethernet.begin and/or before the read, write....
Hypothesis:
Problem could be the subnetmask; as you use an 10.x.x.x network, the netmask should be 255.0.0.0 iso 255.255.255.0 (Class A network IIRC ) which is automatically made when the subnet and gateway are not passed to the Ethernet class = see sources of ethernet.lib
Could you try this.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 }; <<<<<<<<<<< fill in your own
byte sn[] = { 255, 0, 0, 0};
byte gw[] = { 10, 0, 0, 1}; <<<<<<<<<<< fill in your own
byte telnetServer[] = { 64, 233, 187, 99 }; <<<<<<<<<<< fill in your own
Client client(telnetServer, 8090);
void setup()
{
Ethernet.begin(mac, ip, nm, gw);
Serial.begin(115200);
delay(1000);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("Arn.Preg:123:");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
if (client.available() > 0) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
robtillaart:
Problem could be the subnetmask; as you use an 10.x.x.x network, the netmask should be 255.0.0.0 iso 255.255.255.0 (Class A network IIRC )...
CIDR networks obsoleted class A networks a very long time ago. Very, very few people who use 10.x networks still use the entire 10.0/8 as a single broadcast domain.
btw: can I use the MAC Address that is in the code or do I have to change it?
edit I just found something interesting - arduino tells me "disconnecting" and not connection failed, what means, that he probably gets over the setup function but then he can't communicate in the main function (i hope you know what I mean)
the program is called UIPCX and runs as a PLUG in for Xplane ( a flight simulator) It distributes variables that are preset in a config over network, that way you can run various programs that use the IOCP server (that is what it is called) for example SIOC or PRosim737. In the readme of this plugin there is a short example to test if the server is running. It says you can connect with telnet and then can either call a variable with the command "Arn.Preg:number of variable:" (I corrected the ":" in your sample code) or subscribe to a feed for that variable that gets updatet
q2: I did and it gives me: connecting.... disconnecting
nothing works for me. I just tried the other examples and it seems as if the Arduino doesn't connect to my network. Neither the TX/RX/LINK LEDS on the board or the leds on my switch or router light up when I connect it. Maybe it is defect???? I just emailed the vendor to see if I can get a new one.
Is there anything else I can try??? I just want to see if the shield works....
mrmaster:
nothing works for me. I just tried the other examples and it seems as if the Arduino doesn't connect to my network. Neither the TX/RX/LINK LEDS on the board or the leds on my switch or router light up when I connect it. Maybe it is defect???? I just emailed the vendor to see if I can get a new one.
That's a layer-1 problem - you don't have an Ethernet link, and there's no point trying anything in sketches until you do.
How are you powering the Arduino and Ethernet shield? If it's USB powered, the voltage may not be high enough. I have a Freetronics Etherten which needs to be powered from an external 12V PSU for the Ethernet chip to initialise properly.