This example shows using the SeeedStudio arch pro as an Ethernet server. It will initialize the Ethernet port, display its IP address, display the IP address of connecting clients and then return data sent from the client.

Dependencies:   EthernetInterface SeeedGrayOLED USBDevice mbed-rtos mbed

Fork of Ethernet_Status_HelloWorld by Edoardo De Marchi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Author: Dan Cohen
00003  * Date: 15/02/14
00004  * Notes: Displays the IP address then any data that is sent from a connecting client
00005  * Client command line on my system:  
00006  * ./client 192.168.2.3 2000
00007  * 
00008 */
00009 
00010 
00011 #include "mbed.h"
00012 #include "EthernetInterface.h"
00013 #include "eth_status.h"
00014 #include "SeeedGrayOLED.h"
00015 #include "USBSerial.h"
00016 
00017 DigitalOut led1(LED1);
00018 DigitalOut led2(LED2);
00019 DigitalOut led3(LED3);
00020 DigitalOut led4(LED4);
00021 
00022 SeeedGrayOLED SeeedGrayOled(P0_10, P0_11);
00023 
00024 USBSerial serial;
00025 
00026 //ETHERNET
00027 #define ECHO_SERVER_PORT   2000
00028 
00029 EthernetInterface    eth;
00030 TCPSocketServer      server;
00031 TCPSocketConnection  connection;
00032 
00033 char readBuffer[1024];
00034 
00035 // This is required because the Seeed Arch Pro doesn't have a 
00036 // unique MAC address (if you don't have this function then the Ethernet Interface init()
00037 // function will hang when it tries to find it.
00038 extern "C" void mbed_mac_address(char *mac)
00039 {
00040 
00041     mac[0] = 0x00;
00042     mac[1] = 0x02;
00043     mac[2] = 0xF7;
00044     mac[3] = 0xF1;
00045     mac[4] = 0x91;
00046     mac[5] = 0x9F;
00047 };
00048 
00049 int Init()
00050 {
00051     led1 = 1;
00052     led2 = 1;
00053     led3 = 1;
00054     led4 = 1;
00055 
00056     // Initialize the OLED display
00057     SeeedGrayOled.init();             // initialize SEEED OLED display
00058     SeeedGrayOled.clearDisplay();     // Clear Display.
00059     SeeedGrayOled.setNormalDisplay(); // Set Normal Display Mode
00060     SeeedGrayOled.setVerticalMode();  // Set to vertical mode for displaying text
00061 
00062     SeeedGrayOled.setTextXY(1,0);     //set Cursor to first line, 0th column
00063 
00064     SeeedGrayOled.setGrayLevel(15);   //Set Grayscale level. Any number between 0 - 15.
00065 
00066     eth.init();
00067     serial.printf("initiated\n");
00068 
00069     eth.connect();
00070     serial.printf("connected\n");
00071     serial.printf("My IP address: %s\n\n", eth.getIPAddress());
00072     SeeedGrayOled.clearDisplay();     // Clear Display.
00073     SeeedGrayOled.setTextXY(1,0); 
00074     SeeedGrayOled.putString("My IP:");
00075     SeeedGrayOled.setTextXY(3,0); 
00076     SeeedGrayOled.putString(eth.getIPAddress());
00077 
00078     server.bind(ECHO_SERVER_PORT);
00079     serial.printf("bound to port %d\n", ECHO_SERVER_PORT);
00080     server.listen(1);
00081     serial.printf("%s  - Speed: %d Mbps\n", get_transmission_status(), get_connection_speed());
00082 
00083     return 0;
00084 }
00085 
00086 int main()
00087 {
00088 
00089     Init();
00090 
00091     while(true) {
00092         serial.printf("\nWait for new connection...\n");
00093         server.accept(connection);
00094 
00095         led3 = 0;
00096 
00097         serial.printf("Connection from: %s\n", connection.get_address());
00098         SeeedGrayOled.setTextXY(5,0); 
00099         SeeedGrayOled.putString("Connect IP: ");
00100         SeeedGrayOled.setTextXY(7,0);         
00101         SeeedGrayOled.putString(connection.get_address());
00102 
00103         char buffer[300];
00104         int ret;
00105 
00106         ret = connection.receive(buffer, sizeof(buffer)-1);
00107         if (ret <= 0)  {
00108             serial.printf("didn't get any message from the server connection\n");
00109         } else {
00110             buffer[ret] = '\0';
00111             serial.printf("Received %d chars from server:\n%s\n", ret, buffer);
00112         }
00113 
00114         char retBuffer[300];
00115         sprintf(retBuffer, "%d", buffer);
00116 
00117         serial.printf("Sending %d chars back to client :\n%s\n", ret, buffer);
00118         ret = connection.send_all   (buffer, sizeof(buffer)-1);
00119         SeeedGrayOled.setTextXY(9,0);        
00120         SeeedGrayOled.putString("D:          ");   
00121         SeeedGrayOled.setTextXY(9,4);        
00122         SeeedGrayOled.putString(buffer);
00123         
00124         connection.close();
00125 
00126         led3 = 1;
00127 
00128     }
00129 
00130 }