Analog Sensors Temperature - Light

I’m using the following C code https://github.com/DexterInd/GrovePi, in order to read from my temperature and light sensors. In theory, it is as simple as using analogRead(0)… My code compiles, however it is not working…

I receive insane values, which should be between 0-1023. I get around 2124175136 as temperature reading. I’m using Raspberry 3 with GrovePi interface. It is weird, as everything is working fine except from analog readings.

#include “grovepi.h”

int tPin=0;
int lPin=1;
.
.
.
float temperature=25;
float light=15;
.
.
.
temperature=analogRead(tPin);
printf(“Analog read %d\n”, temperature);
light=analogRead(lPin);
printf(“Analog read %d\n”, light);

And I’m compiling as it is said in the github file gcc programe_name.c grovepi.c -Wall

What could be happening?

Hi @vicente_cacep,

Can you try this python example on Analog read to make sure that there is no problem with the GrovePi hardware or firmware or the sensors. If this works then can you try this analog read in C and check if it works. If the analog read in C isn’t working then can you post the code you wish to have working, so that we can try it out in our end.

If you have trouble with any of these examples, please post a screenshot of the output that you get and also the code that you used.

-Shoban

Hi there Shoban. Python is perfectly working, but C is not. I’m trying to get temperature and light data, and upload it to mysql database.
I’ve managed to succesfully connect to my DB, however the temperature and light data is just trash. I get insane values as mentioned before, even though I disconnect the sensors in my grove PI. I don’t receive error messages, just weird values. I’m a new user, i can’t attach but… here’s my code

#include “my_global.h”
#include “grovepi.h”
#include “mysql.h”
#include <time.h>
#include “string.h”

/SEND ERROR MESSAGE IN CASE OF FAILURE**********************************/
void finish_with_error(MYSQL *con)
{
fprintf(stderr, “%s\n”, mysql_error(con));
mysql_close(con);
exit(1);
}

int main(int argc, char **argv)
{

/VARIABLE INIT*****************/
int temperature=0;
float light=0;
int preLight=0;
int touch=0;
int led=0;
int timeTemp=0;
int status=0;

int sTemp = 0;
int sLight = 1;
int sTouch = 2;
int oLed = 5;

char q[1024];
char time[1024];
char update[1024];
char select[1024];
char host[255]=“10.15.249.6”;;
int port=3306;
char user[255]=“root”;
char password[255]=“root”;
char db[255]=“tec2”;

time_t t = time(NULL);
struct tm tm = *localtime(&t);

printf(“now: %d-%d-%d %d:%d:%d\n”, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

/GROVE PI INIT/

if(init()==-1)
exit(1);
/***********/

/START SQL CONNECTION*****************************/
if (con == NULL)
{
fprintf(stderr, “%s\n”, mysql_error(con));
exit(1);
}

//Revisar que los datos iniciales no sean Nulos
if (mysql_real_connect(con, host, user, password,
db, port, NULL, 0) == NULL)
{
fprintf(stderr, “%s\n”, mysql_error(con));
mysql_close(con);
exit(1);
}

/********************************************************************************************/

while(1)
{

    /**
int sTemp = 0;
int sLight = 1;
int sTouch = 2;
int oLed = 5;*/

sprintf(time, “%d-%d-%d %d:%d:%d\n”, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

    sleep(1);
    temperature=analogRead(sTemp);
    sleep(1);
    preLight=analogRead(sLight);
    sleep(1);
    light=(float)(1023-preLight)*10/preLight;
    sleep(1);

    printf("Temp = %f\n",temperature);
    printf("Light = %f\n",light);
    sleep(1);
//Sleep 1000ms
pi_sleep(1000);

sprintf(update, "UPDATE sensors SET tempValue = %s, lightValue = %s WHERE rnum = %s",round(temperature,3), round(lightValue,3),1 );
sprintf(select, "SELECT stsTemp, ststLight FROM actuators WHERE rnum=1");
sprintf(q,"INSERT INTO record(salon, tempValue, lightValue,d) VALUES(%s,%s,%s,%s)","CETEC101",temperature,light,time);


//CARRY OUT CONNECTIONS

if (mysql_query(con, select)) {
finish_with_error(con);
status=0;
}else{
status=1;
}
sleep(1);

if (mysql_query(con, update)) {
finish_with_error(con);
status=0;
}else{
status=1;
}
sleep(1);
if (mysql_query(con, q)) {
finish_with_error(con);
status=0;
}else{
status=1;
}

if(status){
digitalWrite(5,1);
}else{
digitalWrite(5,0);
}

sleep(1);
}

mysql_close(con);

printf(“MySQL client version: %s\n”, mysql_get_client_info());

exit(0);

return 0;

}

COMPILING WITH: gcc C_EMBEDDEDFINAL.c grovepi.c -Wall -o createdb -std=c99 mysql_config --cflags --libs

@vicente_cacep,
It would be hard for us to debug the entire program that you had posted above. However I ran a smaller program with just 2 analog reads in C and it works pretty well for me. Can you try compiling and running this :

#include “grovepi.h”

int main(void)
{
int adata1,adata2;

//Exit on failure to start communications with the GrovePi
if(init()==-1)
exit(1);

while(1)
{
adata1=analogRead(0);
adata2=analogRead(1);
printf(“analog read %d,%d\n”,adata1,adata2);
if(adata1==-1 || adata2==-1)
printf(“IO Error”);
}
return 1;
}

compile it using:

gcc grovepi_analog_read.c grovepi.c -Wall

and run it:

./a.out
analog read 249,211
analog read 244,213

Can you try this out and see if it works for you.