Project tutorial
Send Tweets Using Arduino with Artik Cloud

Send Tweets Using Arduino with Artik Cloud

Simple device that sends a tweet when you've arrived at home or you're busy.

  • 1,644 views
  • 0 comments
  • 9 respects

Components and supplies

Apps and online services

About this project

Avoid making your friends worry by telling them when you safely arrive home or when you're busy studying your lessons or doing other stuff by uploading a tweet to your Twitter account automatically.

Send a tweet when you press the button or make the LDR sense light from your lamp shade, this means you're busy. (Set the LDR beside your lamp shade's bulb to make a trigger for when you turn on the lamp.)

Setup

  • Build the circuit (look for Schematics>circuit)
  • Save the Tweet Trigger Code as "TweetButton.ino" then open your Arduino IDE.
  • Save the Send Tweet Code as "SendTweet.py" then open your Python IDLE (please don't run the module).
  • Install the Twitter application (https://apps.twitter.com) - make sure to change permissions to "Access level read, write, and direct messages".

  • Change "Consumer Key (API Key)", "Consumer Secret (API Secret)", "AccessToken", "Access Token Secret" on your Python code to your Twitter token and access values.
  • Setup your IFTTT account then create a Recipe (connect your Twitter & Samsung Artik account).
  • Connect the Arduino UNO to your computer and set your Board and Serial Port under Tools menu on Arduino IDE.
  • On your Arduino IDE, Click Verify then Upload.
  • On Python Idle, Run Module / Press F5. This will wait for the serial write / response from your board.
  • Press the tact switch or give light to your LDR to make a trigger.

Actions

If the switch is pressed:

  • '1' will be sent to python via serial to Tweet "Hi friends I'm home! (Sent by Python script) #home... "

If the LDR senses light:

  • '2' will be sent to python via serial to Tweet "I'm busy right now... "

Result

  • Green light will turn on for 3 seconds.
  • Tweet will be posted to Twitter with #home or #busy.
  • IFTTT will trigger the recipe if the new tweet is posted.
  • Artik device will receive the action "setOn".

Output

Demo

Code

Tweet TriggerArduino
Accepts input from the button and Light Dependent Resistor to send serial '1' or '2' to python code.
//variable that will not change
const int  buttonPin = 4;    // the pin that the button is attached to
const int  ldrPin = 3;        // the pin that the Light Dependent Resistor (LDR) is attached to
const int ledPinRed = 13;       // the pin that the LED (Red) is attached to
const int ledPinGreen =9;       // the pin that the LED (Green) is attached to

// Variables that will change
int buttonState = 0;         // current state of the button
int ldrState = 0;         // current state of the LDR
int lastButtonState = 0;     // previous state of the button
int lastLdrState = 0;     // previous state of the LDR

//assign pinModes in your arduino for INPUTS AND OUTPUTS
void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ldrPin, INPUT);
  pinMode(ledPinRed, OUTPUT);
  pinMode(ledPinGreen, OUTPUT);
  Serial.begin(9600); // for sending data to python, '9600' is default
}


void loop() {
  buttonState = digitalRead(buttonPin); // get value of the button
  ldrState = digitalRead(ldrPin);   // get value of the LDR
  
  digitalWrite(ledPinRed,HIGH); //always set RED LED voltage to HIGH / on

// Condition for Button
 if (buttonState != lastButtonState) {
    if (buttonState == HIGH) { // check if button is on
      digitalWrite(ledPinGreen,HIGH); // set Green LED on
      digitalWrite(ledPinRed,LOW); // set Red LED off
      Serial.write("1"); // Send '1' to python to send 'Im home... tweet'
      delay(3000); // make green LED on & red LED off for 3 seconds
    }else {
      digitalWrite(ledPinRed,HIGH); // set Red LED on
      digitalWrite(ledPinGreen,LOW); // set Green LED off
    }
    digitalWrite(ledPinGreen,LOW); // set Green LED off
    delay(50); // delay to avoid bouncing / sending redundant data
  }
  lastButtonState = buttonState; // copy current button state for checking
  
// Condition for LDR
  if (ldrState != lastLdrState) {
    if (ldrState == HIGH) { // check if LDR is on
      digitalWrite(ledPinGreen,HIGH); // set Green LED on
      digitalWrite(ledPinRed,LOW); // set Red LED off
      Serial.write("2"); // Send '2' to python to send 'Im busy... tweet'
      delay(3000);  // make green LED on & red LED off for 3 seconds
    }else {
      digitalWrite(ledPinRed,HIGH); // set Red LED on
      digitalWrite(ledPinGreen,LOW); // set Green LED off
    }
    digitalWrite(ledPinGreen,LOW); // set Green LED off
    delay(50); // delay to avoid bouncing / sending redundant data
  }
  lastLdrState = ldrState; // copy current button state for checking
}
Send TweetPython
Receive the serial write (1 or 2) from Arduino to update tweet status to Twitter.

Note:
- Change "COM5" to your serial port
- Change Consumer Key (API Key), Consumer Secret (API Secret), Access Token, Access Token Secret based on your created Twitter Application. (https://apps.twitter.com/)
#libraries
import tweepy 
import time 
import serial

ser = serial.Serial("COM5", 9600)  #change 'COM5' to your serial port used in your arduino IDE
while(1):
    sequence = ser.read() #read serial from Arduino
    if(sequence > 0):
        auth = tweepy.OAuthHandler("Consumer Key (API Key)", "Consumer Secret (API Secret)") #change this to your twitter keys
        auth.set_access_token("Access Token", "Access Token Secret")    #change this to your twitter access tokens
        api = tweepy.API(auth)
        
        if(sequence=='1'):
            api.update_status(status="Hi friends I'm home! (Sent by Python script) #home "
                          +time.strftime("%d/%m/%Y") + " " + time.strftime("%H:%M:%S"))
        else:
            api.update_status(status="I'm busy right now. (Sent by Python script) #busy "
                          +time.strftime("%d/%m/%Y") + " " + time.strftime("%H:%M:%S"))
        print "Status tweeted!"
        print "Sequence: " + sequence;
        print (time.strftime("%d/%m/%Y"))
        ## 12 hour format ##
        print (time.strftime("%I:%M:%S"))
        ## 24 hour format ##
        print (time.strftime("%H:%M:%S"))
ser.close #close serial

Schematics

Circuit
Breadboard bb

Comments

Similar projects you might like

BLE Sharp Dust Sensor - ARTIK IoT Cloud

Project tutorial by Elijah Scheele

  • 4,653 views
  • 1 comment
  • 21 respects

AirLock with Artik Cloud

Project tutorial by Akash Ravichandran and Prabhakaran Senthilnathan

  • 2,904 views
  • 0 comments
  • 9 respects

BLE Motion Detector - ARTIK Cloud

Project tutorial by Elijah Scheele

  • 2,190 views
  • 0 comments
  • 4 respects

Water Quality Monitoring Using MKR1000 and ARTIK Cloud

Project tutorial by Team Animo!

  • 7,076 views
  • 12 comments
  • 30 respects

Monitor Fire and Temperature Using ARTIK Cloud

Project tutorial by Samsung IoT

  • 2,657 views
  • 0 comments
  • 21 respects
Add projectSign up / Login