エラーの人のブログ

エラーの解決方法、いろいろな導入方法など自分用のまとめ※大体自分用なので適当

Xbeeの子に親のPIN ID,DH,DLを自動設定するコード

子のPAN ID:0 DH:0 DL:0
の状態から親の値を取得し設定するコード
すべて0の状態でも親とは通信できるため、親の情報がどこまで
必要になるのかは分からないがとりあえず

値を設定後ATWRをすることで設定を書き込むことができる

Raspberry Piのgccにバグがあるらしく、多少問題あるかも

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

#include <wiringPi.h>
#include <wiringSerial.h>

#define WAIT_TIME 100

int check_at(int fd, char check){
	char ret_c;
	delay(WAIT_TIME);
	ret_c = serialGetchar(fd);
 	fflush(stdout);
 	if(ret_c != check){
 		puts("error");
 		exit(0);	
 	} 
	return 0;
}

char* wait_at(int fd, char check){
	char ret_c = '\0';
	char *sub;
	int i = 0;

	sub = malloc(sizeof(char) * 16);
	delay(WAIT_TIME);
	while(ret_c != check){
		ret_c = serialGetchar(fd);
	 	sub[i] = ret_c;
	 	fflush(stdout);
	 	i++;
	 }
	sub[i] = '\0';
	return sub;
}

char *get_at(int fd, int count){
	int i;
	char *sub;
	char ret_c = '\0';

	sub = malloc(sizeof(char) * count);
	for(i = 0 ; i < count ; i++){
		delay(WAIT_TIME);
		ret_c =  serialGetchar(fd);
	 	sub[i] = ret_c;
	}
	sub[i] = '\0';
	return sub;
}


int init_xbee(int fd){

	char start[] = {0x2b, 0x2b, 0x2b};			//+++
	char atnd[] = {0x61, 0x74, 0x6e, 0x64, 0x0d};
	//ATID 1234
	char para3[] = {0x61, 0x74, 0x69, 0x64, 0x31, 0x32, 0x33, 0x34, 0x0d};
	static char *atd;//[14];
	char atop[] = {0x61, 0x74, 0x6f, 0x70, 0x0d};
	char atwr[] = {0x41, 0x54, 0x57, 0x52};
	char atcn[] = {0x41, 0x54, 0x43, 0x4e};
	char *pan_id, *dh, *dl, *get_atid;
	int size;

	get_atid = malloc(sizeof(char) * 10);
	atd = malloc(sizeof(char) * 20); //ATDH and ATDL
	puts("init start");

	//AT通信開始
	serialPuts(fd, start);
	if(check_at(fd, 'O') == -1) return -1;
	if(check_at(fd, 'K') == -1) return -1;
 	while(serialDataAvail(fd))serialGetchar(fd);

 	//親のPAN ID取得
	serialPuts(fd, atop);
	pan_id = wait_at(fd, 0x0d);

	if(strcmp(pan_id, "0") == 0){puts("error");exit(0);}

	//PAN ID 設定
	strcat(get_atid, "ATID");
	strcat(get_atid, pan_id);
	size  = sizeof("ATID") + sizeof(pan_id);
	get_atid[size] = 0x0d;
	get_atid[size+1] = '\0';
 	serialPuts(fd, get_atid);
	if(check_at(fd, 'O') == -1) return -1;
	if(check_at(fd, 'K') == -1) return -1;
 	while(serialDataAvail(fd))serialGetchar(fd);

 	//親のアドレス取得
 	serialPuts(fd, atnd);
 	wait_at(fd, 0x0d);
 	dh = get_at(fd, 8);
	wait_at(fd, 0x0d);
  	dl = get_at(fd, 8);
 	while(serialDataAvail(fd))serialGetchar(fd);

 	//親のDHを設定
 	strcat(atd, "ATDH");
 	strcat(atd, dh);
 	atd[12] = 0x0d;
 	atd[13] = 0x00;
 	serialPuts(fd, atd);
 	if(check_at(fd, 'O') == -1) return -1;
	if(check_at(fd, 'K') == -1) return -1;
 	while(serialDataAvail(fd))serialGetchar(fd);

 	//親のDLを設定
 	strcpy(atd, "ATDL");
 	strcat(atd, dl);
	atd[12] = 0x0d;
	atd[13] = 0x00;
 	serialPuts(fd, atd);
 	if(check_at(fd, 'O') == -1) return -1;
	if(check_at(fd, 'K') == -1) return -1;
 	while(serialDataAvail(fd))serialGetchar(fd);

 	
	serialPuts(fd, atwr);
	if(check_at(fd, 'O') == -1) return -1;
	if(check_at(fd, 'K') == -1) return -1;
 	while(serialDataAvail(fd))serialGetchar(fd);

 	printf("set\nPAN ID : %s\nDH : %s\nDL : %s \n", pan_id, dh, dl);
 	free(get_atid);
 	free(atd);
	return 0;

}

int main(){

	/* シリアルポートオープン */
	int fd = serialOpen("/dev/ttyAMA0",9600);
	if(fd<0){
		printf("can not open serialport");
	}
	delay(1000);

	init_xbee(fd);
	exit(0);
	while(1){
		/* 受信処理 */
		while(serialDataAvail(fd)){
		 	printf("recive : %c\n" , serialGetchar(fd) );
		 	fflush(stdout);
		 }

		// /* 送信処理 */
		// serialPuts(fd,param);
		delay(1000);
	}

return;
}

XbeeのAT通信

PiRT-Unitを使ってXbee通信をいろいろ試してるが、たまに設定が分からなくなり
通信できなくなると困るのでc⌒ っ゚д゚)っφ メモメモ...

AT通信

  • 親(Corrdinator)

Modem : XBP24BZ7
Function Set : ZIGBEE COORDINATOR AT
Version : 2070

DH : 0
DL : FFFF
//ブロードキャスト

  • 子(Router)

Modem : XBP24BZ7
Function Set : ZIGBEE ROUTER AT
Version : 2270

DH : 0
DH : 0
//自動で親に送る

SM : 0
//寝ないように

  • プログラム(wiringPi使用)
#include <stdio.h>
#include <string.h>

#include <wiringPi.h>
#include <wiringSerial.h>

int main(){

	/* シリアルポートオープン */
	int fd = serialOpen("/dev/ttyAMA0",9600);
	if(fd<0){
		printf("can not open serialport");
	}

	while(1){
		/* 受信処理 */
		while(serialDataAvail(fd)){
		 	printf("recive : %c\n" , serialGetchar(fd) );
		 	fflush(stdout);
		 }

		/* 送信処理 */
		serialPuts(fd,"hello world\n");
		delay(1000);
	}

return;
}

PiRT-Unitを用いたSPI入力データ取得プログラム

PiRT-UnitでSPIをPythonで利用するコードはあったものの
C言語で利用できるコードがなかったため作成
※wiringPiSPIもあるけど、使い方がいまいちわからなかったので・・・

HWの仕様等々を知りたい場合は
http://openrtm.org/openrtm/ja/content/pirt-unit-programming
を参考にしてください。

4つあるADCから入力を取得する方法は
https://github.com/doceme/py-spidev
pythonをwrapしてる上のサイトを参考にしました。

おそらく送信するbufに取得する場所の値を入れておく必要があると
思うのですが、なぜ送信すると取得できるのか謎です・・・
そしてval2の変換のやり方も真似しただけなのでよくわかっていません

とりあえずこれで各ADCの電圧等々が取得できるようになりました。


※コードは雑です

#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <linux/types.h>
#include <linux/spi/spidev.h>

static const char *device = "/dev/spidev0.0";
static uint8_t mode;
static uint8_t bits = 8;
static uint32_t speed = 200000;
static uint16_t delay;

int transfer(int fd, int channel)
{
	int ret;
	int val[4];
	int val2;

	uint8_t tx[] = {
		0x00, 0x00, 0x00, 0x00,
	};

	if(channel == 0){
		*tx = 0x00;
	} else if(channel == 1){
		*tx = 0x08;
	} else if(channel == 2){
		*tx = 0x10;
	} else {
		*tx = 0x18;
	}

	uint8_t rx[ARRAY_SIZE(tx)] = {0, };
	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)tx,
		.rx_buf = (unsigned long)rx,
		.len = ARRAY_SIZE(tx),
		.delay_usecs = delay,
		.speed_hz = speed,
		.bits_per_word = bits,
	};

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1)
		pabort("can't send spi message");

	for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
		val[ret] = (int)rx[ret]; 
	}
//	printf("%d %d %d %d \n",val[0], val[1], val[2], val[3]);
	 val2 = ((val[2] << 6 ) & 0x300) |  ((val[2] << 6) & 0xc0) | ((val[3] >> 2) & 0x3f);
	return val2;
}


int main(int argc, char *argv[])
{
	int ret = 0;
	int fd;
	int val;

//	parse_opts(argc, argv);

	fd = open(device, O_RDWR);
	if (fd < 0)
		pabort("can't open device");

	/*
	 * spi mode
	 */
	ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
	if (ret == -1)
		pabort("can't set spi mode");

	ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
	if (ret == -1)
		pabort("can't get spi mode");

	/*
	 * bits per word
	 */
	ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
	if (ret == -1)
		pabort("can't set bits per word");

	ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
	if (ret == -1)
		pabort("can't get bits per word");

	/*
	 * max speed hz
	 */
	ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
	if (ret == -1)
		pabort("can't set max speed hz");

	ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
	if (ret == -1)
		pabort("can't get max speed hz");

	printf("spi mode: %d\n", mode);
	printf("bits per word: %d\n", bits);
	printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
	printf("fd: %d\n", fd);
	while(1){
		val = transfer(fd, 0);
		printf("%1.5f(%4x) ", ((float)val * 5 / 1024),val);
		val = transfer(fd, 1);
		printf("%1.5f(%4x) ", ((float)val * 5 / 1024),val);
		val = transfer(fd, 2);
		printf("%1.5f(%4x) ", ((float)val * 5 / 1024),val);
		val = transfer(fd, 3);
		printf("%1.5f(%4x) ", ((float)val * 5 / 1024),val);
		puts("");
		sleep(1);
	}
	close(fd);

	return ret;
}

Raspberry Piのパーティション拡張

Raspberry Piにイメージを書き込んだ際にデフォルトだと2Gしか利用できません。
そして最新のだと書き込んだ時点で93%ほど利用しているので、パーティションを拡張
しないと使いづらいと思います。

$ sudo fdisk /dev/mmcblk0

Command (m for help): p
Device Boot Start End Blocks Id System
/dev/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA)
/dev/mmcblk0p2 122880 XXXXXXX XXXXXXX 83 Linux

Command (m for help): d #d(delete)
Partition number (1-4): 2 #2を削除
Partition 2 is deleted

Command (m for help): n #n(new?)
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (1-4, default 2): 2
First sector (2048-xxxxxxx, default 2048): 122880
Last sector, +sectors or +size{K,M,G} (8192-xxxxxxx, default xxxxxxx): hoge
Using default value 10485760

Command (m for help): p
Device Boot Start End Blocks Id System
/dev/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA)
/dev/mmcblk0p2 122880 21094399 10485760 83 Linux

Command (m for help): w #w(write)

$ sudo reboot

hogeの部分に拡張したいサイズを記載してください。

First sectorの時点でEnterを押すとデフォのスタートサイズを利用してしまうので、これでそのまま書き込んでしまうと次の起動ができなくなってしまうので注意すべき

その後パーティションを再構築

$ sudo resize2fs /dev/mmcblk0p2

これで拡張されてるハズ

Raspberry PiでのSPI デバイス利用方法

SPIデバイスを使うためにあwiringPiのライブラリが必要となります

cd /tmp
wget http://project-downloads.drogon.net/files/wiringPi.tgz

tar xfz wiringPi.tgz
cd wiringPi/wiringPi
make
sudo make install
cd ../gpio
make
sudo make install

でインストールができます。
インストール先は

$ ls /usr/local/lib/
libwiringPiDev.so
libwiringPiDev.so.2.0
libwiringPi.so
libwiringPi.so.2.0

$ls /usr/local/include/
wiringPi.h
wiringPiI2C.h
wiringPiSPI.h
wiringSerial.h
wiringShift.h

そしてコンパイルするためには

sudo gcc hoge.c -o hoge -I/usr/local/include -L/usr/local/lib -lwiringPi

でコンパイルする必要がでてきます

参考サイト
http://mas-home.hatenablog.com/entry/2013/01/06/204616

Raspberry Pi のJAPキーボード適応

Raspberry Piは初期設定はUSキーなので
その変更方法をc⌒っ゚д゚)っφ メモメモ...

$ sudo raspi-config

で設定画面へ

4 Internationalsation Options - Set up Language and regional settings to match your location

13 Change Keybord Layout - Set the keyboard layout to match your keyborad

画面遷移にちょっと時間かかる

Generic 105-key (intl) PC

をセレクト(恐らくデフォ)

んで

Other -> Japanese -> Japanese - Japanese (OADG 109A) -> 
The defaukt for the keyborad layout -> No cpmpose key -> No

これで設定は変わってるはず

RubyでTwitterのbot作成

とりあえず個人用にTwitterbotrubyのgemを使って作ったのでメモ

CONSUMER_KEY = ""
CONSUMER_SECRET = ""
OAUTH_TOEKN = ""
OAUTH_TOEKN_SECRET = ""

Twitter.configure do |config|
  config.consumer_key       = CONSUMER_KEY
  config.consumer_secret    = CONSUMER_SECRET
  config.oauth_token        = OAUTH_TOEKN
  config.oauth_token_secret = OAUTH_TOEKN_SECRET
end

とりあえずTwitterの垢をアプリ登録?して各種キーを入手すれば大抵はできる

例えばツイートするには

Twitter.update("HelloWorld")

これで終わり

検索したいときとかには

Twitter.search("serchward", :count => 100, :lang => "ja")

な感じで検索ワードを指定すれば簡単に取得できる
検索にはオプションが指定可能で、:countが取得数(最大100) :langが言語
あとは:pageってのも存在し、これは最大20ページまで指定できる。つまり、過去2000ツイートまで
検索できるということだった気がする

検索したのを見たいときは戻り値を適当な変数に入れて

tweets.statuses.each do |tweet|
 p tweet.text
end

とかすればツイートが見れる、この構造体の中を見たいときはppとか使えば簡単に見れる