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