Advertisement
Guest User

Untitled

a guest
Oct 9th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 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 www.twitch.tv/noycebru
  8. *******************************************************************/
  9.  
  10. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  11. #include <IRCClient.h>
  12. //#include <Servo.h>
  13. #include <Wire.h>
  14. #include <Adafruit_PWMServoDriver.h>
  15. #include "secret.h"
  16. #define secret_ssid "my ssid"
  17. #define SERVOMIN 125 //can be adjusted
  18. #define SERVOMAX 575 //can be adjusted
  19. //Servo servo;
  20.  
  21. //define your default values here, if there are different values in config.json, they are overwritten.
  22.  
  23. #define IRC_SERVER "irc.chat.twitch.tv"
  24. #define IRC_PORT 6667
  25.  
  26. //------- Replace the following! ------
  27. //------------------------------
  28.  
  29. //int led = 5;
  30. //int Slap = 12;
  31. int sda = 4;
  32. int scl = 5;
  33. int addr = 0x40; //default adafruit address
  34. int Axis_1_Angle = 90; //its a trap //prospect edit
  35. int Axis_2_Angle = 0; //it's a trap sequel
  36.  
  37. int Axis_Angle[4] = {0,0,0,0,0};
  38.  
  39. Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(addr, &Wire);
  40. String ircChannel = "";
  41.  
  42. WiFiClient wiFiClient;
  43. IRCClient client(IRC_SERVER, IRC_PORT, wiFiClient);
  44.  
  45. // put your setup code here, to run once:
  46. void setup() {
  47. Serial.begin(115200);
  48. Serial.println("Hello");
  49. Wire.begin(sda, scl);
  50. pwm.begin();
  51. pwm.setPWMFreq(60); //ada fruit mentions 50hz
  52. yield();
  53. //pinMode(led, OUTPUT);
  54. //pinMode(Slap, OUTPUT);
  55. //servo.attach(12);
  56. //servo.write(0);
  57. //delay(2000);
  58. //Serial.begin(115200);
  59. //Serial.println();
  60.  
  61. // Set WiFi to station mode and disconnect from an AP if it was Previously
  62. // connected
  63. WiFi.mode(WIFI_STA);
  64. WiFi.disconnect();
  65. delay(100);
  66.  
  67. // Attempt to connect to Wifi network:
  68. Serial.print("Connecting Wifi: ");
  69. Serial.println(ssid);
  70. WiFi.begin(ssid, password);
  71. while (WiFi.status() != WL_CONNECTED) {
  72. Serial.print(".");
  73. delay(500);
  74. }
  75. Serial.println("");
  76. Serial.println("WiFi connected");
  77. Serial.println("IP address: ");
  78. IPAddress ip = WiFi.localIP();
  79. Serial.println(ip);
  80.  
  81. ircChannel = "#" + twitchChannelName;
  82.  
  83. client.setCallback(callback);
  84. }
  85.  
  86. void loop() {
  87.  
  88. // Try to connect to chat. If it loses connection try again
  89. if (!client.connected()) {
  90. Serial.println("Attempting to connect to " + ircChannel );
  91. // Attempt to connect
  92. // Second param is not needed by Twtich
  93. if (client.connect(TWITCH_BOT_NAME, "", TWITCH_OAUTH_TOKEN)) {
  94. client.sendRaw("JOIN " + ircChannel);
  95. Serial.println("connected and ready to rock");
  96. sendTwitchMessage("Ready to go Boss!");
  97. } else {
  98. Serial.println("failed... try again in 5 seconds");
  99. // Wait 5 seconds before retrying
  100. delay(5000);
  101. }
  102. return;
  103. }
  104. client.loop();
  105. }
  106.  
  107. void sendTwitchMessage(String message) {
  108. client.sendMessage(ircChannel, message);
  109. }
  110.  
  111. void MoveServoSlowly(int ServoNumber, int FinalAngle, int MotionDelay)
  112. {
  113. if (Axis_Angle[ServoNumber] > FinalAngle)
  114. {
  115. for (Axis_Angle[ServoNumber]; Axis_Angle[ServoNumber] >= FinalAngle; Axis_Angle[ServoNumber]--)
  116. {
  117. delay(MotionDelay); //delay 10th of second
  118. pwm.setPWM(ServoNumber, 0, angleToPulse(Axis_Angle[ServoNumber])); //
  119. Serial.print("Moving Servo");
  120. Serial.print(ServoNumber);
  121. Serial.print(" Angle: ");
  122. Serial.print(Axis_Angle[ServoNumber]);
  123. }
  124. }
  125. if (Axis_Angle[ServoNumber] < FinalAngle)
  126. {
  127. for (Axis_Angle[ServoNumber]; Axis_Angle[ServoNumber] <= FinalAngle; Axis_Angle[ServoNumber]++)
  128. {
  129. delay(MotionDelay); //delay 10th of second
  130. pwm.setPWM(ServoNumber, 0, angleToPulse(Axis_Angle[ServoNumber])); //
  131. Serial.print("Moving Servo");
  132. Serial.print(ServoNumber);
  133. Serial.print(" Angle: ");
  134. Serial.print(Axis_Angle[ServoNumber]);
  135. }
  136. }
  137. }
  138.  
  139. void callback(IRCMessage ircMessage) {
  140. //Serial.println("In CallBack");
  141.  
  142. if (ircMessage.command == "PRIVMSG" && ircMessage.text[0] != '\001') {
  143. //Serial.println("Passed private message.");
  144.  
  145. ircMessage.nick.toUpperCase();
  146.  
  147. String message("<" + ircMessage.nick + "> " + ircMessage.text);
  148.  
  149. //prints chat to serial
  150. Serial.println(message);
  151. if (ircMessage.text.indexOf("!center") > -1 )
  152. {
  153. MoveServoSlowly(0, 90, 100);
  154. }
  155.  
  156. if (ircMessage.text.indexOf("!fwd") > -1 ) {
  157.  
  158. if (Axis_1_Angle > 30)
  159. {
  160. for (Axis_1_Angle; Axis_1_Angle > 30; Axis_1_Angle--)// >90 decrease incremitally
  161. {
  162. delay(100); //delay 10th of second
  163. pwm.setPWM(0, 0, angleToPulse(Axis_1_Angle)); //
  164. Serial.print("Axis_1_Angle");
  165. }
  166. }
  167.  
  168. if (Axis_1_Angle < 30)
  169. {
  170. for (Axis_1_Angle; Axis_1_Angle < 30; Axis_1_Angle++)
  171. {
  172. delay(100);
  173. pwm.setPWM(0, 0, angleToPulse(Axis_1_Angle));
  174. }
  175. }
  176.  
  177. delay(100);
  178. }
  179.  
  180. if (ircMessage.text.indexOf("!nudge+") > -1 )
  181. {
  182. MoveServoSlowly(0, Axis_Angle[0]+10, 100);
  183. }
  184.  
  185. if (ircMessage.text.indexOf("!nudge-") > -1 )
  186. {
  187. MoveServoSlowly(0, Axis_Angle[0]-10, 100);
  188. }
  189.  
  190. if (ircMessage.text.indexOf("!erect") > -1 )
  191. {
  192. MoveServoSlowly(1, 90, 100);
  193. }
  194.  
  195. if (ircMessage.text.indexOf("!erectfwd") > -1 )
  196. {
  197. MoveServoSlowly(1, Axis_Angle[1]+10, 100);
  198. }
  199. yield();
  200.  
  201. }
  202. return;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement