I will explain how to create a system that acquires the brightness of the room using Arduino and an optical sensor and tweets the information acquired from the Raspberry Pi.
The code is posted on GitHub. https://github.com/Choke222/Arduino_RaspberrPi
--Creating a Twitter application
In order to link Rasbery Pi and Twitter, you need to create a Twitter application. Specifically, the following four pieces of information are acquired.
Reference site: Creating a Twitter application (Consumer key, Consumer secret, Access token, Access token secret confirmation method) % E3% 83% AA% E3% 82% B1% E3% 83% BC% E3% 82% B7% E3% 83% A7% E3% 83% B3% E3% 81% AE% E4% BD% 9C% E6 % 88% 90% EF% BC% 88consumer-key% E3% 80% 81consumer-secret% E3% 80% 81access-token% E3% 80% 81access-token-secret /)
--Install the package on python on Raspberry Pi
sudo apt-get update
sudo apt-get install python-setuptools
sudo easy_install pip #Install pip command
sudo pip install twython #Install twython with pip command
--Circuit creation
Created using a CdS element and a 10kΩ resistor. Reference site: Illuminance sensor circuit
--The illuminance acquired by the CdS element is output to the serial monitor.
arudino_cds.ino
int pin = 0; //Sensor pin number
int get_a0 = 0; //Sensor data acquisition
int flag = 0;
int s=0;
void setup(){
Serial.begin(9600);
}
void loop()
{
get_a0 = analogRead(pin); //Get data from illuminance sensor
s = 0;
Serial.println(s); //Output to serial monitor
if ( get_a0 <= 200 ) {
if(flag == 0){
s = 1000;//OFF!
Serial.println(s); //Output to serial monitor
}
flag=1;
} else if ( get_a0 > 200 ) {
if(flag == 1){
s = 2000;//ON
Serial.println(s); //Output to serial monitor
}
flag=0;
}
delay(200);
}
--Various imports
tweet_cds.py
import serial
import time
import os
from twython import Twython
--Twitter API settings
tweet_cds.py
#print('Enter your twitter credentials')
CONSUMER_KEY = 'xxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxx'
ACCESS_KEY = 'xxxxxxxxxxxxxxxx'
ACCESS_SECRET = 'xxxxxxxxxxxxxxxx'
api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
--Settings for serial communication with Arduino
tweet_cds.py
#print('Start serial communication')
ser = serial.Serial('/dev/ttyACM0', 9600)
--Get illuminance data from Arduino and tweet
tweet_cds.py
while 1:
data = ser.readline()#Read data from Arduino
data2 = int(data)#Convert to int type
print('Room lighting monitoring' + time.ctime() + ',' + 'Lighting situation' + ',' + str(data))
#Tweet the acquired illuminance information
if data2 == 2000:
print('The lights are on!')
api.update_status(status= time.ctime()+'Good morning!')
time.sleep(1)
elif data2 == 1000:
print('The electricity is off!')
api.update_status(status= time.ctime()+'good night')
time.sleep(1)
for your information