I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor

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

Things necessary

Preparation

--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.

  1. Consumer key
  2. Consumer secret
  3. Access token
  4. Access token secret

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

Arduino side

--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);
}

Raspberry Pi side

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

Recommended Posts

I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
Using the digital illuminance sensor TSL2561 with Raspberry Pi
I tried using the DS18B20 temperature sensor with Raspberry Pi
Periodically log the value of Omron environment sensor with Raspberry Pi
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Logging the value of Omron environment sensor with Raspberry Pi (USB type)
MQTT RC car with Arduino and Raspberry Pi
I made an npm package to get the ID of the IC card with Raspberry Pi and PaSoRi
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
Try to visualize the room with Raspberry Pi, part 1
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
I tried running Movidius NCS with python of Raspberry Pi3
I tried connecting Raspberry Pi and conect + with Web API
Production of temperature control system with Raspberry Pi and ESP32 (1)
I sent the data of Raspberry Pi to GCP (free)
I made a system with Raspberry Pi that regularly measures the discomfort index of the room and sends a LINE notification if it is a dangerous value
I tried to move ROS (Melodic) with the first Raspberry Pi (Stretch) at the beginning of 2021
I compared the speed of Hash with Topaz, Ruby and Python
I learned how the infrared remote control works with Raspberry Pi
Graph display of household power consumption with 3GPI and Raspberry Pi
I replaced the numerical calculation of Python with Rust and compared the speed
Pet monitoring with Rekognition and Raspberry pi
[I touched the Raspberry Pi (1)] I summarized the basic operations of Minecraft Pi Edition (2015.5.23 pre-release)
Note: I want to do home automation with Home Assistant + Raspberry Pi + sensor # 1
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
Use the Grove sensor on the Raspberry Pi
I vectorized the chord of the song with word2vec and visualized it with t-SNE
Improved motion sensor made with Raspberry Pi
What I did when I couldn't find the feature point with the optical flow of opencv and when I lost it
Use PIR motion sensor with raspberry Pi
I measured the speed of list comprehension, for and while with python2.7.
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
Production of temperature control system with Raspberry Pi and ESP32 (2) Production of transmission device
Simple VPN construction of IPsec gateway with Ubuntu 20.04 and Raspberry Pi ―― 1. StrongSwan introduced
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Connect to the console of Raspberry PI and display local IP and SD information
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE
I compared the moving average of IIR filter type with pandas and scipy
I played RPG Maker 2000 Ruina ~ The Story of the Abandoned City ~ on Raspberry Pi
I tried L-Chika with Raspberry Pi 4 (Python edition)
CSV output of pulse data with Raspberry Pi (CSV output)
Observe the Geminids meteor shower with Raspberry Pi 4
Get CPU information of Raspberry Pi with Python
Raspberry + am2302 Measure temperature and humidity with temperature and humidity sensor
I read and implemented the Variants of UKR
Get temperature and humidity with DHT11 and Raspberry Pi
Measure CPU temperature of Raspberry Pi with Python
I made a Python program for Raspberry Pi that operates Omron's environmental sensor in the mode with data storage
A story about getting the Atom field (XML telegram) of the Japan Meteorological Agency with Raspberry Pi and tweeting it
Edit and debug the code in the Raspberry Pi with VS Code's SSH connection feature
Realize a super IoT house by acquiring sensor data in the house with Raspberry Pi
When I tried to do socket communication with Raspberry Pi, the protocol was different
Simple VPN construction of IPsec gateway with CentOS 8 and openSUSE (Raspberry Pi) ―― 1. StrongSwan introduced
I tried to automate the article update of Livedoor blog with Python and selenium.
I just wanted to extract the data of the desired date and time with Django
Simple VPN construction of IPsec gateway with Ubuntu 20.04 and Raspberry Pi --2 StrongSwan VPN connection confirmation
I tried to compare the processing speed with dplyr of R and pandas of Python
I want to be notified of the connection environment when the Raspberry Pi connects to the network