From 3f7643b35a0f361e249fad63e80ddf947d5b24f8 Mon Sep 17 00:00:00 2001 From: witnessmenow Date: Mon, 6 Aug 2018 23:53:38 +0100 Subject: [PATCH 1/3] Added support for Twitch Chat and added example --- .gitignore | 2 + README.adoc | 1 + .../BasicESP8266/BasicESP8266.ino | 0 .../BasicESP8266Reply/BasicESP8266Reply.ino | 0 .../TwtichSendReceive/TwtichSendReceive.ino | 123 ++++++++++++++++++ src/IRCClient.cpp | 16 +++ src/IRCClient.h | 1 + 7 files changed, 143 insertions(+) create mode 100644 .gitignore rename examples/{ => ESP8266}/BasicESP8266/BasicESP8266.ino (100%) rename examples/{ => ESP8266}/BasicESP8266Reply/BasicESP8266Reply.ino (100%) create mode 100644 examples/ESP8266/TwtichSendReceive/TwtichSendReceive.ino diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa85607 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/arduino.json +.vscode/c_cpp_properties.json diff --git a/README.adoc b/README.adoc index 38b2525..5300264 100644 --- a/README.adoc +++ b/README.adoc @@ -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 diff --git a/examples/BasicESP8266/BasicESP8266.ino b/examples/ESP8266/BasicESP8266/BasicESP8266.ino similarity index 100% rename from examples/BasicESP8266/BasicESP8266.ino rename to examples/ESP8266/BasicESP8266/BasicESP8266.ino diff --git a/examples/BasicESP8266Reply/BasicESP8266Reply.ino b/examples/ESP8266/BasicESP8266Reply/BasicESP8266Reply.ino similarity index 100% rename from examples/BasicESP8266Reply/BasicESP8266Reply.ino rename to examples/ESP8266/BasicESP8266Reply/BasicESP8266Reply.ino diff --git a/examples/ESP8266/TwtichSendReceive/TwtichSendReceive.ino b/examples/ESP8266/TwtichSendReceive/TwtichSendReceive.ino new file mode 100644 index 0000000..252efb6 --- /dev/null +++ b/examples/ESP8266/TwtichSendReceive/TwtichSendReceive.ino @@ -0,0 +1,123 @@ +/******************************************************************* + 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 //https://github.com/esp8266/Arduino +#include + +//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 + if (client.connectTwitch(TWITCH_BOT_NAME, ircChannel, TWITCH_OAUTH_TOKEN)) { + 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; + } +} diff --git a/src/IRCClient.cpp b/src/IRCClient.cpp index 1921fc4..0850957 100644 --- a/src/IRCClient.cpp +++ b/src/IRCClient.cpp @@ -45,6 +45,22 @@ boolean IRCClient::connect(String nickname, String user) { return true; } +boolean IRCClient::connectTwitch(String nickname, String user, String password) { + if (!connected()) { + int result = client->connect(this->host, this->port); + if (result == 1) { + this->nickname = nickname; + sendIRC("PASS " + password); + sendIRC("NICK " + nickname); + sendIRC("JOIN " + user); + this->isConnected = true; + return true; + } + return false; + } + return true; +} + boolean IRCClient::loop() { if (connected() && client->available()) { String message = ""; diff --git a/src/IRCClient.h b/src/IRCClient.h index 1fb4b47..537a209 100644 --- a/src/IRCClient.h +++ b/src/IRCClient.h @@ -45,6 +45,7 @@ class IRCClient IRCClient& setCallback(IRC_CALLBACK_SIGNATURE); IRCClient& setSentCallback(IRC_SENTCALLBACK_SIGNATURE); boolean connect(String nickname, String user); + boolean connectTwitch(String nickname, String user, String password); boolean loop(); boolean connected(); void sendRaw(String data); From b4af4f663a462c3d5736277d4dfdf4ada9d61e28 Mon Sep 17 00:00:00 2001 From: witnessmenow Date: Mon, 6 Aug 2018 23:56:18 +0100 Subject: [PATCH 2/3] Updated library.properties --- library.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library.properties b/library.properties index 0788c20..c9df1ef 100644 --- a/library.properties +++ b/library.properties @@ -1,8 +1,8 @@ name=ArduinoIRC -version=0.1.0 +version=0.2.0 author=Fredi Machado maintainer=Fredi Machado -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 From 4b05da496cac2ed1269ad8230d5c2dc44afc66d0 Mon Sep 17 00:00:00 2001 From: witnessmenow Date: Tue, 7 Aug 2018 17:02:20 +0100 Subject: [PATCH 3/3] Modifying based on PR comments. moving back to a single connect function --- .../TwtichSendReceive/TwtichSendReceive.ino | 4 +++- src/IRCClient.cpp | 22 +++++-------------- src/IRCClient.h | 3 +-- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/examples/ESP8266/TwtichSendReceive/TwtichSendReceive.ino b/examples/ESP8266/TwtichSendReceive/TwtichSendReceive.ino index 252efb6..ef56208 100644 --- a/examples/ESP8266/TwtichSendReceive/TwtichSendReceive.ino +++ b/examples/ESP8266/TwtichSendReceive/TwtichSendReceive.ino @@ -79,7 +79,9 @@ void loop() { if (!client.connected()) { Serial.println("Attempting to connect to " + ircChannel ); // Attempt to connect - if (client.connectTwitch(TWITCH_BOT_NAME, ircChannel, TWITCH_OAUTH_TOKEN)) { + // 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 { diff --git a/src/IRCClient.cpp b/src/IRCClient.cpp index 0850957..fc7a417 100644 --- a/src/IRCClient.cpp +++ b/src/IRCClient.cpp @@ -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; @@ -45,22 +49,6 @@ boolean IRCClient::connect(String nickname, String user) { return true; } -boolean IRCClient::connectTwitch(String nickname, String user, String password) { - if (!connected()) { - int result = client->connect(this->host, this->port); - if (result == 1) { - this->nickname = nickname; - sendIRC("PASS " + password); - sendIRC("NICK " + nickname); - sendIRC("JOIN " + user); - this->isConnected = true; - return true; - } - return false; - } - return true; -} - boolean IRCClient::loop() { if (connected() && client->available()) { String message = ""; diff --git a/src/IRCClient.h b/src/IRCClient.h index 537a209..e6b86c1 100644 --- a/src/IRCClient.h +++ b/src/IRCClient.h @@ -44,8 +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 connectTwitch(String nickname, String user, String password); + boolean connect(String nickname, String user, String password = ""); boolean loop(); boolean connected(); void sendRaw(String data);