1

I have built a twitter bot with my Arduino and an Ethernet shield right now, the way to make it tweet something is to manually add a phase into the code and upload it to the board for each new tweet,

I would like to create a simple way of scrolling through a queue of phrases and tweeting each consecutive one after a given amount of time (several hours). the issue I'm seeing is that, the code needs to rerun for each tweet, and I can't figure out what needs to be in what loop. Also the entire thing needs to delay for 3.6e^6(1 hour) milliseconds (the code takes delays in milliseconds) so that I don't spam the feed, the twitter for this project is @ether_bot.

Code is below:

#include <SPI.h>
#include <Ethernet.h>
#include <Twitter.h>

/*
void speech()
{
  //this is where i would put the que of tweets
}
*/

// the next three lines connect the ethernet shield to the local network and establish a link with the twitter account
byte ip[] = { 192,168,1,104 }; 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEU }; //address for the shield
Twitter twitter("2790106893-5ljE7WPyyGe16SqTMEm8CN5gpwDquhGnyjH2TCM"); 
char msg[] = "hello again, im getting some major work done soon!"; // message to tweet

void setup()
{
  delay(30000);
  Ethernet.begin(mac, ip);  // makes the connection 
  Serial.begin(9600); //opens a serial interface similar to the prnt window in bluejay
}

void loop()
{
  Serial.println("connecting ...");

  if (twitter.post(msg)) 
  {
    int status = twitter.wait();

    if (status == 200)
    { 
      Serial.println("OK.");
    }
    else
    {
      Serial.print("failed : code ");  //stuff happens here idk 
      Serial.println(status);
    }
  }
  else
  {
    Serial.println("connection failed.");
  }

  do {} while(1);
}
5
  • You mention a "queue" of messages; a queue has to be fed somehow: how would it be fed in your situation? If it is static, then it is not really a queue and the problem is rather simple to solve (see @geometrikal answer below). – jfpoilpret Oct 4 '14 at 12:48
  • i would like the next tweet in the queue to be tweeted after a certain amount of time, (say every few hours) – bline the lynx Oct 5 '14 at 2:04
  • You did not answer my question: is the lsit of tweets in the queue "static", ie known and fixed at compile-time, or is it dynamic, ie it can change at runtime; in the latter cas, how is the queue fed with new tweets? – jfpoilpret Oct 5 '14 at 5:48
  • static. the tweets would be typed in before the compile. i would generate all of them myself and they would all be in the code from the beginning. after the tweets in the queue are all tweeted, then i would type new ones into the code and re-upload – bline the lynx Oct 5 '14 at 15:33
  • Ok. Then @geometrikal answer fits your question. – jfpoilpret Oct 5 '14 at 16:56
1

First create an array of all the messages:

#define NUM_MESSAGES 3
const char *msg[NUM_MESSAGES] = { 
    "This is the first message", 
    "This is the second",
    "Third time lucky" };
int msg_num = 0;

Then in the loop, switch between messages and add a delay

void loop()
{
  Serial.println("connecting ...");
  if (twitter.post(*msg[msg_num])) 
  {
    int status = twitter.wait();
    if (status == 200)
    { 
      Serial.println("OK.");
    }
    else
    {
      Serial.print("failed : code ");  //stuff happens here idk 
      Serial.println(status);
    }
  }
  else
  {
    Serial.println("connection failed.");
  }

  //Next message
  msg_num++;
  if (msg_num >= NUM_MESSAGES) msg_num = 0;
  delay(1000*60*60);
}
2
  • 1
    so i put some stuff together and got this error at this line... im a totatl noob at coding so i dont really know how to make it work,,,, if (twitter.post(msg[msg_num])) sketch_oct06a.ino: In function 'void loop()': sketch_oct06a:18: error: invalid conversion from 'const char' to 'const char' sketch_oct06a:18: error: initializing argument 1 of 'bool Twitter::post(const char*)' – bline the lynx Oct 6 '14 at 23:49
  • Try removing the * in front of *msg[msg_num]. This now compiles for me but I don't have a WiFi/Ethernet shield to test it. – geometrikal Oct 7 '14 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.