Вы находитесь на странице: 1из 6

Food

Living

Outside

Play

Technology

Workshop

Use Twitter to control Arduino Uno via Visual Basic 2010


by techbitar on September 24, 2011 Table of Contents Use Twitter to control Arduino Uno via Visual Basic 2010 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Intro: Use Twitter to control Arduino Uno via Visual Basic 2010 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 5 5 6

http://www.instructables.com/id/Use-Twitter-to-control-Arduino-Uno-via-Visual-Basi/

Intro: Use Twitter to control Arduino Uno via Visual Basic 2010
(For some reason, I am unable to re-order some of the slides. As a result, the slide about adding reference to TwitterVB is not in order. ) This Instructable 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"

http://www.instructables.com/id/Use-Twitter-to-control-Arduino-Uno-via-Visual-Basi/

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 -----------------------------------

Image Notes 1. This is your Twitter user ID which will be checked by the VB app for predefined keyword to trigger certain actions for Arduino. In this program, we use keywords to turn on or off Pin 13. 2. This is the keyword that you want the VB to search for. The keyword could be anything that you want it to be. NOTE: The VB app assumes the keyword is the ONLY word in the Twitter status. So if your keyword is TURNMOTOR, the VB app will not recognize a Tweet that says TIME TO TURNMOTOR. Only TURNMOTOR by itself in the Twitter status line. 3. Match this field with the COM port your PC connects to Arduino. 4. This is where you send a command 1 or 0 for Arduino to turn PIN 13 on or off. 5. For this VB app, I hard-coded PIN 13 to send commands to. You can change that in the code or from the text box default value. 6. For this version of the VB program, this button will only search Twitter once for a user/command match.

Image Notes 1. One way to register a Twitter App is to go to "Profile" then click "Developer" link.

http://www.instructables.com/id/Use-Twitter-to-control-Arduino-Uno-via-Visual-Basi/

Image Notes 1. Create a Twitter app so you can use its access info in your VB app to communicate with Twitter

Image Notes 1. If you don't have a website for your Twitter application simply type a valid domain name

Image Notes 1. When you clicked button "Create my access tokens" Twitter generated those tokens which seem like long strings of random numbers and chars. You will use those strings to give your VB app access to your Twitter user account. Image Notes 1. Click on this button to generate the needed Twitter access info to be used by external application such as the VB app in this example to access your Twitter account and tweets.

Image Notes 1. These are the twitter VB parameters which need to be replaced with the numbers generated by Twitter. Sorry for poor quality of image. It must be the scaling that takes place after the uploading.

Image Notes 1. 1) From VB project explorer, right click on project name and select "Add Reference" 2. 2) From VB 2010, browse to directory where you downloaded TwitterVB.dll and select/OK file.

http://www.instructables.com/id/Use-Twitter-to-control-Arduino-Uno-via-Visual-Basi/

Image Notes 1. 1) In VB project explorer, right click on project name to open up project properties. 2. 2) Click the "Compile" tab 3. 3) Click "Advanced Compile Options"

Image Notes 1. 4) Change "Target framework" to .NET Framework 3.5

File Downloads

SerialPort.pde (746 bytes) [NOTE: When saving, if you see .tmp as the file ext, rename it to 'SerialPort.pde']

Form1.Designer.vb (10 KB) [NOTE: When saving, if you see .tmp as the file ext, rename it to 'Form1.Designer.vb']

Form1.vb (2 KB) [NOTE: When saving, if you see .tmp as the file ext, rename it to 'Form1.vb']

Related Instructables

Using Visual Basic 2010 to control Arduino Uno (Photos) by techbitar

Arduino motor drives cheap toy car (Photos) by techbitar

Twitter controlled Arduino Outputs - no PC - LCD Display + Sensor data to Twitter by XenonJohn

AlarmingTweet by willnue

Twitter Mention Mood Light by pdxnat

Tweet-a-Pot: Twitter Enabled Coffee Pot by frenzy

http://www.instructables.com/id/Use-Twitter-to-control-Arduino-Uno-via-Visual-Basi/

Comments
2 comments

Add Comment
Sep 25, 2011. 8:56 AM REPLY

randofo says:
Nice interface. Do you have any plans to do anything fancy with it and the Arduino?

techbitar says:
I do but soon as I get the other half done (sending from Arduino to Twitter)

Sep 25, 2011. 10:28 AM REPLY

http://www.instructables.com/id/Use-Twitter-to-control-Arduino-Uno-via-Visual-Basi/

Вам также может понравиться