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
Revision 15:edeb0aed160d, committed 2014-09-30
- Comitter:
- olympux
- Date:
- Tue Sep 30 21:18:05 2014 +0000
- Parent:
- 14:18eda020a589
- Child:
- 16:84a5bf7285d0
- Commit message:
- Added auto update function using TCP client mode
Changed in this revision
--- a/W5500Interface.lib Sun Sep 28 21:54:34 2014 +0000 +++ b/W5500Interface.lib Tue Sep 30 21:18:05 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/#af0ed4fbca02 +http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/#da52bf0e0e5d
--- a/main.cpp Sun Sep 28 21:54:34 2014 +0000 +++ b/main.cpp Tue Sep 30 21:18:05 2014 +0000 @@ -78,7 +78,7 @@ * Network configuration */ #define TCP_SERVER -//#define TCP_CLIENT +#define TCP_CLIENT #define UDP_SERVER //#define UDP_CLIENT #define NTP @@ -87,9 +87,20 @@ #define TCP_SERVER_RECEIVE_TIMEOUT 3000 #define UDP_SERVER_RECEIVE_TIMEOUT 200 + +// TCP server function +TCPSocketServer tcp_server; +TCPSocketConnection tcp_client; +// TCP client function +TCPSocketConnection tcp_sock; +// UDP server +UDPSocket udp_server; +Endpoint ep_udp_client; +// NTP NTPClient ntp; + /* * Variables for network configuration, server */ @@ -109,7 +120,9 @@ char str_server_ip_addr[16];// for printf, converted from 16-bits u16server_ip_addr uint16_t u16tcp_server_port; // directly loaded from eeprom -char buffer[256]; // socket buffer +char tcp_client_buffer[256]; // socket buffer +char udp_server_buffer[256]; +char tcp_server_buffer[256]; /* @@ -164,6 +177,7 @@ char *msg; }; Queue<message_t, 16> uart_queue; +Queue<bool, 1> auto_update_queue; Mutex uart_mutex; @@ -187,6 +201,19 @@ } +// Timer thread for auto update +void auto_update_timer_thread(void const* args) { + bool update_flag; + + Thread::wait(500); + while(true) { + update_flag = true; + auto_update_queue.put(&update_flag); + Thread::wait(1000*transmit_time_period); + } +} + + /* * Ethernet init */ @@ -221,22 +248,32 @@ message_t message; int n, ret; + Thread::wait(2000); // turn on delay + /* * Configure */ uart.baud(115200); - + printf("\r\nStarting...\r\n"); /* * UI threads */ Thread t1(uart_thread); + Thread t2(auto_update_timer_thread); + + //// send to uart + //buffer[n] = '\0'; + //message.len = n; + //message.msg = buffer; + //uart_queue.put(&message); /* * FLASH */ load_eeprom_network(); + load_eeprom_tcpserver(); /* * Ethernet @@ -252,43 +289,17 @@ * TCP/UDP setup */ #ifdef TCP_SERVER - TCPSocketServer tcp_server; - TCPSocketConnection tcp_client; - tcp_server.bind(tcp_server_local_port); tcp_server.listen(); printf("TCP server started...\r\n"); tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT); #endif - #ifdef TCP_CLIENT - TCPSocketConnection tcp_sock; - if (!tcp_sock.is_connected()) { - ret = tcp_sock.connect("mbed.org", 80); - if (ret > -1) { - printf("Successfully connected to TCP server\r\n"); - char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; - tcp_sock.send_all(http_cmd, sizeof(http_cmd)-1); - - while (true) { - n = tcp_sock.receive(buffer, sizeof(buffer)-1); - if (n <= 0) - break; - buffer[n] = '\0'; - printf("Received %d chars from server:\n%s\n", n, buffer); - } - tcp_sock.close(); - } - else printf("Connecting to TCP server failed\r\n"); - } + //RtosTimer tcp_client_auto_update_timer(tcp_client_auto_update, osTimerPeriodic, NULL); #endif - #ifdef UDP_SERVER - UDPSocket udp_server; - Endpoint ep_udp_client; - ret = udp_server.bind(udp_server_local_port); printf("UDP started (sock.bind = %d)\r\n", ret); udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT); @@ -299,6 +310,31 @@ * Network processor */ while (true) { +// FOR AUTO TRANSMIT DEVICE STATUS +#ifdef TCP_CLIENT + // connect to TCP server if required + if (!tcp_sock.is_connected()) { + ret = tcp_sock.connect(str_server_ip_addr, u16tcp_server_port); + if (ret > -1) { + printf("Successfully connected to %s on port %d\r\n", str_server_ip_addr, u16tcp_server_port); + } + else { + printf("Unable to connect to %s on port %d\r\n", str_server_ip_addr, u16tcp_server_port); + } + } + + // transmit data if connected + if (tcp_sock.is_connected()) { + osEvent evt = auto_update_queue.get(1); // timeout after 1ms + if (evt.status == osEventMessage) { + printf("Updating...\r\n"); + update_sending_frame(tcp_client_buffer); + tcp_sock.send_all(tcp_client_buffer, SENDING_PROTOCOL_LENGTH); + } + } +#endif + + // FOR INTERFACING #ifdef TCP_SERVER // no tcp client connected @@ -314,66 +350,56 @@ // loop waiting and receiving data within timeout tcp_client.set_blocking(false, TCP_SERVER_RECEIVE_TIMEOUT); // Timeout after x seconds while (true) { - n = tcp_client.receive(buffer, sizeof(buffer)); + n = tcp_client.receive(tcp_server_buffer, sizeof(tcp_server_buffer)); if (n <= 0) break; // got some data, test it - printf("TCP server received: %s\r\n", buffer); - - //// 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; - + printf("TCP server received: %s\r\n", tcp_server_buffer); // process received data switch (n) { // length 58-bytes, Receiving protocol case RECEIVING_PROTOCOL_LENGTH: { printf("Checking device ID..."); // check device id - char* id = strstr(buffer, DEVICE_ID); + char* id = strstr(tcp_server_buffer, DEVICE_ID); if (id == NULL) break; - else if ((id - buffer) > 0) + else if ((id - tcp_server_buffer) > 0) break; printf("Correct.\r\n"); // firstly, update outputs if required // digital outputs - if (buffer[RECEIVING_PROTOCOL_EN_DO_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { + if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_DO_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { printf("Update digital outputs\r\n"); char str_dout[9]; - memcpy(str_dout, &buffer[RECEIVING_PROTOCOL_DO_POS], 8); + memcpy(str_dout, &tcp_server_buffer[RECEIVING_PROTOCOL_DO_POS], 8); str_dout[8] = '\0'; update_digital_outputs(str_dout); } // analog output 0 - if (buffer[RECEIVING_PROTOCOL_EN_A0O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { + if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_A0O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { printf("Update analog output 0\r\n"); } // analog output 1 - if (buffer[RECEIVING_PROTOCOL_EN_A1O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { + if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_A1O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { printf("Update analog output 1\r\n"); } // UART - if (buffer[RECEIVING_PROTOCOL_EN_UART_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { + if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_UART_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { printf("UART data: "); char str_uart[33]; - memcpy(str_uart, &buffer[RECEIVING_PROTOCOL_UART_POS], 32); + memcpy(str_uart, &tcp_server_buffer[RECEIVING_PROTOCOL_UART_POS], 32); str_uart[32] = '\0'; printf("%s\r\n", str_uart); } // then, check query status command and sending protocol if required - if (buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) { + if (tcp_server_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) { printf("Sent device status through TCP\r\n"); // sending protocol - update_sending_frame(buffer); - tcp_client.send_all(buffer, SENDING_PROTOCOL_LENGTH); + update_sending_frame(tcp_server_buffer); + tcp_client.send_all(tcp_server_buffer, SENDING_PROTOCOL_LENGTH); } break; @@ -381,7 +407,6 @@ default: break; } - } // end loop if no data received within timeout tcp_client.close(); } // if client connected @@ -393,43 +418,33 @@ // ONLY FOR CONFIGRATION #ifdef UDP_SERVER // wait for udp packet within timeout - n = udp_server.receiveFrom(ep_udp_client, buffer, sizeof(buffer)); + n = udp_server.receiveFrom(ep_udp_client, udp_server_buffer, sizeof(udp_server_buffer)); if (n <= 0) continue; // got some data, test it - printf("UDP received: %s\r\n", buffer); - - //// send to uart - //buffer[n] = '\0'; - //message.len = n; - //message.msg = buffer; - //uart_queue.put(&message); - //// echo - //printf("Received packet from: %s\r\n", client.get_address()); - //udp_server.sendTo(ep_udp_client, buffer, n); - + printf("UDP received: %s\r\n", udp_server_buffer); // process received data switch (n) { // length = 6, a CONFIGURATION command (discovery command, TCP port, or UDP port) // Format: NNIODS, NNIOTP or NNIOUP case 6: // discovery command - if (strstr(buffer, "NNIODS") != NULL) { + if (strstr(udp_server_buffer, "NNIODS") != NULL) { udp_server.sendTo(ep_udp_client, eth.getIPAddress(), strlen(eth.getIPAddress())); } // NNIODS // ask for TCP server port - else if (strstr(buffer, "NNIOTP") != NULL) { + else if (strstr(udp_server_buffer, "NNIOTP") != NULL) { char port[5]; sprintf(port, "%5d", tcp_server_local_port); udp_server.sendTo(ep_udp_client, port, strlen(port)); } // NNIOTP // ask for UDP server port - else if (strstr(buffer, "NNIOUP") != NULL) { + else if (strstr(udp_server_buffer, "NNIOUP") != NULL) { char port[5]; sprintf(port, "%5d", udp_server_local_port); udp_server.sendTo(ep_udp_client, port, strlen(port)); } // NNIOUP - else if (strstr(buffer, "NNIOTM") != NULL) { + else if (strstr(udp_server_buffer, "NNIOTM") != NULL) { #ifdef NTP char str_time[50]; @@ -461,14 +476,29 @@ // (NNIO; IP: 192.168.0.120; Subnet: 255.255.255.0; GW: 192.168.0.1; MAC: 0 0 1) case 19:{ // check device id - char* id = strstr(buffer, DEVICE_ID); + char* id = strstr(udp_server_buffer, DEVICE_ID); if (id == NULL) break; - else if ((id - buffer) > 0) + else if ((id - udp_server_buffer) > 0) break; printf("Received user configuration\r\n"); - write_eeprom_network(&buffer[4]); // parameters from 3rd char, 15-bytes + write_eeprom_network(&udp_server_buffer[strlen(DEVICE_ID)]); // parameters from 5th char, 15-bytes + break; + } + // length = 12, SET TCP SERVER CONFIGURATION + // auto update & its time period, TCP server configuration (IP & port) + // Format: 4E 4E 49 4F 5A 01 C0 A8 00 09 E0 2E (LSB MSB) + // NNIO Auto 1s 192.168.0.9 12000 + case 12: { + char* id = strstr(udp_server_buffer, DEVICE_ID); + if (id == NULL) + break; + else if ((id - udp_server_buffer) > 0) + break; + + printf("Received TCP server configuration\r\n"); + write_eeprom_tcpserver(&udp_server_buffer[strlen(DEVICE_ID)]); // parameters from 5th char break; } default: @@ -517,14 +547,14 @@ buf[SENDING_PROTOCOL_DO_POS+6] = (dout6 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; buf[SENDING_PROTOCOL_DO_POS+7] = (dout7 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; - uint16_t val = ain0.read_u16(); - memcpy(&buf[SENDING_PROTOCOL_AI0_POS], &val, 2); - val = ain1.read_u16(); - memcpy(&buf[SENDING_PROTOCOL_AI1_POS], &val, 2); - val = 0; - memcpy(&buf[SENDING_PROTOCOL_AO0_POS], &val, 2); - val = 0; - memcpy(&buf[SENDING_PROTOCOL_AO1_POS], &val, 2); + uint16_t val = ain0.read_u16(); // 16-bits normalised + memcpy(&buf[SENDING_PROTOCOL_AI0_POS], &val, 2); // LSB MSB + val = ain1.read_u16(); // 16-bits normalised + memcpy(&buf[SENDING_PROTOCOL_AI1_POS], &val, 2); // LSB MSB + val = 0x0180; + memcpy(&buf[SENDING_PROTOCOL_AO0_POS], &val, 2); // LSB MSB + val = 0x0180; + memcpy(&buf[SENDING_PROTOCOL_AO1_POS], &val, 2); // LSB MSB buf[SENDING_PROTOCOL_CR_POS] = 0x0D; buf[SENDING_PROTOCOL_CR_POS+1] = '\0'; } \ No newline at end of file
--- a/my_eeprom_funcs.lib Sun Sep 28 21:54:34 2014 +0000 +++ b/my_eeprom_funcs.lib Tue Sep 30 21:18:05 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/olympux/code/my_eeprom_funcs/#6bdc99dd8e0a +http://mbed.org/users/olympux/code/my_eeprom_funcs/#57d40eaa1b1b
--- a/readme.txt Sun Sep 28 21:54:34 2014 +0000 +++ b/readme.txt Tue Sep 30 21:18:05 2014 +0000 @@ -21,8 +21,13 @@ 5. Set new network configuration + Send: 19 bytes in total, NNIO + 15-byte - 15-byte = 4-byte IP address + 4-byte subnet + 4-byte gateway + 3-byte MAC + NNIO 4-byte IP address 4-byte subnet 4-byte gateway 3-byte MAC + 4E 4E 49 4F C0 A8 00 78 FF FF FF 00 C0 A8 00 01 00 00 01 +6. Set TCP server info (only when the device is as a TCP client) + + Send: 12 bytes in total, NNIO + 8 bytes + NNIO 1-byte auto flag 1-byte time period (s) 4-byte IP 2-byte port (LSB MSB) + 4E 4E 49 4F A5 or others 05 (5s) C0 A8 00 09 E0 2E (0x2EE0 = 12000) INTERFACING SECTION (TCP) @@ -32,7 +37,7 @@ OP: 4F 4F 4F 4F 51 IP: C0 A8 00 78 DO: 48 48 48 48 48 48 48 48 -AO: 80 00 80 00 +AO: 80 00 80 00 (no DAC) UART: 32-byte CR: 0D @@ -51,9 +56,9 @@ 'H': output set to high 'L': output set to low + Field AO_0 (2-bytes): 16-bit analog output value, channel 0 - Range is from 0x0000 to 0x0FFF + no DAC + Field AO_1 (2-bytes): 16-bit analog output value, channel 1 - Range is from 0x0000 to 0x0FFF + no DAC + UART output (32-bytes): max 32 bytes, stop string by NULL + End char: CR @@ -67,14 +72,14 @@ + Field DO[n]: 8-bit digital output values 'H' if output is HIGH 'L' if output is LOW - + Field AI_0 (2-bytes): analog 16-bit input value, channel 0 - Range is from 0x0000 to 0x0FFF - + Field AI_1 (2-bytes): analog 16-bit input value, channel 1 - Range is from 0x0000 to 0x0FFF + + Field AI_0 (2-bytes): analog 16-bit input value, normalised, channel 0 + 1st byte = LSB, 2nd byte = MSB + + Field AI_1 (2-bytes): analog 16-bit input value, normalised, channel 1 + 1st byte = LSB, 2nd byte = MSB + Field AO_0 (2-bytes): 16-bit analog output value, channel 0 - Range is from 0x0000 to 0x0FFF + no DAC, fixed + Field AO_1 (2-bytes): 16-bit analog output value, channel 1 - Range is from 0x0000 to 0x0FFF + no DAC, fixed + End char: CR 6 Commands: