Introduction: Make an Easy Button Tweet the Hard Way

There are several ways to make a microcontroller tweet. The easy methods are well covered in other instructables.
  1. Connect it to a computer running a python script or similar.
  2. Connect it to a hacked router (nice - you know who you are).
  3. Using a third party proxy server e.g. ThingTweet, NeoCat's Arduino library, Arduino Tweet Library.
A method that is not well covered is handling the entire process of generating a Tweet using the microcontroller only. This method has a couple of advantages over the other methods.
  • It doesn't require an external computer/router.
  • It doesn't have the security risks of method three. Passing security credentials over an unsecure transport is risky. If you are using a proxy server to generate your Tweets make sure you understand the risks.
Of course this method also has disadvantages in terms of code size and complexity; which is why I have termed it the "hard way".

The intention of this instructable is to give others a basic standalone framework for generating Tweets from a microcontroller.The application itself is pretty simple; press the button and it generates a semi random Tweet.

Step 1: Parts

I have used the STM32 Discovery Board (STM32F103RG) for this project, because I am familiar with it and I had one on hand. Most of the code above the driver level (easybutton.c, nokia5110.c and wiznet5100.c) should be platform independent so porting the code to other microcontrollers should be straightforward.

Only a few parts are required for this project

Step 2: Modifying the Easy Button

Modifying the Staples Easy Button is, to coin a phrase, easy.
  1. Remove the four rubber feet and the four small screws under them.
  2. Unsolder the Vss and Vdd wires that connect to the battery terminals.
  3. Remove the battery terminals .
  4. Solder new wires to the Vss and Vdd pads.
  5. Solder a wire to the empty pad near the push button switch (see image).
  6. Drill a hole into the battery compartment and pass the three wires through it.
  7. Reassemble the button.
You will now have a button with three wires sticking out.
  • Vss - ground
  • Vdd - positive supply voltage (3V although it seems to handle 3.3V quite happily)
  • Button sense - 0V rising to 3V when button is pressed.

Step 3: Hardware Schematic

The hardware is pretty basic. Time constraints have meant that it is still laid out on a breadboard but I plan to shortly make up a more permanent circuit with all the parts fitting into a small base under the button.

The power supply provides the Discovery Board with 5V and this is then stepped down to 3.3V by the onboard regulator to run all the peripherals.

The modified button is connected to PA1 of the Discovery Board and supplied with 3.3V which doesn't seem to effect its standard operation (the "that was easy" soundtrack).

The WIZnet W5100 Network module is connected to the SPI1 peripheral (pins PA5, PA6 and PA7) with additional connections for /Reset (pin PA2) and /Slave Select (pin PA4)

The Nokia 5110 Graphic LCD module is connected to the SPI2 peripheral (pins PB13 and PB15) with additional connections for /Reset (pin PB10), Data/Command (pin PB11) and /Slave Select (pin PB12).

There are a wide variety of general purpose IO pins and peripherals remaining in the Discovery Board for more advanced Twittering applications in the future.

Step 4: Workflow and Code Modules

The main.c module contains the workflow for the project (see image). After initializing the microcontroller board a request is sent to the network DHCP server to get an IP address and the network settings. Next a request is sent to a Simple Network Time Protocol server to get the current time and date. The code then enters a loop waiting for you to browse to the IP address and set the Oauth parameters. Once the parameters are set the code waits for the Easy Button to be pressed and then Tweets a random message from the selection provided.

The binary file currently stands at just under 35K in size. The code is not currently optimized so it should be possible to get the binary under 32K with a little work; of course that depends on the platform and the compiler.

The following gives an overview of the source code modules for the project. Time constraints mean the code is not as well commented as I would usually like but hopefully with the documentation in this instructable the modules should be fairly easy to follow.

Common
dictionary.c
  • a useful helper class that controls a sorted linked list of name/value pairs.
Encryption
sha1.c
hmacsha1.c
  • Used to create a hash of the OAuth request and then encrypt it with the Consumer Secret and Access Token Secret.
  • I can't take credit for these modules; they come largely unaltered from the AVR Crypto Library (http://www.das-labor.org/wiki/Crypto-avr-lib).
base64.c
  • Base64 encode and decode.

Drivers
easybutton.c
  • Driver for the modified Staples Easy Button.
nokia5110.c
  • Nokia 5100 Graphic LCD driver (SPI).
wiznet5100.c
  • WIZnet W5110 Network Module driver (SPI).

Protocols
Transport
udp.c
tcp.c
  • The WIZnet module takes care of these two transport layer protocols so technically these also fall under the driver modules but they have been separated out for clarity.

Application
http.c
formdata.c
  • HTTP client methods for making GET and POST requests to a web server.
  • HTTP server methods for accepting incoming GET and POST calls.
dhcp.c
  • Get an IP address, subnet mask, gateway and DNS server IP from the network DHCP server.
dns.c
  • Resolve a given URL to a corresponding IP address.
sntp.c
  • OAuth demands an accurate timestamp; this module requests the current time from a Simple Network Time Protocol server.

Web Server
webserver.c
  • A listener for HTTP GET and POST requests.
  • Allows for setting the OAuth Consumer Key, Consumer Secret, Access Token and Access Token Secret.

Twitter
twitter.c
  • Implementation of the Twitter API (currently only status update is used).
  • Most of the heavy lifting is handled by the OAuth module.
oauth.c
  • The core module of the Twitter framework.
  • Generate a unique identifier for the request (nonce).
  • Generate the timestamp for the request.
  • Generate the signature base for the OAuth request.
  • Calculate a hash of the signature base (SHA1).
  • Sign the hash using the Consumer Secret and Access Token Secret (HMAC-SHA1).
  • Send the signed request to the web server (HTTP GET or POST).

Step 5: Create a New Twitter Application

In order to tweet you need to have the following
  • Consumer Key
  • Consumer Secret
  • Access Token
  • Access Token Secret
The Consumer Key tells Twitter what application is sending the Tweet.
The Token tells Twitter what user is Tweeting.

The Consumer Secret and Token Secret are not sent to the Twitter API but are used to calculate a cryptographic hash of the request which is used as a signature to prevent tampering. The request includes a unique identifier (nonce) and a timestamp that must be within five minute of the UTC time. Only one request will be accepted for a given nonce/timestamp pairing.

To get these key/secrets you need to create a new Twitter application.

Go to https://dev.twitter.com/apps - use your Twitter credentials to sign in.

Select "Register a new app".
Enter the application details
  • Application name - anything you like
  • Description - anything you like
  • Application Website - (required) anything you like
  • Organization - (optional) anything you like
  • Application Type - Client
  • Default Access Type - Read & Write
  • Application Icon - use default or upload your own
This will create a new application and give you a Consumer Key and Consumer Secret.

Normally for applications you would request an Access Token and Access Token Secret for each user; in this case since there will only be a single user for the application there is a shortcut. From the application settings page select "My Access Token" to get an Access Token and an Access Token Secret.

Congratulations you are now ready to make your first tweet.

Step 6: Operation

Operation of this project is straightforward
  1. Connect the Ethernet jack to you network/router.
  2. Check the LCD to see what IP address has been assigned.
  3. In a web browser browse to the IP address (e.g. http://192.168.0.13).
  4. Enter the Consumer Key, Consumer Secret, Access Token and Access Token Secret you obtained and click Set.
  5. The LCD will now display "Ready" and the button will be armed.
  6. Tweet away until you are bored or your friends stop following you :)

Comments

author
jerpenner made it!(author)2014-07-15

Is there a place where you can customize the message being sent? Where are the semi-random tweets being set?

Thanks, looks interesting!

author
lanky.ninja made it!(author)2011-07-19

Unfortunately this is to advanced for me, but i still managed to understand what it was that was happening thanks to great explenations. I look forward to coming back to this in the future as a learning tool.
Great Job!

author
Barnaby+Walters made it!(author)2011-07-04

In a sea of 'MAKE ARDUINO TWEET IN 5 SECONDS" instructables, this is a rare and well written guide! I always like it when people go the extra mile and make something like this, a standalone device. Thanks a lot!

author
Layout made it!(author)2011-07-04

Thanks for the kind comments. Unfortunately in terms of the "Make it Tweet" competition if it didn't use an Arduino it never had a shot. Unfortunately I get the impression that the Microcontroller contest will go the same way - let's hope I'm wrong.

author
Barnaby+Walters made it!(author)2011-07-04

I'd say you have a chance in the MCU contest — after all, this is quite impressive! Although there does seem to be emphasis on making things move. It's more impressive than my entries, anyway!

author
Layout made it!(author)2011-07-04

Yeh, this instructable was an afterthought in the Microcontroller contest. My main entry is this one https://www.instructables.com/id/Delta-Robot-Clock/.

author
pdxnat made it!(author)2011-06-28

A fun hack and a well written instructable! Nice work.

author
frank26080115 made it!(author)2011-06-27

Every ARM project makes me smile

author
wilgubeast made it!(author)2011-06-27

If something's worth doing, it's worth doing right. This is awesome.

About This Instructable

6,596views

3favorites

License:

Bio: By day a senior software engineer. By night an inventor who has not yet managed to give up his day job.
More by Layout:Show your support in snowSimple Ground Beef (Mince) PieMake an Easy Button Tweet the Hard Way
Add instructable to: