Jasper
Published

Send Tweets Using Arduino with Artik Cloud

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

BeginnerFull instructions provided1 hour2,794
Send Tweets Using Arduino with Artik Cloud

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
I used Gizduino 328 (local version of Arduino in the Philippines)
×1
Breadboard (generic)
Breadboard (generic)
×1
LED (generic)
LED (generic)
×2
Resistor 10k ohm
Resistor 10k ohm
×2
Light Dependent Resistor (LDR)
×1
Tact Switch
×1
Jumper wires (generic)
Jumper wires (generic)
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE
Python IDLE
Twitter
Twitter
Twitter service
IFTTT Twitter service
ARTIK Cloud for IoT
Samsung ARTIK Cloud for IoT

Story

Read more

Schematics

Circuit

Code

Tweet Trigger

Arduino
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 Tweet

Python
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

Send Tweet

Credits

Jasper

Jasper

1 project • 1 follower

Comments