Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter pill dispenser project code
A caveat - my aim here was just to get this code to work. As I'm pretty new to coding this should not be considered clean or good code, I just basically kept trying stuff till I got it to work. If this code helps other beginners make progress on their own projects, great!
These tutorials helped a ton:
https://itp.nyu.edu/physcomp/labs/labs-serial-communication/lab-serial-communication-with-node-js/
https://itp.nyu.edu/physcomp/labs/labs-serial-communication/lab-serial-control-of-an-arduino/
And I based my pill dispenser on this one, after a simpler version failed - https://www.youtube.com/watch?v=tcoCbNUKJPc&list=WL&index=17&t=0s
-----------------------------------------------------------------------------------------
Here is the Javascript code, it's heavily commented to make things as clear as possible:
-----------------------------------------------------------------------------------------
// serial port initialization:
var SerialPort = require('serialport'); // include the serialport library
var portName = process.argv[2]; // get the port name from the command line
var Twit = require('twit') // include the twit library
var T = new Twit({ // these are unique and need to be obtained from the Twitter developer site
consumer_key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
consumer_secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
access_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
access_token_secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
})
var previousTweet = 0; // this will cause a pill to be dispensed right at the beginning every time the program starts - as a test.
var myPort = new SerialPort(portName, 9600); // open the port
myPort.on('open', openPort); // called when the serial port opens
function openPort() {
console.log('port open');
function checkForNewTweet() {
T.get('statuses/user_timeline', { screen_name: 'realDonaldTrump', count: 1, tweet_mode: 'extended' }, function(err, data, response) {
currentTweet = data[0].full_text; // use the tweet text only
if (currentTweet !== previousTweet) { // if currentTweet is not equal to previousTweet...
console.log('-----> New tweet') // log it to the console
var numberToSend = 1;
myPort.write(numberToSend.toString()); // send a 1 to the Arduino
console.log('-----> Data sent to the Arduino'); // log it to the console
previousTweet = currentTweet; // the previousTweet is now the same as the currentTweet
} else { // if currentTweet is equal to previousTweet...
console.log('No new tweet') // log it to the console and do nothing else
}
})
}
setInterval(checkForNewTweet, 10000); // run the function every 10 seconds - this is to stay under the allowed Twitter API call limit.
}
-----------------------------------------------------------------------------------------
Here is the Arduino code:
-----------------------------------------------------------------------------------------
#include <Stepper.h>
const int stepsPerRevolution = 2058;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
// set the speed at 10 rpm:
myStepper.setSpeed(10);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
char input = Serial.read(); // read first available byte into a variable
if (input == '1') { // if the variable equals 1
Serial.println("one revolution");
myStepper.step(stepsPerRevolution);
delay(500);
}
}
}
-----------------------------------------------------------------------------------------
Other code:
-----------------------------------------------------------------------------------------
I ran the Javascript through Node JS
In a terminal window, you change directory into the folder that has the JS file, then specify your port at the end of the node run command. On my macbook air, my port was usbmodem14201. Node run command:
node twitter-arduino.js /dev/tty.usbmodem14201
As well as installing Node, I also installed the Node SerialPort library, Installed the Twit library, and got personal developer keys from the twitter developer site.
-----------------------------------------------------------------------------------------
Hope this was helpful!
Cheers, Paul
-----------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.