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 https://www.twitch.tv/noycebru
- *******************************************************************/
- #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
- #include <IRCClient.h>
- #include <Stepper.h>
- //#include "secret.h"
- //define your default values here, if there are different values in config.json, they are overwritten.
- #define secret_ssid "my ssid"
- #define IRC_SERVER "irc.chat.twitch.tv"
- #define IRC_PORT 6667
- #define ssid "test123"
- #define password "test123"
- #define TWITCH_BOT_NAME "test123"
- #define TWITCH_OAUTH_TOKEN "test123"
- String twitchChannelName = "";
- const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
- const int maxX = 2100;
- const int maxZ = 2200;
- int currentX = 0;
- int currentZ = 0;
- // for your motor
- // initialize the stepper library on pins 8 through 11:
- Stepper stepperx(stepsPerRevolution, 14, 12, 13, 15);
- Stepper stepperz(stepsPerRevolution, 16, 5, 4, 0);
- int led = 5;
- String ircChannel = "";
- WiFiClient wiFiClient;
- IRCClient client(IRC_SERVER, IRC_PORT, wiFiClient);
- // put your setup code here, to run once:
- void setup() {
- pinMode(led, OUTPUT);
- 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);
- // set the speed at 60 rpm:
- stepperx.setSpeed(60);
- stepperz.setSpeed(60);
- // initialize the serial port:
- }
- 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 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);
- int x;
- int z;
- if (ircMessage.text.indexOf("!ass") > -1)
- {
- // Match whats in parenthesis
- int left; int right; String mid;
- left = ircMessage.text.indexOf("(")+1;
- right = ircMessage.text.indexOf(")");
- mid = ircMessage.text.substring(left,right);
- x = mid.substring(0,mid.indexOf(",")).toInt();
- z = mid.substring(mid.indexOf(",")+1).toInt();
- Serial.println(mid);
- // Check for value greater than max
- if (x > maxX) { x = maxX; }
- if (z > maxZ) { z = maxZ; }
- // Check for value less than 0
- if (x < 0) { x = 0; }
- if (z < 0) { z = 0; }
- // Trick to get x offset from relative position
- if (currentX > x) {
- x = x - currentX;
- } else {
- x = currentX - x;
- }
- // Trick to get z offset from relative position
- if (currentZ > z) {
- z = z - currentZ;
- } else {
- z = currentZ - z;
- }
- for (int i = 1; i <= abs(x) / 100; i++) {
- if (x > 0) {
- stepperx.step(100);
- currentX = currentX - 100;
- };
- if (x < 0) {
- stepperx.step(-100);
- currentX = currentX + 100;
- };
- Serial.println(i);
- delay(5);
- }
- Serial.println("clockwise");
- for (int i = 1; i <= abs(z) / 100; i++) {
- if (z > 0) {
- stepperz.step(100);
- currentZ = currentZ + 100;
- };
- if (z < 0) {
- stepperz.step(-100);
- currentZ = currentZ - 100;
- };
- Serial.println(i);
- delay(5);
- }
- }
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement