Instructables

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

Picture of Twitter controlled Arduino Outputs - no PC - LCD Display + Sensor data to Twitter
Control digital out pins by Twitter messages. Sensor readings sent back to Twitter.
MODIFIED BY JOHN DINGLEY FROM Twitter2LCD originally written by Fabrizio Granelli
See http://www.instructables.com/id/Displaying-Twitter-feed-without-a-PC/

*You can send text messages to the Arduino LCD screen from twitter if there is anyone is around at the other (Arduino) end you want to send text messages to.

*You can control the digital output pins of your arduino by sending it twitter "data" messages from same twitter page.

*Your Arduino can send twitter messages or data back from analog port sensors to the person who is using twitter on the other side of the world.



..............So full 2 way control with feedback!




* Allows twitter messages to display on an LCD display via arduino and ethernet shield. No PC required!
* ALSO ALLOWS CONTROL OF 4 DIGITAL OUTPUT PORTS VIA SIMPLE TWITTER MESSAGES

*This means I can control 4 relays for example just by sending a Twitter message.

* Arduino reads the twitter messages at 1 minute intervals and displays them on an LCD screen.

* They can be normal text messages to someone near to the Arduino OR if they have a D as first letter, they are read as "data" messages and used to control the Digital Output Pins.
* Also, the arduino can take measurements from sensors, for example a thermometer and Tweet the readings back to the remote operator who is sending the data/text tweets to the Arduino. So you have FULL REMOTE 2 WAY CONTROL VIA TWITTER


NOTE: IF YOU JUST WANT TO TURN THE OUTPUT PINS ON AND OFF REMOTELY, AND HAVE SENSOR DATA SENT TO TWITTER, YOU DON'T NECESSARILY HAVE TO HAVE THE LCD SCREEN PRESENT, the sketch will run fine, just don't bother connecting up an LCD module.

 
Remove these adsRemove these ads by Signing Up

Step 1: What it does

Picture of What it does
How to use it:
* Set up your twitter account. Choose a username that has 6 (SIX) characters in it, no more and no less.
* - To send a text twitter message to be displayed on the arduino-LCD display, just send it as normal.
* - To send a DATA message (to turn one of 4 LED's on the arduino digital output ports on or off) send a twitter formatted as follows:
* a) Start with a capital "D" , this tells the arduino this twitter has data in it which it needs to look for
* b) After the "D" , with no spaces enter FOUR digits. These must be 0 or 1.
* c) EXAMPLE: the twitter message D0100 will turn on LED 2, D1000 would turn on LED1, D0010 would turn on LED3, D0001 will turn on LED4
* Therefore you can modify this code to turn relays on and off or control anything you like.

NO PC REQUIRED AT ALL

Just require:
Arduino (I used a Uno) and Arduino 22
Ethernet shield
Ethernet cable to connect shield to your router box.
I use a standard Serial-to-LCD display. This connects to arduino with 4 wires in total so much easier to set up than a pure LCD screen. Arduino sends it text via a serial wire, using a print.Serial("yourtext"); type of command using SoftwareSerial library. LCD is actually optional, it will still work as a 2 way control system without it.

I have had trouble with some of the blue screened serial-to-LCD displays. I would get a completely standard green one from your Arduino supplier then you know it should work. Try to get one with 4 rows of text so everything fits on the screen.


In photo below you will see I tweeted the message D0101fgh to the Arduino. The Arduino sees the "D" at the front an therefore knows it is a data tweet not just a text message. It then looks at the next 4 characters after the D (which will always be 0 or 1), and these trun the 4 Output Pins ON or OFF.

[Note: The few random letters after the 0's and 1's are there because twitter will not send repeat messages that are the same as ones sent previously. A few random letters get around this (or regularly delete your tweets).

When the Arduino is tweeting data from the analog sensors back to the person using twitter on the other side of the world, it does the same thing but then automatically generates a few random harmless characters at the end of the message to achieve the same objective in the other direction.]

Step 2: Setup

Picture of Setup

Photo of Arduino + Ethernet shield + Serial-LCD display.

LED's on the 4 digital output pins we are using are there just to show which ones are ON and which ones are OFF.

So here Digital output Pin7 (in the pin sequence 4,5,6,7) is turned ON according to the "data" tweet it has just received, so 4th LED from the left (connected to Pin7) is indeed turned ON as you can see....it works.


(The 5th LED on the right is just there to flash once per second while one minute delay is running before it looks for the next Tweet message, you can set any time delay you like).

Step 3: Twitter page

Picture of Twitter page

Here is a view of the Twitter page the person remotely controlling the Arduino will be using.

- As you can see the person has sent a "data" message to the Arduino (D0101) to turn on LED's attached to pins 5 and 7.

- They have also received some twitter messages from the Arduino showing data values from a sensor on Analog Pin 1 (value displayed is 1023 at the moment).

To stop twitter rejecting the repeat readings from the analog sensor which are still 1023 - the same as previous readings, my code puts the reading in brackets so it is clear to the person looking at it, then puts some random numbers between 1000 and 9999 at the end to make the total tweet NOT the same as any of the previous ones, so Twitter accepts it as a new message and displays it OK.

Step 4: Setting up

Picture of Setting up

Now for the setup.

You will be pleased to hear that you do not have to do any port-forwarding with your router or anything like that.

However, you do need to set up aTwitter account for your remote control arduino project, which from now on we will call your "machine account".

Also you need to use your normal PC to find out some information about your own router (the box that your phone cable goes into and your PC is also connected to to allow it to "talk" to the internet).

LIBRARIES
In Arduino on the PC that you will use to program your Arduino board with (via the temporarily attached USB cable) there is a folder called Libraries.

Inside this folder there are more folders for different functions. These are the individial libraries containing code for specific specialist functions the Arduino will perform. You need to add some extra folders like these to the main libraries folder.

Here is a list of the library folders you will need to add to your main arduino "Libraries" folder:

SPI.h (this one should already be there)
Ethernet.h (this one should already be there on later versions of arduino)
EthernetDHCP.h
TextFinder.h
SoftwareSerial.h  (this one should already be there)
EthernetDNS.h
Twitter.h


a) TextFinder is available from here:     http://www.arduino.cc/playground/Code/TextFinder
b) EthernetDNS and EthernetDHCP are found here:     http://gkaindl.com/software/arduino-ethernet/dns
- Click to download their file called Arduinoethernet. Extract the files. Open the folder and inside you will find
more folders. Get the ones called EthernetDNS and EthernetDHCP and copy these folders into your main arduino "libraries" folder.
c) Twitter.h library is found here:  http://www.arduino.cc/playground/Code/TwitterLibrary
d) Ethernet.h library is already in the libraries in latest versions of Arduino


RIGHT

Having done this you need to log on to www.twitter.com and set up an account.
NOTE: for my program to work without being modified, you need to choose yourself a 6 (SIX) character long Username (no smaller and no longer !) From now on we will refer to this account as your "machine account."

NEXT: You need to find out what your twitter account "Token" is as you will need to write it down and insert it into the Arduino sketch later.

In the Arduino sketch there is a line like this:
Twitter twitter("275938231-D93Q2mTtMv3vg4quP25qOe1Lp5eER09ZdHsbibOE"); //this one is my jddat2 token where jddat2 is my 6 digit twitter username. You need to get your own twitter token and insert it here instead of mine


HOW TO GET YOUR TWITTER TOKEN?
a) Log onto Twitter (www.twitter.com) and set up an account for these data transfers. We shall call this your machine account
 from now on.
b) Make sure your username has SIX (6) characters in it else the arduino data reader code below will need to be modified.
c) Then, open this web tutorial: http://tronixstuff.wordpress.com/2011/03/08/moving-forward-with-arduino-%e2%80%93-chapter-30-%e2%80%93-twitter/
d) It tells you what to do to get a Twitter TOKEN for your "machine account" that you have just set up.
e) Once you have your token, write it down as you will later enter it between the " and " in line of code in my sketch (see later on)  that starts with Twitter twitter("..............


Page 1 of web tutorial (c) screenshot attached.
Click where it says "this page".................. 

Step 5: Get Twitter token (2)

Picture of get Twitter token (2)
This is what you should now see

Step 6: Get Twitter Token 3

Picture of get Twitter Token 3
This now comes up, click on "Allow"

NOTE: I think you should be logged on to the Twitter account you have just set up while you do this (minimise the Twitter window)

Step 7: Get Twitter Token 4

Picture of get Twitter Token 4
Finally it gives you your token.

Copy and paste it somewhere safe as we will need this later to insert into the Arduino sketch.

This is my test token for this instructable, yours will be different.

Step 8: Get your Twitter RSS feed

Picture of Get your Twitter RSS feed
OK, now you have to get something called your Twitter RSS feed number

On Twitter page below, see the RSS feed icon (you have to click on your user name (top right of Twitter page) select the "switch to Old Twitter" option to see the RSS feed icon like this)
NOTE: This "Old Twitter" format option will end soon so at end of this instructable (pages 18 onwards) I have described an alternative way to get your RSS feed number from the "new" Twitter display page - a bit more involved but you can still do it.

So if you have managed to get the "old Twitter" display option up on screen as in example in photo, right-click on the RSS feed icon bottom right.

Step 9: RSS feed 2

Picture of RSS feed 2
Now you see a window like this

Click on "properties"


Step 10: RSS feed 3

Picture of RSS feed 3
Final step for this:


As you can see in the midst of this is a 9 digit number - write it down as your Twitter RSS feed number

Step 11: Find out about your router

Picture of Find out about your router
Don't worry this is boring but we are nearly there with this information gathering, just go through it methodically:

We now need to find out some last bits of information.
You need to find out some strange things called your ip and your gateway and your subnet addresses:

There are probably easier ways to do this but this is what I did:
- Open this website: http://portforward.com/help/setup_static_ip_address.htm
- Download their software called PF Setup Static IP Address
- Run the program, a window will open displaying properties of your system, in particular your router (example screenshot shown in photo)
DO NOT ALTER ANYTHING , JUST LOOK AT THE VALUES AND WRITE THEM ALL DOWN, they include your router ip address, the gateway address and the subnet mask among others.
- Close program down, you can delete it later. Not needed again




Step 12: CODE: Now add info to Arduino Sketch

If you have got this far well done .

Now, open the Arduino sketch. It is attached as a text file so you will need to paste it into a blank Arduino sketch compile it for your board save it and upload it to your board in normal way.

However first you will need to look at the sketch and insert some of the numbers you have just collected and written down in previous few steps:

1) There is a line of code after
char myStringBuffer[80];
which starts like this:   byte mac[] = { OX90, OXA2     and so on.
This is the MAC address of your ethernet shield. You need to change the numbers in the brackets to the ones written on your shield on the sticky label attached to the shield (newer shields). Apparently it may well work OK even if you don't bother changing it but this is what I did anyway.

2) There is a line of code:  byte server[ ] = {199, 59, 148, 10};          This is the ip address of the Twitter server in California. In the original instructable on which this part is based they had a different number here which I could not get to work. I googled until I found this number which DOES work (April 2011). Not sure if it changes occasionally, this is why I point this out.


3) There is a line of code below this as follows:  Twitter twitter("enormous long string of text and numbers");
You need to insert your twitter TOKEN that you have just carefully obtained and saved somewhere into this code between the " and "

4) In the Void loop of the arduino sketch I have highlighted an area where you have to insert 9 digits into a statement.
client.println(GET http://www.twitter.com etc etc
You must insert the RSS of your twitter account you have just created into this line of code so your 9 digits replace the ones that are there now. You will have obtained your RSS 9 digit number earlier on and written it down safely.


5) FINALLY(!) Scroll down towards the bottom of the sketch and you will find a section that starts with
void transmittweet1 ( ) {  

It now says:
byte ip[ ] = {     This should be Ok as it is
byte gateway[ ] = {  Change these values to the values you wrote down earlier for your Gateway IP address
byte subnet[ ] = { 255, 255 etc       Change these values to the values you wrote down earlier for your Subnet address


HOORAY you have now finished modifying the Arduino sketch. All the above is also commented in the code so you can see where you need to insert the new numbers you have collected in the previous steps.

Compile it and save it and upload it to your Arduino.

Main problem may well be you have a library missing that you have to go back and find, if it doesn't compile right away. If a library is missing it will tell you in the error code.

Step 13: Connections

Picture of Connections

In photo I have the following

Ethernet shield connected on top of Arduino

Arduino connected temporarily to a laptop so I can upload sketch to arduino.

Ethernet cable between Ethernet shield and my router

Power supply to Arduino.

The serial-LCD display is connected by 4 wires:
+5V from arduino to display to power it
A wire from display to GND on arduino
Tx and Rx are Digital output pins 2 and 3

REMEMBER: IF YOU JUST WANT TO TURN THE OUTPUT PINS ON AND OFF REMOTELY, AND HAVE SENSOR DATA SENT TO TWITTER, YOU DON'T NECESSARILY HAVE TO HAVE THE LCD SCREEN PRESENT, the sketch will run fine, just don't bother connecting up a serial-LCD module.

NOTE: Before you embark on this project, it is worth checking your LCD display is connected up and working OK with your arduino. There are examples of serial LCD displays and how to connect them on Arduino website. Go through one of these tutorials with their test code and check it works. Usual problem is that Rx and Tx are crossed over, try them the other way around.
This is the exact serial-LCD module I used in this instructable:
http://www.robot-electronics.co.uk/acatalog/LCD_Displays.html

Finally I have some LED's to show the status of my digital output pins that I am trying to control with messages sent from Twitter.

They are attached to Digital Output Pins 4,5,6,7 (via suitable resistors). If you don't know what resistor to use (but you do know what LED you have bought) you can use any of a number of online calculators such as this one.
http://led.linear1.org/1led.wiz
Just set the Voltage to 5 Volts.

Another LED is on Pin 9 which simply flashes when the Arduino is waiting 1 minute before looking for the next Twitter message.

Step 14: Connections to Router

Picture of Connections to Router
This photo shows the other end of the ethernet cable going from ethernet shield into back of your router box

NOTE: Your main PC does not have to be turned on, only the router has to be working once this has all been set up.


Step 15: Arduino sends data to Twitter

Picture of Arduino sends data to Twitter
Here the Arduino has read the value on Analog pin 2 (currently 1023 on the scale of 0-1023)and is sending it to your twitter page:

Step 16: Arduino data to Twitter

Picture of Arduino data to Twitter

Here is the data appearing on the Twitter page.

You can also see that the person using Twitter has sent a data file (beginning with a D so the sketch "knows" a data twitter is coming) back to the arduino to turn on the second and fourth LED's in the sequence of 4 (Pins 5 and 7).

Step 17: Arduino responds to the data twitter

Picture of Arduino responds to the data twitter
Here you can see the pins coming on as a response to the data twitter that the user has just sent.

Remember, the tweeter has added a few letters to end of the data tweet so it is not the same as previous ones (which Twitter would refuse to send again)

Also, if you send a text tweet to the Arduino, in case a human being is around nearby and you want to send them a message, then that is OK too.
Probably best not to start your text tweet with a capital "D"

Step 18: Deleting tweets

Picture of Deleting tweets
You will end up with a lot of tweets after a while!

This is how you delete a tweet

Step 19: Getting RSS feed (alternative method) (1)

Picture of Getting RSS feed (alternative method) (1)
The option to "Select old Twitter" display format (with RSS feed icon in that page) is going soon.

This will describe how you get your Twitter RSS feed number from the new front page (where there is NOT an RSS feed icon you can just click on).

Step 1:
In your internet browser somewhere there should be an RSS feed icon.
Click on it..........

Step 20: Getting RSS feed (alternative method) (2)

Picture of Getting RSS feed (alternative method) (2)
It now asks you if you want to subscribe to this feed.

Click on "subscribe to this feed" option

Step 21: Getting RSS feed (alternative method) (3)

Picture of Getting RSS feed (alternative method) (3)
It now asks you if you want to subscribe, - you do!

Step 22: Getting RSS feed (alternative method) (4)

Picture of Getting RSS feed (alternative method) (4)
Now you are given a "view feed properties" option bottom right of this pop up box, click on it....

Step 23: Getting RSS feed (alternative method) (5)

Picture of Getting RSS feed (alternative method) (5)
Finally, the properties box comes up and inside is your 9 digit Twitter RSS feed code, write it down somewhere to insert into the Arduino sketch.
krfoerster1 year ago
Thanks been using it for awhile. Works great! kinda a noob on coding. Can you asisst in helping me get the code to update more than 1 sensor. I have a dallas temp sensor working currently. The 4 control pins run things on my aquarium. Want to be able tosee more sensor data on twitter and then be able to react. thanks Kenny
juergen991 year ago
I'm a noob but got this partially working. I decided to skip the lcd portion so that I can get the basic sketch working. It compiled without any problem. I monitor the twitter feed and get the expected "Analog Pin 1 reading = [0] 3365" ( I have pin 1 shorted. I get 1023 for 5V as expected). I can see the led hooked to pin 9 blink as expected. I decided to turn on the serial monitor on the pc to monitor the serial out. I get something like this:

OKOK.
Connection failed
Disconnected
TEXT TWEET COMING
9206
connecting ...
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Vary: Accept-Encoding
Date: Fri, 12 Oct 2012 03:13:02 GMT
Server: Google Frontend

The number after "TEXT TWEET COMING" corresponds to the random number in the twitter feed. When I tweet D1000 or other combinations, none of the leds on pins 4,5,6 or 7 light up.

It is definitely sending the analog value to the twitter feed but I can't seem to control the 4 digital output pins.

Any help would be appreciated.
krfoerster1 year ago
Great UploD Mn. Kinda a noob here. My code keeps hangin up. For for awile and keeps hanging. Any ideas?
abdique1 year ago
I sometimes get this error when trying to read the tweets.

Rate limit exceeded. Clients may not make more than 150 requests per hour

How to handle this problem??
hi, i wanna ask something.. instead of using arduino, is it possible if i use just the atmega8 to build this project? one more thing, i'm a very noob when it comes to programming.. so, could somebody please help me design this project step by step (i know that there is already step by step guide given by the project owner but the truth is i really do not understand especially the codng part).. i would really appreciate if somebody could help me.. thanks in advance..
addie1012 years ago
Hi,
I love tis instructable very much and used it as basis of some projects I did.
Unfortunaly it doesn't compile on the Arduino 1.0 IDE.

What do I need to do to make it work under arduino 1.0

Addie
I just used your project as a templet to control relay for my Xmas tree. I turned off the tweet back and im running it with out a LDC screen and im loving it :D Thanks a lot for putting such a nice ible together
daytreze2 years ago
woow.. thanks for sharing :)

I jut tried this, but I got problem to change twitter to old version eventhougt I clicked my username on the top right..
so How I can look RSS feed??
Is it anyway else?
PoisonMondo2 years ago
plz delete earlier post ill post fixed updated code
PoisonMondo2 years ago
I have ported the code for normal lcd setup instead of Serial display ill post code here:

#include


/*
* MODIFIED BY JOHN DINGLEY FROM Twitter2LCD originally written by Fabrizio Granelli See http://www.instructables.com/id/Displaying-Twitter-feed-without-a-PC/
* ALSO USES TUTORIAL FROM THIS WEBSITE: http://tronixstuff.wordpress.com as the basis of the part of code that gets Arduino to send data back to Twitter

* Allows twitter messages to display on an LCD display via arduino and ethernet shield. No PC required!
* ALSO ALLOWS CONTROL OF 4 DIGITAL OUTPUT PORTS VIA SIMPLE TWITTER MESSAGES
* Also allows Arduino to send data back to the twitter user for example sensor data to complete the very-remote control loop

* Twitter2LCD
* gets xml data from http://twitter.com/statuses/user_timeline/16297873.rss
* reads the most recent tweet from field:
* writes the output to the LCD.
* NOTES (J Dingley):
* Set up your twitter account. Choose a username that has 6 (SIX) characters in it, no more and no less.
* - To send a text twitter message to be displayed on the arduino-LCD display, just send it as normal.
* - To send a DATA message (to turn one of 4 LED's on trhe arduino digital output ports on or off) send a twitter formatted as follows:
* a) Start with a capital "D" , this tells the arduino this twitter has data in it which it needs to look for
* b) After the "D" , with no spaces enter FOUR digits. These must be 0 or 1.
* c) EXAMPLE: the twitter message D0100 will turn on LED 2, D1000 would turn on LED1, D0010 would turn on LED3, D0001 will turn on LED4
* Therefore you can modify this code to turn relays on and off or control anything you like.

*NOTE: If you have sent a lot of tweets in this format, twitter may stop you sending them as it thinks "you have already sent this message"
* Options are to add a few random letters after your data tweet, such as D0100kjjkh, so it is now different to your previous D0100 data tweets
(code only looks at the 4 digits that come after the D) OR in longer term, DELETE your past tweets!!

* Set up for SERIAL LCD display, green 4 line display.
* Compiles with Arduino 22 and runs ok on an Arduino UNO.
*NOTE: the various libraries have to be added as folders to the main libraries folder within your arduino environment


*/


//LIBRARIES you will need to have in your libraries folder within arduino
#if defined(ARDUINO) && ARDUINO > 18 // If using Arduino 0019 or later, include SPI.h
#include
#endif
//#include
#include
#include
#include
#include
#include
#include

//Setup of pins as digital outputs (in this case with LED's attached to them, but intention is to use them to control things with)
#define LED1Pin 16
#define LED2Pin 17
#define LED3Pin 18
#define LED4Pin 19
#define DelayFlashPin 9 //this pin flashes while arduino is waiting 1 minute before looking for next tweet

long randNumber;

int analog1;

char myStringBuffer[80];

//byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };//default ethernet shield MAC
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x20, 0x3B };//my particular ethernet shield MAC (this number is WRITTEN on a label on the Ethernet Shield)
//byte server[] = {128,242,240,20}; // Twitter...I found this did NOT work so googled to find current Twitter server IP
byte server[] = {199,59,148,10}; // Twitter server IP address (checked 29/3/11)


Twitter twitter("340667820-dDubuIMtsTPtL2UwsB6arQrbJgYP2kGQzSXmGpU8"); //this one is my jddat2 twitter "token" as in instructable.
//You need to get your own twitter token and insert it here instead of mine
//XXXXXXXXXXXXXXXXXXXX HOW TO GET YOUR TWITTER TOKEN? XXXXXXXXXXXXXXXXXXXXXXXXXX
//a) Log onto Twitter (www.twitter.com) and set up an account for these data transfers. We shall call this your machine account
// from now on.
//b) Make sure your username has SIX (6) characters in it else the arduino data reader code below will need to be modified.
//c) Then, open this web tutorial: http://tronixstuff.wordpress.com/2011/03/08/moving-forward-with-arduino-%e2%80%93-chapter-30-%e2%80%93-twitter/
//d) It tells you what to do to get a Twitter TOKEN for your "machine account" that you have just set up.
//e) Once you have your token, enter it between the " and " in line of code above that starts with Twitter twitter("..............
//XXXXXXXXXXXXXXXXXXXX END OF HOW TO GET YOUR TWITTER TOKEN XXXXXXXXXXXXXXXXXXXXX

char tweet[140];

Client client(server, 80);

TextFinder finder( client );

//SETUP SOFTWARE SERIAL FOR LCD DISPLAY
#define rxlcdprintPin 2 //sets new software rx as pin2, from LCD serial-LCD display usnit
#define txlcdprintPin 3 //sets new software tx pin as pin 3 to LCD serial-LCD display usnit
SoftwareSerial lcdSerial = SoftwareSerial (rxlcdprintPin, txlcdprintPin); //define lcdSerial as rxlcdprintPin (2) and txlcdprintPin (3)
//END OF SOFTWARE SERIAL LCD DISPLAY SETUP

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup()
{

lcd.begin(20, 4);
Serial.begin(9600);
delay(100);

//FOR LCD DISPLAY
pinMode (rxlcdprintPin, INPUT);
pinMode (txlcdprintPin, OUTPUT);
lcdSerial.begin(9600);// LCD serial out
//end of LCD setup



//For digital output pins (with LEDS on them, pins 4 5 6 7, also 13 which flashes as the one minute counter counts down)
pinMode(LED1Pin,OUTPUT);
pinMode(LED2Pin,OUTPUT);
pinMode(LED3Pin,OUTPUT);
pinMode(LED4Pin,OUTPUT);
pinMode(DelayFlashPin,OUTPUT);



Serial.println("Twitter2LCD");
delay(50);
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

lcd.clear(); //clears screen
delay(50);
lcd.print("Twitter to LCD and"); //lcd text
lcd.setCursor(0, 1); //newline
lcd.print("control 4 outputs"); //lcd text
delay(50);
lcd.setCursor(0, 2);//newline
lcd.print("J.Dingley V1.1");
lcd.setCursor(0, 3);
lcd.print("Ported lcd by sapc"); //lcd text
//lcdSerial.print(13, BYTE); //newline
delay(500);
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

randomSeed(analogRead(0)); //reads analog port 0 which must have nothing connected to it as a seed for the random number generator



EthernetDHCP.begin(mac);
}


void loop()
{
//lcd.clear();
if (client.connect()) {

//XXXXXXXXXXXXXXX CHANGE THE 9 DIGITS IN STATEMENT BELOW TO THOSE OF YOUR OWN TWITTER RSS feed xxxxxxxxxxxxxxxxxxxxxxx
//XXXXXXXXXXXXXXX My instructable tells you how to get your twitter RSS feed number XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
client.println("GET http://www.twitter.com/statuses/user_timeline/340667820.rss HTTP/1.0"); // twitter rss for jddat2
client.println();
}
else {

Serial.println("Connection failed");
delay(200);
//XXXXXXXXXXX Display XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
lcd.clear(); //clears screen
delay(50);
lcd.print("Connection"); //lcd text
lcd.setCursor(0, 1); //newline
lcd.print("FAILED"); //lcd text

delay(500);
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
}
if (client.connected()) {
// get the last tweet by simply parsing the item and title tags
if((finder.find("")&&(finder.getString("","",tweet,140)!=0)))
{
Serial.println(tweet);
delay(100);
//XXXXXXXXXXXX Display XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
lcd.clear(); //clears screen
delay(200);
lcd.print("tweet coming"); //lcd tweet text
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

delay(500);


}
else {
Serial.println("Could not find item field");
delay(100);
//XXXXXXXXXXXXX Display XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
lcd.clear(); //clears screen
delay(50);
lcd.print("Could not find"); //lcd text
lcd.setCursor(0, 1);//newline
lcd.print("item field"); //lcd text
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
}
}
else {
Serial.println("Disconnected");
delay(100);
lcd.clear(); //clears screen
delay(50);
lcd.print("Disconnected"); //lcd text
}
client.stop();
client.flush();


/*
//XXXXXXXXXXXXX LED control debugging your data read (LED/output pin controller) tweet routine XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Serial.println("LED control routine");
Serial.print("Last Tweet:");
Serial.print(tweet);
Serial.print("character at position 8 is:"); //if your username has 6 characters, then position 8 in the tweet should be the letter "D" you used at start of this data-tweet
Serial.println(tweet[8]);
Serial.print("character at position 9 is:"); //if your username has 6 characters, then position 9 in the tweet should be the first of the 4 control digits (0 or 1)after the "D"
Serial.println(tweet[9]);
Serial.print("character at position 10 is:");//if your username has 6 characters, then position 10 in the tweet should be the second of the 4 digits after the "D"
Serial.println(tweet[10]);
Serial.print("character at position 11 is:");//if your username has 6 characters, then position 11 in the tweet should be the third of the 4 digits after the "D"
Serial.println(tweet[11]);
Serial.print("character at position 12 is:");//if your username has 6 characters, then position 12 in the tweet should be the fourth of the 4 digits after the "D"
Serial.println(tweet[12]);
*/

if (tweet[8] == 68){ //i.e. if character at position 8 of your tweet, where first position is position 0 (zero), is 68 (a capital letter "D" in your tweet
//..indicating that the next 4 characters you type will be DATA to the 4 LEDS (i.e. 0's or 1's) then we have a DATA tweet....
//if no "D" as first letter of your tweet (after your automatic username) then it will assume we have a TEXT tweet and just display it but
//not use it to alter LED outputs i.e not alter any control pin outputs on the arduino, it will leave them as they were until next data tweet comes along
Serial.println("DATA TWEET COMING");
lcd.clear(); //clears screen
lcd.print("DATA TWEET COMING"); //lcd tweet text
delay(2000);
lcd.clear(); //clears screen
lcd.print(" DATA TWEET...."); //lcd tweet text
lcd.setCursor(0, 1); //newline
delay(200);
lcd.print(tweet); //lcd tweet text

lcd.print(13, BYTE); //newline




if (tweet[9] == 49){ //i.e. if character at position 8 of your tweet, where first position is position 0 (zero), is 49 (a number 1 in your tweet) then....
Serial.print("LED Pin 4: ON ");
lcd.print("Pin4 ON ");
digitalWrite(LED1Pin, HIGH); // Activate LED1 (i.e, digital output pin 4)

}
else {
Serial.print("LED Pin 4: OFF ");
lcd.print("Pin4 OFF ");
digitalWrite(LED1Pin, LOW); // De-ctivate LED1 (i.e, digital output pin 4)
}

if (tweet[10] == 49){
Serial.print("LED Pin 5: ON ");
lcd.print("Pin5 ON");
digitalWrite(LED2Pin, HIGH); // Activate LED2 (i.e, digital output pin 5)
}
else {Serial.print("LED Pin 5: OFF ");
lcd.print("Pin5 OFF");
digitalWrite(LED2Pin, LOW); // De-activate LED2 (i.e, digital output pin 5)
}

lcd.setCursor(0, 1); //newline

if (tweet[11] == 49){
Serial.print("LED Pin 6: ON ");
lcd.print("Pin6 ON ");
digitalWrite(LED3Pin, HIGH); // Activate LED3 (i.e, digital output pin 6)
}
else {Serial.print("LED Pin 6: OFF ");
lcd.print("Pin6 OFF ");
digitalWrite(LED3Pin, LOW); // De-activate LED3 (i.e, digital output pin 6)
}

if (tweet[12] == 49){
Serial.println("LED Pin 7: ON");
lcd.print("Pin7 ON");
digitalWrite(LED4Pin, HIGH); // Activate LED4 (i.e, digital output pin 7)
}
else {Serial.println("LED Pin 7: OFF");
lcd.print("Pin7 OFF");
digitalWrite(LED4Pin, LOW); // De-activate LED4 (i.e, digital output pin 7)
}
} //end of check for a data tweet





//XXXXXXXXXXXXXXXXXXXX else if text tweet (no data)just send it to LCD screen and leave LEDS as they were since last data text
//REMEMBER, IF YOU START YOUR TEXT TWEETS WITH A LETTER "D" IT SHOULD BE OK AS DATA READER SEES THE D THEN LOOKS FOR "1" VALUES TO TURN ON LEDS WITH
//IF IT DOES NOT SEE A "1" IT DEFAULTS THE LED STATUS TO OFF. SO PROBABLY OK MOST OF THE TIME TO START SOME TEXT TWEETS WITH A "D" IF YOU REALLY MUST!
else {
//XXXXXXXXXXXXXXXXput text tweet onto LCD XXXXXXXXXXXXXXXXXXXXXXXXX
lcd.clear(); //clears screen
Serial.println("TEXT TWEET COMING");
lcd.print("TEXT TWEET COMING"); //lcd tweet text
delay(2000);
lcd.clear(); //clears screen
delay(200);
Serial.print(tweet); //Serial viewer
lcd.print(tweet); //lcd tweet text
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
}

transmittweet1 (); //send a data tweet to twitter (you could write different versions of transmittweet for different parameters, sent at different time intervals)

//60 SECOND DELAY BEFORE LOOKING FOR NEXT TWEET. ALTER THIS TO SUIT YOUR REQUIREMENTS.
for (int j=0; j<60; j++) {
digitalWrite(DelayFlashPin, HIGH); // Activate Pin 9 LED so you know it is counting down 1 min not just crashed!
delay(300); // delay 0.3 second
digitalWrite(DelayFlashPin, LOW); // De-activate Pin 9 LED built into arduino board so it flashes on and off
delay(700); // delay 0.7 second
}
}

void transmittweet1 (){
//XXXXXXXXXXXXXXXXXXXXXXXXXXXX TRANSMIT A TWEET SECTION xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//XXXX THIS IS WHERE THE ARDUINO TAKES A MEASUREMENT FROM A SENSOR FOR EXAMPLE AND TWEETS THE VALUE TO MY TWITTER PAGE XXXXXXXXXX
// THE TWEET HAS A 4 DIGIT RANDOM NUMBER ATTACHED TO THE END OF IT SO THAT IF THE DATA READING SENT HAS SAME VALUE AS AN EARLIER TWEET
//TWITTER WILL STILL ACCEPT IT (TWIITER TENDS TO BLOCK REPEAT IDENTICAL MESSAGES OTHERWISE)


// Ethernet Shield Settings
// Alter ip, gateway and subnet to suit your own network!

byte ip[] = { 192,168,0,177}; // choose your own IP for ethernet shield - this one should work though as it stands
byte gateway[] = { 192,168,0,1 }; // insert your network gateway here
byte subnet[] = { 255, 255, 255, 0 }; // insert your network subnet address here (Instructable shows you how to do this)
//XXXXXXXXXXXXXXXXXX How to find your gateway and subnet ? (leave the ip[] above alone)XXXXXXXXXXXXXXXXX
//Probably easier ways to do this but this is what I did:
//Open this website: http://portforward.com/help/setup_static_ip_address.htm
//Download their software called PF Setup Static IP Address
//Run the program, a window will open displaying properties of your system, in particular your router
//DO NOT ALTER ANYTHING, JUST LOOK AT THE VALUES AND WRITE THEM DOWN, they include your router ip address, the gateway address and the subnet mask
//Close program down, you can delete it later. Not needed again
//XXXXXXXXXXXXXXXXX End of how to find your gateway address and subnet mask XXXXXXXXXXXXXXXXXXXXXXXXXXXX


delay(5000);
Ethernet.begin(mac, ip, gateway, subnet);


delay(1000);
BufferClear (); //clears myStringBuffer NOTE: buffer has 80 characters at present as max length
strcat(myStringBuffer, "Sent to twitter: Analog Pin 1 reading = ["); //adds this to start of myStringBuffer
analog1=(analogRead(1)); //reads analog port 1
char measurement1buffer[7];
itoa (analog1,measurement1buffer,10);//converts measured value (0-1023) to a char string.
strcat(myStringBuffer, measurement1buffer); //adds this measurement1buffer to my string buffer
strcat(myStringBuffer, "] "); //adds this to end of the data value we have just added to the myStringBuffer, the next 4 digits will just be the random ones to be ignored

getrandomnumber();
char endbuffer [8];
itoa (randNumber,endbuffer,10); //converts random number into same number but in a char string with ?? a terminal null added

strcat(myStringBuffer, endbuffer); //adds this randomly generated 4 digit endbuffer to my string buffer so twitter accepts
//and displays new measurements even if they are the same as previous ones!!


sendtweet(myStringBuffer); //send the completed tweet text with the 4 digit random number tacked on the end
//xxxxxxxxxxxxxxxxxxxxxxxxx END OF TRANSMIT A TWEET function XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
}

void sendtweet(char msg[])
{
Serial.println("connecting ...");
if (twitter.post(msg))
{
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);
if (status == 200)
{
Serial.println("OK.");
} else
{
Serial.print("failed : code ");
Serial.println(status);
}
} else
{
Serial.println("connection failed.");
}
}


//string manipulation function for adding each new letter to the existing sentence string
void strcat(char* original, char appended)
{
while (*original++)
;
*original++ = appended;
if (appended)
*original = '\0';
}


void BufferClear (){
//to clear the character string ready for a new sentence when "new" is clicked on the screen
for (int k = 1;k <= 80;k++){
myStringBuffer[k - 1] = 0; // blank buffer content with null characters
}
}


void getrandomnumber() {
// print a random number from 0 to 299
//randNumber = random(300);
//Serial.println(randNumber);

// print a random number from 1000 to 9999
randNumber = random(1, 9999);
Serial.println(randNumber);

delay(50);
}
ellisgl2 years ago
Awesome! I've been waiting for this!
TheGreatS2 years ago
Awesome! Love it!
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!