IRC on a GLCD

IRC on a GLCD!

I found this thing about making an IRC bot in Python, and thought: since I know how to send serial information from Python using pySerial and how to receive serial information on the Arduino as well as knowing how to output text on a GLCD, why not make an Arduino program that shows IRC text on a GLCD?

Basically, the IRC bot connects to a server and channel you choose, and it just stays in there, sending serial information to the Arduino. Using the GLCD library 3 beta, the Arduino simply prints each character received to a text area and checks for newlines.

The IRC bot isn’t that great (probably still needs a lot of work) but it gets the job done.

Source code available here and in the full post. Note that the Python script requires pySerial and Python 3.1.

Source code for Arduino:
[sourcecode]#include
#include “fonts/SystemFont5x7.h” // system font

gText textArea = gText(textAreaFULL);

char charRead;

void setup() {
GLCD.Init(INVERTED);
GLCD.SelectFont(System5x7);
GLCD.ClearScreen();

textArea.DefineArea(textAreaFULL);
textArea.SelectFont(System5x7, BLACK);
textArea.ClearArea();

Serial.begin(9600);
}

void loop() {
if (Serial.available()) {
charRead = char(Serial.read());
if (charRead == ‘\n’) {
textArea.println();
}
else
textArea.print(charRead);
}
}
[/sourcecode]
Source code for Python (3.1). I included struct for another thing I was testing; you can probably just remove them:
[sourcecode]# Based on http://www.devshed.com/c/a/Python/Python-and-IRC/

import socket, serial, struct, os

def sendBytes(inputString):
irc.send( bytes(inputString,ENCODING) )

def output(inputString):
print(inputString, sep=”)
arduino.write(inputString.encode(ENCODING))

def getNick():
return data.split(‘!’)[0].replace(‘:’, ”)

def getMessage():
return ‘:’.join(data.split(‘:’)[2:]).replace(‘\r\n’,’\n’)

def say(string):
sendBytes(“PRIVMSG %s :%s\r\n” % (CHANNEL, string) )
output(“<%s> %s” % (NICKNAME, string))

OWNER = “KirbY”

ENCODING = “utf8”
NICKNAME = “WalfBot”
USER = “WalfBot”
HOST_NAME = “WalfBot”
SERVER_NAME = “WalfServ”
REAL_NAME = “WalfBot”
CHANNEL = “#blahblahtest”
JOIN_MESSAGE = “WalfBot initiated”
QUIT_MESSAGE = “WalfBot terminated”

NETWORK = “irc.whateverserver.com”
PORT = 6667

SERIAL_PORT = “COM8” # Change this

########
# Main #
########

allowedUsers = [OWNER]

arduino = serial.Serial(SERIAL_PORT, 9600, timeout=5)

irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( NETWORK, PORT ) )
irc.recv ( 4096 )
sendBytes(“NICK %s\r\n” % NICKNAME)
sendBytes(“USER %s %s %s :%s\r\n” % \
(USER, HOST_NAME, SERVER_NAME, REAL_NAME) )
sendBytes(“JOIN %s\r\n” % CHANNEL)
say(JOIN_MESSAGE)

while True:
data = str(irc.recv ( 4096 ),ENCODING)
print(data)
if data.find (“PING”) != -1:
sendBytes(“PONG “+ data.split()[1] + “\r\n” )
elif data.find(“PRIVMSG”) != -1:
destination = ”.join(data.split(‘:’)[:2]).split(‘ ‘)[-2]
if destination == NICKNAME:
output(“(PRIVATE) “)

if getMessage().find(“ACTION”) != -1:
print(getMessage())
print(getMessage().split(“ACTION”))
try:
output(“* %s%s” % (getNick(), getMessage().split(“ACTION”)[1]))
except:
pass
else:
output(“<%s> %s” % (getNick(), getMessage()))

if getMessage().find(“!quit”) != -1 and getNick() == OWNER:
sendBytes(“QUIT %s\r\n” % QUIT_MESSAGE)
arduino.close()
os._exit(99)

elif data.find(“JOIN”) != -1:
output(“* %s has joined %s” % (getNick(), CHANNEL))

elif data.find(“PART”) != -1:
output(“* %s has left %s (%s)” % (getNick(), CHANNEL, getMessage()))

elif data.find(“QUIT”) != -1:
output(“* %s has quit (%s)” % (getNick(), getMessage()))

elif data.find(“TOPIC”) != -1:
output(“* %s changes topic to ‘%s’\n” % (getNick(), getMessage().replace(‘\n’,”)))

elif data.find(“MODE”) != -1:
output(“* %s sets mode:%s” % (getNick(), data.split(CHANNEL)[1]))

[/sourcecode]

Tagged with:
 

Comments are closed.