![](/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:
- 6:b38a3b476a45
- Parent:
- 5:0ab8292e37da
- Child:
- 7:df33cd7ec9de
--- a/main.cpp Mon Mar 16 09:43:13 2015 +0000 +++ b/main.cpp Sun Aug 28 11:47:29 2016 +0000 @@ -5,14 +5,31 @@ * 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/>. */ -#include <mbed.h> -#include <UIPEthernet.h> -#include <UIPServer.h> -#include <UIPClient.h> -#include <string> + +//#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards! + +#if defined(TARGET_STM32F103C8T6) + #define LED_PIN PC_13 + const int OFF = 1; + const int ON = 0; +#else + #define LED_PIN LED1 + const int OFF = 0; + const int ON = 1; +#endif + +#include "mbed.h" +#include "UIPEthernet.h" +#include "UIPServer.h" +#include "UIPClient.h" +#include "string" using namespace std; +Serial pc(USBTX, USBRX); + +#define DHCP 1 // if you'd like to use static IP address comment out this line + // 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) @@ -21,12 +38,11 @@ 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 +#elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) \ + || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) \ + || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB) \ + || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) +UIPEthernetClass UIPEthernet(D4, D5, D3, D2); // 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. @@ -45,14 +61,11 @@ // 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 }; - -// 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); // In this example we are turning on/off LED1. -DigitalOut sw(LED1); // Change LED1 to a pin of your choice. +DigitalOut sw(LED_PIN); // Change LED_PIN 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! @@ -75,7 +88,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 analyseURL(string& str) { if(str.substr(5, PASSWORD.size()) != PASSWORD) return(-1); @@ -92,10 +105,10 @@ string cmd(str.substr(pos, 5)); if(cmd == "?sw=0") - return(0); + return(OFF); if(cmd == "?sw=1") - return(1); + return(ON); return(-3); } @@ -106,7 +119,7 @@ * @param * @retval */ -string& moved_perm(uint8_t flag) { +string& movedPermanently(uint8_t flag) { if(flag == 1) httpContent = "/" + PASSWORD + "/"; else @@ -123,19 +136,19 @@ * @param * @retval */ -string& view(uint8_t status) { +string& showWebPage(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"; + if(status == ON) { + httpContent += "<pre>\r\n <font color=#FF0000>ON </font>"; + httpContent += " <a href=\"./?sw=0\">[Turn off]</a>\r\n"; } else { - httpContent += "<pre>\r\n <font color=#00FF00>OFF</font>"; - httpContent += " <a href=\"./?sw=1\">[switch on]</a>\r\n"; + httpContent += "<pre>\r\n <font color=#BBBBBB>OFF</font>"; + httpContent += " <a href=\"./?sw=1\">[Turn on]</a>\r\n"; } - httpContent += " <a href=\".\">[refresh status]</a>\r\n"; +// httpContent += " <a href=\".\">[refresh status]</a>\r\n"; httpContent += "</pre>\r\n"; httpContent += "<hr>\r\n"; return httpContent; @@ -147,7 +160,7 @@ * @param * @retval */ -void http_send(EthernetClient& client, string& header, string& content) { +void sendHTTP(EthernetClient& client, string& header, string& content) { char content_length[5] = { }; header += "\r\nContent-Type: text/html\r\n"; @@ -169,7 +182,24 @@ * @retval */ int main(void) { +#if defined(DHCP) + pc.printf("Searching for DHCP server..\r\n"); + if(UIPEthernet.begin(MY_MAC) != 1) { + pc.printf("No DHCP server found.\r\n"); + pc.printf("Exiting application.\r\n"); + return 0; + } + pc.printf("DHCP server found.\r\n"); +#else + // IP address must be unique and compatible with your network. + const IPAddress MY_IP(192, 168, 1, 181); // Change as appropriate. UIPEthernet.begin(MY_MAC, MY_IP); +#endif + IPAddress localIP = UIPEthernet.localIP(); + pc.printf("Type "); + for(uint8_t i = 0; i < 3; i++) + pc.printf("%d.", localIP[i]); + pc.printf("%d/secret/ into your web browser and hit ENTER\r\n", localIP[3]); myServer.begin(); while(1) { EthernetClient client = myServer.available(); @@ -184,43 +214,43 @@ if(received.substr(0, 3) != "GET") { httpHeader = HTTP_OK; httpContent = "<h1>200 OK</h1>"; - http_send(client, httpHeader, httpContent); + sendHTTP(client, httpHeader, httpContent); continue; } if(received.substr(0, 6) == "GET / ") { httpHeader = HTTP_OK; httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n"; - http_send(client, httpHeader, httpContent); + sendHTTP(client, httpHeader, httpContent); continue; } - int cmd = analyse_get_url(received); + int cmd = analyseURL(received); if(cmd == -2) { // redirect to the right base url httpHeader = MOVED_PERM; - http_send(client, httpHeader, moved_perm(1)); + sendHTTP(client, httpHeader, movedPermanently(1)); continue; } if(cmd == -1) { httpHeader = UNAUTHORIZED; httpContent = "<h1>401 Unauthorized</h1>"; - http_send(client, httpHeader, httpContent); + sendHTTP(client, httpHeader, httpContent); continue; } - if(cmd == 1) { - sw = 1; // switch on + if(cmd == ON) { + sw = ON; // turn the switch on } - if(cmd == 0) { - sw = 0; // switch off + if(cmd == OFF) { + sw = OFF; // turn the switch off } httpHeader = HTTP_OK; - http_send(client, httpHeader, view(sw)); + sendHTTP(client, httpHeader, showWebPage(sw)); } } }