Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************
- 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
- Created with code from noycebru www.twitch.tv/noycebru
- *******************************************************************/
- #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
- #include <IRCClient.h>
- //#include <Servo.h>
- #include <Wire.h>
- #include <Adafruit_PWMServoDriver.h>
- #include "secret.h"
- #define secret_ssid "my ssid"
- #define SERVOMIN 125 //can be adjusted
- #define SERVOMAX 575 //can be adjusted
- //Servo servo;
- //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! ------
- //------------------------------
- //int led = 5;
- //int Slap = 12;
- int sda = 4;
- int scl = 5;
- int addr = 0x40; //default adafruit address
- int Axis_1_Angle = 90; //its a trap //prospect edit
- int Axis_2_Angle = 0; //it's a trap sequel
- int Axis_Angle[4] = {0,0,0,0,0};
- Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(addr, &Wire);
- String ircChannel = "";
- WiFiClient wiFiClient;
- IRCClient client(IRC_SERVER, IRC_PORT, wiFiClient);
- // put your setup code here, to run once:
- void setup() {
- Serial.begin(115200);
- Serial.println("Hello");
- Wire.begin(sda, scl);
- pwm.begin();
- pwm.setPWMFreq(60); //ada fruit mentions 50hz
- yield();
- //pinMode(led, OUTPUT);
- //pinMode(Slap, OUTPUT);
- //servo.attach(12);
- //servo.write(0);
- //delay(2000);
- //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 MoveServoSlowly(int ServoNumber, int FinalAngle, int MotionDelay)
- {
- if (Axis_Angle[ServoNumber] > FinalAngle)
- {
- for (Axis_Angle[ServoNumber]; Axis_Angle[ServoNumber] >= FinalAngle; Axis_Angle[ServoNumber]--)
- {
- delay(MotionDelay); //delay 10th of second
- pwm.setPWM(ServoNumber, 0, angleToPulse(Axis_Angle[ServoNumber])); //
- Serial.print("Moving Servo");
- Serial.print(ServoNumber);
- Serial.print(" Angle: ");
- Serial.print(Axis_Angle[ServoNumber]);
- }
- }
- if (Axis_Angle[ServoNumber] < FinalAngle)
- {
- for (Axis_Angle[ServoNumber]; Axis_Angle[ServoNumber] <= FinalAngle; Axis_Angle[ServoNumber]++)
- {
- delay(MotionDelay); //delay 10th of second
- pwm.setPWM(ServoNumber, 0, angleToPulse(Axis_Angle[ServoNumber])); //
- Serial.print("Moving Servo");
- Serial.print(ServoNumber);
- Serial.print(" Angle: ");
- Serial.print(Axis_Angle[ServoNumber]);
- }
- }
- }
- 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);
- if (ircMessage.text.indexOf("!center") > -1 )
- {
- MoveServoSlowly(0, 90, 100);
- }
- if (ircMessage.text.indexOf("!fwd") > -1 ) {
- if (Axis_1_Angle > 30)
- {
- for (Axis_1_Angle; Axis_1_Angle > 30; Axis_1_Angle--)// >90 decrease incremitally
- {
- delay(100); //delay 10th of second
- pwm.setPWM(0, 0, angleToPulse(Axis_1_Angle)); //
- Serial.print("Axis_1_Angle");
- }
- }
- if (Axis_1_Angle < 30)
- {
- for (Axis_1_Angle; Axis_1_Angle < 30; Axis_1_Angle++)
- {
- delay(100);
- pwm.setPWM(0, 0, angleToPulse(Axis_1_Angle));
- }
- }
- delay(100);
- }
- if (ircMessage.text.indexOf("!nudge+") > -1 )
- {
- MoveServoSlowly(0, Axis_Angle[0]+10, 100);
- }
- if (ircMessage.text.indexOf("!nudge-") > -1 )
- {
- MoveServoSlowly(0, Axis_Angle[0]-10, 100);
- }
- if (ircMessage.text.indexOf("!erect") > -1 )
- {
- MoveServoSlowly(1, 90, 100);
- }
- if (ircMessage.text.indexOf("!erectfwd") > -1 )
- {
- MoveServoSlowly(1, Axis_Angle[1]+10, 100);
- }
- yield();
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement