-
-
Save perenecabuto/7d781bb6b8313b2db5c1fb27b8aa2807 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include <linux/i2c-dev.h> | |
#include <fcntl.h> | |
#include <sys/ioctl.h> | |
static void | |
HMC5883L_write( | |
unsigned char address, | |
unsigned char data, | |
int fd) | |
{ | |
unsigned char buf[2]; | |
buf[0] = address; | |
buf[1] = data; | |
if((write(fd,buf,2))!=2) { | |
printf("Error writing to i2c slave\n"); | |
exit(1); | |
} | |
return; | |
} | |
static unsigned char | |
HMC5883L_read( | |
unsigned char address, | |
int fd) | |
{ | |
unsigned char buf[1]; | |
buf[0] = address; | |
if((write(fd,buf,1))!= 1){ | |
printf("Error writing to i2c slave\n"); | |
exit(1); | |
} | |
if(read(fd,buf,1)!=1){ | |
printf("Error reading from i2c slave\n"); | |
exit(1); | |
} | |
return buf[0]; | |
} | |
static void | |
HMC5883L_readData( | |
int *idata, | |
int fd) | |
{ | |
unsigned char i,addr,data[6]; | |
for(i=0; i<6; i++){ | |
addr = 0x03 + i; | |
data[i]=HMC5883L_read(addr,fd); | |
} | |
idata[0]=((int)data[0]<<24|(int)data[1]<<16)>>16; | |
idata[1]=((int)data[2]<<24|(int)data[3]<<16)>>16; | |
idata[2]=((int)data[4]<<24|(int)data[5]<<16)>>16; | |
return; | |
} | |
static void | |
HMC5883L_init(int fd) | |
{ | |
unsigned char data; | |
data = HMC5883L_read(0x0a,fd); | |
if (data != 0x48) { | |
printf("Identification Register 0x0a check failure [%02x]",data); | |
exit(1); | |
} | |
data = HMC5883L_read(0x0b,fd); | |
if (data != 0x34) { | |
printf("Identification Register 0x0b check failure [%02x]",data); | |
exit(1); | |
} | |
data = HMC5883L_read(0x0c,fd); | |
if (data != 0x33) { | |
printf("Identification Register 0x0c check failure [%02x]",data); | |
exit(1); | |
} | |
HMC5883L_write(0x00,0xe0,fd); /* set default val to Config Reg A */ | |
HMC5883L_write(0x02,0x00,fd); /* set continuous mode to Mode Reg */ | |
return; | |
} | |
int | |
main( | |
int argc, | |
char **argv) | |
{ | |
int i,j,fd,data[3]; | |
char *i2cfile = "/dev/i2c-1"; | |
unsigned char i2caddr,status; | |
double x,y,z; | |
i2caddr = 0x1e; | |
if ((fd = open(i2cfile, O_RDWR)) < 0) { | |
printf("Faild to open i2c port\n"); | |
exit(1); | |
} | |
if (ioctl(fd, I2C_SLAVE, i2caddr) < 0) { | |
printf("Unable to get bus access to talk to slave\n"); | |
exit(1); | |
} | |
HMC5883L_init(fd); | |
x = y = z = 0; | |
for(i=0,j=0; i<1000; i++) { | |
status = HMC5883L_read(0x09,fd); | |
if (status | 0x1) { | |
HMC5883L_readData(data,fd); | |
x += (double)data[0]; | |
z += (double)data[1]; | |
y += (double)data[2]; | |
#if 0 | |
printf("x[%05d] z[%05d] y[%05d]\n", | |
data[0],data[1],data[2]); | |
#endif | |
j++; | |
} | |
usleep(1000*10); | |
} | |
printf("%5.1lf %5.1lf %5.1lf\n",x/j,y/j,z/j); | |
return; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding:utf-8 | |
require "pp" | |
require "net/https" | |
require "fileutils" | |
def xively(x,y,z) | |
uri = URI.parse('https://api.xively.com/v2/feeds/1679002984.xml') | |
https = Net::HTTP.new(uri.host,uri.port) | |
https.use_ssl = true | |
headers = { | |
'X-ApiKey' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX', | |
'Content-Type' => 'application/xml' | |
} | |
data =<<-EOS | |
<?xml version="1.0" encoding="UTF-8"?> | |
<eeml> | |
<environment> | |
<data id="HMC5883L_X"> | |
<current_value>#{x}</current_value> | |
</data> | |
<data id="HMC5883L_Y"> | |
<current_value>#{y}</current_value> | |
</data> | |
<data id="HMC5883L_Z"> | |
<current_value>#{z}</current_value> | |
</data> | |
</environment> | |
</eeml> | |
EOS | |
res = https.send_request('PUT',uri.request_uri,data,headers) | |
end | |
now = Time.now | |
dir = File.expand_path(File.dirname(__FILE__)) | |
command = File.join(dir,"hmc5883l") | |
x,y,z = `#{command}`.split(" ") | |
exit 0 unless x && y && z | |
dir = File.join(ENV["HOME"],"hmc588l",now.strftime("%Y"),now.strftime("%m")) | |
file = File.join(dir,now.strftime("%d")+".txt") | |
FileUtils.mkdir_p(dir) | |
mode = File.exists?(file) ? "a" : "w" | |
File.open(file,mode) do |io| | |
out = "#{now.strftime("%Y%m%d%H%M%S")}\t#{x}\t#{y}\t#{z}" | |
io.puts out | |
end | |
begin | |
xively(x,y,z) | |
rescue Exception => ex | |
puts ex | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment