Advertisement
MrHohenheim

ircbot

Mar 12th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.29 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3.  
  4. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Mac address
  5. IPAddress server(80,65,51,138); // Public IRC server;
  6. const char *nick = "MrArduino"; // Nickname on the IRC server
  7. const char *chan = "#Calclavia"; // Channel on the IRC server
  8.  
  9. // Local settings
  10. IPAddress ip(192,168,1,104); // IP address of the Arduino
  11. IPAddress dnss(193,110,56,8); // IP address of the DNS server
  12. IPAddress gw(192,168,1,1); // IP address of the gateway.
  13.  
  14. // Initialise variables
  15. EthernetClient client;
  16. char *msg;
  17.  
  18. void setup() {
  19.  Serial.begin(9600);
  20.  while (!Serial) {
  21.   ; // wait for serial port to connect. Needed for Leonardo only
  22.  }
  23.  
  24.  Serial.println("Booting Ethernet chip...");
  25.  
  26.  Ethernet.begin(mac, ip, dnss, gw);
  27.  
  28.  // Wait for the ethernet chip to initialise
  29.  delay(750);
  30.  
  31.  Serial.print("Connecting to the server... ");
  32.  if (client.connect(server, 5555)) {
  33.   Serial.println("connected!");
  34.  }
  35.  else {
  36.   Serial.println("connection failed.");
  37.  }
  38.  
  39.  if (client.connected()) {
  40.   client.print("NICK ");
  41.   client.print(nick);
  42.   client.print("\r\n");
  43.   client.print("USER ");
  44.   client.print(nick); client.print(" ");
  45.   client.print(nick); client.print(" ");
  46.   client.print(nick); client.print(" ");
  47.   client.print(":AVRIRCCLI\r\n");
  48.  }
  49.  
  50.  msg = (char *)malloc(256);
  51.  
  52.  // Turn light off.
  53.  analogWrite(A0, 0);
  54. }
  55.  
  56. void startup() {
  57.  client.print("JOIN #Calclavia\r\n");
  58.  client.print("PRIVMSG #Calclavia :Hi");
  59.  #if defined(__AVR_ATmega328P__)
  60.  client.print(".");
  61.  #else
  62.  client.print("Atmel chip");
  63.  #endif  
  64.  client.print("\r\n");
  65. }
  66.  
  67. void privmsg(char *channel, char *text) {
  68.  client.print("PRIVMSG #");
  69.  client.print(channel);
  70.  client.print(" :");
  71.  client.print(text);
  72.  client.print("\r\n");
  73. }
  74.  
  75. void parsemsg(char *nick, char *user, char *server, char *channel, char *text) {
  76.  if (strstr(text, "ping") != NULL) privmsg(channel, "pong");
  77.  if (strstr(text, "turn light off") != NULL) {
  78.   analogWrite(A0, 0);
  79.  }
  80.  if (strstr(text, "turn light on") != NULL) {
  81.   analogWrite(A0, 1023);
  82.  }
  83.  if (strstr(text, "show light status") != NULL) {
  84.   if (analogRead(A0) == 0) privmsg(channel, "The light is off.");
  85.   else privmsg(channel, "The light is on.");
  86.  }
  87.  if (strstr(text, "show pin status") != NULL) {
  88.   client.print("PRIVMSG #");
  89.   client.print(channel);
  90.   client.print(" :Status: ");
  91.   client.print("A0="); client.print(analogRead(A0) * (5.0 / 1023.0)); client.print("V,");
  92.   client.print("A1="); client.print(analogRead(A1) * (5.0 / 1023.0)); client.print("V,");
  93.   client.print("A2="); client.print(analogRead(A2) * (5.0 / 1023.0)); client.print("V,");
  94.   client.print("A3="); client.print(analogRead(A3) * (5.0 / 1023.0)); client.print("V,");
  95.   client.print("A4="); client.print(analogRead(A4) * (5.0 / 1023.0)); client.print("V,");
  96.   client.print("A5="); client.print(analogRead(A5) * (5.0 / 1023.0)); client.print("V,");
  97.   client.print("\r\n");
  98.  }
  99. }
  100.  
  101. void loop() {
  102.  if (client.available()) {
  103.   char c = client.read();
  104.   if (c != (char)10) { // character 10 = \n
  105.    size_t cur_len = strlen(msg);
  106.    if(cur_len < 254) {
  107.     msg[cur_len] = c;
  108.     msg[cur_len+1] = '\0';
  109.    }
  110.   }
  111.   else {
  112.    // This is a full command
  113.    if (strstr(msg, "/MOTD") != NULL) {
  114.     startup();
  115.    } else if (strstr(msg, "PING") != NULL) {
  116.     char *pch;
  117.     pch = strtok(msg, " ");
  118.     if ((pch != NULL) && (strcmp(pch, "PING") == 0)) {
  119.      pch = strtok(NULL, " ");
  120.      if (pch != NULL) {
  121.       client.print("PONG ");
  122.       client.print(pch);
  123.       client.print("\r\n");
  124.      }
  125.     }
  126.    } else if (strstr(msg, "PRIVMSG") != NULL) {
  127.     char nick[32], user[33], server[64], channel[64], body[256];
  128.     sscanf(msg, ":%31[^!]!%32[^@]@%63s PRIVMSG %63s :%255[^\n]", nick, user, server, channel, body);
  129.     parsemsg(nick, user, server, channel, body);
  130.    }
  131.    msg[0] = '\0';
  132.   }
  133.   Serial.print(c);
  134.  }
  135.  
  136.  // as long as there are bytes in the serial queue,
  137.  // read them and send them out the socket if it's open:
  138.  /*while (Serial.available() > 0) {
  139.   char inChar = Serial.read();
  140.   if (client.connected()) {
  141.    client.print(inChar);
  142.   }
  143.  }*/
  144.  
  145.  // if the server's disconnected, stop the client:
  146.  if (!client.connected()) {
  147.   Serial.println();
  148.   Serial.println("disconnecting.");
  149.   client.stop();
  150.   while(true); // TODO: Retry
  151.  }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement