radioReception.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <wiringPi.h>
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <sys/time.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <sched.h>
  8. #include <sstream>
  9. using namespace std;
  10. int pin = 7;
  11. void log(string a){
  12. //Décommenter pour avoir les logs
  13. cout << a << endl;
  14. }
  15. void scheduler_realtime() {
  16. struct sched_param p;
  17. p.__sched_priority = sched_get_priority_max(SCHED_RR);
  18. if( sched_setscheduler( 0, SCHED_RR, &p ) == -1 ) {
  19. perror("Failed to switch to realtime scheduler.");
  20. }
  21. }
  22. void scheduler_standard() {
  23. struct sched_param p;
  24. p.__sched_priority = 0;
  25. if( sched_setscheduler( 0, SCHED_OTHER, &p ) == -1 ) {
  26. perror("Failed to switch to normal scheduler.");
  27. }
  28. }
  29. string longToString(long mylong){
  30. string mystring;
  31. stringstream mystream;
  32. mystream << mylong;
  33. return mystream.str();
  34. }
  35. int pulseIn(int pin, int level, int timeout)
  36. {
  37. struct timeval tn, t0, t1;
  38. long micros;
  39. gettimeofday(&t0, NULL);
  40. micros = 0;
  41. while (digitalRead(pin) != level)
  42. {
  43. gettimeofday(&tn, NULL);
  44. if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
  45. micros += (tn.tv_usec - t0.tv_usec);
  46. if (micros > timeout) return 0;
  47. }
  48. gettimeofday(&t1, NULL);
  49. while (digitalRead(pin) == level)
  50. {
  51. gettimeofday(&tn, NULL);
  52. if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
  53. micros = micros + (tn.tv_usec - t0.tv_usec);
  54. if (micros > timeout) return 0;
  55. }
  56. if (tn.tv_sec > t1.tv_sec) micros = 1000000L; else micros = 0;
  57. micros = micros + (tn.tv_usec - t1.tv_usec);
  58. return micros;
  59. }
  60. int main (int argc, char** argv)
  61. {
  62. //scheduler_realtime();
  63. string command;
  64. string path = "php ";
  65. path.append(argv[1]);
  66. log("Demarrage du programme");
  67. pin = atoi(argv[2]);
  68. if(wiringPiSetup() == -1)
  69. {
  70. log("Librairie Wiring PI introuvable, veuillez lier cette librairie...");
  71. return -1;
  72. }
  73. pinMode(pin, INPUT);
  74. log("Pin GPIO configure en entree");
  75. log("Attente d'un signal du transmetteur ...");
  76. for(;;)
  77. {
  78. int i = 0;
  79. unsigned long t = 0;
  80. int prevBit = 0;
  81. int bit = 0;
  82. unsigned long temperature = 0;
  83. unsigned long emiter = 0;
  84. unsigned long positive = 0;
  85. bool group=false;
  86. bool on =false;
  87. unsigned long recipient = 0;
  88. command = path+" ";
  89. t = pulseIn(pin, LOW, 1000000);
  90. while((t < 2550 || t > 2900)){
  91. t = pulseIn(pin, LOW,1000000);
  92. }
  93. while(i < 24)
  94. {
  95. t = pulseIn(pin, LOW, 1000000);
  96. if(t > 500 && t < 1500)
  97. {
  98. bit = 0;
  99. }
  100. else if(t > 2000 && t < 3000)
  101. {
  102. bit = 1;
  103. }
  104. else
  105. {
  106. i = 0;
  107. cout << "FAIL? = " << t << endl;
  108. break;
  109. }
  110. if(i % 2 == 1)
  111. {
  112. if((prevBit ^ bit) == 0)
  113. {
  114. i = 0;
  115. break;
  116. }
  117. if(i < 15)
  118. {
  119. temperature <<= 1;
  120. temperature |= prevBit;
  121. }else if(i == 15){
  122. positive = prevBit;
  123. }else{
  124. emiter <<= 1;
  125. emiter |= prevBit;
  126. }
  127. }
  128. prevBit = bit;
  129. ++i;
  130. }
  131. if(i>0){
  132. log("------------------------------");
  133. log("Donnees detectees");
  134. cout << "temperature = " << temperature << " C" << endl;
  135. cout << "positif = " << positive << endl;
  136. cout << "code sonde = " << emiter << endl;
  137. command.append("UPDATE_ENGINE_STATE ");
  138. command.append(" "+longToString(emiter));
  139. command.append(" "+longToString(temperature));
  140. command.append(" "+longToString(positive));
  141. log("Execution de la commande PHP...");
  142. log(command.c_str());
  143. system(command.c_str());
  144. }else{
  145. log("Aucune donnee...");
  146. }
  147. delay(3000);
  148. }
  149. //scheduler_standard();
  150. }