Jump to content
Echo66

TCP/IP client for Tiva C connected Launchpad

Recommended Posts

Hello Gentlemen,


 


I'm trying to develop a TCP/IP client with the new TI Connected LaunchPad board but I have some problem with the code that I couldn't figure out and maybe someone can give me a hand, here is the code (based on the Web client example but with some modifications):



#include <Ethernet.h>

// Enter a MAC address for your controller below.
byte mac[] = { 0x00, 0x1F, 0xB0, 0x02, 0xFE, 0x30 };
IPAddress ipLocal(169,254,41,200);
IPAddress server(169,254,41,198);


unsigned int localPort = 8080;      // local port to listen on
char SendBuffer[] = "Something to send\n";        //test data
char ReceiveBuffer[64] = " ";        //receive buffer


// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;


void setup() {
 
  //start the Ethernet
  Ethernet.begin(mac,ipLocal, server);
  Ethernet.enableLinkLed();
  Ethernet.enableActivityLed();
 
  //Connect to server
  client.connect(server, localPort);
 
  //start the serial library:
  Serial.begin(9600);
  Serial.println("\n\nTCP/IP Client setup");
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("connecting to server\n");
  //Serial.println(server);
  server.printTo(Serial);




  // if you get a connection, report back via serial:
 
  if (client.connect(server, localPort)) {
    Serial.println("connected to");
    server.printTo(Serial);
    // Make a HTTP request:
    //client.println("GET /search?q=arduino HTTP/1.0");
    //client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}


void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
 
  client.write(SendBuffer);


  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();


    // do nothing forevermore:
    while(true);
  }
}
//End of code

On the serial monitor it displays "TCP/IP Client setup" then stops, when I try to find the board using "arp -a" command from the windows cmd it shows nothing about the board (neither the MAC nor the IP address I have assigned to it), tried to connect to the board using a small sockets example written in C++ and for sure nothing happened (just to make sure windows networking utilities aren't screwed).  


 

Thanks in advance folks :)

Share this post


Link to post
Share on other sites

Hello

 

if you have router with DHCP, you can try  Ethernet.begin(0); and not  Ethernet.begin(mac,ipLocal, server);

It's work perfectly !

Do not use:

// start the Ethernet connection:

//  if (Ethernet.begin(mac) == 0) {
//    Serial.println("Failed to configure Ethernet using DHCP");
//    // no point in carrying on, so do nothing forevermore:
//    while(true);

Share this post


Link to post
Share on other sites
Thanks JCR for the information, quite useful one especially with a newbie to Energia syntax like me, much appreciated.

 

Actually I'm having another problem with this piece of firmware and you might be able to help me solve it, when I turn down the client app on the PC side and re-run it the server isn't able to establish a connection and I need to restart the board to establish a link, though the board is still pingable (still I can ping), have a look on the code and let me know your opinion:

 

#include <Ethernet.h>

#include <EthernetServer.h>

 

EthernetServer server(8888);

boolean alreadyConnected = false; // whether or not the client was connected previously

 

char TxBuffer[15] = "something here\n";

err_t error = 0;

 

void setup() {

  // initialize the ethernet device

  Ethernet.begin(0); 

  // start listening for clients

  server.begin();

  Serial.begin(9600);

  

  Ethernet.enableLinkLed();

  Ethernet.enableActivityLed();

  

  Serial.print("Chat server address:");

  Serial.println(Ethernet.localIP());

}

 

void loop() {

  // wait for a new client:

  EthernetClient client = server.available();

 

  // when the client sends the first byte, say hello:

  if (client) {

    if (!alreadyConnected) {

      // clead out the input buffer:

      client.flush();    

      Serial.println("We have a new client");

      //client.println("Hello, client!"); 

      alreadyConnected = true;

    } 

 

    if (client.available() > 0) {

      // read the bytes incoming from the client:

      char RxBuffer = client.read();

 

      // echo the bytes back to the client:

      client.println(TxBuffer);

    }

  }

}

 

Thanks again for the help and have a good day.

Share this post


Link to post
Share on other sites

hello

you must read the example: EthernetWebServer.ino  in energia Ethernet  examples.

and in your file you must close the connection in the main Loop:

 
 
void loop(){
  client = server.available();
if (client){
    your program..............
 
    // close the connection:
        client.stop();
}// end if (client)
 
}// end Main Loop

Share this post


Link to post
Share on other sites

Hello JCR,

 

Sorry for the late reply, first thanks for the help, much appreciated.

Second, it doesn't work, actually if I put the "client.stop()" this way in the code it kills the whole connection with the client.

 

I have tried also the "client.available()" and nothing as well, will keep digging and update the post with anything new.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×