Tonight I've been working on getting my Arduino UNO with the R3 Ethernet shield to connect to the IRC server that im using. Getting the arduino to connect to the server was simple enough with the Ethernet libray. But parsing strings or chars in a manner that are manageable and manipulable was a hassle.
At first i used char arrays to store the incoming messages from the server, but not having vast amount of C++ knowledge i ended up confused and derailed. I then tried to take use of the String class, even though it is more memory intensive it was much easier to handle. After that making the Arduino do the initial handshake with the server was a breeze, not soon after it responded to pings and was idle sitting in our channel observing.
But when i asked the arduino to answer my PM's it started to freeze at random.
It connected fine every time. i could see messages for a minute or so before the stream went silent.
I would believe its the strings that causes SRAM to overload (stack overflow?). The arduino sometimes restarts automatically and begins from scratch without me touching the reset.
Right now ive removed the PM answering part of the code and connected it to the server. It seems to be playing ping pong fine now. only been on for about 15 min so far. So i thought id come here and see if anyone of you could point out eventual pitfalls ive ventured into, or general mistakes ive made.
Also on a side note.
FYI: The strings that are stored in temp are usualy around 130~200 bytes large.
TL;DR
Arduino IRC bot stops working after couple of minutes. Possible SRAM fault?
Please have a look at my code.
Hope you guys can help me out
-Thomas
#include <SPI.h>
#include <Ethernet.h>
// ----------VARIABLES----------
// Ethernet Related
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(10,0,0,100);
char serverName[] = "underworld2.no.quakenet.org";
// Handshake
boolean ForceClose = false;
String nick = "CoffieBOT";
String channel = "#####";
// -----------INIT-----------
// initialize the library instance:
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// attempt a DHCP connection:
Serial.println("Attempting to get an IP address using DHCP:");
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Serial.println("failed to get an IP address using DHCP, trying manually");
while(true) {
}
}
Serial.print("My address:");
Serial.println(Ethernet.localIP());
}
void loop()
{
//Connect to the server
if (!client.connected()){
if (client.connect(serverName, 6667)) {
Serial.println("Connected");
Handshake(nick);
}
}
readFromServer();
// Does the server need to shut down?
if(ForceClose && client.connected()) {
Serial.println("Closing connection forcefully");
client.stop();
} //End of ForceClose
} //End of Loop
void Handshake(String name) {
client.println("NICK " + name + "\nUSER " + name + " 8 * : CoffieMachine\n");
} //End of Handshake
void readFromServer(){
String temp;
String response;
while (client.available()){
char c = client.read();
temp = "";
//pull out characters from the client serial and assemble a string
while (client.available() && c != '\n'){
temp = temp + c;
c = client.read();
}
Serial.print(temp);
respond(temp);
} //End of While
} // End of Read
void respond(String temp) {
String response = "";
int space_index = temp.indexOf(' ') + 1;
int colon_index = temp.indexOf(':', space_index) + 1;
String recipient = temp.substring(1,temp.indexOf("!"));
if (temp.startsWith("PING")){
response = temp;
response.setCharAt(1,'O');
Serial.println();
}
//JOIN Channel if Handshake MOTD is displayed
if (temp.startsWith("221 " + nick + " +i",space_index)){
response = "JOIN " + channel;
}
// // Someone Private Messaged the bot
// if (temp.startsWith("PRIVMSG " + nick,space_index)) {
// if (temp.startsWith("Hello",colon_index)) {
// response = "PRIVMSG " + recipient + " : Hello there" ;
// Serial.println();
// }
//
// }
//
client.println(response);
Serial.println(response);
}