忍者ブログ

nyaruratoの製作記録

フィギュアやプラモデルについてもそもそと語っていきます。 最近は電子工作・ゲーム製作に興味アリ

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

ラズベリーパイで温度センサ(ADT7410)を触った

アキバに行ったので秋月電子で温度センサ買ってきました。
I2Cモジュールがラズパイでは使いやすいらしいということで、ADT7410が載った温度センサモジュールを購入。500円。

とりあえずラズベリーパイと温度センサモジュールとをつなぎます。
基板にVDD、SCL、SDA、GNDと小さく書いてあるのでその順につなげました。


次にI2Cを使えるようにしました。
その辺は他のブログが詳しいです。http://nanicananica.blog.fc2.com/blog-entry-21.html

シェルで動作確認ですが、
$ sudo i2cdetect -y 1
でいけました。
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --     
こういう風に出てきます。このモジュールのバスアドレスは0x48なので一致します。

ここから温度のデータを取っていきます。
シェル上では
i2cget 1 -y [I2Cバス] [センサの内部アドレス]  w
でデータが取れるようです。

I2Cバスは0x48でいいのですが、センサの内部アドレスがどうしたらいいかわかりませんでした。
そこでデータシート(http://www.analog.com/static/imported-files/data_sheets/ADT7410.pdf)の13ページあたりを見るとTemperature value most significant byteと書かれたものがあったのでこれを使いました。よってセンサの内部アドレスは0x00です。

$ sudo i2cget -y 1 0x48 0x00 w
こうなります。
打つと0xc80bみたいな値が出てくると思います。

このままだと温度はわからないので分かる値へと変えていきます。
まずエンディアンを逆にしなきゃならないそうです。

こんな感じっぽいです。詳しくはググってください。間違ってたら教えてください。
とりあえず0x0bc8という数字を使います。

下位3ビットを捨てるらしいので8で割ります。
なんで8で割るのかなと思って調べたらこのサイト(http://www.geocities.jp/zaqzaqpa/2sinsuu10.htm)の右シフトの部分を見てなるほど~となりました。

あとはこのサイト(http://atelier-orchard.blogspot.jp/2013/12/raspberry-pi.html)の温度の取得のところを参考に0x0bc8を8で割った後、0.0625をかけるとそれっぽい値が出てきました。



今度はスクリプトから温度を取得してみました。
http://nanicananica.blog.fc2.com/blog-entry-17.html
http://nanicananica.blog.fc2.com/blog-entry-22.html
https://projects.drogon.net/raspberry-pi/wiringpi/i2c-library/
これらのページを参考にC言語で書きました。
ページを見るとわかると思いますが、wiringPiを使ってます。

ハマってしまったのは
gccでコンパイルした時に
test.c:(.text+0x18): undefined reference to `wiringPiI2CSetup'
test.c:(.text+0x48): undefined reference to `wiringPiI2CReadReg16'
collect2: ld returned 1 exit status
といったエラーがでたことです。
これは
gcc test.c -lwiringPi
といったように -lwiringPiをつけてやるのを忘れたために起きていました。

とても汚いですがソースを公開します。


#include <stdio.h>
#include <wiringpii2c.h>
#include <stdlib.h>
#include <string.h>

int main(){
int ID = 0x48,reg = 0x00;//regは内部アドレス
int fd,temp,this_is_temp;//tempは仮の温度入れ、this_is_tempは最終的な温度入れ
char buf[5],temp_buf[5];


fd = wiringPiI2CSetup(ID);
temp = wiringPiI2CReadReg16(fd,reg);
printf("original temp data : %d\n",temp);

//エンディアンが逆に出ているのを直すために16進数の文字列にする
sprintf(buf,"%x",temp);

printf("now : %s \n" , buf);

//エンディアンが逆なのを直すところ
temp_buf[0] = buf[2];
temp_buf[1] = buf[3];
temp_buf[2] = buf[0];
temp_buf[3] = buf[1];
temp_buf[4] = buf[4];
printf("next : %s \n" ,temp_buf);

//文字列をintに
sscanf(temp_buf,"%x",&this_is_temp);

printf("convert temp: %x , %d \n",this_is_temp,this_is_temp);
//温度を出すための計算
this_is_temp = this_is_temp / 8 * 0.0625;
printf("This is temperature : %d\n" , this_is_temp);

//ツイート用
char temp_texts[3],texts[140]="tw テスト 今の部屋の温度は";
sprintf(temp_texts,"%d",this_is_temp);

strcat(texts,temp_texts);
strcat(texts,"度なう");

system(texts);

return 0;
}







出力結果はこんなかんじ
original temp data : 30731
now : 780b 
next : 0b78 
convert temp: b78 , 2936 
This is temperature : 22
tweet "テスト 今の部屋の温度は22度なう"?  (17 chars)
[Y/n]

こんな感じでツイートされます

ちなみにtwitterへの投稿にはtwを使っています。

拍手[0回]

PR

Comment

お名前
タイトル
E-MAIL
URL
コメント
パスワード

カレンダー

03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

リンク

最新CM

[07/16 listener]
[07/15 nyarurato]
[07/12 listener]
[12/19 satotaku]

最新TB

プロフィール

HN:
にゃるらと
性別:
男性
自己紹介:
こんにちは。
プラモやフィギュアなどについて書いていくつもりのブログです。
Twitter
https://twitter.com/sakura_metal

バーコード

ブログ内検索

Copyright © nyaruratoの製作記録 : All rights reserved

TemplateDesign by KARMA7

忍者ブログ [PR]