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 Chau Vo

Revision:
15:edeb0aed160d
Parent:
14:18eda020a589
Child:
16:84a5bf7285d0
--- 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