Srinivas Nistala / Mbed 2 deprecated TELNET_TEST

Dependencies:   UIPEthernet mbed-STM32F103C8T6 mbed Adafruit_GFX

Fork of WebSwitch_ENC28J60 by Zoltan Hudak

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* 
00002  * In this project LED1 on the mbed board is switched on/off using a web browser.
00003  * However, you can easily modify the project to remotely switch on/off any external device.
00004  * The HTTP server is built from an mbed board and an ENC28J60 board.
00005  * ENC28J60 is driven by the UIPEthernet library <https://github.com/ntruchsess/arduino_uip>.
00006  * The example is based on the Tuxgraphics Web Switch <http://www.tuxgraphics.org/>.
00007  */
00008  
00009  //data transmission from telnet to UART is very smooth but from uart to telnet is not good when I pasted a text of one page all i got was 
00010  //garbage.
00011  
00012 #define TARGET_STM32F103C8T6  1     // uncomment this line when using STM32F103C8T6 boards!                                    
00013 
00014 #if defined(TARGET_STM32F103C8T6)
00015     #define LED_PIN PC_13
00016     const int OFF = 1;
00017     const int ON  = 0;
00018 #else
00019     #define LED_PIN LED1
00020     const int OFF = 0;
00021     const int ON  = 1;
00022 #endif
00023 
00024 #include "mbed.h"
00025 #include "UIPEthernet.h"
00026 #include "UIPServer.h"
00027 #include "UIPClient.h"
00028 #include "string"
00029 
00030 
00031 using namespace     std;
00032 
00033 Serial pc(PA_9, PA_10); // tx, rx
00034 
00035 
00036 /////////OLED STUFF//////////////////////////////
00037 #include "Adafruit_SSD1306.h"
00038 //I2C obj(sda,scl);
00039 I2C i2c2(PB_11,PB_10); //read more info about SSD1306 trouble in "SSD1306_test" project. and I don't know why but using PB_8 and PB_9 for I2C screws up SPI or atleast the ENC28J60, need to find out maybe from STM32F103 DATASHEET.
00040 //Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
00041 Adafruit_SSD1306_I2c lcd(i2c2,PB_13,0x78,64,128); //also mbed takes i2c address in 8bit mode, any unused pin can be given as "reset" pin.
00042 
00043 /////////////////////////////////////////////////////
00044 
00045 
00046 #define DHCP    1   // if you'd like to use static IP address comment out this line 
00047 
00048 // UIPEthernet is the name of a global instance of UIPEthernetClass.
00049 // Do not change the name! It is used within the UIPEthernet library.
00050 UIPEthernetClass    UIPEthernet(PB_5, PB_4, PB_3, PB_6);            // mosi, miso, sck, cs
00051 
00052 // However, make sure that it does not collide with any of the SPI pins
00053 // already used in the UIPEthernet(...) constructor above!
00054 // In this example we are turning on/off LED1.
00055 DigitalOut          sw(LED_PIN);            // Change LED_PIN to a pin of your choice.
00056 
00057 
00058 // If your board/plaform is not present yet then uncomment
00059 // the following two lines and replace TARGET_YOUR_BOARD as appropriate.
00060 
00061 //#elif defined(TARGET_YOUR_BOARD)
00062 //UIPEthernetClass    UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);   // mosi, miso, sck, cs
00063 
00064 //#endif
00065 
00066 // Note:
00067 // If it happends that any of the SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS pins collide with LED1 pin
00068 // then either use different SPI port (if available on the board) and change the pin names
00069 // in the constructor UIPEthernet(...) accordingly or instead of using LED1 pin, select
00070 // a free pin (not used by SPI port) and connect to it an external LED which is connected
00071 // to a 220 Ohm resitor that is connected to the groud.
00072 // In the second case remember to replace LED1 in sw(LED1) constructor (see below).
00073 // MAC number must be unique within the connected network. Modify as appropriate.
00074 const uint8_t       MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x06 };
00075 const uint16_t      MY_PORT = 23;           // for TELNET connection
00076 EthernetServer      server = EthernetServer(MY_PORT);
00077 #define MAX_NUM_CLIENT 1
00078 EthernetClient clients[MAX_NUM_CLIENT];
00079 
00080 void loop();
00081 
00082 typedef uint8_t byte;
00083 
00084 int main(void) {
00085     
00086     pc.baud(57600);
00087    // pc.format(8,SerialBase::Even,1); 
00088     pc.printf("Program begins\r\n");
00089     //lcd.printf("%ux%u OLED Display\r\n", lcd.width(), lcd.height());
00090     lcd.printf("Telnet Server\n");
00091     lcd.printf("Waiting for DHCP..\n");
00092     lcd.display();
00093 
00094 #if defined(DHCP)
00095     pc.printf("Searching for DHCP server..\r\n");
00096     if(UIPEthernet.begin(MY_MAC) != 1) {
00097         pc.printf("No DHCP server found.\r\n");
00098         pc.printf("Exiting application.\r\n");
00099         return 0;
00100     }
00101     pc.printf("DHCP server found.\r\n");
00102 #else
00103     // IP address must be unique and compatible with your network.
00104     const IPAddress MY_IP(192, 168, 1, 181);    //  Change as appropriate.
00105     UIPEthernet.begin(MY_MAC, MY_IP);
00106 #endif
00107     IPAddress   localIP = UIPEthernet.localIP();
00108     pc.printf("DHCP assigned IP : ");
00109     //lcd.clearDisplay();
00110     //lcd.setTextCursor(0,0);
00111     lcd.setTextSize(2);
00112     lcd.printf("IP:");
00113     for(uint8_t i = 0; i < 4; i++){
00114         pc.printf("%d.", localIP[i]);
00115         lcd.printf("%d.", localIP[i]);
00116         }
00117     lcd.display();
00118     pc.printf("Starting Telnet server");
00119     server.begin();
00120     while(1) 
00121     {
00122         loop();
00123     }
00124     
00125 }
00126 
00127           
00128 void clientPrint(char*, EthernetClient*);
00129 
00130 void loop() {
00131   // wait for a new client:
00132   EthernetClient client = server.available();
00133 
00134   if (client) {
00135 
00136     bool newClient = true;
00137     for (byte i=0;i<MAX_NUM_CLIENT;i++) {
00138       //check whether this client refers to the same socket as one of the existing instances:
00139       if (clients[i]==client) {
00140         newClient = false;
00141         break;
00142       }
00143     }
00144 
00145     if (newClient) {
00146       //check which of the existing clients can be overridden:
00147       for (byte i=0;i<MAX_NUM_CLIENT;i++) {
00148         if (!clients[i] && clients[i]!=client) {
00149           clients[i] = client;
00150           // clead out the input buffer:
00151           client.flush();
00152           // clead out the input buffer:
00153           client.flush();
00154           pc.printf("We have a new client\r\n");
00155           clientPrint("Hello Client\r\n",&client);
00156           clientPrint("my IP ",&client);
00157           //char localIP[]= UIPEthernet.localIP();
00158           //client.write(localIP,strlen(localIP));
00159           break;
00160         }
00161       }
00162     }
00163 
00164     if (client.available() > 0) {
00165       // read the bytes incoming from the client:
00166       char thisChar = client.read();
00167       // echo the bytes back to all other connected clients:
00168       for (byte i=0;i<MAX_NUM_CLIENT;i++) {
00169         if (clients[i] && clients[i]!=client) {
00170           clients[i].write(thisChar);
00171         }
00172       }
00173       // echo the bytes to the server as well:
00174       pc.putc(thisChar);
00175     }
00176   }
00177   for (byte i=0;i<MAX_NUM_CLIENT;i++) {
00178     if (!(clients[i].connected())) {
00179       // client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
00180       clients[i].stop();
00181     }
00182   }
00183   
00184   
00185   //This part is what I copied from ESP8266 WIFI TELNET example.
00186   //check UART for data
00187   if(pc.readable()){
00188     /*
00189     size_t len = Serial.available();
00190     uint8_t sbuf[len];
00191     Serial.readBytes((char*)sbuf, len);
00192     */
00193     char data_from_uart = pc.getc();
00194     //push UART data to all connected telnet clients
00195     for(uint8_t i = 0; i < MAX_NUM_CLIENT; i++){
00196       if (clients[i] && clients[i].connected()){
00197         clients[i].write(data_from_uart);
00198         //wait(0.001);
00199       }
00200     }
00201   }
00202   
00203 }
00204 
00205 void clientPrint(char * str, EthernetClient* client)
00206 {//TODO: make a member for client printf deriving from printf class
00207     uint8_t i=0;
00208     while(str[i]!='\0')
00209     {
00210         client->write((uint8_t)str[i]);
00211         i++;
00212     }
00213 }