![](/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:
- 7:df33cd7ec9de
- Parent:
- 6:b38a3b476a45
- Child:
- 8:bb8aa896c7f5
--- a/main.cpp Sun Aug 28 11:47:29 2016 +0000 +++ b/main.cpp Thu Dec 01 11:42:27 2016 +0000 @@ -6,7 +6,7 @@ * The example is based on the Tuxgraphics Web Switch <http://www.tuxgraphics.org/>. */ -//#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards! +#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards! #if defined(TARGET_STM32F103C8T6) #define LED_PIN PC_13 @@ -26,23 +26,19 @@ using namespace std; -Serial pc(USBTX, USBRX); +Serial pc(PA_9, PA_10); // tx, rx #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) -UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs -#elif defined(TARGET_LPC1114) -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) || 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 +UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs + +// However, make sure that it does not collide with any of the SPI pins +// already used in the UIPEthernet(...) constructor above! +// In this example we are turning on/off LED1. +DigitalOut sw(LED_PIN); // Change LED_PIN to a pin of your choice. + // If your board/plaform is not present yet then uncomment // the following two lines and replace TARGET_YOUR_BOARD as appropriate. @@ -50,7 +46,7 @@ //#elif defined(TARGET_YOUR_BOARD) //UIPEthernetClass UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); // mosi, miso, sck, cs -#endif +//#endif // Note: // If it happends that any of the SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS pins collide with LED1 pin @@ -61,127 +57,20 @@ // 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 }; -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(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! -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 +const uint16_t MY_PORT = 23; // for TELNET connection +EthernetServer server = EthernetServer(MY_PORT); +EthernetClient clients[4]; -// analyse the url given -// return values: -1 invalid password -// -2 no command given but password valid -// -3 just refresh page -// 0 switch off -// 1 switch on -// -// The string passed to this function will look like this: -// GET /password HTTP/1..... -// GET /password/ HTTP/1..... -// GET /password/?sw=1 HTTP/1..... -// GET /password/?sw=0 HTTP/1..... -int8_t analyseURL(string& str) { - if(str.substr(5, PASSWORD.size()) != PASSWORD) - return(-1); - - uint8_t pos = 5 + PASSWORD.size(); - - if(str.substr(pos, 1) == " ") - return(-2); - - if(str.substr(pos, 1) != "/") - return(-1); - - pos++; - - string cmd(str.substr(pos, 5)); - - if(cmd == "?sw=0") - return(OFF); - - if(cmd == "?sw=1") - return(ON); - - return(-3); -} +void loop(); -/** - * @brief - * @note - * @param - * @retval - */ -string& movedPermanently(uint8_t flag) { - if(flag == 1) - httpContent = "/" + PASSWORD + "/"; - else - httpContent = ""; - - httpContent += "<h1>301 Moved Permanently</h1>\r\n"; - - return(httpContent); -} - -/** - * @brief - * @note - * @param - * @retval - */ -string& showWebPage(uint8_t status) { - httpContent = "<h2>Web Switch</h2>\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=#BBBBBB>OFF</font>"; - httpContent += " <a href=\"./?sw=1\">[Turn on]</a>\r\n"; - } +typedef uint8_t byte; -// httpContent += " <a href=\".\">[refresh status]</a>\r\n"; - httpContent += "</pre>\r\n"; - httpContent += "<hr>\r\n"; - return httpContent; -} - -/** - * @brief - * @note - * @param - * @retval - */ -void sendHTTP(EthernetClient& client, string& header, string& content) { - char content_length[5] = { }; +int main(void) { + + pc.baud(57600); + // pc.format(8,SerialBase::Even,1); + pc.printf("Program begins\r\n"); - header += "\r\nContent-Type: text/html\r\n"; - header += "Content-Length: "; - sprintf(content_length, "%d", content.length()); - header += string(content_length) + "\r\n"; - 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()); -} - -/** - * @brief - * @note - * @param - * @retval - */ -int main(void) { #if defined(DHCP) pc.printf("Searching for DHCP server..\r\n"); if(UIPEthernet.begin(MY_MAC) != 1) { @@ -200,58 +89,98 @@ 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(); - if(client) { - size_t size = client.available(); - if(size > 0) { - uint8_t* buf = (uint8_t*)malloc(size); - size = client.read(buf, size); - string received((char*)buf); - free(buf); - - if(received.substr(0, 3) != "GET") { - httpHeader = HTTP_OK; - httpContent = "<h1>200 OK</h1>"; - sendHTTP(client, httpHeader, httpContent); - continue; - } + server.begin(); + while(1) + { + loop(); + } + +} + + +void clientPrint(char*, EthernetClient*); + +void loop() { + // wait for a new client: + EthernetClient client = server.available(); + + if (client) { - if(received.substr(0, 6) == "GET / ") { - httpHeader = HTTP_OK; - httpContent = "<p>Usage: http://host_or_ip/password</p>\r\n"; - sendHTTP(client, httpHeader, httpContent); - continue; - } + bool newClient = true; + for (byte i=0;i<4;i++) { + //check whether this client refers to the same socket as one of the existing instances: + if (clients[i]==client) { + newClient = false; + break; + } + } - int cmd = analyseURL(received); + if (newClient) { + //check which of the existing clients can be overridden: + for (byte i=0;i<4;i++) { + if (!clients[i] && clients[i]!=client) { + clients[i] = client; + // clead out the input buffer: + client.flush(); + // clead out the input buffer: + client.flush(); + pc.printf("We have a new client\r\n"); + clientPrint("Hello Client\r\n",&client); + clientPrint("my IP ",&client); + //char localIP[]= UIPEthernet.localIP(); + //client.write(localIP,strlen(localIP)); + break; + } + } + } - if(cmd == -2) { - // redirect to the right base url - httpHeader = MOVED_PERM; - sendHTTP(client, httpHeader, movedPermanently(1)); - continue; - } - - if(cmd == -1) { - httpHeader = UNAUTHORIZED; - httpContent = "<h1>401 Unauthorized</h1>"; - sendHTTP(client, httpHeader, httpContent); - continue; - } + if (client.available() > 0) { + // read the bytes incoming from the client: + char thisChar = client.read(); + // echo the bytes back to all other connected clients: + for (byte i=0;i<4;i++) { + if (clients[i] && clients[i]!=client) { + clients[i].write(thisChar); + } + } + // echo the bytes to the server as well: + pc.putc(thisChar); + } + } + for (byte i=0;i<4;i++) { + if (!(clients[i].connected())) { + // client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false; + clients[i].stop(); + } + } + + + //This part is what I copied from ESP8266 WIFI TELNET example. + //check UART for data + if(pc.readable()){ + /* + size_t len = Serial.available(); + uint8_t sbuf[len]; + Serial.readBytes((char*)sbuf, len); + */ + char data_from_uart = pc.getc(); + //push UART data to all connected telnet clients + for(uint8_t i = 0; i < 4; i++){ + if (clients[i] && clients[i].connected()){ + clients[i].write(data_from_uart); + wait(0.001); + } + } + } + +} - if(cmd == ON) { - sw = ON; // turn the switch on - } - - if(cmd == OFF) { - sw = OFF; // turn the switch off - } - - httpHeader = HTTP_OK; - sendHTTP(client, httpHeader, showWebPage(sw)); - } - } +void clientPrint(char * str, EthernetClient* client) +{//TODO: make a member for client printf deriving from printf class + uint8_t i=0; + while(str[i]!='\0') + { + client->write((uint8_t)str[i]); + i++; } -} +} \ No newline at end of file