![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TELNET TO UART using STM32F103C8 generic board and ENC28J60. It is a very initial version with just for test program. Uses UIPEthernet Library courtesy of Zoltan hudak and Norbert Truchsess. Thanks
Dependencies: UIPEthernet mbed-STM32F103C8T6 mbed Adafruit_GFX
Fork of WebSwitch_ENC28J60 by
Diff: main.cpp
- Revision:
- 5:0ab8292e37da
- Parent:
- 4:d34811deedab
- Child:
- 6:b38a3b476a45
--- a/main.cpp Wed Feb 04 19:27:26 2015 +0000 +++ b/main.cpp Mon Mar 16 09:43:13 2015 +0000 @@ -1,63 +1,67 @@ -/* In this example LED1 is switched on/off using a web browser connected to this HTTP server. +/* + * In this project LED1 on the mbed board is switched on/off using a web browser. + * However, you can easily modify the project to remotely switch on/off any external device. + * The HTTP server is built from an mbed board and an ENC28J60 board. + * ENC28J60 is driven by the UIPEthernet library <https://github.com/ntruchsess/arduino_uip>. * The example is based on the Tuxgraphics Web Switch <http://www.tuxgraphics.org/>. - * This HTTP server is built around the the ENC28J60 chip - * driven by the UIPEthernet library <https://github.com/ntruchsess/arduino_uip> - * ported to mbed. */ - #include <mbed.h> #include <UIPEthernet.h> #include <UIPServer.h> #include <UIPClient.h> #include <string> -using namespace std; +using namespace std; // UIPEthernet is the name of a global instance of UIPEthernetClass. // Do not change the name! It is used within the UIPEthernet library. #if defined(TARGET_LPC1768) -UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs +UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs #elif defined(TARGET_LPC1114) -UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs +UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs #elif defined(TARGET_LPC11U68) -UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs -#elif defined (TARGET_NUCLEO_F103RB) -UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs -#elif defined (TARGET_NUCLEO_F401RE) -UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs -#elif defined (TARGET_NUCLEO_F411RE) -UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs +UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs +#elif defined(TARGET_NUCLEO_F103RB) +UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs +#elif defined(TARGET_NUCLEO_F401RE) +UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs +#elif defined(TARGET_NUCLEO_F411RE) +UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs -// If your board/plaform is not present yet then uncomment the following two lines and replace TARGET_YOUR_BOARD as appropriate. +// If your board/plaform is not present yet then uncomment +// the following two lines and replace TARGET_YOUR_BOARD as appropriate. -//#elif defined (TARGET_YOUR_BOARD) -//UIPEthernetClass UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); // mosi, miso, sck, cs +//#elif defined(TARGET_YOUR_BOARD) +//UIPEthernetClass UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); // mosi, miso, sck, cs #endif // Note: // If it happends that any of the SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS pins collide with LED1 pin -// then either use different SPI port (if available on the board) and change the pin names in the constructor UIPEthernet(...) accordingly -// or instead of using LED1 pin, select a free pin (not used by SPI port) and connect to it an external LED which is connected to a resitor that is connected to the groud. +// then either use different SPI port (if available on the board) and change the pin names +// in the constructor UIPEthernet(...) accordingly or instead of using LED1 pin, select +// a free pin (not used by SPI port) and connect to it an external LED which is connected +// to a 220 Ohm resitor that is connected to the groud. // In the second case remember to replace LED1 in sw(LED1) constructor (see below). +// MAC number must be unique within the connected network. Modify as appropriate. +const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x06 }; - -// MAC number must be unique within the connected network. Modify as appropriate. -const uint8_t MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x06}; // IP address must be also unique and compatible with your network. Change as appropriate. -const IPAddress MY_IP(192,168,1,181); -const uint16_t MY_PORT = 80; // for HTTP connection -EthernetServer myServer = EthernetServer(MY_PORT); +const IPAddress MY_IP(192, 168, 1, 181); +const uint16_t MY_PORT = 80; // for HTTP connection +EthernetServer myServer = EthernetServer(MY_PORT); + // In this example we are turning on/off LED1. -DigitalOut sw(LED1); // Change LED1 to a pin of your choice. However, make sure that it does not collide with any of the SPI pins already used in the UIPEthernet(...) constructor above! +DigitalOut sw(LED1); // Change LED1 to a pin of your choice. -const string PASSWORD = "secret"; // change as you like -const string HTTP_OK = "HTTP/1.0 200 OK"; -const string MOVED_PERM = "HTTP/1.0 301 Moved Permanently\r\nLocation: "; -const string UNAUTHORIZED = "HTTP/1.0 401 Unauthorized"; -string httpHeader; // HTTP header -string httpContent; // HTTP content - +// However, make sure that it does not collide with any of the SPI pins +// already used in the UIPEthernet(...) constructor above! +const string PASSWORD = "secret"; // change as you like +const string HTTP_OK = "HTTP/1.0 200 OK"; +const string MOVED_PERM = "HTTP/1.0 301 Moved Permanently\r\nLocation: "; +const string UNAUTHORIZED = "HTTP/1.0 401 Unauthorized"; +string httpHeader; // HTTP header +string httpContent; // HTTP content // analyse the url given // return values: -1 invalid password @@ -71,9 +75,7 @@ // GET /password/ HTTP/1..... // GET /password/?sw=1 HTTP/1..... // GET /password/?sw=0 HTTP/1..... - -int8_t analyse_get_url(string& str) -{ +int8_t analyse_get_url(string& str) { if(str.substr(5, PASSWORD.size()) != PASSWORD) return(-1); @@ -87,7 +89,7 @@ pos++; - string cmd(str.substr(pos, 5)); + string cmd(str.substr(pos, 5)); if(cmd == "?sw=0") return(0); @@ -98,26 +100,37 @@ return(-3); } -string& moved_perm(uint8_t flag) -{ +/** + * @brief + * @note + * @param + * @retval + */ +string& moved_perm(uint8_t flag) { if(flag == 1) - httpContent = "/" + PASSWORD + "/"; + httpContent = "/" + PASSWORD + "/"; else httpContent = ""; httpContent += "<h1>301 Moved Permanently</h1>\r\n"; - return (httpContent); + return(httpContent); } -string& page(uint8_t status) -{ +/** + * @brief + * @note + * @param + * @retval + */ +string& view(uint8_t status) { httpContent = "<h2>Web Switch</h2>\r\n"; if(status == 1) { httpContent += "<pre>\r\n <font color=#FF0000>ON</font>"; httpContent += " <a href=\"./?sw=0\">[switch off]</a>\r\n"; - } else { + } + else { httpContent += "<pre>\r\n <font color=#00FF00>OFF</font>"; httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n"; } @@ -128,9 +141,14 @@ return httpContent; } -void http_send(EthernetClient& client, string& header, string& content) -{ - char content_length[5] = {}; +/** + * @brief + * @note + * @param + * @retval + */ +void http_send(EthernetClient& client, string& header, string& content) { + char content_length[5] = { }; header += "\r\nContent-Type: text/html\r\n"; header += "Content-Length: "; @@ -139,27 +157,31 @@ header += "Pragma: no-cache\r\n"; header += "Connection: About to close\r\n"; header += "\r\n"; - string webpage = header + content; - client.write((uint8_t*)webpage.c_str(),webpage.length()); + + string webpage = header + content; + client.write((uint8_t*)webpage.c_str(), webpage.length()); } -int main() -{ - UIPEthernet.begin(MY_MAC,MY_IP); +/** + * @brief + * @note + * @param + * @retval + */ +int main(void) { + UIPEthernet.begin(MY_MAC, MY_IP); myServer.begin(); while(1) { - EthernetClient client = myServer.available(); - if (client) { - size_t size = client.available(); + EthernetClient client = myServer.available(); + if(client) { + size_t size = client.available(); if(size > 0) { - uint8_t* buf = (uint8_t*)malloc(size); + uint8_t* buf = (uint8_t*)malloc(size); size = client.read(buf, size); - string received((char*)buf); + string received((char*)buf); free(buf); + if(received.substr(0, 3) != "GET") { - // head, post or other method - // for possible status codes see: - // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html httpHeader = HTTP_OK; httpContent = "<h1>200 OK</h1>"; http_send(client, httpHeader, httpContent); @@ -190,18 +212,16 @@ } if(cmd == 1) { - sw = 1; // switch on + sw = 1; // switch on } if(cmd == 0) { - sw = 0; // switch off + sw = 0; // switch off } httpHeader = HTTP_OK; - http_send(client, httpHeader, page(sw)); + http_send(client, httpHeader, view(sw)); } } } } - -