ESP8266 - Twitch chat command to rotate a servo

Hey guys,
This is pretty much the first time I've ever attempted something like this, I don't have much knowledge on this type of stuff. But I feel like I'm overlooking something simple or small, so I came here for help.
I'm trying to make it to where a servo is rotated when a command is typed into my Twitch chat to trigger a feeder for my ducks.

The bot enters chat fine with "Ready to go Boss!"
but that's all I get, it compiles fine in IDE though.

Using the ESP8266 and the servo on a breadboard with yellow signal wire on D1, red power on VIN, and brown on GND. I've also used a 5V power supply for bread boards and tried attaching the red power wire to it, after verifying it was giving me 5V DC.

With red power connected to VIN, the servo moves to a fixed location and refuses to move either way, this only happens when yellow signal is connected to D1, unplugged or in another port the servo moves freely by hand.

This is the servo
https://www.amazon.com/gp/product/B07MFK266B/ref=ppx_yo_dt_b_asin_title_o02_s00?ie=UTF8&psc=1

This is the ESP8266

It would only let me post 2 links, if you need the breadboard and power supply links let me know and ill see if i can post more in the comments.

Any help or input helping me understand this and get this working would be amazing!
Thank you all for any help you have to offer!

This is the code

/*******************************************************************
  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[] = "Duckberg";       // your network SSID (name)
char password[] = "********";  // your network key

//The name of the channel that you want the bot to join
const String twitchChannelName = "duckbergducks"; //this is case sensitive!

//The name that you want the bot to have
#define TWITCH_BOT_NAME "duckbergbot"

//OAuth Key for your twitch bot
// https://twitchapps.com/tmi/
#define TWITCH_OAUTH_TOKEN "oauth:******************************"


//------------------------------



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("!feed") > -1 && ircMessage.nick == "duckbergbot")
    {

      servo.attach(5);
      servo.write(180);
      delay(750);
      servo.write(0);
      delay(1000);
      servo.detach();

    }
  }


  return;
}

If something does not work as a completed project.
Go back to test each functional part on its own with testprograms that are well known to work.

So one testcode that does nothing more than moving the servo back and forth to check if the servo-signal creation and power-supplying of the servo works.

Then a testprogram that prints to the serial monitor with mesages like
"servo move to pos1"

"servo move to pos2"

best regards Stefan

seems like you need to verify that your code receives anything, first

but it also looks like the code is looking for "!feed" but also invoked toUpperCase(). should the code looks for "!FEED"?

Thanks for the reply!

Could I do that in IDE? like I said, this is my first go around with anything close to something like this, I copied the code from another user on this forum who got this code to work the same way I'm trying to use it.

I also reset the board to re enter chat and tried !FEED a few different way with no luck :frowning:

callback() reports the received "message". do you see it on the serial monitor?

208744470_945994679553762_8731585287548286408_n

This is what I see in the serial monitor upon uploading

too small and out of focused

try copy and past

sorry about that!
Here is the message i get

18:17:30.281 -> ⸮U⸮⸮s4 ⸮M⸮⸮⸮

and when i type !feed in chat i get an additional upside down question mark

is the bit-rate of the Serial monitor the same as the interface in the code: 115200

it is now! thank you!

now i get
22:51:49.152 -> Connecting Wifi: Duckberg
22:51:49.152 -> .......
22:51:53.395 -> WiFi connected
22:51:53.395 -> IP address:
22:51:53.395 -> ...**
22:51:53.395 -> Attempting to connect to #duckbergducks
22:51:54.128 -> connected and ready to rock

as well as the chat feed, but still no servo movement

I don't want to be misunderstood. This forum IS for helping to gain more knowledge and supporting to get projects finished. Yes of course.
But in a way of investing at least some own effort that goes beyond
typing a a generalised question. This is what you do.

How can I check if your hardware is wired correctly? rhetoric question.
I can't. What does it make easy to check the hardware and the wiring?
A small testprogram that is well known to work properly.

SO my comment is:
Copy & Paste does not work in the microcontroller-word.
You really should start learning the basics.
If you refuse to learn the basics about electronics and about programming
you will be back here in the forum every 15 minutes with a new beg for help
can you code this for me?
can you explain this to me?
the serial monitors shows this? What does it mean?
How can I ....?

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

And the same website also has tutorials about electronics.
best regards Stefan

shouldn't you attach() to the servo once in setup()? is there a need to detach()?

This is a very basic servo-testing-code

#include <Servo.h>
Servo servoMain; // Define our Servo
void setup()
{
  servoMain.attach(5); // servo on digital pin 5
}
void loop()
{
  servoMain.write(45); // Turn Servo Left to 45 degrees
  delay(1000); // Wait 1 second
  servoMain.write(0); // Turn Servo Left to 0 degrees
  delay(1000); // Wait 1 second
  servoMain.write(90); // Turn Servo back to center position (90 degrees)
  delay(1000); // Wait 1 second
  servoMain.write(135); // Turn Servo Right to 135 degrees
  delay(1000); // Wait 1 second
  servoMain.write(180); // Turn Servo Right to 180 degrees
  delay(1000); // Wait 1 second
  servoMain.write(90); // Turn Servo back to center position (90 degrees)
  delay(1000); // Wait 1 second
}

The comments explain what the servo will do (or should do)
DIS-connect everything except the servo and then upload this program
and report what you observe while this testprogram is running.

Of course you can go on guessing around. Trying this trying that.
Me personally I prefer analysing what is really going on
by narrowing down possible causes. If you think analysing is boring and you enjoy guessing around go on guessing. In your freetime you are free to do sensefree things whatever you want as long as it is not against moral or laws.
best regards Stefan

OK i got it working, but now it moves the servo anytime anything is said in chat, I've tried changing a few things and its the same anytime anything is said servo moves.
Here is the updated code.

/*******************************************************************
  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[] = "Duckberg";       // your network SSID (name)
char password[] = "11111111";  // your network key

//The name of the channel that you want the bot to join
const String twitchChannelName = "duckbergducks"; //this is case sensitive!

//The name that you want the bot to have
#define TWITCH_BOT_NAME "duckbergbot"

//OAuth Key for your twitch bot
// https://twitchapps.com/tmi/
#define TWITCH_OAUTH_TOKEN "oauth:axlenulip6mppnoqoq3cz05lfqozgg"


//------------------------------



int feeder = 2;
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(2);
  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("TEST") > -1 && ircMessage.nick == "duckbergbot")
    {


      servo.attach(2); //D4

      servo.write(0);

      delay(2000);

    }


    servo.write(130);

    delay(1000);

    servo.write(0);

    delay(1000);

  }


}

You should re-active the outcommented serial output and take a look at this

the above line are executed whenever there is input without testing what that input is

i got it working guys thank you!

just needed a servo.detach();
at the end
Thank you!!!

it is a working solution. But it is a solution found by fiddling around without understanding what your program does.
normally a servo.attach(5) is only done once in the setup-function.

To keep a servo in its position a continously and neverending servosignal is needed.

As soon as you execute a servo.detach(); the microcontroller will stop creating a servo-signal
leaving the servo in an undefined status which may lead to twitching again as soon as you do a new servo-attach();

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.