top of page

Weekend IoT Project: Send a Tweet

The Weekend IoT Project blog series provides interesting and easy to follow evening or weekend projects for students, developers and IoT enthusiasts.

This blog will show you how to easily send a Tweet from virtually any IoT device, and should take less than an hour to complete (assuming you already have an IoT device and the pre-requisites are met). The examples below will provide code in the following languages: C++, Python and Node.js, and the IoT devices used are an Arduino-compatible chip (the MXChip), a Raspberry Pi, and the Matrix Creator hat.

Note: You can still follow most of the steps below without an actual IoT device if you are interested in learning how to tweet without an SDK. To do so, follow the Node.js instructions under the Matrix Creator section after you have configured Twitter and Enzo Online.

Pre-Requisites

  1. An IoT device that you can already program and deploy code to that is either an Arduino-compatible chip (ex: MXChip), a Raspberry Pi, or a Matrix Creator hat.

  2. An IoT device that can communicate to the Internet using the HTTP protocol

  3. A Twitter account

  4. An account with Enzo Online (sign up for free: http://portal.enzounified.com)

Create a Twitter Application

The first step is to create an Application registered with Twitter, so that the IoT device can tweet on your behalf. To create your Twitter application, visit https://apps.twitter.com/. Login with your Twitter account.

NOTE: Your Twitter profile must have a phone number specified; visit your settings on Twitter and add a mobile phone number before proceeding.

  1. Click on the button "Create New App"

  2. Enter a Name, Description, a website URL you own, and agree to the developer agreement terms. You do not need to specify a Callback URL.

  3. Click on Create Twitter Application

That's it! You have registered a new Application with Twitter; by default you can read from and write to your Twitter timeline. From the Application page click on Keys and Access Tokens then click on Create my Access Tokens at the bottom of the page. Copy the following information as you will need them later:

  • Consumer Key (API Key)

  • Consumer Secret (API Secret)

  • Access Token

  • Access Token Secret

Create an Enzo Twitter Configuration

The next step is to configure Enzo Online with the above Twitter secrets, so your IoT device can tweet through basic HTTP calls. Let's configure an Enzo Twitter Service:

  1. Login to Enzo Online (https://portal.enzounified.com)

  2. Click on the Twitter service on the left menu

  3. Click on the Create button

  4. In the name field, enter the configuration name (for example: twitterconfig)

  5. Select the Instance where the Enzo Twitter service will be running on

  6. In the remaining fields, enter the API Keys and Access Token information previously obtained from your Twitter application

  7. Click Save

NOTE: Sensitive information is stored encrypted in the Enzo Online service data store, and all communications take place over HTTPS for strong security.

At this point, the configuration of Twitter is complete; you can start sending tweets and search for tweets using simple HTTP commands.

Sending a Tweet from an Arduino Chip

The following code will send a Tweet using the C++ language, which is the language you need to use to program an Arduino or Arduino compatible board such as the MXChip. For a complete tutorial on how to program against the MXChip see my previous blog on How to Send Temperature Data to Azure Tables.

NOTE: The Arduino HTTP libraries may require the SSL public certificate, which is included below for convenience. Other languages do not require the certificate.

NOTE: Replace "ENZO_AUTH_ID" with your Enzo AuthToken as found on the Enzo Online Portal under Enzo Accounts.

include "http_client.h";

const char enzoAuthId[] = "ENZO_AUTH_ID";

const char configName[] = "twitterconfig";

const char enzoURI[] = "https://daas001.enzounified.com:49550/bsc/twitter/poststatus";

const char enzoCERT[] =

"-----BEGIN CERTIFICATE-----\r\nMIIFdzCCBF+gAwIBAgIQAX5ydW3JgcN8+u68lX4M2zANBgkqhkiG9w0BAQsFADBC\r\nMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNR2VvVHJ1c3QgSW5jLjEbMBkGA1UEAxMS\r\nUmFwaWRTU0wgU0hBMjU2IENBMB4XDTE3MDUxMTAwMDAwMFoXDTE4MDUxMTIzNTk1\r\nOVowHDEaMBgGA1UEAwwRKi5lbnpvdW5pZmllZC5jb20wggEiMA0GCSqGSIb3DQEB\r\nAQUAA4IBDwAwggEKAoIBAQCsiFzG45oBbw6XhFBcaQzXwDRZTmAo9J3r3koV5M8e\r\nyOtgVfD9H0Kq+gKLreiaGThVwqOunSYQaqGxpGDy4+7pBc1e5ysAstsAv38XQTxO\r\nBfe8Gaw/Yj8cDSb0JlmUmzYwHLaf0XBfiE9hqRCGwBlLuFTYsJohqAoTezpwHgC5\r\nkoeIx9kWpYvt3CbV/63YGHb0C0dUhGW8rEYZwFYoiwyvrD9wqvjVFxYb6BROVDfi\r\nRVjyqcqmAv0zoDwXfsSji22wEbz+XSMRjc0ymmlPxZ7ny+EhMclINRZvRCCdUELa\r\nJFEhc/sD8hg5gJUCG/Gjb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-----\r\n";

// Method that sends a tweet

void sendTweet(const char * data)

{

char tweet[256]; // add the @enzo_unified tag

snprintf(tweet, 256, "%s @enzo_unified"

,data

);

HTTPClient client = HTTPClient(enzoCERT, HTTP_POST, enzoURI);

client.set_header("authToken", enzoAuthId);

client.set_header("_configName", configName);

client.set_header("status", tweet);

const Http_Response *response = client.send(NULL, 0);

}

void setup() {

// initialization code for the arduino board

}

int counter = 0;

void loop() {

// the main programming loop

// Send a single tweet

if (count == 0) {

sendTweet("Sending a tweet from an IoT device! ");

}

count++;

}

Sending a Tweet from a Raspberry Pi

In this section of the blog, you will send a tweet from a Raspberry Pi, using Python. If are not familiar with Python, check this introduction from the Raspberry Pi website: https://www.raspberrypi.org/documentation/usage/python

Python is a simple programming language that, like other languages, uses libraries to communicate through HTTP. Unlike the C++ libraries available on the Arduino, the HTTP libraries for Python do not require the certificate chain of the Enzo service.

NOTE: While we are sending an HTTP POST, we are not posting any data; the tweet content is provided as a header. However, you could also send the tweet as a data payload by specifying the Content-Type as application/x-www-form-urlencoded. See the Enzo Online documentation for more information.

import sys import urllib import urllib2 import requests

enzourl=”https://daas001.enzounified.com:49550/bsc/twitter/postStatus” enzoAuthId=”ENZO_AUTH_ID” configName=”twitterconfig”

headers={‘status’:'Sending a tweet using Python - @enzo_unified', ‘authToken’:enzoAuthId, ‘_configName’:configName, }

response=requests.post(enzourl,headers=headers, data = { })

As you can see above, sending a tweet using Python and Enzo Online is trivial; no SDK or special libraries are needed.

Sending a Tweet from the Matrix Creator

The Matrix Creator is a Raspberry Pi hat that requires special configuration; this blog assumes you can already program against the Matrix Creator and publish a Node.js application to the board. To download Node.js, follow this link.

The following code written in Node.js sends a single tweet through Enzo Online.

var request = require('request');

const enzoUrl = "https://daas001.enzounified.com:49550/bsc/twitter/poststatus";

var headers = {

'_configName': 'twitterConfig',

'authToken': 'ENZO_AUTH_ID',

'status': 'Sending a tweet from Matrix Creator - @enzo_unified' }

var options = {

url: enzoUrl,

method: 'POST',

headers: headers }

// FUNCTION: send a tweet

function sendTweet() {

request(options, function (error, response, body) {

if (!error && response.statusCode === 200) {

console.log('Tweet sent'); } else {

console.log('Status Code ERROR: ' + response.statusCode);

}

});

}

// Send the tweet now

sendTweet();

The above code sends a single tweet. You can run the above code directly from a Node.js console; I saved the code in a file called tweet.js:

Conclusion

This blog shows how to easily send a tweet from three programming languages: C++, Python and Node.js. This in turns allows you to easily tweet from a large number of IoT devices that can communicate through an internet connection.

bottom of page