Project Description

This project consists of a C# GUI application to query tweets from a specific account and control peripherals using Remote Procedure Calls (RPC) on an mbed. The application allows users to acquire tweets by username and store them in a Microsoft Excel spreadsheet on the local machine. RPC functions are used to print tweets selected in the GUI to an LCD and control status LEDs connected to the mbed.

Demo

Here's a short demo video of the usage and functionality of the application



Used Hardware

Hardware Setup

The 100 ohm resistors are connected to pins 22, 23, and 24. These correspond to the blue, green, and red leads on the RGB LED.

The TX, RX, and Reset pins on the uLCD are connected to pins 28, 27, and 29 respectively.

Writing RPC functions for the mbed

RPC functions are objects that wrap functions that can be executed on the mbed through RPC. Below is the code that enables the mbed to listen for and execute instructions passed to it from the C# application. Click on this link to access the repository containing the mbed portion of the project. This tutorial contains more information about the RPC library used in our code.

#include "mbed.h"
#include "mbed_rpc.h"
#include "uLCD_4DGL.h"
#include <ctype.h>
#include "RGB_LED.h"
 
//Use the RPC enabled wrapped class  - see RpcClasses.h for more info
Serial pc(USBTX, USBRX);
PinName tx, rx, rst;
RGBLed myRGBled(p22,p23,p24);
uLCD_4DGL uLCD(p28, p27, p29);
char text;
//string screen;
 
void clearLCD(Arguments *in, Reply *out);
RPCFunction rpcClear(&clearLCD, "clearLCD");
 
void printLCDAndLightRGB(Arguments *in, Reply *out);
RPCFunction rpcPrintLCDAndLightRGB(&printLCDAndLightRGB, "printLCDAndLightRGB");
  
int main() {
 
    char buf[256], outbuf[256];
    while(1) {
        pc.gets(buf, 256);
        //Call the static call method on the RPC class
        RPC::call(buf, outbuf); 
        pc.printf("%s\n", outbuf);
    }
}
 
void clearLCD(Arguments *in, Reply *out)
{
    uLCD.cls();
}
 
void printLCDAndLightRGB(Arguments *in, Reply *out)
{
        uLCD.cls();
        char * text = in->getArg<char *>();
        int value = strlen(text);
        if(text[0] == '1'){
          myRGBled.write(0,1.0f,0);
          uLCD.printf("Tweet is found!\n");
          uLCD.printf("\n");
        } else if(text[0] == '0'){
          myRGBled.write(0,0,1.0f);
        }
        text[0] = '-';
        for(int i = 0; i< value; i++) {
            if(text[i] == '|') {
               text[i] = ' ';   
            }
        }
        uLCD.printf(text);
 }

Creating the C# GUI Application

Step 1: Download the Visual Studio Solution Files

Download the solution files provided here, unzip, and open them in Visual Studio. Make sure to have .NET Framework 4.0 or higher installed.

Step 2: Adding TweetinviAPI to Visual Studio Project

Tweetinvi is already included in the Solution Files for this project. However, if you want to add the TweetinviAPI (or any other package) to your own project

  1. Open the Solution Explorer
  2. Right Click on 'References'
  3. Click 'Manage NuGet Packages'
  4. Under 'Browse' search Tweetinvi
  5. Install

Step 3: Twitter Account and OAuth Credentials

Twitter applications require special credentials to make API calls and Twitter warns developers that these keys "should never be human-readable in your application" and "Do not share your access token secret with anyone." Our credentials aren't included in the source code that we've provided. Below is a few lines of code from the 'App.config' file where a user can place their own unique credentials.

...
<configuration>
    <appSettings>
      <add key="cKey" value="your_cred" />
      <add key="cSecret" value="your_cred" />
      <add key="aKey" value="your_cred"/>
      <add key="aSecret" value ="your_cred"/>
    </appSettings>
...

Head to Twitter to create an application and generate the credentials to replace the lines containing "your_cred" in the App.config file. Visit the 'Keys and Access Tokens' tab to get the Consumer Key, Consumer Secret, Access Token, and Access Token Secret. These lines of code below in the source code for the Windows Form Authenticate the application

string ckey = ConfigurationManager.AppSettings["cKey"];
string cSecret = ConfigurationManager.AppSettings["cSecret"];
string aKey = ConfigurationManager.AppSettings["aKey"];
string aSecret = ConfigurationManager.AppSettings["aSecret"];

Auth.SetUserCredentials(ckey,cSecret,aKey,aSecret);

Step 4: Choose Path for spreadsheet

Change line 84 in the 'Form1.cs' source file and replace "YourDirectory" with the preferred location for the Microsoft Excel spreadsheet that will store the queried tweets.

xlWorkBook.SaveAs(@"YourDirectory" + targetAccount + "_Report.xls", Excel.XlFileFormat.xlWorkbookNormal);
xlWorkBook.Close();
xlApp.Quit();

Step 5: Build and run

Build the solution and run the application. Start searching for tweets!