6 years, 11 months ago.

Ethernet connection between two mbeds

hi guys

I'm trying to connect two mbeds with ethernet. I have never done something with ethernet before, so it's all new for me. Can someone help me? I took the main code from https://developer.mbed.org/handbook/Socket

Thanks!

My Code:

Mbed A /media/uploads/Jimmy700/s_main_b.cpp

MbedB /media/uploads/Jimmy700/mbed_b.txt

1 Answer

6 years, 11 months ago.

Hello Jimmy,
I would recommend to get started with something more simple (mbed + PC). However, it's up to you.
If I understand correctly you would like to connect two mbed's directly to each other over an Ethernet cable (without involving an ethernet switch). In that case you should use a crossover cable and one mbed shall be programmed as client and the other one as server. Your code for Mbed A (to be programmed as client) seems OK. I have corrected just the comments regarding the IP adress asignement:

Mbed A (TCP client)

// Mbed A 

#include "mbed.h"
#include "EthernetInterface.h"
#include "uLCD_4DGL.h"

DigitalOut l1(LED1);
DigitalOut l2(LED2);
DigitalOut l3(LED3);
DigitalOut l4(LED4);
uLCD_4DGL uLCD  (p9,p10,p8);      // serial tx, serial rx, reset pin;

const int ECHO_SERVER_PORT = 7;
const char* ECHO_SERVER_ADDRESS = "192.168.1.1";    // Adress of the other Mbed (Mbed B)
 
int main () 
{
    EthernetInterface eth;
    const char* ip = "192.168.1.2";     // MBED A = 2; MBED B = 1
    const char* mask = "255.255.255.0";
    const char* gateway = "192.168.1.10";
    eth.init(ip, mask, gateway);     //I'dont use DHCP bacuse i use two mbeds
    eth.connect();
    uLCD.printf("Client IP Address is %s\n", eth.getIPAddress());
    
    // Connect to Server
    TCPSocketConnection socket;
    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) 
    {
        uLCD.printf("Unable to connect to (%s) \non port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
        wait(1);
    }
    uLCD.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
    
    // Send message to server
    char hello[] = "Hello World";
    uLCD.printf("Sending  message to Server : '%s' \n",hello);
    socket.send_all(hello, sizeof(hello) - 1);
    
    // Receive message from server
    char buf[256];
    int n = socket.receive(buf, 256);
    buf[n] = '\0';
    uLCD.printf("Received message from server: '%s'\n", buf);
    
    // Clean up
    socket.close();
    eth.disconnect();
    
    while(true) {}
}

I would suggest to modify your code for Mbed B (to be programmed as server) as follows:

Mbed B (TCP server)

#include "mbed.h"
#include "EthernetInterface.h"
#include "uLCD_4DGL.h"
 
#define ECHO_SERVER_PORT   7

DigitalOut l1(LED1);
DigitalOut l2(LED2);
DigitalOut l3(LED3);
DigitalOut l4(LED4);
uLCD_4DGL uLCD  (p9,p10,p8);      // serial tx, serial rx, reset pin;
 
int main (void) {
    const char* ip = "192.168.1.1";     // MBED A = 2; MBED B = 1
    const char* mask = "255.255.255.0";
    const char* gateway = "192.168.1.10";

    EthernetInterface eth;

    eth.init(ip, mask, gateway);     //I'dont use DHCP bacuse i use two mbeds
    eth.connect();
    uLCD.printf("\nServer IP Address is %s\n", eth.getIPAddress());
    
    TCPSocketServer server;
    server.bind(ECHO_SERVER_PORT);
    server.listen();
    
    while (true) {
        uLCD.printf("\nWait for new connection...\n");
        TCPSocketConnection client;
        server.accept(client);
        client.set_blocking(false, 1500); // Timeout after (1.5)s
        
        uLCD.printf("Connection from: %s\n", client.get_address());
        char buffer[256];
        while (true) {
            int n = client.receive(buffer, sizeof(buffer));
            if (n <= 0) break;
            
            // print received message to terminal
            buffer[n] = '\0';
            uLCD.printf("Received message from Client :'%s'\n",buffer);
            
            // reverse the message
            char temp;
            for(int f = 0, l = n-1; f<l; f++,l--){
                temp = buffer[f];
                buffer[f] = buffer[l];
                buffer[l] = temp;
                }
            
            // print reversed message to terminal
            uLCD.printf("Sending message to Client: '%s'\n",buffer);
            
            // Echo received message back to client
            client.send_all(buffer, n);
            if (n <= 0) break;
        }
        
        client.close();
    }
}

NOTE: Please notice that I haven't checked the pin assignment for the uLCD.

Accepted Answer

Thank you

I will try the code and tell you if it works. The uLCD (graphic Display for debuging) is working.

Have a nice day.

posted by Jimmy James 12 Apr 2017