Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: W5500Interface WebSocketClient mbed
Fork of Websocket_Ethernet_W5500 by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "Websocket.h" 00004 00005 #define _DHCP 00006 //#define _USE_WIZ550io 00007 DigitalOut myled(LED1); 00008 00009 int main() 00010 { 00011 #if defined(TARGET_LPC1114) 00012 SPI spi(dp2, dp1, dp6); // mosi, miso, sclk 00013 EthernetInterfaceW5500 eth(&spi, dp25, dp26); // spi, cs, reset 00014 00015 #elif defined(TARGET_LPC1768) 00016 SPI spi(p11, p12, p13); // mosi, miso, sclk 00017 EthernetInterfaceW5500 eth(&spi, p14, p15); // spi, cs, reset 00018 00019 #elif defined(TARGET_LPC11U68) 00020 SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk 00021 EthernetInterfaceW5500 eth(&spi, P0_2, P1_28);//scs(P0_2), nRESET(P1_28) 00022 spi.format(8,0); // 8bit, mode 0 00023 spi.frequency(7000000); // 7MHz 00024 wait(1); // 1 second for stable state 00025 #elif defined(TARGET_KL25Z) 00026 Serial pc(USBTX, USBRX); 00027 pc.baud(115200); 00028 printf("spi init\r\n"); 00029 SPI spi(D11, D12, D13); // mosi, miso, sclk 00030 wait(1); // 1 second for stable state 00031 EthernetInterface eth(&spi, D10, D9);//scs(D10), nRESET(PTA20) 00032 printf("App Start\r\n"); 00033 wait(1); // 1 second for stable state 00034 #endif 00035 00036 #ifndef _USE_WIZ550io 00037 // as your env. change to real MAC address; 00038 uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x1c, 0xb8, 0x4a}; // for example 00039 #endif 00040 00041 #ifdef _DHCP 00042 00043 #ifdef _USE_WIZ550io 00044 eth.init(); 00045 #else 00046 eth.init((uint8_t *)mac_addr); //Use DHCP 00047 #endif 00048 eth.connect(); 00049 //printf("IP Address is %s\n\r", eth.getIPAddress()); 00050 printf("Initialized, MAC: %s\n", eth.getMACAddress()); 00051 printf("Connected, IP: %s, MASK: %s, GW: %s\n", 00052 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); 00053 00054 #else 00055 // as your env. change to real IP address and so on. 00056 #ifdef _USE_WIZ550io 00057 //int ret = eth.init("222.98.173.212", "255.255.255.192", "222.98.173.254"); 00058 int ret = eth.init("192.168.77.10", "255.255.255.0", "192.168.77.1"); 00059 #else 00060 int ret = eth.init((uint8_t *)mac_addr, "192.168.77.10", "255.255.255.0", "192.168.77.1"); 00061 #endif 00062 if (!ret) { 00063 printf("Initialized, MAC: %s\n", eth.getMACAddress()); 00064 printf("Connected, IP: %s, MASK: %s, GW: %s\n", 00065 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); 00066 } else { 00067 printf("Error eth.init() - ret = %d\n", ret); 00068 return -1; 00069 } 00070 00071 #endif 00072 00073 // Websocket ws("ws://sockets.mbed.org:443/ws/demo/wo"); 00074 // ws.connect(); 00075 00076 // this code from http://mbed.org/teams/mbed/code/Websocket_Ethernet_HelloWorld/file/bd0a76c1d9d0/main.cpp 00077 // view @ http://sockets.mbed.org/demo/viewer 00078 Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); 00079 ws.connect(); 00080 00081 char str[100]; 00082 for(int i=0; i<0x10; ++i) { 00083 sprintf(str, "%d WebSocket for WIZnet W5500 Hello World over Ethernet : No Sensor", i); 00084 ws.send(str); 00085 00086 // clear the buffer and wait a sec... 00087 memset(str, 0, 100); 00088 wait(0.5f); 00089 00090 // websocket server should echo whatever we sent it 00091 if (ws.read(str)) { 00092 printf("rcv'd: %s\n", str); 00093 } 00094 } 00095 ws.close(); 00096 eth.disconnect(); 00097 00098 while(1) { 00099 myled = 1; 00100 wait(0.2); 00101 myled = 0; 00102 wait(0.2); 00103 } 00104 }
Generated on Wed Jul 13 2022 08:48:50 by
1.7.2
