Use Twitter to control Arduino Uno via Visual Basic 2010

 by techbitar
twitterVBarduino.png
UPDATE Mar 29,2012: The TwitterVB libraries once published on http://twittervb.codeplex.com have been unpublished by their developers.  A while ago, I was hoping to spend time to update this guide for an alternative solution. I have not found the time as my project backlog is constantly increasing and my workload is not decreasing. Some users say it's possible to find TwitterVB floating somewhere in cyberspace. 

This guide is a skeletal VB/Sketch app that shows how a Tweet can control Arduino Uno using Visual Basic 2010 and Arduino Sketch.

No net shield is needed for this. Just a PC with internet connection and Arduino Uno. 

So you can develop apps based on this utility to send a tweet with a special keyword from anywhere in the world, and the PC running this app or your mix of the app will monitor and intercept it then send it to the Arduino Uno to turn a light on, shut down a PC, turn the music on, etc.  

The app presents the user with a form to enter the name of the Twitter user ID to monitor and the keyword/code in the Twitter status line to look for, as well as the the COM port number used to communicate with Arduino, and whether to turn pin 13 on or off. 

Most of this code is mixed from work of others such as DWRoelands (http://twittervb.codeplex.com/) and Jason (http://jdsitecare.com/1041/twitter-api-vb)


These are the main steps I followed to accomplish this integration:

- Create a Twitter account. You will need a Twitter user ID to register a Twitter application: http://www.twitter.com

- Register a Twitter App: https://dev.twitter.com/apps/new (make sure you create it as Read/Write). When done, twitter will provide you with info that you will need to plug into the VB code to allow it to access your Twitter account/status. 

- Download & install Visual Basic 2010 Express: http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-basic-express

- Download the TwitterVB.dll library (TwitterVB-3.0) : http://twittervb.codeplex.com

- Create a new Visual Basic project and change Advanced Compile Option's Target .NET Framework from 4.0 to 3.5. Then add the SerialPort control to Form1

- Connect your PC to Arduino Uno

- Connect your PC to the internet

- Use the VB 2010 and Sketch code I supplied to test integration. You can then change it to suit your requirements.

- Run twitter2VB2arduino and fill form then click Monitor Twitter button. The VB app will search Twitter and if a match is found it 
will send either 1 or 0 (depends on your selection) over serial port to Arduino Uno to turn PIN 13 on or off. 

NOTE:  Efficiency of VB and Sketch code was compromised in pursuit of simplicity. I am sure there are better ways of doing this Instructable so please feel free to share your thoughts. 

--------------------- Arduino Uno Sketch ----------------------------------------

// Mixed by: Hazim Bitar and based on many examples
// NOTE: For some reason if I keep the Serial Monitor open, I get a COM port access denied error on VB app

int valPin = 13; 

void setup() {
  Serial.begin(9600); // COM port speed 
  pinMode(valPin, OUTPUT);
}

void loop(){
  while (Serial.available() == 0); // keep looping if nothing is sent over serial port
  int valAct = Serial.read() - '0'; // if there's data read it 
  Serial.print(valAct);

  if (valAct == 1) { // test for 1
digitalWrite(valPin, HIGH); // turn on LED
}
else if (valAct == 0) // test for command 0
{
digitalWrite(valPin, LOW); // turn off LED
}
else
{
// do nothing
}
Serial.flush(); // clear serial port
}
  
---------------- END OF SKETCH --------------------------------------


--------------------START OF VB 2010 CODE ------------------------
' Mixed by Hazim Bitar / email: techbitar {@} gmail {dot} com / date: 24 Sep 2011
' Based on the good work of DWRoelands http://www.codeplex.com/site/users/view/DWRoelands and to Jason http://jdsitecare.com/1041/twitter-api-vb/

Imports TwitterVB2
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Dim twitter As New TwitterAPI
    Dim PinCommand As String = "z"
    

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        s.Close()
        s.PortName = "com" + txtCOM.Text
        s.BaudRate = 9600
        s.DataBits = 8
        s.Parity = Parity.None
        s.StopBits = StopBits.One
        s.Handshake = Handshake.None
        s.Encoding = System.Text.Encoding.Default

        For Each Tweet As TwitterStatus In twitter.HomeTimeline
            'txtTweets.AppendText(Tweet.User.ScreenName + vbNewLine + Tweet.Text + vbNewLine + Str(Tweet.ID) + vbNewLine + Tweet.CreatedAt + vbNewLine + vbNewLine)
            If Trim(LCase(Tweet.Text)) = Trim(LCase(txtCommand.Text)) And LCase(Tweet.User.ScreenName) = LCase(Trim(txtUser.Text)) Then
                TweetCreated = Tweet.CreatedAt
                If btnOFF.Checked = True Then
                    PinCommand = "0"
                    s.Open()
                    s.Write(PinCommand)
                    s.Close()
                ElseIf btnON.Checked = True Then
                    PinCommand = "1"
                    s.Open()
                    s.Write(PinCommand)
                    s.Close()
                End If
                 ' DEBUG: MsgBox(txtCommand.Text + " | " + txtCOM.Text + " | " + txtPIN.Text + " | " + txtUser.Text + " | " + PinCommand + " | " + TweetCreated)
                Exit For
            End If
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         twitter.AuthenticateWith("Your ConsumerKey", "Your ConsumerSecret", "Your TokenKey", "Your TokenSecret")
        ' the above keys and tokens are generated by Twitter when you register your application

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub


End Class

-------------------------END OF VB CODE -----------------------------------
zaragorn says: Jul 30, 2012. 10:05 PM
How do I connect the arduino through serial?
techbitar (author) in reply to zaragornJul 31, 2012. 4:41 AM
If your Arduino is connected to your PC via USB then you already established a serial connection.
zaragorn in reply to techbitarJul 31, 2012. 8:59 AM
TY i got the wrong port... i found the twittervb here: http://www.4shared.com/zip/b4FnBpyR/twittervb-30.html. It works great!!! I added a OnTime to create a loop every 5 minutes so it's automatically checking for a match
techbitar (author) in reply to zaragornAug 14, 2012. 12:55 PM
Congrats!
hightekrednek2396 says: Dec 8, 2011. 5:36 PM
I'm not able to get the twittervb. The project hasnt been published yet and the one on googlecode is gone. so how did you get it?
techbitar (author) in reply to hightekrednek2396Dec 9, 2011. 2:46 AM
Thanks for the warning. It was available when I wrote this guide. It seems it got unpublished. I have to revisit the guide to take this into consideration. If you Google TwitterVB you will find other sites listing offering twittervb. I will not list those sites here because I have not had the chance to test their twittervb with my code. But will do so in the next few days.
ledone in reply to techbitarJan 8, 2012. 3:39 PM
Good thing I downloaded the DLL before it was removed. Good job with this instruction guide.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!