I have a circuit connected to a thermistor and using system(command) I can get it to read the value into the terminal but I can't get it to send the value properly to my C program.
I'm sending code to the terminal to run, and this code is returning a value in the terminal. I want to take this value into my C program
Here is my code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> //for Log
float Vo; //voltage out
float Ro = 5000; //value of resistance at 25C in the thermistor
float R ; //value of fixed resistor
float B = 3977; //Beta constant
float T0 = 298.15; //25 degrees C in Kelvin
//float logR2, R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07; // Ste$
int main(){
char command[50];
//get the Analog value
sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw"$
Vo = system(command);
printf("Analog Reading: %d\n", Vo);
return 0;
}
An example output would be
3973
Analog Reading: 4319452
where is the 4319452 coming from? I want it to read 3973
After reading the comments I tried to use fopen in the following way
//#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> //for Log
float Vo; //voltage out
float Ro = 5000; //value of resistance at 25C in the thermistor
float R ; //value of fixed resistor
float B = 3977; //Beta constant
float T0 = 298.15; //25 degrees C in Kelvin
//float logR2, R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07; // Steinhart-Hart and Hart Coefficients
int main(){
FILE *fp;
char command[50];
int c;
//get the Analog value
//sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw" );
fp = fopen("/sys/bus/iio/devices/iio\\:device0/in_voltage0_raw","r");
while(1) {
c = fgetc(fp);
if( feof(fp) ) {
break ;
}
printf("%c", c);
}
fclose(fp);
//Vo[i] = system(command);
printf("Analog Reading: %d\n", c);
return 0;
}
I get
Segmentation fault
after running the code
I edited the code in the following way
sprintf(command, "cd /sys/bus/iio/devices/iio:device0 && cat in_voltage0_raw"$
After I run it I get the following output
3 9 6 8
Analog Reading: ▒
It reads the output correctly once but in the second
printf("Analog Reading: %d\n", c);
it gives me
Analog Reading: ▒
Full code up to this point
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> //for Log
int Vo; //voltage out
float Ro = 5000; //value of resistance at 25C in the thermistor
float R ; //value of fixed resistor
float B = 3977; //Beta constant
float T0 = 298.15; //25 degrees C in Kelvin
//float logR2, R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07; // Steinhart-Hart and Hart Coefficients
int main(){
FILE *fp;
char command[50];
int c;
//get the Analog value
//sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw" );
fp = fopen("/sys/bus/iio/devices/iio:device0/in_voltage0_raw","r");
if ( fp == NULL ){
perror("Error: ");
return(-1);
}
while(1) {
c = fgetc(fp);
if( feof(fp) ) {
break ;
}
printf("%c ", c);
// Vo = c;
}
fclose(fp);
//Vo[i] = system(command);
printf("Analog Reading: %c\n", c);
return 0;
}
I solved the problem by taking every character read and putting it in an array
Here the working code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> //for Log
char Vo[10]; //voltage out
float Ro = 5000; //value of resistance at 25C in the thermistor
float R ; //value of fixed resistor
float B = 3977; //Beta constant
float T0 = 298.15; //25 degrees C in Kelvin
//float logR2, R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07; // Steinhart-Hart and Hart Coefficients
int main(){
FILE *fp;
char command[50];
int c;
//get the Analog value
//sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw" );
fp = fopen("/sys/bus/iio/devices/iio:device0/in_voltage0_raw","r");
if ( fp == NULL ){
perror("Error: ");
return(-1);
}
int i=0;
while(1) {
c = fgetc(fp);
if( feof(fp) ) {
break ;
}
printf("%c ", c);
if (c != '\0'){
Vo[i] = c;
++i;
}
// Vo = c;
}
fclose(fp);
//Vo[i] = system(command);
printf("Analog Reading: %s\n", Vo);
return 0;
}
Output:
3 9 7 0
Analog Reading: 3970