我正在为学校制作一个项目,我需要建立一个将温度保持在设定温度附近的系统(这是通过一个电池传感器完成并转动加热和冷却 元素打开和关闭。)传感器值可以用公式((当前温度+50)* 4)转换为度数,我让它在20摄氏度的条件下工作,但现在我想从中获取温度数据库并将其保持在温度范围内(因此可以在加热和冷却元件所在的房间内改变温度)。但是当我尝试将我从数据库获得的温度转换为传感器值时,临时变量开始给出随机数,我不知道为什么。有人可以解释或帮助吗?
(请参阅针对问题定位的代码示例底部的注释)
//my code is a code written for controlling the temprature in a room
//this is done by a phidget sensor and turning a heating and cooling
//element on and of.
#include <phidget21.h> // voor phidgetfucties
#include <stdio.h> // voor printf()
#include <mysql/mysql.h> // voor mysql querys
#include <my_global.h>
int main (int argc, char* argv[])
{ //variables
int val;
int gewensteTempratuur = 280;
int gewensteGradenCelcius = 0;
int i = 0;
int temp = 0;
CPhidgetInterfaceKitHandle ifKit = 0;
CPhidgetInterfaceKit_create(&ifKit);
CPhidget_open((CPhidgetHandle)ifKit,-1);
for(;;)
{
CPhidgetInterfaceKit_getSensorValue(ifKit,0,&val);
printf("Value %d \n", val);
if(val < (gewensteTempratuur - 4)){
//verwarming
CPhidgetInterfaceKit_setOutputState(ifKit,0,1);
//airco
CPhidgetInterfaceKit_setOutputState(ifKit,1,0);
}
if(val > (gewensteTempratuur + 4)){
//verwarming
CPhidgetInterfaceKit_setOutputState(ifKit,0,0);
//airco
CPhidgetInterfaceKit_setOutputState(ifKit,1,1);
}
if((val < (gewensteTempratuur + 4)) && (val > (gewensteTempratuur - 4))) {
//verwarming
CPhidgetInterfaceKit_setOutputState(ifKit,0,0);
//airco
CPhidgetInterfaceKit_setOutputState(ifKit,1,0);
}
//Database Connection
MYSQL *conn;
//Verbings gegevens
char *server = "server";
char *user = "user";
char *password = "pass";
char *database = "database";
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
}
//getting information from the database
if (mysql_query(conn, "SELECT Gewenstetemperatuur FROM SensorValue")){
mysql_error(conn);
}
MYSQL_RES *result = mysql_store_result(conn);
if (result == NULL){
mysql_error(conn);
}
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))){
for (i = 0; i < num_fields; i++){
printf("%s ", row[i] ? row[i] : "NULL");
gewensteGradenCelcius = (int)row[i];
temp = gewensteGradenCelcius;
printf("\n %s \n", gewensteGradenCelcius);
printf("%d \n",temp )
//Here is were is get stuck if i try to take the value and run it trough a
//formula like (gewensteGradenCelcius + 50) * 4 and the try to print the
//gewensteGradenCelcius just puts out nothing
}
printf("\n");
}
mysql_free_result(result);
usleep(1500000);
}
}
答案 0 :(得分:0)
row [i]是一个文本(第&#34; 20&#34;或&#34; 18&#34;),所以这一行错了:
gewensteGradenCelcius = (int)row[i];
你应该写一下
gewensteGradenCelcius = atoi(row[i]);
答案 1 :(得分:0)
string 格式( row [i] )中包含的 number 代码中的转换操作不正确.... < / p>
C string
无法像您尝试的那样复制到int
。 C确实提供了几种方法,包括以下内容......更改:
gewensteGradenCelcius = (int)row[i];//incorrect
要:
//EITHER
gewensteGradenCelcius = atoi(row[i]);//simple and legal C99, but not recommended
//OR
char *endptr;
gewensteGradenCelcius = (int)strtol(row[i], &endptr, 10);//recommended method
//OR
sscanf(row[i], "%d",&gewensteGradenCelcius);//recommended method
(为什么不推荐 atoi() )
然后,由于gewensteGradenCelcius
是您int
语句的printf
,请使用int
format specifier 。变化:
printf("\n %s \n", gewensteGradenCelcius);//wrong format specifier for int
要:
printf("\n %d \n", gewensteGradenCelcius);//correct format specifier
^^