Advertisement
Guest User

Untitled

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