Webserver only w/o any other functions, single thread. Running on STM32F013+W5500
Dependencies: NTPClient W5500Interface Watchdog device_configuration eeprom_flash mbed-rpc-nucleo mbed-rtos mbed
Fork of F103-Serial-to-Ethernet by
main.cpp
- Committer:
- olympux
- Date:
- 2014-09-21
- Revision:
- 3:972ed747474c
- Parent:
- 2:18f10e7209f4
- Child:
- 4:568c97f2a407
File content as of revision 3:972ed747474c:
/* * * Alarm and Monitoring application */ #include "mbed.h" #include "EthernetInterface.h" #include "rtos.h" /* * Hardware defines */ #define ST_NUCLEO // hardware pin mapping #ifdef ST_NUCLEO // ST Nucleo SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk EthernetInterface eth(&spi, PC_8, PC_9); // spi, cs, reset #endif Serial uart(USBTX,USBRX); /* * Network configuration */ #define TCP_SERVER_PORT 10000 #define UDP_LOCAL_PORT 11000 //#define USE_DHCP // DHCP or static #ifndef USE_DHCP // for static IP setting const char * IP_Addr = "192.168.0.120"; const char * IP_Subnet = "255.255.255.0"; const char * IP_Gateway = "192.168.0.1"; #endif #define TCP_SERVER //#define TCP_CLIENT //#define UDP_SERVER //#define UDP_CLIENT //#define NTP char buffer[256]; // socket buffer /* * RTOS */ struct message_t { int len; char *msg; }; Queue<message_t, 16> uart_queue; Mutex uart_mutex; /* * Threads */ void uart_thread(void const *args) { message_t *p_message; while (true) { osEvent evt = uart_queue.get(); if (evt.status == osEventMessage) { p_message = (message_t*)evt.value.p; uart_mutex.lock(); //uart.printf("len=%d\n", p_message->len); uart.printf("%s\n", p_message->msg); uart_mutex.unlock(); } } } int main() { message_t message; /* * configure */ uart.baud(115200); /* * UI threads */ Thread t1(uart_thread); /* * Ethernet */ uint8_t mac[6]; mbed_mac_address((char *)mac); // using the MAC address in LPC11U24 or LPC1178 mac[0] = 0x00; mac[1] = 0x08; mac[2] = 0xDC; mac[3] = 0x00; mac[4] = 0x00; mac[5] = 0x00; printf("Start\n"); #ifdef USE_DHCP int ret = eth.init(mac); //Use DHCP #else int ret = eth.init(mac, IP_Addr, IP_Subnet, IP_Gateway); // static #endif if (!ret) { uart.printf("Initialized, MAC: %s\n", eth.getMACAddress()); } else { uart.printf("Error eth.init() - ret = %d\n", ret); return -1; } ret = eth.connect(); if (!ret) { uart.printf("IP: %s, MASK: %s, GW: %s\n", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); } else { uart.printf("Error eth.connect() - ret = %d\n", ret); return -1; } #ifdef TCP_SERVER TCPSocketServer tcp_server; TCPSocketConnection tcp_client; tcp_server.bind(TCP_SERVER_PORT); tcp_server.listen(); #endif #ifdef UDP_SERVER UDPSocket udp_server; Endpoint ep_udp_client; ret = udp_server.bind(UDP_LOCAL_PORT); //printf("sock.bind = %d\n", ret); #endif #ifdef TCP_CLIENT TCPSocketConnection tcp_socket; #endif #ifdef UDP_CLIENT UDPSocket udp_socket; Endpoint ep_udp_server; #endif /* * TCP server */ #ifdef TCP_SERVER while (true) { //uart.printf("\nWait for new connection...\n"); tcp_server.accept(tcp_client); tcp_client.set_blocking(false, 10000); // Timeout after (10)s //uart.printf("Connection from: %s\n", client.get_address()); while (true) { int n = tcp_client.receive(buffer, sizeof(buffer)); if (n <= 0) break; // send to uart buffer[n] = '\0'; message.len = n; message.msg = buffer; uart_queue.put(&message); // echo to tcp client tcp_client.send_all(buffer, n); if (n <= 0) break; } tcp_client.close(); } #endif /* * UDP server */ #ifdef UDP_SERVER while (true) { //printf("\nWait for packet...\n"); int n = udp_server.receiveFrom(ep_udp_client, buffer, sizeof(buffer)); if (n < 0) continue; // send to uart buffer[n] = '\0'; message.len = n; message.msg = buffer; uart_queue.put(&message); // echo //printf("Received packet from: %s\n", client.get_address()); udp_server.sendTo(ep_udp_client, buffer, n); } #endif /* * TCP client */ #ifdef TCP_CLIENT while (tcp_socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); wait(1); } char hello[] = "Hello World\n"; tcp_socket.send_all(hello, sizeof(hello) - 1); char buf[256]; int n = tcp_socket.receive(buf, 256); buf[n] = '\0'; printf("%s", buf); tcp_socket.close(); eth.disconnect(); while(true) {} #endif /* * UDP client */ #ifdef UDP_CLIENT ret = udp_socket.init(); udp_socket.bind(0); printf("sock.bind = %d\n", ret); if (ret == -1) printf("Socket creation Fail\n"); ep_udp_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); printf("\nSend UDP data\n"); char out_buffer[] = "Hello World\n"; ret = udp_socket.sendTo(ep_udp_server, out_buffer, sizeof(out_buffer)); if (ret < 0) printf("UDP Send Error\n"); else printf("UDP Send: %d\n", ret); char in_buffer[256]; int n = udp_socket.receiveFrom(ep_udp_server, in_buffer, sizeof(in_buffer)); in_buffer[n] = '\0'; printf("%s\n", in_buffer); udp_socket.close(); eth.disconnect(); while(1) {} #endif /* * NTP */ #ifdef NTP time_t ctTime; ctTime = time(NULL); printf("1. Current Time is: %s\r\n", ctime(&ctTime)); printf("Trying to update time...\r\n"); if (ntp.setTime("0.pool.ntp.org") == 0) { ctTime = time(NULL); printf("2. Current Time is: %s\r\n", ctime(&ctTime)); // resetting GMT+9 set_time( time(NULL) + 32400 ); // 9x60x60 // printf("Set time successfully\r\n"); //time_t ctTime; ctTime = time(NULL); printf("Time is set to (UTC): %s\r\n", ctime(&ctTime)); } else { printf("Error\r\n"); } eth.disconnect(); while(1) { } #endif while(true) {} }