|Scrolling text from Twitter on Arduino MegaEthernet Shield|
When I started writing the code I only had two LED Matrix Links to play with so I used the Arduino Leonardo. As I added more code for Ethernet and Twitter functions, I quickly ran out of SRAM so I needed to find a microcontroller with more SRAM in my stash. I had two options, the Arduino Mega 2560 or the Arduino Due Beta. I quickly ruled out the Arduino Due for now since it is 3.3v even though the Ethernet Shield will work as the MAX7219 needs 5v. (It turns out I could use the AS1107 which only needs 2.7v but I would have to swap out the 33k resistor for a lower value to maintain brightness.) So the Arduino Mega 2560 it is!
|Scrolling text from Twitter on Arduino Due BetaEthernet Shield|
Mixing code from this project (https://www.mobilewill.us/2012/10/new-project-sneak-peek.html) and this forum post I was able to get a tweet based on a hashtag search to show up on the display. The code uses a unauthenticated API so this limits how many times you can query Twitter per hour. Generally you don’t want to do more once or twice per minute. My first thought was I am sure sometimes I am going to get more than one tweet per minute. Hmmm…. After some research and playing with the Twitter API I added a loop to read all of the tweets up to a certain time. So say you want the last hour of tweets it will keep reading tweets from the Ethernet stream until the time-stamp is an hour prior to the first received tweet. This helps limit the memory used and allows you to cycle those tweets on the display.
currTime = finder.getValue(':');
if (currTime < firstTime - tweetHistoryTime) break;
The next part was how to manage the Tweet queue. The great thing about the Arduino world is there are a lot of great libraries. I found the QueueList library. Exactly what the doctor called for! As each tweet comes in it “pushes” it into the queue then when needed I can “pop” it out.
tweetMsg.push(tweet); //Need to figure out a way to manage memory when there is a lot of Tweets.
Great, so now I just need to update the display for each Tweet. Using the millis() function I can delay how often I update the display based on how many tweets are in the queue. My first trial, I divided the number of tweets from the last connection by the history time. This gives each tweet a even amount of time. The problem is if there are too many tweets then each tweet will not be visible long enough to read. The solution I used was to set a minimum display time. So if the calculated display time is less than 5 seconds, then set it to 5 seconds.
displayCycleTime = delayTwitter / tweetMsg.count(); //Calculation to spread display of tweets evenly over the time between connections
if (displayCycleTime < minDisplayTime) displayCycleTime = minDisplayTime; //If there are too many tweets for messages to be display set display time to min display time.
I think you can see what will happen when there are too many tweets. Firstly, we will never catch up and second, we will run out of SRAM pretty quickly, which will either freezes or reset the micro. Neither are reliable. The solution at first thought is to start dropping tweets when the SRAM starts to get low using the freeRam() function (using the last example, function doesn’t work on Arduino Due, I haven’t found an alternative) . The problem I found with this solution in using QueueLists is the SRAM never gets freed up even if we empty the queue. For now I am leaving it like this since I want to try it with all 8 LED Matrix Links.
In the last prototyping batch I ordered enough PCBs to build all 8 LED Matrix Links which also allowed me to test the AS1107. Which by the way, works great and has a wider voltage range than the MAX7219. Once I got them built and connected I noticed the Arduino Mega2560 isn’t fast enough to run all 8 with scrolling text. At this point I really want to try the Arduino Due to see what it can do! This makes a good project to take advantage of those extra MHz.
After porting the code I was able to get it up and running. I figured I can trying running the MAX7219 with less voltage to see what happens. On my first test it worked well but just with dimmer LEDs which is fine for testing. In the meantime I ordered a bi-directional level shifter from Adafruit. The odd thing is the code doesn’t run exactly the same. It runs fine for the first message but after that it doesn’t display correctly. The few letters it does display, there are large gaps between them. Really, an odd problem. My guess, somehow the array with the current display message is getting corrupt once it gets updated with a new message.
So after all this I am trying to decide the best course of action. One thing I thought of while reading an older Adafruit blog article in Google Reader was to use the Raspberry Pi. I don’t know why I didn’t think of that before. If I use C++ and I can probably port most of my code over and then bit bang the MAX7219AS1107’s. I still might have the memory problem, but I only need it to last a few hours during the reception. And who knows maybe I won’t get enough tweets to have this problem at all. Right now I am using the hashtag #Jack since it seems to get a lot of tweets for testing.
Finally I need to come up with the best way to get Internet to the ArduinoRaspberry Pi. I am thinking of using my phone as a Wifi hot spot with a Wifly radio on the Arduino, but it is dead, instead I could use the Raspberry Pi with Wifi. The other option is my netbook which has a 3G radio and I could bridge that connection over the Ethernet port to the ArduinoRaspberry Pi.
Decisions, decisions, decisions…..
The full code is on my github: https://github.com/FriedCircuits/LED_Twitter_Due
If you have any ideas that could help please comment below. I have just a few weeks to get this all sorted out.
Here is a video comparison of the two:
Update: Solved! Well at least the display problem. I should be okay on the memory usage for the Wedding, I don’t think I will have to many tweets. It was this line:
char msg = "No Msg...";
char msg = "No Msg...";
So really it has been a problem all along and explains all the display problems I had with extra LEDs lit up that shouldn’t be sometimes.
The problem now that didn’t occur to me earlier is that not everyone uses Twitter so now I need to look at other options such as SMS like using the Raspberry Pi to receive texts from Google Voice. I did find Python code to talk to Google Voice. The key is to keep it simple during the Wedding and easy for people to send messages to.
Update2: I realized that it can run at 5v with the Arduino Due since all the signals are outgoing and nothing is coming back to the Arduino. But this is slightly out of spec for the MAX7219. Right now I am running mixed ICs with the AS1107.
Update3: I found a solution for guests who don’t have Twitter. I had the idea of making a SMS to Twitter gateway. Before I wrote my own I decided to give Google a search. This is what I found: http://blog.accraplusplus.net/2011/09/sms-tweetbox-simple-sms-to-twitter.html. Now I can use my phone to receive texts and forward them to Twitter. No messing with a Raspberry Pi or writing code to a SMS gateway service. Just load the app and I am good to go! Already tested and it works great. The app can look for a keyword and add a hashtag to the message. You can also have a whitelist of phone numbers if needed. So I don’t have to publish my phone I am going to setup a Google Voice phone to forward texts to my spare phone that will be running the app. Its a Samsung Galaxy S (AT&T Captivate), perfect for the job.
Last thing to do is filter the hashtag from displaying on the LED Matrix Display. – Done!
If you want to get your own LED Matrix Links: https://tindie.com/products/FriedCircuits/led-matrix-link/
Also you can support the LED Matrix Master: https://tindie.com/products/FriedCircuits/led-matrix-master/. This is an Atmega328p Arduino compatible microcontroller in the same form factor as the LED Matrix Link.
Here is part two where I ported over to using the Sparkfun WiFly shield instead of Ethernet. https://www.mobilewill.us/2013/04/led-matrix-link-wedding-twitter-and.html