A Banana Pro Weather Logger
get_w_data; a C program to poll weather data on the LeoStick.
The C program get_w_data is used to poll the LeoStick for data via I2C and print a line of comma delimited data to stdout. It is called from a function in the program wl_jobs (to be discussed in the next section) after wl_jobs has set GPIO pin 0 HIGH and consequently poll-weather-data.ino running on the LeoStick has assigned new fresh values to the four weather data variables'
/*///////////////////////////////////////////////////////////////////////
/ get-w-data.c RM20190422 /
/ Used to poll weather data on LeoStick via I2C. Program used to return /
/ in a comma delimitd line for use in a .csv file. /
///////////////////////////////////////////////////////////////////////*/
include <string.h>
include <unistd.h>
include <errno.h>
include <stdio.h>
include <stdlib.h>
include <linux/i2c-dev.h>
include <sys/ioctl.h>
include <fcntl.h>
include <unistd.h>
// BananaPro I2C address.
#define ADDRESS 04
// The I2C bus for the BananaPro is /dev/i2c-1.
static const char *devName = "/dev/i2c-1";
int main(int argc, char** argv) {
// Provide a small delay between LeoStcick I2C comms with sensors
// and Banana Pro I2C comms with LeoStck.
usleep(2000);
int file;
file = open(devName, O_RDWR);
ioctl(file, I2C_SLAVE, ADDRESS);
int command;
for (command= 1; command<=4; command++){
unsigned char cmd[16];
cmd[0] = command;
if (write(file, cmd, 1) == 1){
char buf[6];
//read 4 bytes from i2c
if (read(file, buf, 4) == 4){
volatile float value_received;
// convert 4 bytes received into a float
memcpy((char*)&value_received,buf,5);
if (command==1){
printf("%.2f,", value_received);
}
if (command==2){
printf("%.2f,", value_received);
}
if (command==3){
printf("%.2f,", value_received);
}
if (command==4){
printf("%.2f\n", value_received);
}
}
}
}
return (0);
}