Update Twitter with ThingSpeak

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;

void setup()
    pinMode(led, OUTPUT);
    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

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.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
        Serial.println("Tweet sent.");
        // Failed to connect to Thingspeak
        Serial.println("Unable to connect to Thingspeak.");

Awesome work… We will share this with the rest of the ThingSpeak Community and get some more sparks online!

1 Like

That’s good news. Once I get it in library form I will update this post. Any chance someone could help me with the formatting for the talkback api calls?

What do you need on TalkBack? We can help. Here’s the forum that you can use for TalkBack questions. http://community.thingspeak.com/forum/thingspeak-apps/ Thanks!

1 Like

work it but how to make it repeat the tweet because it only tweet once?any help

Twitter blocks duplicate tweets - similar content within an hour. Most people will use a date time parameter to make the tweet body slightly different.

ThingSpeakTweet(“Starting the ThingSpeak tweet example from my Spark Core. %%datetime%%”);

ThingSpeak will replace the variable with the latest time. You can also replace channel data using %%channel_1417_field_1%%

ThingSpeakTweet(“The current CheerLights color is %%channel_1417_field_1%%”);

Here’s more info: https://www.mathworks.com/help/thingspeak/thingtweet-app.html

hello sorry to disturb you…but it didn’t’ work with parameter time or channel…or i should put that % in the coding or not?can you give me another example coding about this for better understanding…need your help :sob: