Page 12 of 14
Page 227
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int fd = open("/sys/class/hwmon/hwmon0/temp1_input", O_RDONLY);
char buf[100] = {0};
read(fd, buf, 100);
printf("%s\n\r", buf);
float temp;
sscanf(buf, "%f", &temp);
temp = temp / 1000;
printf("%f\n\r", temp);
}
Page 234
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
void loadHTU21();
int main(int argc, char **argv)
{
loadHTU21();
float temp;
float hum;
{
char output[1024] = {0};
int fdtemp = open("/sys/bus/iio/devices/iio:device0/in_temp_input", O_RDWR);
read(fdtemp, output, sizeof(output));
close(fdtemp);
printf("%s\n\r", output);
sscanf(output, "%f", &temp);
temp = temp / 1000;
fflush(stdout);
}
{
char output[1024] = {0};
int fdhum = open("/sys/bus/iio/devices/iio:device0/in_humidityrelative_input", O_RDWR);
read(fdhum, output, sizeof(output));
close(fdhum);
printf("%s\n\r", output);
sscanf(output, "%f", &hum);
hum = hum / 1000;
fflush(stdout);
}
printf("%f\n\r", temp);
printf("%f\n\r", hum);
return (EXIT_SUCCESS);
}
void loadHTU21()
{
FILE *fd = doCommand("sudo dtparam -l");
char output[1024];
int txfound = 0;
while (fgets(output, sizeof(output), fd) != NULL)
{
printf("%s\n\r", output);
fflush(stdout);
if (strstr(output, "i2c-sensor htu21=true") != NULL)
{
txfound = 1;
}
}
pclose(fd);
if (txfound == 0)
{
fd = doCommand("sudo dtoverlay i2c-sensor htu21");
pclose(fd);
}
}