chaconGetRemote.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. Ce programme est basé sur RadioReception de Valentin Carruesco
  3. Il permet de récupérer un code radio chacon dans le format de RCsend
  4. g++ HEreceive.cpp -o HEreceive -lwiringPi
  5. Cette page récupere les informations du signal radio recu par le raspberry PI et execute une page PHP
  6. en lui fournissant tout les paramêtres.
  7. Vous pouvez compiler cette source via la commande :
  8. g++ radioReception.cpp -o radioReception -lwiringPi
  9. N'oubliez pas d'installer auparavant la librairie wiring pi ainsi que l'essentiel des paquets pour compiler
  10. Vous pouvez lancer le programme via la commande :
  11. sudo chmod 777 radioReception
  12. ./radioReception /var/www/radioReception/radioReception.php 7
  13. Les deux parametres de fin étant le chemin vers le PHP a appeller, et le numéro wiringPi du PIN relié au récepteur RF 433 mhz
  14. @author : Valentin CARRUESCO (idleman@idleman.fr)
  15. @contributors : Yann PONSARD, Jimmy LALANDE
  16. @webPage : http://blog.idleman.fr
  17. @references & Libraries: https://projects.drogon.net/raspberry-pi/wiringpi/, http://playground.arduino.cc/Code/HomeEasy
  18. @licence : CC by sa (http://creativecommons.org/licenses/by-sa/3.0/fr/)
  19. RadioPi de Valentin CARRUESCO (Idleman) est mis à disposition selon les termes de la
  20. licence Creative Commons Attribution - Partage dans les Mêmes Conditions 3.0 France.
  21. Les autorisations au-delà du champ de cette licence peuvent être obtenues à idleman@idleman.fr.
  22. */
  23. #include <wiringPi.h>
  24. #include <iostream>
  25. #include <stdio.h>
  26. #include <sys/time.h>
  27. #include <time.h>
  28. #include <stdlib.h>
  29. #include <sched.h>
  30. #include <sstream>
  31. using namespace std;
  32. //initialisation du pin de reception
  33. int pin;
  34. int received_code;
  35. int ok_code = 0;
  36. int wait_cycles = 1;
  37. //Fonction de log
  38. void log(string a){
  39. //Décommenter pour avoir les logs
  40. //cout << a << endl;
  41. }
  42. //Fonction de conversion long vers string
  43. string longToString(long mylong){
  44. string mystring;
  45. stringstream mystream;
  46. mystream << mylong;
  47. return mystream.str();
  48. }
  49. //Fonction de passage du programme en temps réel (car la reception se joue a la micro seconde près)
  50. void scheduler_realtime() {
  51. struct sched_param p;
  52. p.__sched_priority = sched_get_priority_max(SCHED_RR);
  53. if( sched_setscheduler( 0, SCHED_RR, &p ) == -1 ) {
  54. perror("Failed to switch to realtime scheduler.");
  55. }
  56. }
  57. //Fonction de remise du programme en temps standard
  58. void scheduler_standard() {
  59. struct sched_param p;
  60. p.__sched_priority = 0;
  61. if( sched_setscheduler( 0, SCHED_OTHER, &p ) == -1 ) {
  62. perror("Failed to switch to normal scheduler.");
  63. }
  64. }
  65. //Recuperation du temp (en micro secondes) d'une pulsation
  66. int pulseIn(int pin, int level, int timeout)
  67. {
  68. struct timeval tn, t0, t1;
  69. long micros;
  70. gettimeofday(&t0, NULL);
  71. micros = 0;
  72. while (digitalRead(pin) != level)
  73. {
  74. gettimeofday(&tn, NULL);
  75. if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
  76. micros += (tn.tv_usec - t0.tv_usec);
  77. if (micros > timeout) return 0;
  78. }
  79. gettimeofday(&t1, NULL);
  80. while (digitalRead(pin) == level)
  81. {
  82. gettimeofday(&tn, NULL);
  83. if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
  84. micros = micros + (tn.tv_usec - t0.tv_usec);
  85. if (micros > timeout) return 0;
  86. }
  87. if (tn.tv_sec > t1.tv_sec) micros = 1000000L; else micros = 0;
  88. micros = micros + (tn.tv_usec - t1.tv_usec);
  89. return micros;
  90. }
  91. //Programme principal
  92. int main (int argc, char** argv)
  93. {
  94. //On passe en temps réel
  95. scheduler_realtime();
  96. //on récupere l'argument 2, qui est le numéro de Pin GPIO auquel est connecté le recepteur radio
  97. pin = atoi(argv[1]);
  98. //Si on ne trouve pas la librairie wiringPI, on arrête l'execution
  99. if(wiringPiSetup() == -1)
  100. {
  101. log("WiringPI:Notfounded");
  102. return -1;
  103. }else{
  104. log("WiringPI:OK");
  105. }
  106. pinMode(pin, INPUT);
  107. log("GPIO configured");
  108. log("Waiting for signal ...");
  109. //We received GPIO State to see if something is happening
  110. for (int timer = 0; timer < 50; ++timer)
  111. {
  112. received_code = digitalRead(pin);
  113. if (received_code == 1) { ok_code++; } //If PIN IS HIGH add to ok_code
  114. //printf("%i",digitalRead(pin));
  115. delay(10);
  116. }
  117. if (ok_code == 0) //If the PIN was never on HIGH we assume there was a problem
  118. {
  119. printf("Wrong GPIO\n");
  120. exit(1);
  121. }
  122. else //If test1 PASS
  123. {
  124. //On boucle pour ecouter les signaux
  125. for(int cycles=0;cycles < wait_cycles;cycles++)
  126. {
  127. int i = 0;
  128. unsigned long t = 0;
  129. //avant dernier byte reçu
  130. int prevBit = 0;
  131. //dernier byte reçu
  132. int bit = 0;
  133. //mise a zero de l'idenfiant télécommande
  134. unsigned long sender = 0;
  135. //mise a zero du groupe
  136. bool group=false;
  137. //mise a zero de l'etat on/off
  138. bool on =false;
  139. //mise a zero de l'idenfiant de la rangée de bouton
  140. unsigned long recipient = 0;
  141. t = pulseIn(pin, LOW, 1000000);
  142. //Verrou 1
  143. while((t < 2700 || t > 2800)){
  144. t = pulseIn(pin, LOW,1000000);
  145. }
  146. log("Lock 1 detected");
  147. // données
  148. while(i < 64)
  149. {
  150. t = pulseIn(pin, LOW, 1000000);
  151. //cout << "t = " << t << endl;
  152. //Définition du bit (0 ou 1)
  153. if(t > 180 && t < 420)
  154. {
  155. bit = 0;
  156. }
  157. else if(t > 1280 && t < 1480)
  158. {
  159. bit = 1;
  160. }
  161. else
  162. {
  163. i = 0;
  164. break;
  165. }
  166. if(i % 2 == 1)
  167. {
  168. if((prevBit ^ bit) == 0)
  169. {
  170. // doit être 01 ou 10,,pas 00 ou 11 sinon ou coupe la detection, c'est un parasite
  171. i = 0;
  172. break;
  173. }
  174. if(i < 53)
  175. {
  176. // les 26 premiers (0-25) bits sont l'identifiants de la télécommande
  177. sender <<= 1;
  178. sender |= prevBit;
  179. }
  180. else if(i == 53)
  181. {
  182. // le 26em bit est le bit de groupe
  183. group = prevBit;
  184. }
  185. else if(i == 55)
  186. {
  187. // le 27em bit est le bit d'etat (on/off)
  188. on = prevBit;
  189. }
  190. else
  191. {
  192. // les 4 derniers bits (28-32) sont l'identifiant de la rangée de bouton
  193. recipient <<= 1;
  194. recipient |= prevBit;
  195. }
  196. }
  197. prevBit = bit;
  198. ++i;
  199. }
  200. //Si les données ont bien été détéctées
  201. if(i>0){
  202. log("------------------------------");
  203. log("Code de la télécommande");
  204. //on construit la commande qui vas envoyer les parametres au PHP
  205. cout << sender << endl;
  206. exit(0);
  207. }else{
  208. log("Aucun code detecté");
  209. }
  210. }
  211. scheduler_standard();
  212. }
  213. }