Skip to content

Commit

Permalink
Merge pull request #3 from witnessmenow/master
Browse files Browse the repository at this point in the history
Added PASS Support so we can use it with Twitch chat
  • Loading branch information
fredimachado authored Aug 7, 2018
2 parents 9986ce4 + 4b05da4 commit 86cc95a
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode/arduino.json
.vscode/c_cpp_properties.json
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Connects your arduino project to an IRC server.
* Easy connection to an IRC server through a Client implementation;
* Receive parsed IRC messages through a callback function;
* Send raw messages to the server or messages to a channel or user;
* Supports Twitch Chat

== Usage

Expand Down
File renamed without changes.
125 changes: 125 additions & 0 deletions examples/ESP8266/TwtichSendReceive/TwtichSendReceive.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*******************************************************************
Connect to Twtich Chat with a Bot
Created with code from TheOtherLoneStar (https://www.twitch.tv/theotherlonestar)
Hackaday IO: https://hackaday.io/otherlonestar
By Brian Lough (https://www.twitch.tv/brianlough)
YouTube: https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA
*******************************************************************/

#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <IRCClient.h>

//define your default values here, if there are different values in config.json, they are overwritten.

#define IRC_SERVER "irc.chat.twitch.tv"
#define IRC_PORT 6667

//------- Replace the following! ------
char ssid[] = "ssid"; // your network SSID (name)
char password[] = "password"; // your network key

//The name of the channel that you want the bot to join
const String twitchChannelName = "EXAMPLE_CHANNEL_NAME";

//The name that you want the bot to have
#define TWITCH_BOT_NAME "exampleName"

//OAuth Key for your twitch bot
// https://twitchapps.com/tmi/
#define TWITCH_OAUTH_TOKEN "oauth:124678uhgfhfghgfjgfjfgjgfhhgfh"


//------------------------------


int led = LED_BUILTIN;
String ircChannel = "";

WiFiClient wiFiClient;
IRCClient client(IRC_SERVER, IRC_PORT, wiFiClient);

// put your setup code here, to run once:
void setup() {

pinMode(led, OUTPUT);

Serial.begin(115200);
Serial.println();

// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);

ircChannel = "#" + twitchChannelName;

client.setCallback(callback);
}

void loop() {

// Try to connect to chat. If it loses connection try again
if (!client.connected()) {
Serial.println("Attempting to connect to " + ircChannel );
// Attempt to connect
// Second param is not needed by Twtich
if (client.connect(TWITCH_BOT_NAME, "", TWITCH_OAUTH_TOKEN)) {
client.sendRaw("JOIN " + ircChannel);
Serial.println("connected and ready to rock");
sendTwitchMessage("Ready to go Boss!");
} else {
Serial.println("failed... try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
return;
}
client.loop();
}

void sendTwitchMessage(String message) {
client.sendMessage(ircChannel, message);
}


void callback(IRCMessage ircMessage) {
//Serial.println("In CallBack");

if (ircMessage.command == "PRIVMSG" && ircMessage.text[0] != '\001') {
//Serial.println("Passed private message.");
ircMessage.nick.toUpperCase();

String message("<" + ircMessage.nick + "> " + ircMessage.text);

//prints chat to serial
Serial.println(message);

for (int i = 0; i < 6; i++) {
digitalWrite(led, HIGH);
delay(50);
digitalWrite(led, LOW);
delay(25);
}



return;
}
}
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name=ArduinoIRC
version=0.1.0
version=0.2.0
author=Fredi Machado
maintainer=Fredi Machado <[email protected]>
sentence=Easy library to make an Arduino IRC Bot.
sentence=Easy library to make an Arduino IRC Bot (Also supports Twitch chat).
paragraph=This library allows your project to connect to an IRC server.
category=Communication
url=https://github.com/fredimachado/ArduinoIRC
Expand Down
6 changes: 5 additions & 1 deletion src/IRCClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ IRCClient& IRCClient::setSentCallback(IRC_SENTCALLBACK_SIGNATURE) {
return *this;
}

boolean IRCClient::connect(String nickname, String user) {
boolean IRCClient::connect(String nickname, String user, String password) {
if (!connected()) {
int result = client->connect(this->host, this->port);
if (result == 1) {
this->nickname = nickname;
sendIRC("HELLO");
if(password != "")
{
sendIRC("PASS " + password);
}
sendIRC("NICK " + nickname);
sendIRC("USER " + user + " 8 * :Arduino IRC Client");
this->isConnected = true;
Expand Down
2 changes: 1 addition & 1 deletion src/IRCClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class IRCClient
IRCClient(const char*, uint16_t, Client& client);
IRCClient& setCallback(IRC_CALLBACK_SIGNATURE);
IRCClient& setSentCallback(IRC_SENTCALLBACK_SIGNATURE);
boolean connect(String nickname, String user);
boolean connect(String nickname, String user, String password = "");
boolean loop();
boolean connected();
void sendRaw(String data);
Expand Down

0 comments on commit 86cc95a

Please sign in to comment.