Projects

Tweet The World

Oliver Higgins

Issue 2, August 2017

This article includes additional downloadable resources.
Please log in to access.

Log in

Want an easy way to interact with users, the world and your fans? Tweet it! Using just an Arduino UNO and an Ethernet shield, you can easily add the ability to tweet any piece of information or data.

Who remembers the days before Twitter? Back when celebrities had to go through public relations people or wait until the media discovered that they did something stupid. Now we all know right away, thanks to the likes of Twitter, which provides us with a way to get information out to the masses – fast and with minimal overheads. So how do we harness this power to send our project’s data to those who want it?

THE BROAD OVERVIEW

One of the fundamental things we consider with the projects here at DIYODE, is how we deliver information to the user. Serial outputs and LCDs have their place, but you are not always in a position to be looking at the physical unit. This is part of the appeal, and the drive behind the rapid uptake of the Internet of Things, which is the tech buzzword of the decade. Not just in the maker space. So how can we deliver information from a remote location, with minimal overheads that the end user will be able to access and use, without requiring any additional knowledge or training?

One solution may be to set up a data server and write your information to the database, read it back and display it on a webpage. Fairly straight forward but there is a bit of work and overhead when it comes to maintenance. The question may simply be, “what do you want to do with the data?” Do you need to have longitudinal data or do you only want to inform the individuals that a specific event has occurred? Believe it or not using Twitter is one of the simplest ways to do this. Twitter has 317 million active users per month, with over 500 million tweets being sent per day! There is an excellent chance that the people you want to reach are already on it, allowing you to interact without them needing any additional knowledge.

Of course, let's consider things for a minute... Twitter may not be the answer! If you're sending private information or any sort of information that's potentially sensitive (such as, there's nobody home right now), it's probably not a good idea to use a public service to do the talking. If you're automating public information specifically, or you're tweeting information that's not sensitive at all, then it provides a great, reliable platform. Those 317 million active users rely on Twitter to be live virtually 100% of the time. For that reason, you can rely on it too!

HOW IT WORKS

Let’s back up for a minute and look at Twitter’s infrastructure. At its simplest Twitter is an XML feed. That’s all, remember RSS (Really Simple Syndication) – or am I just showing my age here?

The developers at Twitter took this a step further, and opened the world to microblogging. They took a small story of just 140 characters, and gave us a way to pull the data we wanted to see out of the stream. Feedback from the users soon resulted in the introduction of hashtags. The famous # now creates a channel of ideas or content, which allows individuals to find the information they are interested in, very quickly and consistently.

The Internet of Things (IoT) has sprung up in a huge way over the past few years; however, to create a positive community user experience we need to have a front-end that will allow them to interact with your device. This can be difficult and very time consuming to build, but by using Twitter, you can easily output information to anyone who wants it.

The crew here at DIYODE have felt that we should open a bar. This place will be pretty exclusive with a limited amount of room for patrons. Now we understand that you will all want to come, but you may not want to wait in line to get in. To address this, we will be instigating an Arduino-powered doorman. The role of this doorman will be to count how many people come in and out. Abiding by the legal building requirements, of course, when the bar is full it will send a tweet to all our followers, to let them know. Once some people leave we will then tweet again, to tell our followers that there is now some available space. Almost full? There’s a tweet for that too! Drink specials, $2 Sonic Screwdrivers! #youknowyouwantit

the basics of how this can happen:

You’ll need the Arduino and Ethernet shield (you could also use a WiFi shield with some modification). You’ll also need to have three switches: the first will be the “in” counter; the second will be the “out” counter, and the final switch will be used to deliver a message to the followers, such as drinks specials or other announcements

So what are the rules?

The maximum number of people in our bar is 10 (okay, it’s not a big bar but you don’t want to be doing 200 clicks in testing, right?) Each time we click the “in” button the counter increases. When it gets to 3, it tweets a special drinks offer to entice more customers to come in. When it gets to 8, it sends out a tweet stating that there are limited spots available. Once it gets to 10, it tweets that the bar is full. As we click the “out” button it decreases until it reaches 8, at which point it will send a tweet, and when it returns to 3, it again tweets the drinks special. Finally, we have the third button, which when pushed, sends out a preprogrammed tweet.

THE BUILD

Parts Required: Jaycar Altronics
1 x Arduino UNO R3 XC4410 Z6240
1 x Ethernet Shield XC4412 Z6242
3 x Tactile Switches SP0608 S1112
3 x 10kΩ Resistors RR0596 R7782
SOFTWARE REQUIREMENTS:
Arduino IDE
Twitter Library

BUILDING THE CIRCUIT

The initial sketch requires no circuit. Simply connect the Ethernet shield into the UNO, connect Ethernet and power cables. The system will attempt to send a single "Hello World!" tweet, then sit idle until you restart it. It's the simplest way to test that your access via the Twitter API is successful.

The main sketch has three buttons. Place them one after another on your breadboard. Connect the 5V and GND rails to the Arduino. Connect up the three resistors, which will pull the pin to GND on the pins when the button are not pressed. Connect the GND and 5V connectors from the breadboard rail to the switches. Finally, connect the switch to the Arduino, pin 2 is the “send tweet” button, while pins 5 and 6 are the count up/down switches.

Schematic 1

THE SETUP

The first requirement of this project is to setup a Twitter account. If you already have one then great, otherwise go to Twitter and follow the prompts to sign up. It's probably worth noting that even if you do have an account already, it's often useful to sign up for a new account dedicated to your IoT explorations. It will keep your twitter account clear of all the clutter you'll likely put out during testing.

Once complete, log into your Twitter account. We have named the bar the “Mosfet Cantina”. Feel free to follow us @mosfetcantina (and of course, @diyodemag for real tweets!).

Over the last few years, Twitter has increased its security and so it’s no longer quite as straight-forward to merely put in your username and password to tweet. We have to now go through a developer program to get our OAUTH token. OAUTH is used to all access to external applications, including the Twitter Application Programming Interface (API), without revealing your username and password to outside sources.

We will not go into depth here about creating an app from scratch; instead, we’ll use one already provided by the Arduino community.

CREATING AN ACCOUNT

  1. If you don’t already have a Twitter account then create one. The process includes uploading a photo and filling out a brief bio.
  2. You need to be logged into your Twitter account then go to Tweet Library for Arduino to create a secret key.
  3. Connect the Arduino, Ethernet shield and RJ45 cable.
  4. Make sure the Twitter library is installed on your development machine. We have provided it in our resources also.

Now let’s test it out with a simple tweet. Make sure you insert your secret code from step 2 into the “Twitter twitter” line.

You should get a tweet sent from your account saying “Hello World!” Be mindful if you try to repeatedly tweet the same message again, the API will give you an error. You must change the message or wait for a while before resending the same one.

Note: When using this, there are some limitations on how many tweets can be sent and how often. You can send as many as you want, but if you fire them off too quickly then the Twitter API will begin to limit you. You may also overload the Arduino.

#include <SPI.h> 
#include <Ethernet.h> 
#include <Twitter.h> 
byte mac[] = 
      { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
Twitter twitter("YOUR CODE FROM STEP TWO"); 
char msg[] = "Hello, World!"; 
void setup() 

  delay(1000); 
  Ethernet.begin(mac); 
  Serial.begin(9600); 
  Serial.println("connecting ..."); 
  if (twitter.post(msg)) { 
    int status = twitter.wait(&Serial); 
    if (status == 200) { 
      Serial.println("OK."); 
    } else { 
    Serial.print("failed : code "); 
    Serial.println(status); 
  } 
  } else { 
    Serial.println("connection failed."); 
  } 
}
  
void loop() 

Hook up the breadboard and put the 5V and GND rails to the Arduino. Connect the 3 buttons as per the diagram.

Button 1 is our “send” button. This could be used to send a random tweet to all our followers, but in this case, we’ll simply be sending a predefined text message. This code is very similar to the code example above, but adds into the loop a part so as to check the button.

Button 2 increases the pplCounter Integer; button 3 decreases it. It returns to the loop and checks the pplCounter button against certain thresholds. If it is equal to them, then it sends out a preprogrammed message: “Sonic Screwdrivers $2 #mosfetcantina #nightlife”. It then sets the “msgSent” integer to 1. As the loop comes back again, it checks to see if the msgSent is 1 or not. This way we don’t resend the message over and over again. Once a button is clicked again, the msgSent variable is set to 0, and the counter loops again to check if it is required to send another message at this level.

Let's Do Something Different

What if you don't own a bar? What if you don't care how many people are in your building? What if you just want to automate the whole process?

Let's look at a different application, while still using the Twitter platform.

In issue #001, we used the DHT11 Temperature / Humidity sensor, displaying the output to screen in our Arduino Web Server project.

What if you could get tweets for the temperature of your home brew? Or when it gets REALLY cold? The DHT11 can do that.

THE BUILD

ADDITIONAL Parts Required: Jaycar Altronics
1 x DHT11 Temp/Humidity Sensor XC4520 Z6320
ADDITIONAL SOFTWARE REQUIREMENTS:
DHT11 Library
Schematic 2

BUILDING THE CIRCUIT

Only a few wires are required, as well as the DHT11 sensor. Load the Twitduino_DHT_0_0_4.ino sketch onto your Arduino (provided in the resources).

Every 2 minutes, your Arduino will tweet out the current temperature and humidity. Easy! As noted previously, Twitter tends to automatically disregard duplicate messages that follow too quickly. So if during testing your tweets don't seem to be getting through, this may be why. Of course, keeping an eye on your serial monitor while you test things out will provide generous feedback on all that's happening.

WHERE TO FROM HERE?

If you wanted to expand this project, you could easily add an IR trip beam or laser sensor to button 2 and 3 on the real door at your home or shop. You could then add in an extra variable to count in the total traffic and tweet it at the end of the day.

The current code presents a very hard line in the sand, so that when you hit 10, it tweets. Therefore, if you had 11 people and 1 left you return to 10, which automatically fires off another tweet. The code could do with some fuzzy logic to decide if patron numbers are on their way up or down, and to build in different thresholds.

Doing this could open up an amazing world for you to communicate with your users and fans. It would not take much to turn this into a full blown weather station, or what about live tweeting lap times on a race circuit? You could add in the Arduino camera module and then also live tweet pictures, traffic jam warnings, GPS location trackers, or a house alarm. Any piece of data you want to communicate can easily be sent via Twitter, provided the message is 140 characters or less of course.