raspberrypi2とMCP3002でアナログ入力 覚え書き 2018/05

結線
MCP3002Pin→GPIOPin
 1 (CS)→24
 2 (CH0)ボリューム2番ピン
 3 (CH1)未使用
 4 (Vss)→6
 5 (Din)→19
 6 (Dout)→21
 7 (CLK)→23
 8 (Vdd)→1
ボリューム1番ピン→4.7kΩ→Vdd(3.3V)
ボリューム3番ピン→GND

adc_mcp3002.py
「AnalogInputs for Raspberry Pi Using the MP3008」pdfを手本にした。
第10回 高校生ものづくりコンテスト全国大会 電子回路組立部門 長野県大会製作技術講習会資料pdf のSPI通信のタイミングチャートと、PICマイコンのCプログラムがとても良い資料になった。

01: !/usr/bin/env python
02: 
03: # Written by Limor "Ladyada" Fried for Adafruit Industries, (c) 2015
04: # This code is released into the public domain
05: 
06: import time
07: import RPi.GPIO as GPIO
08: import subprocess
09: 
10: GPIO.setmode(GPIO.BCM)
11: 
12: def readadc(channel, clockpin, mosipin, misopin, cspin):
13:         GPIO.output(cspin, True)
14:         GPIO.output(clockpin, False)  # start clock low
15:         GPIO.output(cspin, False)     # bring CS low
16: 
17: #       commandout = 0 #adcnum
18: #       commandout |= channel  # start bit + single-ended bit
19:         for i in range(4):
20:                 if (channel & 0x08): #channelの最上位ビットが“1”なら
21:                         GPIO.output(mosipin, True)
22:                 else:
23:                         GPIO.output(mosipin, False)
24:                 channel <<= 1 # 例"1101"左に1つシフトすると "101x"
25:                 GPIO.output(clockpin, True)
26:                 GPIO.output(clockpin, False) #クロックを1つ進める
27:  # read in one empty bit, one null bit and 10 ADC bits
28:         adcout = 0
29:         for i in range(11): #MCP3002はempty bitは無い)
30:                 GPIO.output(clockpin, True)
31:                 GPIO.output(clockpin, False)
32:                 adcout <<= 1
33:                 if (GPIO.input(misopin)):
34:                         adcout |= 0x1 #misopinが"1"ならadcoutを +1
35:         GPIO.output(cspin, True)
36: 
37:         adcout >>= 1       # first bit is 'null' so drop it
38:         return adcout
39: # change these as desired - they're the pins connected from the
40: # SPI port on the ADC to the Cobbler
41: SPICLK = 11 #18
42: SPIMISO = 9 #23
43: SPIMOSI = 10 #24
44: SPICS = 8   #25
45: # set up the SPI interface pins
46: GPIO.setup(SPIMOSI, GPIO.OUT)
47: GPIO.setup(SPIMISO, GPIO.IN)
48: GPIO.setup(SPICLK, GPIO.OUT)
49: GPIO.setup(SPICS, GPIO.OUT)
50: 
51: # 10k trim pot connected to adc #0
52: while True:
53:         # read the analog pin
54:         channel=0xd  #0b"1101" Start="1"、Single入力="1"、チャンネル="0"、,MSBより出力="1" 
55:                        #channel 1  ( MCP3002 3Pin)なら channel=0xf 0b"1111"  
56:          trim_pot = readadc(channel, SPICLK, SPIMOSI, SPIMISO, SPICS)
57:         set_volume = trim_pot / 10.24           # convert 10bit adc0 (0-1024) trim pot read into 0-100 volume level
58:         set_volume = round(set_volume)          # round out decimal value
59:         #set_volume = int(set_volume)            # cast volume as integer
60: 
61:         cmd= "echo "+(str(set_volume))+" > real_2" #set_volume値を文字として ./real に書き込む
62:         subprocess.Popen(cmd,shell=True)
63:         # hang out and do nothing for a half second
64:         time.sleep(0.5)

「AnalogInputs for・・・」にあるプログラムを基にしたので、コメントはそのまま残し、 日本語のコメントは私が追記。
sudo chmod +x ./adc_mcp3002.py で実行権限を、sudo ./adc_mcp3002.py で起動する。

Nginx
ドキュメントルートディレクトリは /var/www/html 
特に設定をしていないのに、自動で起動。
同じWebサーバーのApachiと異なり初期設定もせずに動くが、でもこれで良いのかな? の疑問が…
 
グーグルチャートのゲージでボリュームの動きを表示
グーグルチャートギャラリー のGaugeを加工した。
15行の time.txtは現在時刻、48行の real_2は上記adc_mcp3002.pyの処理値を収めるファイルで、 このhtmlファイルと同じディレクトリに置く。 12〜25行でtime.txtを取り込んで71〜74行で表示しますがコメントアウトしてある。
29〜41行でゲージ目盛りのデザインをする。

01:<html lang="ja">
02:<head>
03:<meta charset="utf-8">
04:<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
05:<script type='text/javascript' src='https://www.google.com/jsapi'></script>
06:<script type='text/javascript'>
07: google.load('visualization', '1', {packages:['gauge']});
08: google.setOnLoadCallback(drawChart);
09: 
10: function drawChart() {
11: 
12:     function reloadData(){
13:         $.ajax({
14:         type:'GET',
15:         url:'./time.txt',
16: 
17:         success:function(result){
18:         $("#data").text(result);
19: 
20:             }
21:         });
22:     }
23: 
24:     setInterval(reloadData,100);
25: }
26: 
27: google.setOnLoadCallback(drawChart1);
28: 
29: function drawChart1() {
30: 
31: 
32:     var data;
33:     var options = {
34:         width: 240, height: 240,
35:         redFrom: 90, redTo: 100,
36:         yellowFrom:80, yellowTo: 90,
37:         max:100,
38:         majorTicks : ['0','','','','','50','','','','','100'],
39: 
40:         minorTicks: 6
41:     };
42:     var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
43: 
44: 
45:     function reloadData(){
46:         $.ajax({
47:         type:'GET',
48:         url:'./real_2',
49:         dataType:'json',
50:         success:function(text){
51:             var response = [['Label','Value'],['trim',text]];
52:             data = google.visualization.arrayToDataTable(response);
53:             chart.draw(data, options);
54:             }
55:         });
56:     }
57: 
58:         reloadData();
59: 
60:     setInterval(reloadData,100);
61: 
62: }
63: 
64:</script>
65:</head>
66:<html>
67:<body>
68: 
69:<p>
70:<b>
71:<!--
72:<div style="display:inline-block; _disply: inline" >---現在時刻</div>
73:<div style="display:inline-block; _disply: inline" id='data'></div>---</p>
74: -->
75:<div id='chart_div'></div>
76: 
77:</b>
78:</body</html>