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

main.cpp

Committer:
olympux
Date:
2014-09-20
Revision:
2:18f10e7209f4
Parent:
0:c2eac797face
Child:
3:972ed747474c

File content as of revision 2:18f10e7209f4:

/*
*
*  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 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



/*
* RTOS
*/
struct message_t {
    int len;
    char *msg;
};
Queue<message_t, 16> queue;



/*
* Threads
*/
void uart_thread(void const *args) {
    message_t *p_message;
    
    while (true) {
        osEvent evt = queue.get();
        if (evt.status == osEventMessage) {
            p_message = (message_t*)evt.value.p;
            uart.printf("len=%d\n",p_message->len);
            uart.printf("msg=%s\n",p_message->msg);
        }
    }
}


int main()
{
    Thread t1(uart_thread);
    //Thread t2(tcp_server_thread);
    
    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; 
// you can alo use WIZ550io's MAC address by enabling "USE_WIZ550IO_MAC" in wiznet.h
    
    uart.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;
    }

    TCPSocketServer server;
    server.bind(TCP_SERVER_PORT);
    server.listen();

    while (true) {
        uart.printf("\nWait for new connection...\n");
        TCPSocketConnection client;
        server.accept(client);
        client.set_blocking(false, 10000); // Timeout after (10)s

        uart.printf("Connection from: %s\n", client.get_address());
        char buffer[256];
        message_t message;
        while (true) {
            int n = client.receive(buffer, sizeof(buffer));
            if (n <= 0) break;
            
            // send to uart
            buffer[n] = '\0';
            message.len = n;
            message.msg = buffer;
            queue.put(&message);
            
            // echo to tcp client
            client.send_all(buffer, n);
            if (n <= 0) break;
        }

        client.close();
    }
}