Advertisement
Guest User

Untitled

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