Pages: [1]   Go Down
Author Topic: Trying to combine twitter with another program. Also prevent duplicates.  (Read 412 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
First off, thank you for being an awesome community.

I'm trying to achieve two things and am having difficulty pulling it off. I have an Uno with an Ethernet shield which is connected to an RF remote that controls lights. I am able to instantly turn on and off my lights using any device connected to my wi-fi. The code I used is

Code:
#include <SPI.h>
#include <Ethernet.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 127 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

String readString;

void setup(){
  pinMode(8, OUTPUT);
  pinMode(4, OUTPUT);
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.begin(9600);
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 100) {
          readString += c;
        }

        //if HTTP request has ended
        if (c == '\n') {

          Serial.println(readString); //print to serial monitor for debuging

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<style> body { background-color:#d0e4fe; }</style>");
          
          client.println("<TITLE>Arduino's Home Automation</TITLE>");
          
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Arduino's Home Automation</H1>");
          
          client.println("<hr />");

          client.println("<br />");

          client.println("<a href=\"/?lighton1\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>");
          client.println("<a href=\"/?lightoff1\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a><br /><br />");    
          client.println("</BODY>");
          client.println("</HTML>");
          delay(1);
          //stopping client
          client.stop();

          if(readString.indexOf("?lighton1") >0)//checks for on
          {
            digitalWrite(8, HIGH);
            delay(250);
            digitalWrite(8, LOW);
            Serial.println("Lamp 1 On");
            client.println("<a href=\"/?lightAon\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>");
          }
          else{
          if(readString.indexOf("?lightoff1") >0)//checks for off
          {
            digitalWrite(4, HIGH);
            delay(250);
            digitalWrite(4, LOW);
            Serial.println("Lamp 1 Off");
            client.println("<link rel='apple-touch-icon' href='http://files6.fliiby.com/images/_original/ud5qurg7qw.png' />");
          }
          }
          readString="";

        }
      }
    }
  }
}

What I'm looking to do is to also have Twitter support. So if I tweet anything that starts with an "h", I want it to turn my lights on. I have some code that is able to achieve this, except there's two problems that I'm struggling with. The first problem is that the Twitter feed is checked every minute. This minute delay is fine for control with tweets, but not okay for my control with local wifi. I want the twitter delay to not interfere with my instant control of the lights.

The second problem is, let's say I send a tweet, "hello", and my lights turn on since the first letter is "h", the program keeps checking for tweets every minute, and keeps reading the same tweet and turning on the lights. I want the program to read the time of my tweet, and if there are no new tweets, to do nothing.

tl;dr 1)Twitter refresh delay to be separate from other program; 2) Read new tweets only.
The code for twitter is shown below. Any help is appreciated.
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] =  {
  192,168,1,127}; //change this
byte subnet[] = {
  255,255,255,0}; //change this
byte gateway[] = {
  192,168,1,1}; //change this
EthernetClient client;
EthernetServer server(80);
String TwitterName ="Arduinolump"; //change this to your own twittername
char tweet[140];
byte charsize;
byte charsize2;
char serverName[] = "api.twitter.com";  // twitter URL
String SearchDate="<description>Arduinolump: ";
char Twitdate[140];
boolean switcher=1;
char code[] = "h";

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // attempt a DHCP connection:
  Ethernet.begin(mac, ip);
  pinMode(8, OUTPUT);
  server.begin();
  // connect to Twitter:
  delay(300);
}
void loop(){
  if (switcher ==1){
    //  Serial.println("Activating Server Connection");
    if (client.connect(serverName, 80)) {
      TextFinder  finder( client,2 );
      //    Serial.println("HTTP Request Initialized");
      // make HTTP GET request to twitter:
      client.print("GET /1/statuses/user_timeline.rss?screen_name=");
      client.print(TwitterName);
      client.println("&count=1 HTTP/1.1");
      client.println("HOST: api.twitter.com");
      client.println();
      while (client.connected()) {
        if (client.available()) {
          Serial.println("Searching for new tweet");
          charsize2 = SearchDate.length();
          char StartHere[charsize2];  
          char EndHere[] = "</title>";
          char EndDate[]= "</pubDate>";
          SearchDate.toCharArray(StartHere,charsize2);
          if((finder.find("<item>")&&(finder.getString(StartHere,EndHere,Twitdate,140)!=0)))
            Serial.println(Twitdate);
            Serial.println(Twitdate[1]);
            if(Twitdate[1]==code[0]){
              digitalWrite(8, HIGH);
            }
              else{
                digitalWrite(8, LOW);
              }    
          break;
        }
      }
      delay(1);
      client.stop();
    }
    delay (60000);
  }
}
« Last Edit: May 18, 2013, 02:15:34 am by ArduinoMinusPlus » Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Startby removing the delay from your twitter code, then put each one into a function, e.g checkTwitter() and checkWiFi(). In your main loop you will want to call chekWiFi() every time and checkTwitter() every minute. So something like:
Code:
unsigned long nextTwitterCheck;

void loop(){
   checkWiFi();
   if(millis()>nextTwitterCheck){
      checkTwitter();
      nextTwitterCheck+=60000;
   }
}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if(millis()>nextTwitterCheck){

I'd suggest a small change so that timer rollover is handled correctly:

Code:
   if(millis() - lastTwitterCheck > TwitterCheckInterval)
    {
        lastTwitterCheck  += TwitterCheckInterval;
        etc ...
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot guys. I made some progress. I think I'm really close to making it work.
For some reason, when I run my program, both the wifi and twitter functions stop working. If I comment out one out of the two, it works. To make the checkTwitter function work, I have to comment out the entire checkWifi function, not only the one in the main loop, therefore I think the problem is coming from my checkWifi function. I thought it might be the client variable, so I made two separate ones, but no luck. I believe it may have something to do with the server port, but I'm not sure how to fix it. Any help will be appreciated.

Code:
#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {
  192, 168, 1, 127 }; // ip in lan
byte gateway[] = {
  192, 168, 1, 1 }; // internet access via router
byte subnet[] = {
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
EthernetClient client;
EthernetClient client2;
String readString;
String TwitterName ="josemunoz552"; //change this to your own twittername, or follow me ;-)
char tweet[140];
byte charsize2;
char serverName[] = "api.twitter.com";
String SearchDate="<description>josemunoz552: ";
char code[] = "h";
char Twitdate[140];
long TwitterCheckInterval=60000;
unsigned long lastTwitterCheck;
//////////////////////

void setup(){

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  //the pin for the servo co
  //enable serial data print
  Serial.begin(9600);
}

void loop(){
  // Create a client connection
  checkWifi();
  if (millis()-lastTwitterCheck > TwitterCheckInterval){
        checkTwitter();
    Serial.println("checking Twitter now");
    lastTwitterCheck += TwitterCheckInterval;

  }
}

void checkWifi(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

            client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<style> body { background-color:#d0e4fe; }</style>");

          client.println("<TITLE>Moe's Home Automation</TITLE>");

          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Moe's Home Automation</H1>");

          client.println("<hr />");

          client.println("<br />");

          client.println("<a href=\"/?lighton1\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>");
          client.println("<a href=\"/?lightoff1\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a><br /><br />");
          client.println("<a href=\"/?lighton2\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>");
          client.println("<a href=\"/?lightoff2\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a><br /><br />"); 
          client.println("<a href=\"/?lighton3\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>");
          client.println("<a href=\"/?lightoff3\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a><br /><br />");       

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("?lighton1") >0)//checks for on
          {
            digitalWrite(2, HIGH);    // set pin 2 high
            digitalWrite(8, HIGH);
            delay(250);
            digitalWrite(2, LOW);    // set pin 2 high
            digitalWrite(8, LOW);
            Serial.println("Led 1 On");
            client.println("<a href=\"/?lightAon\"\"><img src='http://files6.fliiby.com/images/_original/ud5qurg7qw.png'></a>");
          }
          else{
            if(readString.indexOf("?lightoff1") >0)//checks for off
            {
              digitalWrite(4, HIGH);    // set pin 2 high
              digitalWrite(8, HIGH);
              delay(250);
              digitalWrite(4, LOW);    // set pin 2 high
              digitalWrite(8, LOW);    // set pin 4 low
              Serial.println("Led 1 Off");
              client.println("<link rel='apple-touch-icon' href='http://files6.fliiby.com/images/_original/ud5qurg7qw.png' />");
            }
          }
          //clearing string for next read

          if(readString.indexOf("?lighton2") >0)//checks for on
          {
            digitalWrite(3, HIGH);    // set pin 2 high
            digitalWrite(8, HIGH);
            delay(250);
            digitalWrite(3, LOW);    // set pin 2 high
            digitalWrite(8, LOW);    // set pin 4 low
            Serial.println("Led 2 On");
            client.println("<link rel='apple-touch-icon' href='http://files6.fliiby.com/images/_original/ud5qurg7qw.png' />");
          }
          else{
            if(readString.indexOf("?lightoff2") >0)//checks for off
            {
              digitalWrite(5, HIGH);    // set pin 2 high
              digitalWrite(8, HIGH);
              delay(250);
              digitalWrite(5, LOW);    // set pin 2 high
              digitalWrite(8, LOW);    // set pin 4 low
              Serial.println("Led 2 Off");
              client.println("<link rel='apple-touch-icon' href='http://files6.fliiby.com/images/_original/ud5qurg7qw.png' />");
            }
          }
          //clearing string for next read

          if(readString.indexOf("?lighton3") >0)//checks for on
          {
            digitalWrite(6, HIGH);    // set pin 2 high
            digitalWrite(8, HIGH);
            delay(250);
            digitalWrite(6, LOW);    // set pin 2 high
            digitalWrite(8, LOW);    // set pin 4 low   // set pin 2 high
            Serial.println("Led 3 On");
            client.println("<link rel='apple-touch-icon' href='http://files6.fliiby.com/images/_original/ud5qurg7qw.png' />");
          }
          else{
            if(readString.indexOf("?lightoff3") >0)//checks for off
            {
              digitalWrite(7, HIGH);    // set pin 2 high
              digitalWrite(9, HIGH);    // YO
              digitalWrite(8, HIGH);
              delay(250);
              digitalWrite(7, LOW);    // set pin 2 high
              digitalWrite(9, LOW);    // YO
              digitalWrite(8, LOW);    // set pin 4 low;    // set pin 4 low
              Serial.println("Led 3 Off");
              client.println("<link rel='apple-touch-icon' href='http://files6.fliiby.com/images/_original/ud5qurg7qw.png' />");
            }
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}
void checkTwitter(){
  if (client2.connect(serverName, 80)) {
    TextFinder  finder( client2,2 );
    //    Serial.println("HTTP Request Initialized");
    // make HTTP GET request to twitter:
    client2.print("GET /1/statuses/user_timeline.rss?screen_name=");
    client2.print(TwitterName);
    client2.println("&count=1 HTTP/1.1");
    client2.println("HOST: api.twitter.com");
    client2.println();
    while (client2.connected()) {
      if (client2.available()) {

        Serial.println("Searching for new tweet");
        charsize2 = SearchDate.length();
        char StartHere[charsize2]; 
        char EndHere[] = "</title>";
        char EndDate[]= "</pubDate>";
        SearchDate.toCharArray(StartHere,charsize2);
        if((finder.find("<item>")&&(finder.getString(StartHere,EndHere,Twitdate,140)!=0)))
          Serial.println(Twitdate);
        Serial.println(Twitdate[1]);
        if(Twitdate[1]==code[0]){
          digitalWrite(9, HIGH);
        }
        else{
          digitalWrite(9, LOW);
        }


        break;
      }
    }
    delay(1);
    client2.stop();
  }
}

Logged

Pages: [1]   Go Up
Jump to: