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

Files at this revision

API Documentation at this revision

Comitter:
olympux
Date:
Thu Oct 02 19:41:38 2014 +0000
Parent:
15:edeb0aed160d
Child:
17:88ef7a078095
Commit message:
Added DBG.; Checked auto transmit flag.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
my_eeprom_funcs.lib Show annotated file Show diff for this revision Revisions of this file
readme.txt Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Sep 30 21:18:05 2014 +0000
+++ b/main.cpp	Thu Oct 02 19:41:38 2014 +0000
@@ -12,6 +12,23 @@
 #include "my_eeprom_funcs.h"
 
 
+//Debug is disabled by default
+#if 1
+//Enable debug
+#include <cstdio>
+#define DBG(x, ...) std::printf("[main : DBG]"x"\r\n", ##__VA_ARGS__); 
+#define WARN(x, ...) std::printf("[main : WARN]"x"\r\n", ##__VA_ARGS__); 
+#define ERR(x, ...) std::printf("[main : ERR]"x"\r\n", ##__VA_ARGS__); 
+
+#else
+//Disable debug
+#define DBG(x, ...) 
+#define WARN(x, ...)
+#define ERR(x, ...) 
+
+#endif
+
+
 /*
 * Hardware defines
 */
@@ -65,13 +82,18 @@
                                      0x3212, 0x3313, 0x3414, 0x3515, // IP_Gateway
                                      0x4212, // TCP server port, not used
                                      0x5212, // UDP server port, not used
-                                     0x8888, // 1st run? 
+                                     0x8888, // 1st run? 0xA5A5 = configured
                                      0x6212, 0x6313, 0x6414, // MAC
                                      
                                      // this section is for the TCP server that this device connects to in TCP client mode
-                                     0x7212, 0x7313, // auto transmit status, time period
+                                     0x7212, 0x7313, // 0xA5 = auto transmit status, time period
                                      0x8212, 0x8313,0x8414, 0x8515, // TCP server IP address
-                                     0x9212 // TCP server port
+                                     0x9212, // TCP server port
+                                     
+                                     // this section is for selecting protocol
+                                     0xA212, // 0xA5A5 = enable TCP server
+                                     0xA313, // 0xA5A5 = eanble TCP client
+                                     0xA414  // 0xA5A5 = enable UDP client
                                      };
 
 /*
@@ -193,8 +215,8 @@
         if (evt.status == osEventMessage) {
             p_message = (message_t*)evt.value.p;
             uart_mutex.lock(); // mutex for stdio is not neccessary
-            //uart.printf("len=%d\n", p_message->len);
-            uart.printf("%s\n", p_message->msg);
+            //DBG("len=%d\n", p_message->len);
+            DBG("%s\n", p_message->msg);
             uart_mutex.unlock();
         }
     }
@@ -218,21 +240,21 @@
 * Ethernet init
 */
 int ethernet_init(void) {
-    printf("Start initialising ethernet\n");
+    DBG("Start initialising ethernet\n");
     int ret = eth.init(u8mac, str_ip_addr, str_ip_subnet, str_ip_gateway); // static
 
     if (!ret) {
-        printf("Initialized, MAC: %s\n", eth.getMACAddress());
+        DBG("Initialized, MAC: %s\n", eth.getMACAddress());
     } else {
-        printf("Error eth.init() - ret = %d\n", ret);
+        ERR("Error eth.init() - ret = %d\n", ret);
         return -1;
     }
 
     ret = eth.connect();
     if (!ret) {
-        printf("IP: %s, MASK: %s, GW: %s\n", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
+        DBG("IP: %s, MASK: %s, GW: %s\n", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
     } else {
-        printf("Error eth.connect() - ret = %d\n", ret);
+        ERR("Error eth.connect() - ret = %d\n", ret);
         return -1;
     }
     
@@ -254,7 +276,7 @@
     * Configure
     */
     uart.baud(115200);
-    printf("\r\nStarting...\r\n");
+    DBG("\r\nStarting...\r\n");
   
     /*
     * UI threads
@@ -280,7 +302,7 @@
     */
     ret = ethernet_init();
     if (ret) {
-        printf("Ethernet initialisation failed. App halted\r\n");
+        ERR("Ethernet initialisation failed. App halted\r\n");
         while (true) {};
     }
     
@@ -291,7 +313,7 @@
 #ifdef TCP_SERVER
     tcp_server.bind(tcp_server_local_port);
     tcp_server.listen();
-    printf("TCP server started...\r\n");
+    DBG("TCP server started...\r\n");
     tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT);
 #endif
 
@@ -301,7 +323,7 @@
     
 #ifdef UDP_SERVER
     ret = udp_server.bind(udp_server_local_port);
-    printf("UDP started (sock.bind = %d)\r\n", ret);
+    DBG("UDP started (sock.bind = %d)\r\n", ret);
     udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT);
 #endif
 
@@ -310,28 +332,30 @@
     * 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);
+    // FOR AUTO TRANSMIT DEVICE STATUS
+    if (auto_transmit_flag == 0xA5A5) {
+        // 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) {
+                DBG("Successfully connected to %s on port %d\r\n", str_server_ip_addr, u16tcp_server_port);
+            }
+            else {
+                ERR("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);
+        
+        // transmit data if connected
+        if (tcp_sock.is_connected()) {
+            osEvent evt = auto_update_queue.get(1); // timeout after 1ms
+            if (evt.status == osEventMessage) {
+                DBG("Updating...\r\n");
+                update_sending_frame(tcp_client_buffer);
+                tcp_sock.send_all(tcp_client_buffer, SENDING_PROTOCOL_LENGTH);
+            }
         }
-    }
+    } // auto transmit
 #endif
 
 
@@ -345,7 +369,7 @@
             
             // tcp client connected
             if (ret > -1) {
-                printf("Connection from: %s\r\n", tcp_client.get_address());
+                DBG("Connection from: %s\r\n", tcp_client.get_address());
                 
                 // loop waiting and receiving data within timeout
                 tcp_client.set_blocking(false, TCP_SERVER_RECEIVE_TIMEOUT); // Timeout after x seconds
@@ -354,24 +378,24 @@
                     if (n <= 0) break;
                     
                     // got some data, test it
-                    printf("TCP server received: %s\r\n", tcp_server_buffer);
+                    DBG("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...");
+                            DBG("Checking device ID...");
                             // check device id
                             char* id = strstr(tcp_server_buffer, DEVICE_ID);
                             if (id == NULL)
                                 break;
                             else if ((id - tcp_server_buffer) > 0)
                                 break;
-                            printf("Correct.\r\n");
+                            DBG("Correct.\r\n");
                             
                             // firstly, update outputs if required
                             // digital outputs
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_DO_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
-                                printf("Update digital outputs\r\n");
+                                DBG("Update digital outputs\r\n");
                                 char str_dout[9];
                                 memcpy(str_dout, &tcp_server_buffer[RECEIVING_PROTOCOL_DO_POS], 8);
                                 str_dout[8] = '\0';
@@ -379,15 +403,15 @@
                             }
                             // analog output 0
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_A0O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
-                                printf("Update analog output 0\r\n");
+                                DBG("Update analog output 0\r\n");
                             }
                             // analog output 1
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_A1O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
-                                printf("Update analog output 1\r\n");
+                                DBG("Update analog output 1\r\n");
                             }
                             // UART
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_UART_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
-                                printf("UART data: ");
+                                DBG("UART data: ");
                                 char str_uart[33];
                                 memcpy(str_uart, &tcp_server_buffer[RECEIVING_PROTOCOL_UART_POS], 32);
                                 str_uart[32] = '\0';
@@ -396,7 +420,7 @@
                             
                             // then, check query status command and sending protocol if required
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) {
-                                printf("Sent device status through TCP\r\n");
+                                DBG("Sent device status through TCP\r\n");
                                 // sending protocol
                                 update_sending_frame(tcp_server_buffer);
                                 tcp_client.send_all(tcp_server_buffer, SENDING_PROTOCOL_LENGTH);
@@ -422,7 +446,7 @@
         if (n <= 0) continue;
 
         // got some data, test it
-        printf("UDP received: %s\r\n", udp_server_buffer);
+        DBG("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)
@@ -448,23 +472,23 @@
 #ifdef NTP
                     char str_time[50];
                     
-                    printf("Trying to update time...\r\n");
+                    DBG("Trying to update time...\r\n");
                     if (ntp.setTime("0.pool.ntp.org") == 0) {
-                        printf("Set time successfully\r\n");
+                        DBG("Set time successfully\r\n");
                         time_t ctTime;
                         ctTime = time(NULL);
                         
-                        printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
+                        DBG("Time is set to (UTC): %s\r\n", ctime(&ctTime));
                         sprintf(str_time, "%s", ctime(&ctTime));
                         udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
                     }
                     else {
-                        printf("Error\r\n");
+                        WARN("Error\r\n");
                         sprintf(str_time, "ERR");
                         udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
                     }
 #elif
-                    printf("NTP disabled\r\n");
+                    WARN("NTP disabled\r\n");
                     sprintf(str_time, "DIS");
                     udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
 #endif
@@ -482,7 +506,7 @@
                 else if ((id - udp_server_buffer) > 0)
                     break;
 
-                printf("Received user configuration\r\n");
+                DBG("Received user configuration\r\n");
                 write_eeprom_network(&udp_server_buffer[strlen(DEVICE_ID)]); // parameters from 5th char, 15-bytes
                 break;
             }
@@ -497,7 +521,7 @@
                 else if ((id - udp_server_buffer) > 0)
                     break;
                 
-                printf("Received TCP server configuration\r\n");
+                DBG("Received TCP server configuration\r\n");
                 write_eeprom_tcpserver(&udp_server_buffer[strlen(DEVICE_ID)]); // parameters from 5th char
                 break;
             }
@@ -512,7 +536,7 @@
 * Update digital outputs following receiving frame from TCP client
 */
 void update_digital_outputs(char* buf) {
-    printf("Digital outputs: %s\n", buf);
+    DBG("Digital outputs: %s\n", buf);
     
     dout0 = (buf[0] == DIGITAL_HIGH)? 1 : 0;
     dout1 = (buf[1] == DIGITAL_HIGH)? 1 : 0;
--- a/my_eeprom_funcs.lib	Tue Sep 30 21:18:05 2014 +0000
+++ b/my_eeprom_funcs.lib	Thu Oct 02 19:41:38 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/olympux/code/my_eeprom_funcs/#57d40eaa1b1b
+http://mbed.org/users/olympux/code/my_eeprom_funcs/#69e1c4ed69e1
--- a/readme.txt	Tue Sep 30 21:18:05 2014 +0000
+++ b/readme.txt	Thu Oct 02 19:41:38 2014 +0000
@@ -27,7 +27,7 @@
 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)
+      4E 4E 49 4F  'Y' or others            05 (5s)                   C0 A8 00 09       E0 2E (0x2EE0 = 12000)
 
 
 INTERFACING SECTION (TCP)