Friday, August 21, 2015

rsync over a non standard SSH port

Use the -e option:

rsync -avz -e "ssh -p $portNumber" user@remoteip:/path/to/files/ /local/path/

Sunday, June 14, 2015

HMC5883L magnetometer to Raspberry Pi connection notes

Some quick notes for connecting Honeywell's 3-Axis Digital Compass IC
HMC5883L to the I2C bus on a Raspbrry Pi board.

HMC5883L breakout board from SparkFun:
https://www.sparkfun.com/products/10530


Connections

GND -> GND (pin 6 on RPi I/O header)
VCC -> 3.3 V (pin 1 on RPi )
SDA -> SDA1 I2C (pin 3 on RPi)
SCL -> SCL1 I2C (pin 5 on RPi)

Configuration

Enable I2C using "raspi-config". Add user to i2c group.

pi@raspberrypi ~ $ i2cdetect 1
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-1.
I will probe address range 0x03-0x77.
Continue? [Y/n]
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1e --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --



Run

See below for example C code.

pi@raspberrypi ~ $ gcc magnetometer.c   -o magnetometer -lm

pi@raspberrypi ~ $ ./magnetometer
Identification: 'H43' HMC5883L sensor detected
1434246270.000000, -548, -649, 349, 67030.4 nT
1434246270.100000, -547, -650, 349, 67038.5 nT
1434246270.200000, -548, -648, 348, 66951.1 nT
1434246270.300000, -548, -650, 350, 67109.8 nT
1434246270.400000, -548, -650, 350, 67109.8 nT
1434246270.500000, -547, -650, 350, 67066.3 nT
1434246270.600000, -547, -650, 349, 67038.5 nT
1434246270.700000, -548, -650, 350, 67109.8 nT
1434246270.800000, -547, -649, 348, 66959.2 nT
1434246270.900000, -548, -650, 351, 67137.6 nT


Code

C code to read and display the magnetometer data
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <linux/i2c-dev.h>
const int HMC5883L_I2C_ADDR = 0x1E;
#define CONFIG_A 0x00
#define CONFIG_B 0x01
#define MODE 0x02
#define DATA 0x03 //read 6 bytes: x msb, x lsb, z msb, z lsb, y msb, y lsb
#define STATUS 0x09
#define ID_A 0x0A
#define ID_B 0x0B
#define ID_C 0x0C
#define ID_STRING "H43"
#define GAIN 1370 //000 setting
void selectDevice(int fd, int addr, char * name)
{
if (ioctl(fd, I2C_SLAVE, addr) < 0)
{
fprintf(stderr, "%s not present\n", name);
//exit(1);
}
}
void writeToDevice(int fd, int reg, int val)
{
char buf[2];
buf[0]=reg;
buf[1]=val;
if (write(fd, buf, 2) != 2)
{
fprintf(stderr, "Can't write to ADXL345\n");
//exit(1);
}
}
int main(int argc, char **argv)
{
int fd;
unsigned char buf[16];
struct timeval tv;
struct timezone tz; //ignored
struct timeval data_timestamp; //
int resolution = 100000; //microseconds
long next_timestamp;
if ((fd = open("/dev/i2c-1", O_RDWR)) < 0)
{
// Open port for reading and writing
fprintf(stderr, "Failed to open i2c bus\n");
return 1;
}
/* initialise HMC5883L */
selectDevice(fd, HMC5883L_I2C_ADDR, "HMC5883L");
//first read the 3 ID bytes
buf[0] = ID_A;
if ((write(fd, buf, 1)) != 1)
{
// Send the register to read from
fprintf(stderr, "Error writing to i2c slave\n");
}
if (read(fd, buf, 3) != 3) {
fprintf(stderr, "Unable to read from HMC5883L\n");
}
buf[3]=0;
printf("Identification: '%s' ",buf);
if (strncmp(buf, ID_STRING, 3) == 0)
printf("HMC5883L sensor detected\n");
else {
printf("unknown sensor. Exiting.\n");
exit(1);
}
//Configuration
//writeToDevice(fd, 0x01, 0);
writeToDevice(fd, CONFIG_A, 0b01101000); //8 sample averaging
writeToDevice(fd, CONFIG_B, 0b00000000); //max gain
writeToDevice(fd, MODE, 0b00000011); //idle mode
//find current time
gettimeofday(&data_timestamp,&tz);
data_timestamp.tv_sec += 1; //start loggin at start of next second
data_timestamp.tv_usec = 0;
while(1){ //record forever
//read time & wait until next reading required
while (1) {
//get time
gettimeofday(&tv,&tz);
//if seconds >= next_secs && usecs >= next_usecs
if (tv.tv_sec >= data_timestamp.tv_sec && tv.tv_usec >= data_timestamp.tv_usec)
break;
usleep(1000);
}
//initiate single conversion
writeToDevice(fd, MODE, 0b00000001); //single measurement
//wait 7 milliseconds
usleep(7000);
buf[0] = DATA;
if ((write(fd, buf, 1)) != 1)
{
// Send the register to read from
fprintf(stderr, "Error writing to i2c slave\n");
}
if (read(fd, buf, 6) != 6) {
fprintf(stderr, "Unable to read from HMC5883L\n");
} else {
short x = (buf[0] << 8) | buf[1];
short y = (buf[4] << 8) | buf[5];
short z = (buf[2] << 8) | buf[3];
float angle = atan2(y, x) * 180 / M_PI;
float mag = sqrt(x*x+y*y+z*z);
//printf("x=%d, y=%d, z=%d\n", x, y, z);
// printf("angle = %0.1f, mag = %0.1f\n", angle,mag);
//printf("time: %ld.%06ld\n",tv.tv_sec, tv.tv_usec);
printf("%ld.%06ld, %d, %d, %d, %0.1f nT\n",data_timestamp.tv_sec, data_timestamp.tv_usec,x,y,z,1e5*mag/GAIN);
fflush(stdout);
}
//advance data timestamp to next required time
data_timestamp.tv_usec += resolution;
if (data_timestamp.tv_usec >= 1e6) {
data_timestamp.tv_sec += 1;
data_timestamp.tv_usec -= 1e6;
}
}
return 0;
}
view raw magnetometer.c hosted with ❤ by GitHub

Tuesday, May 26, 2015

Red Hat and CentOS Linux: Use PEERNTP=no to ignore any NTP server info provided by DHCP

Any NTP server information provided by DHCP can be ignored by setting PEERNTP=no anywhere in the sourced network scripts (e.g. /etc/sysconfig/network). This is useful in situations where a specific NTP setup is required rather than a default configuration supplied via DHCP. 
https://bugzilla.redhat.com/show_bug.cgi?id=809367  

Sunday, March 08, 2015

systemd service script for soundmodem



james@arm:/lib/systemd/system$ more soundmodem.service
[Unit]
Description=Soundcard Packet Radio Modem
After=sound.target network.target multi-user.target

[Service]
Type=forking
ExecStart=/usr/sbin/soundmodem --daemonize -s

[Install]
WantedBy=multi-user.target



james@arm:/lib/systemd/system$ more /etc/debian_version
7.8
james@arm:/lib/systemd/system$ uname -a
Linux arm 3.19.0-armv7-x3 #1 SMP Wed Feb 11 21:58:07 UTC 2015 armv7l GNU/Linux
james@arm:/lib/systemd/system$

Wednesday, January 07, 2015

Baofeng BF-F8+ battery voltage display

The Baofeng BF-F8+ has a built in battery voltage display.

Baofeng BF-F8+

Press and hold the "0 SQL" key

The display will show the DC voltage of the battery. 8.1 V in this example:

Accuracy seems to be OK