[PYTHON] Web shop electronic work DIY: Make the night owl TV fine and get an early rising life ①

Stay up late TV fine

It seems that IoT is popular in Chimata. I'm a web & app shop, but the hardware is so much fun that I personally decided to do this DIY.

Listed in the following order.

-IoT, started: Summary of how to collect information well in the sea of programming technology --Web shop electronic work DIY: Make the night owl TV fine and get an early rising life ① -Web shop electronic work DIY: fine night TV and get an early rising life 1.5 -Web shop electronic work DIY: Make the night owl TV fine and get an early rising life ②

[Target] ・ Create a mechanism to prevent people from staying up late by watching TV at night.

[motion] ・ If the TV is on after the set time, the power is automatically turned off. ・ After that, check every 5 minutes (or check that the TV is on) and Anyway, if it's on, it's OFF ・ If you really want to watch TV You can watch TV for 15 minutes by putting 500 yen in the designated piggy bank. (If you don't have a 500-yen coin, let's sleep quietly)

** [My Spec] ** ・ You can create web and apps / touch the server as it is ・ I've only done hardware with L Chika ・ Purchased -Arduino Entry Kit (Uno Version)-Physical Computing Lab    - RaspberryPi Model B+ ・ Books related to IoT and electronic work that I briefly read before creating -[The mechanism and utilization of IoT / sensors that can be seen in the picture](http://www.amazon.co.jp/%E7%B5%B5%E3%81%A7%E8%A6%8B%E3%81] % A6% E3% 82% 8F% E3% 81% 8B% E3% 82% 8BIoT-% E3% 82% BB% E3% 83% B3% E3% 82% B5% E3% 81% AE% E4% BB% 95% E7% B5% 84% E3% 81% BF% E3% 81% A8% E6% B4% BB% E7% 94% A8-% E6% A0% AA% E5% BC% 8F% E4% BC% 9A % E7% A4% BE% EF% BC% AE% EF% BC% B4% EF% BC% B4% E3% 83% 87% E3% 83% BC% E3% 82% BF-ebook / dp / B00UX9VJDA) I haven't read it yet, but I want to read it properly. -[Electronic work learned with Raspberry Pi](http://www.amazon.co.jp/%EF%BC%B2%EF%BD%81%EF%BD%93%EF%BD%90%EF%BD% 82% EF% BD% 85% EF% BD% 92% EF% BD% 92% EF% BD% 99% E3% 80% 80% EF% BC% B0% EF% BD% 89% E3% 81% A7% E5% AD% A6% E3% 81% B6% E9% 9B% BB% E5% AD% 90% E5% B7% A5% E4% BD% 9C% E3% 80% 80% E8% B6% 85% E5% B0% 8F% E5% 9E% 8B% E3% 82% B3% E3% 83% B3% E3% 83% 94% E3% 83% A5% E3% 83% BC% E3% 82% BF% E3% 81% A7% E9% 9B% BB% E5% AD% 90% E5% 9B% 9E% E8% B7% AF% E3% 82% 92% E5% 88% B6% E5% BE% A1% E3% 81% 99% E3% 82% 8B-% E3% 83% 96% E3% 83% AB% E3% 83% BC% E3% 83% 90% E3% 83% 83% E3% 82% AF% E3% 82% B9-% E9% 87% 91% E4% B8% B8% E9% 9A% 86% E5% BF% 97-ebook / dp / B00UKG5KRW / ref = sr_1_sc_2) -[Raspberry Pi electronic work learned by example](http://www.amazon.co.jp/%E5%AE%9F%E4%BE%8B%E3%81%A7%E5%AD%A6%E3%81 % B6% EF% BC% B2% EF% BD% 81% EF% BD% 93% EF% BD% 90% EF% BD% 82% EF% BD% 85% EF% BD% 92% EF% BD% 92 % EF% BD% 99% E3% 80% 80% EF% BC% B0% EF% BD% 89% E9% 9B% BB% E5% AD% 90% E5% B7% A5% E4% BD% 9C% E3 % 80% 80% E4% BD% 9C% E3% 82% 8A% E3% 81% AA% E3% 81% 8C% E3% 82% 89% E5% BF% 9C% E7% 94% A8% E5% 8A % 9B% E3% 82% 92% E8% BA% AB% E3% 81% AB% E3% 81% A4% E3% 81% 91% E3% 82% 8B-% E3% 83% 96% E3% 83% AB% E3% 83% BC% E3% 83% 90% E3% 83% 83% E3% 82% AF% E3% 82% B9-% E9% 87% 91% E4% B8% B8% E9% 9A% 86 % E5% BF% 97-ebook-x / dp / B019IJPYVI / ref = sr_1_sc_1) I haven't done it until I try it, but it's a learning experience just to read it. -[Melting design](http://www.amazon.co.jp/%E8%9E%8D%E3%81%91%E3%82%8B%E3%83%87%E3%82%B6%E3 % 82% A4% E3% 83% B3-% E2% 80% 95% E3% 83% 8F% E3% 83% BC% E3% 83% 89% C3% 97% E3% 82% BD% E3% 83% 95% E3% 83% 88% C3% 97% E3% 83% 8D% E3% 83% 83% E3% 83% 88% E6% 99% 82% E4% BB% A3% E3% 81% AE% E6% 96% B0% E3% 81% 9F% E3% 81% AA% E8% A8% AD% E8% A8% 88% E8% AB% 96-% E6% B8% A1% E9% 82% 8A% E6% 81 % B5% E5% A4% AA / dp / 486109383) It's not related to practice, but I think it's a good book to read when thinking about IoT. I recommend it because it is insanely interesting.

Think about the composition (first time)

夜更かしテレビ罰金制度導入ツール.jpg

Yes, it's completely ** feeling **! ・ How to detect TV ON / OFF ...? ・ Since it seemed to be troublesome to input analog information with Raspberry Pi I tried to input from Arduino, but is it suitable for usage? ・ How do you connect to the internet to display the results on your browser? And so on, there are many questions at this stage, but I will try it once for the time being. It is a work log to implement the following 2 parts.

Detect optical sensor with Arduino and get result with Raspberry Pi

As a procedure, first run Arduino and then move to acquisition with Raspberry Pi.

Accumulate prerequisite knowledge

What is a breadboard? Which resistor should I use? At first, there were so many things I didn't understand. [Electronic work learned with Raspberry Pi] mentioned above (http://www.amazon.co.jp/%EF%BC%B2%EF%BD%81%EF%BD%93%EF%BD%90%EF % BD% 82% EF% BD% 85% EF% BD% 92% EF% BD% 92% EF% BD% 99% E3% 80% 80% EF% BC% B0% EF% BD% 89% E3% 81 % A7% E5% AD% A6% E3% 81% B6% E9% 9B% BB% E5% AD% 90% E5% B7% A5% E4% BD% 9C% E3% 80% 80% E8% B6% 85 % E5% B0% 8F% E5% 9E% 8B% E3% 82% B3% E3% 83% B3% E3% 83% 94% E3% 83% A5% E3% 83% BC% E3% 82% BF% E3 % 81% A7% E9% 9B% BB% E5% AD% 90% E5% 9B% 9E% E8% B7% AF% E3% 82% 92% E5% 88% B6% E5% BE% A1% E3% 81 % 99% E3% 82% 8B-% E3% 83% 96% E3% 83% AB% E3% 83% BC% E3% 83% 90% E3% 83% 83% E3% 82% AF% E3% 82% B9-% E9% 87% 91% E4% B8% B8% E9% 9A% 86% E5% BF% 97-ebook / dp / B00UKG5KRW / ref = sr_1_sc_2) up to Chapter 3 (OS installation ~ L Chika) I practiced it.

Take value from optical sensor with Arduino

Connect the breadboard wiring as shown in here. Download Arduino, Set the type and port of the Arduino connected from Tools-> Board. File-> Sketch Example-> 03.Analog-> AnalogInOutSerial Let's see an example of how to open and get Analog information. As a result of deleting the part that seems not to be copied, the code looks like this.

cds_test


const int analogInPin = A0;  
int sensorValue = 0;        

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  //Get
  sensorValue = analogRead(analogInPin);

  //output
  Serial.println(sensorValue);

  //Then loop()Wait until
  delay(600);
}

Confirm by pressing the check button at the top left of the file -> Press the right arrow button to write the command to Arduino. If you set the tool-> serial monitor to the 9600bps written by the startup code setup (), You can see that the value of the optical sensor is output.

Connect your Mac and Raspberry Pi

It's a hassle to bring out the monitor one by one, so Raspberry Pi is set to be able to SSH from MacBook Air. Prepare one Lan cable and connect it to the Raspberry Pi <-Lan-Thunderbolt-> Mac. Go back to your Mac, launch System Preferences-> Sharing, and set the following:

スクリーンショット 2016-03-28 10.00.59.png

Launch the console,

$ grep OFFER /var/log/system.log

You can get the IP address of the connected Raspberry Pi by typing, so make a note of it.

$ ssh pi@***

Then you can log in. When quitting the Raspberry Pi

$ sudo halt

You can close it with. Convenient.

Connect Raspberry Pi and Arduino

The policy is to use a USB cable (the one included in the entry kit I wrote first) for serial communication. First, physically connect the Arduino and Raspberry Pi,

$ dmesg

Check the Arduino serial port with the command. Enter the Raspberry Pi with SSH, create a file in a suitable place, and describe the following.

test.py


# -*- cording: utf-8 -*-
import serial
import time

ser = serial.Serial('/dev/ttyACM0(* Serial port here)', 9600)
time.sleep(2) #Wait a minute until the Arduino set is finished 1
line = ser.readline()
print line
ser.close()

$ python test.py

When executed with, the value of the optical sensor confirmed on the serial monitor with Arduino alone You can confirm that it is displayed properly.

Once summarized

If you have a goal, you can fight ...? It is a feeling. But there is no better way than catching someone who is familiar with it. .. It's good to participate in Mokumoku-kai or LT tournament, Favorite websites, Twitter, Facebook, etc. It is surprisingly stimulating to receive information every day.

Also, I think the problem is how to process and return the data after it is acquired. Design, machine learning, and information gathering per cloud will be useful. For the time being, I would like to proceed as it is and continue to detour while reflecting on it.

Recommended Posts

Web shop electronic work DIY: Make the night owl TV fine and get an early rising life ①
[Personal memo] Get data on the Web and make it a DataFrame