SHARE
TWEET

Untitled

a guest Jul 19th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******************************************************************
  2.    Connect to Twtich Chat with a Bot
  3.    Created with code from TheOtherLoneStar (https://www.twitch.tv/theotherlonestar)
  4.    Hackaday IO: https://hackaday.io/otherlonestar
  5.    By Brian Lough (https://www.twitch.tv/brianlough)
  6.    YouTube: https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA
  7.    Created with code from noycebru https://www.twitch.tv/noycebru
  8.  *******************************************************************/
  9.  
  10. #include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
  11. #include <IRCClient.h>
  12. #include <Stepper.h>
  13. //#include "secret.h"
  14.  
  15. //define your default values here, if there are different values in config.json, they are overwritten.
  16. #define secret_ssid "my ssid"
  17. #define IRC_SERVER "irc.chat.twitch.tv"
  18. #define IRC_PORT 6667
  19.  
  20. #define ssid "test123"
  21. #define password "test123"
  22. #define TWITCH_BOT_NAME "test123"
  23. #define TWITCH_OAUTH_TOKEN "test123"
  24. String twitchChannelName = "";
  25.  
  26. const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
  27. const int maxX = 2100;
  28. const int maxZ = 2200;
  29. int currentX = 0;
  30. int currentZ = 0;
  31.  
  32. // for your motor
  33.  
  34. // initialize the stepper library on pins 8 through 11:
  35. Stepper stepperx(stepsPerRevolution, 14, 12, 13, 15);
  36. Stepper stepperz(stepsPerRevolution, 16, 5, 4, 0);
  37. int led = 5;
  38. String ircChannel = "";
  39.  
  40. WiFiClient wiFiClient;
  41. IRCClient client(IRC_SERVER, IRC_PORT, wiFiClient);
  42.  
  43. // put your setup code here, to run once:
  44. void setup() {
  45.  
  46.  pinMode(led, OUTPUT);
  47.  
  48.  Serial.begin(115200);
  49.  Serial.println();
  50.  
  51.  // Set WiFi to station mode and disconnect from an AP if it was Previously
  52.  // connected
  53.  WiFi.mode(WIFI_STA);
  54.  WiFi.disconnect();
  55.  delay(100);
  56.  
  57.  // Attempt to connect to Wifi network:
  58.  Serial.print("Connecting Wifi: ");
  59.  Serial.println(ssid);
  60.  WiFi.begin(ssid, password);
  61.  while (WiFi.status() != WL_CONNECTED) {
  62.   Serial.print(".");
  63.   delay(500);
  64.  }
  65.  Serial.println("");
  66.  Serial.println("WiFi connected");
  67.  Serial.println("IP address: ");
  68.  IPAddress ip = WiFi.localIP();
  69.  Serial.println(ip);
  70.  
  71.  ircChannel = "#" + twitchChannelName;
  72.  
  73.  client.setCallback(callback);
  74.  
  75.  // set the speed at 60 rpm:
  76.  stepperx.setSpeed(60);
  77.  stepperz.setSpeed(60);
  78.  // initialize the serial port:
  79.  
  80. }
  81.  
  82. void loop() {
  83.  
  84.  // Try to connect to chat. If it loses connection try again
  85.  if (!client.connected()) {
  86.   Serial.println("Attempting to connect to " + ircChannel);
  87.   // Attempt to connect
  88.   // Second param is not needed by Twtich
  89.   if (client.connect(TWITCH_BOT_NAME, "", TWITCH_OAUTH_TOKEN)) {
  90.    client.sendRaw("JOIN " + ircChannel);
  91.    Serial.println("connected and ready to rock");
  92.    sendTwitchMessage("Ready to go Boss!");
  93.   } else {
  94.    Serial.println("failed... try again in 5 seconds");
  95.    // Wait 5 seconds before retrying
  96.    delay(5000);
  97.   }
  98.   return;
  99.  }
  100.  client.loop();
  101. }
  102.  
  103. void sendTwitchMessage(String message) {
  104.  client.sendMessage(ircChannel, message);
  105. }
  106.  
  107.  
  108. void callback(IRCMessage ircMessage) {
  109.  //Serial.println("In CallBack");
  110.  
  111.  if (ircMessage.command == "PRIVMSG" && ircMessage.text[0] != '\001') {
  112.   //Serial.println("Passed private message.");
  113.  
  114.   ircMessage.nick.toUpperCase();
  115.  
  116.   String message("<" + ircMessage.nick + "> " + ircMessage.text);
  117.  
  118.   //prints chat to serial
  119.   Serial.println(message);
  120.   int x;
  121.   int z;
  122.  
  123.   if (ircMessage.text.indexOf("!ass") > -1)
  124.   {
  125.    // Match whats in parenthesis
  126.     int left; int right; String mid;
  127.     left = ircMessage.text.indexOf("(")+1;
  128.     right = ircMessage.text.indexOf(")");
  129.     mid = ircMessage.text.substring(left,right);
  130.     x = mid.substring(0,mid.indexOf(",")).toInt();
  131.     z = mid.substring(mid.indexOf(",")+1).toInt();
  132.     Serial.println(mid);
  133.  
  134.    // Check for value greater than max
  135.    if (x > maxX) { x = maxX; }
  136.    if (z > maxZ) { z = maxZ; }
  137.  
  138.    // Check for value less than 0
  139.    if (x < 0) { x = 0; }
  140.    if (z < 0) { z = 0; }
  141.  
  142.    // Trick to get x offset from relative position
  143.    if (currentX > x) {
  144.     x = x - currentX;
  145.    } else {
  146.     x = currentX - x;
  147.    }
  148.  
  149.    // Trick to get z offset from relative position
  150.    if (currentZ > z) {
  151.      z = z - currentZ;
  152.    } else {
  153.      z = currentZ - z;
  154.    }
  155.  
  156.    for (int i = 1; i <= abs(x) / 100; i++) {
  157.     if (x > 0) {
  158.      stepperx.step(100);
  159.      currentX = currentX - 100;
  160.     };
  161.     if (x < 0) {
  162.      
  163.      stepperx.step(-100);
  164.      currentX = currentX + 100;
  165.     };
  166.  
  167.     Serial.println(i);
  168.     delay(5);
  169.    }
  170.    Serial.println("clockwise");
  171.    for (int i = 1; i <= abs(z) / 100; i++) {
  172.     if (z > 0) {
  173.      stepperz.step(100);
  174.      currentZ = currentZ + 100;
  175.     };
  176.     if (z < 0) {
  177.      stepperz.step(-100);
  178.      currentZ = currentZ - 100;
  179.     };
  180.  
  181.     Serial.println(i);
  182.     delay(5);
  183.    }
  184.  
  185.   }
  186.  
  187.  
  188.   return;
  189.  }
  190. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top