Created
November 12, 2015 13:43
-
-
Save staceymakes/6cd6c169ad10413e4163 to your computer and use it in GitHub Desktop.
Uses twitter api to send a tweet from a button press on an arduino. requires arduino to have basic sketch that Serial.prints when button is pressed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//requires the twitter and serialport node modules | |
// requires you capture a button press from the Arduino - see bottom for basic Arduino sketch | |
//NODE[save as app.js] ------------------------------------------------------------------ | |
var Twitter = require('twitter'); | |
var client = new Twitter({ | |
consumer_key: '', | |
consumer_secret: '', | |
access_token_key: '', | |
access_token_secret: '' | |
}); | |
// include the library | |
var serialport = require('serialport'); | |
// make a local instance of it | |
var SerialPort = serialport.SerialPort; | |
// get port name from the command line: | |
var portName = process.argv[2]; | |
var myPort = new SerialPort(portName, { | |
baudRate: 9600, | |
// look for return and newline at the end of each data packet: | |
parser: serialport.parsers.readline("\r\n") | |
}); | |
myPort.on("open", handleOpen); | |
myPort.on("close", handleClose); | |
myPort.on("data", onData); | |
var isSending = false; | |
function handleOpen() { | |
console.log("the connection is open "); | |
} | |
function handleClose() { | |
console.log("the connection has been closed"); | |
} | |
function onData(data) { | |
console.log("data"+data); | |
// whatever data you have | |
if(data=="ON"){ | |
sendTweet(); | |
} | |
} | |
function sendTweet(){ | |
client.post('statuses/update', {status: 'TWEETING ALL THE TWEETS TO TWITS AND TWEEPS'}, function(error, tweet, response){ | |
if(error) throw error; | |
console.log(tweet); // Tweet body. | |
console.log(response); // Raw response object. | |
}); | |
} | |
//------------------------END OF NODE | |
//ARDUINO SKETCH------------------- | |
// constants won't change. They're used here to | |
// set pin numbers: | |
const int buttonPin = 2; // the number of the pushbutton pin | |
const int ledPin = 13; // the number of the LED pin | |
// variables will change: | |
int buttonState = 0; // variable for reading the pushbutton status | |
boolean hasSet = false; | |
void setup() { | |
// initialize the LED pin as an output: | |
pinMode(ledPin, OUTPUT); | |
// initialize the pushbutton pin as an input: | |
pinMode(buttonPin, INPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
// read the state of the pushbutton value: | |
buttonState = digitalRead(buttonPin); | |
// check if the pushbutton is pressed. | |
// if it is, the buttonState is HIGH: | |
if (buttonState == HIGH) { | |
// turn LED on: | |
digitalWrite(ledPin, HIGH); | |
if(!hasSet){ | |
Serial.println("ON"); | |
hasSet = true; | |
} | |
} else { | |
// turn LED off: | |
hasSet = false; | |
digitalWrite(ledPin, LOW); | |
} | |
} | |
//---------------END OF ARDUINO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment