Very much a noob - trying to adjust a code so that a servo is triggered when the command !feed is said in my Twitch chat. This is to create an automated pet feeder.
The bot and StreamElements are working okay, but I'm not sure my servo coding is correct...
I have tested with a simple servo code and that works fine. Signal is connected to D1 / 05
/*******************************************************************
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 www.twitch.tv/noycebru
*******************************************************************/
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <IRCClient.h>
#include <Servo.h>
Servo servo;
//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
//------- Replace the following! ------
char ssid[] = ""; // your network SSID (name)
char password[] = ""; // your network key
//The name of the channel that you want the bot to join
const String twitchChannelName = ""; //this is case sensitive!
//The name that you want the bot to have
#define TWITCH_BOT_NAME ""
//OAuth Key for your twitch bot
// https://twitchapps.com/tmi/
#define TWITCH_OAUTH_TOKEN ""
//------------------------------
int feeder = 5;
String ircChannel = "";
WiFiClient wiFiClient;
IRCClient client(IRC_SERVER, IRC_PORT, wiFiClient);
// put your setup code here, to run once:
void setup() {
pinMode(feeder, OUTPUT);
servo.attach(5);
servo.write(0);
delay(2000);
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);
}
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);
//this is where you would replace these elements to match your streaming configureation.
if (ircMessage.text.indexOf("subscribed") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
pinMode(feeder, OUTPUT);
servo.attach(5);
servo.write(0);
delay(2000);
Serial.begin(115200);
Serial.println();
}
if (ircMessage.text.indexOf("following") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
pinMode(feeder, OUTPUT);
servo.attach(5);
servo.write(0);
delay(2000);
Serial.begin(115200);
Serial.println();
}
if (ircMessage.text.indexOf("!feed") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
pinMode(feeder, OUTPUT);
servo.attach(5);
servo.write(0);
delay(2000);
Serial.begin(115200);
Serial.println();
}
if (ircMessage.text.indexOf("nuts!") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
pinMode(feeder, OUTPUT);
servo.attach(5);
servo.write(0);
delay(2000);
Serial.begin(115200);
Serial.println();
}
//servo control
if (ircMessage.text.indexOf("following") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
for (int i = 0; i <= 0; i++) {
servo.write(180);
delay(200);
}
for (int i = 1; i <= 3; i++) {
servo.write(180);
delay(500);
servo.write(100);
delay(200);
}
for (int i = 3; i <= 3; i++) {
servo.write(0);
delay(500);
}
}
for (int i = 1; i <= 3; i++) {
servo.write(180);
delay(500);
servo.write(100);
delay(200);
}
for (int i = 3; i <= 3; i++) {
servo.write(0);
delay(500);
}
}
return;
}
}
Thanks for replying, I've made some changes to the code based on your suggestions:
/*******************************************************************
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 www.twitch.tv/noycebru
*******************************************************************/
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <IRCClient.h>
#include <Servo.h>
Servo servo;
//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
//------- Replace the following! ------
char ssid[] = ""; // your network SSID (name)
char password[] = ""; // your network key
//The name of the channel that you want the bot to join
const String twitchChannelName = ""; //this is case sensitive!
//The name that you want the bot to have
#define TWITCH_BOT_NAME ""
//OAuth Key for your twitch bot
// https://twitchapps.com/tmi/
#define TWITCH_OAUTH_TOKEN ""
//------------------------------
int feeder = 5;
String ircChannel = "";
WiFiClient wiFiClient;
IRCClient client(IRC_SERVER, IRC_PORT, wiFiClient);
// put your setup code here, to run once:
void setup() {
pinMode(feeder, OUTPUT);
servo.attach(5);
servo.write(0);
delay(2000);
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);
}
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);
//this is where you would replace these elements to match your streaming configureation.
if (ircMessage.text.indexOf("subscribed") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
servo.write(0);
delay(2000);
Serial.println();
}
if (ircMessage.text.indexOf("following") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
servo.write(0);
delay(2000);
Serial.println();
}
if (ircMessage.text.indexOf("!feed") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
servo.write(0);
delay(2000);
Serial.println();
}
if (ircMessage.text.indexOf("nuts!") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
servo.write(0);
delay(2000);
Serial.println();
}
if (ircMessage.text.indexOf("test") > -1 && ircMessage.nick == "myinvoluntarytwitch")
{
servo.write(0);
delay(2000);
Serial.println();
}
//servo control
if (ircMessage.text.indexOf("following") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
for (int i = 0; i <= 0; i++) {
servo.write(180);
delay(200);
}
for (int i = 1; i <= 3; i++) {
servo.write(180);
delay(500);
servo.write(100);
delay(200);
}
for (int i = 3; i <= 3; i++) {
servo.write(0);
delay(500);
}
}
if (ircMessage.text.indexOf("test") > -1 && ircMessage.nick == "myinvoluntarytwitch")
{
for (int i = 0; i <= 0; i++) {
servo.write(180);
delay(200);
}
for (int i = 1; i <= 3; i++) {
servo.write(180);
delay(500);
servo.write(100);
delay(200);
}
for (int i = 3; i <= 3; i++) {
servo.write(0);
delay(500);
}
}
return;
}
}
I just want the servo to move once. The for loops were left over from the original code.
Updated:
/*******************************************************************
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 www.twitch.tv/noycebru
*******************************************************************/
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <IRCClient.h>
#include <Servo.h>
Servo servo;
//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
//------- Replace the following! ------
char ssid[] = ""; // your network SSID (name)
char password[] = ""; // your network key
//The name of the channel that you want the bot to join
const String twitchChannelName = "myinvoluntarytwitch"; //this is case sensitive!
//The name that you want the bot to have
#define TWITCH_BOT_NAME "myinvoluntarytwitch"
//OAuth Key for your twitch bot
// https://twitchapps.com/tmi/
#define TWITCH_OAUTH_TOKEN ""
//------------------------------
int feeder = 5;
String ircChannel = "";
WiFiClient wiFiClient;
IRCClient client(IRC_SERVER, IRC_PORT, wiFiClient);
// put your setup code here, to run once:
void setup() {
pinMode(feeder, OUTPUT);
servo.attach(5);
servo.write(0);
delay(2000);
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);
}
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);
//this is where you would replace these elements to match your streaming configureation.
if (ircMessage.text.indexOf("subscribed") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
servo.write(150);
delay(2000);
Serial.println();
}
if (ircMessage.text.indexOf("following") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
servo.write(150);
delay(2000);
Serial.println();
}
if (ircMessage.text.indexOf("!feed") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
servo.write(150);
delay(2000);
Serial.println();
}
if (ircMessage.text.indexOf("nuts!") > -1 && ircMessage.nick == "StreamElements")
{
servo.write(150);
delay(2000);
Serial.println();
}
if (ircMessage.text.indexOf("test") > -1 && ircMessage.nick == "myinvoluntarytwitch")
{
servo.write(150);
delay(2000);
Serial.println();
}
//servo control
if (ircMessage.text.indexOf("following") > -1 && ircMessage.nick == "STREAMELEMENTS")
{
servo.write(150);
delay(2000);
Serial.println();
}
}
return;
}
Whenever a match is found to any command your code moves the servo to 150 so it stays there once it has moved. Change one or more of the values in servo.write() for other commands and try them
This is absolutely amazing, it's working! I need to adjust the delay so it's not as slow...
I have an SG90 servo - it's vibrating constantly. Are there servos that don't vibrate? Is it healthy for it to be vibrating if I have it running for a long time?