Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. /*
  2.   Telnet client
  3.  
  4.  This sketch connects to a a telnet server (http://www.google.com)
  5.  using an Arduino Wiznet Ethernet shield.  You'll need a telnet server
  6.  to test this with.
  7.  Processing's ChatServer example (part of the network library) works well,
  8.  running on port 10002. It can be found as part of the examples
  9.  in the Processing application, available at
  10.  http://processing.org/
  11.  
  12.  Circuit:
  13.  * Ethernet shield attached to pins 10, 11, 12, 13
  14.  
  15.  created 14 Sep 2010
  16.  modified 9 Apr 2012
  17.  by Tom Igoe
  18.  
  19.  */
  20.  
  21. #define NICKNAME "miek|arduino"
  22. #define CHANNEL "#wtfpwnt"
  23.  
  24. #include <SPI.h>
  25. #include <Ethernet.h>
  26.  
  27. // Enter a MAC address and IP address for your controller below.
  28. // The IP address will be dependent on your local network:
  29. byte mac[] = {
  30.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  31. };
  32. IPAddress ip(192, 168, 1, 177);
  33.  
  34. // Enter the IP address of the server you're connecting to:
  35. IPAddress server(192, 168, 1, 97);
  36.  
  37. // Initialize the Ethernet client library
  38. // with the IP address and port of the server
  39. // that you want to connect to (port 23 is default for telnet;
  40. // if you're using Processing's ChatServer, use  port 10002):
  41. EthernetClient client;
  42.  
  43. void setup() {
  44.   // start the Ethernet connection:
  45.   Ethernet.begin(mac, ip);
  46.   // Open serial communications and wait for port to open:
  47.   Serial.begin(9600);
  48.   while (!Serial) {
  49.     ; // wait for serial port to connect. Needed for Leonardo only
  50.   }
  51.  
  52.  
  53.   // give the Ethernet shield a second to initialize:
  54.   delay(1000);
  55.   Serial.println("connecting...");
  56.  
  57.   // if you get a connection, report back via serial:
  58.   if (client.connect(server, 6667)) {
  59.     Serial.println("connected");
  60.   }
  61.   else {
  62.     // if you didn't get a connection to the server:
  63.     Serial.println("connection failed");
  64.   }
  65.   client.println("NICK " NICKNAME);
  66.   client.println("USER arduino 1 2 3 4 5");
  67. //  client.println("JOIN #wtfpwnt");
  68. }
  69.  
  70. char buf[1024];
  71. char inbuf[256];
  72. void loop()
  73. {
  74.   long i, j, joined;
  75.   i = 0;
  76.   j = 0;
  77.   joined = 0;
  78.   while (1) {
  79.     if (client.available()) {
  80.       char c = client.read();
  81.       switch (c) {
  82.         case 10:
  83.           break;
  84.         case 13:
  85.           if ((buf[0] == 'P') && (buf[1] == 'I') && (buf[2] == 'N') && (buf[3] == 'G')) {
  86.             client.print("PONG");
  87.             client.println(&buf[4]);
  88.           } else {
  89.             Serial.println(buf);
  90.           }
  91.           i = 0;
  92.           break;
  93.         default:
  94.           buf[i++] = c;
  95.           buf[i] = 0;
  96.           break;
  97.       }
  98.       //Serial.print(c);
  99.     }
  100.    
  101.     if (Serial.available() > 0) {
  102.       char inChar = Serial.read();
  103.       if (inChar == 13) {
  104.         client.print("PRIVMSG " CHANNEL " :");
  105.         client.println(inbuf);
  106.         Serial.print("<" NICKNAME "> ");
  107.         Serial.println(inbuf);
  108.         j = 0;
  109.       } else if ((inChar == '/') && !joined) {
  110.         client.println("JOIN " CHANNEL);
  111.         joined = 1;
  112.       } else {
  113.         inbuf[j++] = inChar;
  114.         inbuf[j] = 0;
  115.       }
  116.     }
  117.   }
  118.  
  119.   // as long as there are bytes in the serial queue,
  120.   // read them and send them out the socket if it's open:
  121.   /*while (Serial.available() > 0) {
  122.     char inChar = Serial.read();
  123.     if (client.connected()) {
  124.       client.print(inChar);
  125.     }
  126.   }*/
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement