Guest User

Untitled

a guest
May 5th, 2018
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <postgresql/libpq-fe.h>
  6. #include <unistd.h>
  7.  
  8. #define NSENSORS 5
  9.  
  10. #define OUTDOOR 2
  11. #define HOME 3
  12. #define GARDEN 1
  13.  
  14. const char *conn = "host='db.fe.up.pt' user='sinfe32' password='UgeVMoBA'";
  15. static PGconn *dbconn;
  16.  
  17. long GATE=0,IRRSYS=0,FLOOR=0,LUZ_HOME=0,TEMP_HOME=0;
  18.  
  19. float temperatureSensors[NSENSORS];
  20. float humiditySensors[NSENSORS];
  21. float lightSensors[NSENSORS];
  22. float irlightSensors[NSENSORS];
  23.  
  24. double voltage(int pos1, int pos2)
  25. {
  26. double v;
  27. v = pos1*16*16+pos2;
  28. v = (v/4096)*1.5;
  29. return v;
  30. }
  31.  
  32. double vlight(int pos1, int pos2)
  33. {
  34. double v;
  35. v = pos1*16*16+pos2;
  36. v = (0.625*pow(10,6)*(v/4096)*1.5)/100;
  37. return v;
  38. }
  39.  
  40. double infra(int pos1, int pos2)
  41. {
  42. double i;
  43. i = pos1*16*16+pos2;
  44. i = (0.769*pow(10,5)*(i/4096)*1.5)/100;
  45. return i;
  46. }
  47.  
  48. double temperature(int pos1, int pos2)
  49. {
  50. double t;
  51. t = pos1*16*16+pos2;
  52. t = -39.6+0.01*t;
  53. return t;
  54. }
  55.  
  56. double relhumi(int pos1, int pos2)
  57. {
  58. double r;
  59. r = pos1*16*16+pos2;
  60. r = -2.0468+.0367*r-1.5955*pow(10,-6)*r*r;
  61. return r;
  62. }
  63.  
  64. double comphumi(int pos1, int pos2, int temptfunc, int relahumifunc)
  65. {
  66. double h;
  67. h = pos1*16*16+pos2;
  68. h = (temptfunc-25)*(0.01+0.00008*h)+relahumifunc;
  69. return h;
  70. }
  71.  
  72. void sql_init(){
  73.  
  74. dbconn = PQconnectdb(conn);
  75.  
  76. if (PQstatus(dbconn) == CONNECTION_BAD){
  77. printf("Unable to connect\n");
  78. exit(-1);
  79. }
  80. else{
  81. printf("Able to connect!\n");
  82. }
  83. }
  84.  
  85. //void print_db(){
  86. // char
  87.  
  88.  
  89. //}
  90.  
  91. void sql_update(int id){
  92. char q[500];
  93.  
  94. sprintf(q,"UPDATE has.atuadores SET estado = %d WHERE nome = \'gate\';",GATE);
  95. PQexec(dbconn,q);
  96. sprintf(q,"UPDATE has.atuadores SET estado = %d WHERE nome = \'floor\';",FLOOR);
  97. PQexec(dbconn,q);
  98. sprintf(q,"UPDATE has.atuadores SET estado = %d WHERE nome = \'luz_home\';",LUZ_HOME);
  99. PQexec(dbconn,q);
  100. sprintf(q,"UPDATE has.atuadores SET estado = %d WHERE nome = \'temp_home\';",TEMP_HOME);
  101. PQexec(dbconn,q);
  102. sprintf(q,"UPDATE has.atuadores SET estado = %d WHERE nome = \'irrsys\';", IRRSYS);
  103. PQexec(dbconn,q);
  104.  
  105. sprintf(q,"UPDATE has.sensores SET medicao= %5f, timestamp = now() WHERE mote_id=%d AND nome=\'temp\'",temperatureSensors[id],id);
  106. PQexec(dbconn,q);
  107. sprintf(q,"UPDATE has.sensores SET medicao= %5f, timestamp = now() WHERE mote_id=%d AND nome=\'luz\'",lightSensors[id],id);
  108. PQexec(dbconn,q);
  109. sprintf(q,"UPDATE has.sensores SET medicao= %5f, timestamp = now() WHERE mote_id=%d AND nome=\'hum\'",humiditySensors[id],id);
  110. PQexec(dbconn,q);
  111. sprintf(q,"UPDATE has.sensores SET medicao= %5f, timestamp = now() WHERE mote_id=%d AND nome=\'irr\'",irlightSensors[id],id);
  112. PQexec(dbconn,q);
  113.  
  114. }
  115.  
  116. int main(){
  117.  
  118. int str[23] = {0};
  119. char *token;
  120. char message[255];
  121. double volt;
  122. unsigned long id;
  123.  
  124. sql_init();
  125.  
  126. while(1){
  127.  
  128. int i = 0;
  129.  
  130. fgets(message, 255, stdin);
  131. printf("%s\n", message);
  132. token = strtok(message, " ");
  133.  
  134. while(token != NULL)
  135. {
  136. str[i] = strtol(token,NULL,16);
  137. printf("%d ",str[i]);
  138. token = strtok(NULL, " ");
  139. i++;
  140. }
  141.  
  142. id = str[5]<<8 | str[6];
  143.  
  144. temperatureSensors[id] = temperature(str[16],str[17]);
  145. humiditySensors[id] = relhumi(str[18],str[19]);
  146.  
  147.  
  148. lightSensors[id] = vlight(str[12],str[13]);
  149. irlightSensors[id] = infra(str[14],str[15]);
  150.  
  151. if(irlightSensors[OUTDOOR]==0) (GATE=1);
  152. if(irlightSensors[OUTDOOR]==1) (GATE=0);
  153.  
  154. if(humiditySensors[GARDEN]>25) (IRRSYS=0);
  155. if(humiditySensors[GARDEN]<40) (IRRSYS=1);
  156.  
  157. if(temperatureSensors[GARDEN]<5) (FLOOR=1);
  158. if(temperatureSensors[GARDEN]>20) (FLOOR=0);
  159.  
  160. if(lightSensors[HOME]<300) (LUZ_HOME=1);
  161. if(lightSensors[HOME]>500) (LUZ_HOME=0);
  162. if(temperatureSensors[HOME]<20) (TEMP_HOME=1);
  163. if(temperatureSensors[HOME]>22) (TEMP_HOME=0);
  164.  
  165. printf("\n Temperatura: %fºC\n Humidade rel: %f%% \n Voltage: %fV\n Visible light: %flux \n Infrared light: %flux \n\n", temperatureSensors[id], humiditySensors[id], volt, lightSensors[id],irlightSensors[id]);
  166.  
  167. sql_update(id);
  168.  
  169. }
  170.  
  171. return 0;
  172.  
  173. }
Add Comment
Please, Sign In to add comment