Evan Rust
Published © GPL3+

Twitter Smart Home

Tweet to your timeline and control anything using a Raspberry Pi, Arduino, and the Twitter API.

IntermediateFull instructions provided2 hours2,632
Twitter Smart Home

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Arduino Nano R3
Arduino Nano R3
×1
Relay (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
Android device
Android device
×1
Webcam
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1

Software apps and online services

Twitter
Twitter
Arduino IDE
Arduino IDE
IDLE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schematic

Code

Arduino Code

C/C++
Copy paste. You can add other functions to turn on other things!
#include "DHT.h"
#include <LiquidCrystal.h>

#define DHTPIN 7

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int pageNum = 0;
const int lights = 6;

void setup() {
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  dht.begin();
  pinMode(lights,OUTPUT);
  digitalWrite(lights,HIGH);
}

void loop() {
  delay(2000);
  switch(pageNum){
    case 0:
    initLCD();
    pageNum = 1;
    break;
    case 1:
    waiting();
    break;
  }
}

float getTemp(){
  float t = dht.readTemperature(true);
  return t;
}

float getHum(){
  float h = dht.readHumidity();
  return h;
}

void initLCD(){
  clearAll();
  lcd.print("Initializing");
  delay(1000);
  clearAll();
  lcd.print("Ready!");
}

void waiting(){
  if(Serial.available()){
  while(Serial.available()>0){
    char data = Serial.read();
    Serial.println(data);
    if(data== "o"){
    Serial.println("Lights on!");
    digitalWrite(lights,LOW);
    lcd.print("Lights on.");
    delay(2000);
    clearAll();
    }
    else if(data == "f"){
      digitalWrite(lights,HIGH);
      lcd.print("Lights off");
    }
  }
  }
  else{
    clearAll();
    float t = getTemp();
    float h = getHum();
    lcd.print("Temp: ");
    lcd.print(t);
    delay(2000);
    lcd.setCursor(0,1);
    lcd.print("Humidity: ");
    lcd.print(h);
    delay(5000);
    clearAll();
  }
}

void clearAll(){
  lcd.clear();
  lcd.setCursor(0,0);
}

Python Code

Python
Copy paste. Add other functions if you wish! Make sure to add your Twitter keys.
from twython import Twython
import serial
import json
import time
from cv2 import *
import serial

app_key = "secret"
app_secret = "secret"
oauth_token = "secret"
oauth_token_secret = "secret"
twitter = Twython(app_key,app_secret,oauth_token,oauth_token_secret)
previous = "null"
ser = serial.Serial("COM6",baudrate=9600)
cam =VideoCapture(0)

def sendImage():
        #Takes the folder where your images are as the input
    image_open = open('recent.jpg', 'rb')
    print "I chose: " + str(image_open)
    image_ids = twitter.upload_media(media=image_open)
    twitter.update_status(status='Checked home: ',media_ids=image_ids['media_id'])
def get_image():
    s, img = cam.read()
    if s:    # frame captured without any errors
        imwrite("recent.jpg",img) #save image
def check_twitter():
    global previous
    json_file = json.dumps(twitter.get_home_timeline()[0])

    data = json.loads(json_file)
    current_message = data["text"]

    if previous != current_message:
        if current_message == "lights on":
            ser.write("o")
            print "Turning on lights."
        elif current_message == "check house":
            print "Checking house"
            get_image()
            sendImage()
        elif current_message == "lights off":
            print "Turning off lights."
            ser.write("f")
    elif previous == current_message:
        print "Already done." 
    previous = current_message
while 1:
    check_twitter()
    time.sleep(60)

Credits

Evan Rust

Evan Rust

120 projects • 1052 followers
IoT, web, and embedded systems enthusiast. Contact me for product reviews or custom project requests.

Comments