185 lines
4 KiB
C++
185 lines
4 KiB
C++
|
|
////////////////////////////////////
|
|
// PIÑATA TWITTER //
|
|
// Developed by MID //
|
|
// Programmed by Alex Posada //
|
|
////////////////////////////////////
|
|
|
|
// time betwen every petition
|
|
#define postingInterval 8000
|
|
// time for every tweet
|
|
//#define timeTweet 1000
|
|
|
|
|
|
#include <SPI.h>
|
|
#include <Ethernet.h>
|
|
#include <TextFinder.h>
|
|
//#include <stdlib.h>
|
|
|
|
// assign a MAC address for the ethernet controller.
|
|
// fill in your address here:
|
|
byte mac[] = {
|
|
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
|
|
|
// for manual configuration:
|
|
IPAddress ip(10,0,1,20);
|
|
|
|
// initialize the library instance:
|
|
EthernetClient client;
|
|
|
|
long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
|
|
boolean lastConnected = false; // state of the connection last time through the main loop
|
|
|
|
int globoPin = 7;
|
|
int timeTweet = 1000;
|
|
unsigned long newTweets = 0;
|
|
unsigned long timerTweet = 0;
|
|
unsigned long timeLimit = 0;
|
|
|
|
TextFinder finder( client );
|
|
|
|
void setup() {
|
|
|
|
pinMode(globoPin, OUTPUT);
|
|
digitalWrite(globoPin, HIGH);
|
|
// start serial port:
|
|
Serial.begin(9600);
|
|
// give the ethernet module time to boot up:
|
|
// start the Ethernet connection:
|
|
if (Ethernet.begin(mac) == 0) {
|
|
Serial.println("Failed to configure Ethernet using DHCP");
|
|
// Configure manually:
|
|
Ethernet.begin(mac, ip);
|
|
}
|
|
|
|
// print your local IP address:
|
|
Serial.print("My IP address: ");
|
|
for (byte thisByte = 0; thisByte < 4; thisByte++) {
|
|
// print the value of each byte of the IP address:
|
|
Serial.print(Ethernet.localIP()[thisByte], DEC);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println();
|
|
|
|
connectToserver();
|
|
|
|
digitalWrite(globoPin, LOW);
|
|
|
|
}
|
|
|
|
void loop() {
|
|
|
|
while (client.connected())
|
|
{
|
|
|
|
newTweets = 0;
|
|
|
|
while (client.available()) //while
|
|
{
|
|
|
|
//char c = client.read();
|
|
//Serial.print(c);
|
|
|
|
//finder.findUntil("utf-8", "\n\r");
|
|
finder.find("Connection:");
|
|
newTweets = finder.getValue(); // get numeric value
|
|
Serial.print(newTweets);
|
|
Serial.println(" tweets");
|
|
|
|
lastConnected = 0;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
if( ((!client.connected()) && !lastConnected) )
|
|
{
|
|
Serial.println();
|
|
Serial.println("Disconnecting by server");
|
|
lastConnected = 1;
|
|
client.flush();
|
|
client.stop();
|
|
}
|
|
else
|
|
{
|
|
Serial.print('.');
|
|
delay(1000);
|
|
}
|
|
|
|
|
|
// si no hay nuevos hashtags!!!
|
|
if(newTweets == 0)
|
|
{
|
|
if ( (!client.connected() ) && (millis() - lastConnectionTime > postingInterval) )
|
|
{
|
|
Serial.println();
|
|
Serial.println("Disconnecting by timer");
|
|
lastConnected = 1;
|
|
client.flush();
|
|
client.stop();
|
|
connectToserver();
|
|
}
|
|
}
|
|
else // si hay nuevos hashtags!!!
|
|
{
|
|
//newTweets = 50;
|
|
Serial.print(newTweets);
|
|
Serial.println(" new tweets detected!!!");
|
|
Serial.print("Inflating ");
|
|
//Serial.print(newTweets*2);
|
|
//Serial.println(" seconds");
|
|
|
|
timerTweet = millis();
|
|
timeLimit = newTweets * timeTweet;
|
|
Serial.print(timeLimit);
|
|
Serial.println(" miliseconds");
|
|
while(millis() - timerTweet < timeLimit ) // numero de tweets nuevos por 2 seg cada uno
|
|
{
|
|
digitalWrite(globoPin, HIGH);
|
|
Serial.print('=');
|
|
delay(1000);
|
|
}
|
|
Serial.println("> Inflated!");
|
|
digitalWrite(globoPin, LOW);
|
|
newTweets = 0;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this method makes a HTTP connection to the server:
|
|
void connectToserver() {
|
|
|
|
//////////////////////////////////////////////////////////
|
|
// tweet petition
|
|
//////////////////////////////////////////////////////////
|
|
|
|
if (client.connect("pinyata.mediainteractivedesign.com", 3000)) {
|
|
Serial.println("connecting...");
|
|
Serial.println();
|
|
// send the HTTP PUT request.
|
|
// fill in your feed address here:
|
|
//client.println("GET /list/ HTTP/1.0");
|
|
client.println("GET /t2p/ HTTP/1.0");
|
|
client.println("host: pinyata.mediainteractivedesign.com");
|
|
client.println();
|
|
|
|
// note the time that the connection was made:
|
|
lastConnectionTime = millis();
|
|
}
|
|
else {
|
|
// if you couldn't make a connection:
|
|
Serial.println("connection failed");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|