3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Twitter Mood Light - The World's Mood in a Box

Step 9Programming step 3: Searching Twitter with TCP/IP port 80

Programming step 3: Searching Twitter with TCP/IP port 80
«
  • twitterSearch.jpg
  • example2.jpg
  • example3..jpg
Http is just TCP/IP on port 80

for example:

"Open www.google.com 80"

will open a Http connection to www.google.com.

Twitter actually requires more of the Http protocol than google.

For example, the "Host" field is often required in case there's more than one
domain name mapped to the server's IP address so it can tell which
website you actually want.

Twitter also requires a final linefeed and carriage return ("\r\n")

"GET /\n"
"Host: server\r\n"
"\r\n"

I use search.json rather than search.atom to give results in non-html format, and more easily parsed. (see apiwiki.twitter.com/Twitter-API-Documentation)

 
 
/*
 Parameters: The server to telnet into, the get command that needs to be sent, a custom HtmlParser that
 is called every time a character is received. The parser is responsible for processing the HTML
 that is returned.
*/
bool WiFly::HttpWebRequest(const char* server, const char* getCommand, HtmlParser* parser)
{
 m_printer->println(getCommand);
 FlushRX();
 FlushRX();
 
 // Enter command mode
 EnterCommandMode();
 FlushRX();
 
 // open a TCP connection, port 80 for HTTP
 WriteToWiFly("open ");
 WriteToWiFly(server);
 WriteToWiFlyCR(" 80");
 
 bool openOK = WaitUntilReceived(COMM_OPEN);
 
 if (openOK == false)
 {
    m_printer->println("open port failed!");
    delay(1000);
    WriteToWiFlyCR("close");
    WaitUntilReceived(COMM_CLOSE);
 
    ExitCommandMode();
    return false;
 }
 
 // eg. "GET /search.json?q=foo HTTP/1.1\r\n"
 WriteToWiFlyCRLF(getCommand);
 
 // eg. "Host: search.twitter.com\r\n"
 WriteToWiFly("Host: ");
 WriteToWiFlyCRLF(server);
 
 // "\r\n"
 WriteToWiFlyCRLF("");
 
 // now wait for the response
 
 int timeOut = 0;
 bool ok = false;
 
 while(timeOut < 5000)// timeout after 5 seconds
  {
    if((ReadCharFromWiFly(LSR) & 0x01))
    {
      char incoming_data = ReadCharFromWiFly(RHR);
      m_printer->print(incoming_data,BYTE);
 
      bool done = parser->Parse(incoming_data);
      if (done)
      {
        ok = true;
        break;
      }
 
      timeOut = 0; //reset the timeout
    }
    else
    {
      delay(1);
      timeOut++;
    }
 }
 
 FlushRX();
 
 // disconnect TCP connection.
 WriteToWiFlyCR("close");
 WaitUntilReceived(COMM_CLOSE);
 
 ExitCommandMode();
 
 return ok;
}

« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
29
Followers
1
Author:RandomMatrix