Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Telnet client
- This sketch connects to a a telnet server (http://www.google.com)
- using an Arduino Wiznet Ethernet shield. You'll need a telnet server
- to test this with.
- Processing's ChatServer example (part of the network library) works well,
- running on port 10002. It can be found as part of the examples
- in the Processing application, available at
- http://processing.org/
- Circuit:
- * Ethernet shield attached to pins 10, 11, 12, 13
- created 14 Sep 2010
- modified 9 Apr 2012
- by Tom Igoe
- */
- #define NICKNAME "miek|arduino"
- #define CHANNEL "#wtfpwnt"
- #include <SPI.h>
- #include <Ethernet.h>
- // Enter a MAC address and IP address for your controller below.
- // The IP address will be dependent on your local network:
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
- };
- IPAddress ip(192, 168, 1, 177);
- // Enter the IP address of the server you're connecting to:
- IPAddress server(192, 168, 1, 97);
- // Initialize the Ethernet client library
- // with the IP address and port of the server
- // that you want to connect to (port 23 is default for telnet;
- // if you're using Processing's ChatServer, use port 10002):
- EthernetClient client;
- void setup() {
- // start the Ethernet connection:
- Ethernet.begin(mac, ip);
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- // give the Ethernet shield a second to initialize:
- delay(1000);
- Serial.println("connecting...");
- // if you get a connection, report back via serial:
- if (client.connect(server, 6667)) {
- Serial.println("connected");
- }
- else {
- // if you didn't get a connection to the server:
- Serial.println("connection failed");
- }
- client.println("NICK " NICKNAME);
- client.println("USER arduino 1 2 3 4 5");
- // client.println("JOIN #wtfpwnt");
- }
- char buf[1024];
- char inbuf[256];
- void loop()
- {
- long i, j, joined;
- i = 0;
- j = 0;
- joined = 0;
- while (1) {
- if (client.available()) {
- char c = client.read();
- switch (c) {
- case 10:
- break;
- case 13:
- if ((buf[0] == 'P') && (buf[1] == 'I') && (buf[2] == 'N') && (buf[3] == 'G')) {
- client.print("PONG");
- client.println(&buf[4]);
- } else {
- Serial.println(buf);
- }
- i = 0;
- break;
- default:
- buf[i++] = c;
- buf[i] = 0;
- break;
- }
- //Serial.print(c);
- }
- if (Serial.available() > 0) {
- char inChar = Serial.read();
- if (inChar == 13) {
- client.print("PRIVMSG " CHANNEL " :");
- client.println(inbuf);
- Serial.print("<" NICKNAME "> ");
- Serial.println(inbuf);
- j = 0;
- } else if ((inChar == '/') && !joined) {
- client.println("JOIN " CHANNEL);
- joined = 1;
- } else {
- inbuf[j++] = inChar;
- inbuf[j] = 0;
- }
- }
- }
- // as long as there are bytes in the serial queue,
- // read them and send them out the socket if it's open:
- /*while (Serial.available() > 0) {
- char inChar = Serial.read();
- if (client.connected()) {
- client.print(inChar);
- }
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement