I had previously got updating the normal channel streams for ThingSpeak working here. and now I wanted to get the twitter integration they expose working too. Find the code below. (Note that twitter will not post the same tweet over and over again so you will only see it the first time. make sure you are changing it with each update, this is just for example purposes.)
Next up, working on the TalkBack api and then moving this to a library.
// Thinkspeak twetter api key
String tweetAPIKey = "<<your twitter api key from thingspeak>>";
// TCP socket initialize
TCPClient client;
int led = D7;
/*--------------------------------------------------------
Setup
--------------------------------------------------------*/
void setup()
{
pinMode(led, OUTPUT);
Serial.begin(9600);
delay(10000);
Serial.println("===Starting===");
ThingSpeakTweet("Starting the ThingSpeak tweet example from my Spark Core.");
}
/*--------------------------------------------------------
Main loop
--------------------------------------------------------*/
void loop()
{
digitalWrite(led, HIGH); // Turn ON the LED
delay(1000); // Wait for 1000mS = 1 second
digitalWrite(led, LOW); // Turn OFF the LED
delay(1000);
}
void ThingSpeakTweet(String tweetData)
{
Serial.println("...Attempting to Tweet, "+tweetData);
// Connecting and sending Tweet data to Thingspeak
if(client.connect("api.thingspeak.com", 80))
{
Serial.println("...Connection succesful, updating datastreams");
tweetData = "api_key="+tweetAPIKey+"&status="+tweetData;
client.print("POST /apps/thingtweet/1/statuses/update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tweetData.length());
client.print("\n\n");
client.println(tweetData); //the ""ln" is important here.
// This delay is pivitol without it the TCP client will often close before the data is fully sent
delay(200);
Serial.println("Tweet sent.");
}
else{
// Failed to connect to Thingspeak
Serial.println("Unable to connect to Thingspeak.");
}
if(!client.connected()){
client.stop();
}
client.flush();
client.stop();
}