Monitoring du Phytotron
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rdtemp.ino 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Phytotron
  3. * lecture des capteurs de temperature LM35
  4. */
  5. /* -------------------------------------------------- */
  6. #define NBVAL 4
  7. #define DELAI 1000
  8. #define RELAIS_FRIGO 42
  9. #define RELAIS_VENTILO 40
  10. #define prt(a) Serial.print(a)
  11. #define prtln(a) Serial.println(a)
  12. typedef struct {
  13. unsigned short magic;
  14. char tag[4];
  15. short delai;
  16. short temp_maxi;
  17. short temp_mini;
  18. short control;
  19. } Global;
  20. Global parametres;
  21. /* -------------------------------------------------- */
  22. void setup() {
  23. short foo;
  24. Serial.begin(9600);
  25. pinMode(LED_BUILTIN, OUTPUT);
  26. pinMode(RELAIS_FRIGO, OUTPUT);
  27. pinMode(RELAIS_VENTILO, OUTPUT);
  28. Serial.print("M booting\n");
  29. for (foo=0; foo<10; foo++) {
  30. digitalWrite(LED_BUILTIN, HIGH);
  31. delay(50);
  32. digitalWrite(LED_BUILTIN, LOW);
  33. delay(50);
  34. }
  35. /* XXX */
  36. /* changing the voltage reference of the ADC
  37. * greatly increase the prcision on the limited
  38. * range of our temperatures.
  39. */
  40. analogReference(INTERNAL1V1); // Pour Arduino Mega2560
  41. read_config(0, &parametres);
  42. // display_config(&parametres);
  43. Serial.print("M running now\n");
  44. }
  45. /* ================================================== */
  46. /*
  47. * the light captor is on A4 pin
  48. */
  49. void readlight(void)
  50. {
  51. short value;
  52. delay(100);
  53. value = analogRead(A4);
  54. Serial.print("L ");
  55. Serial.print(value);
  56. Serial.print('\n');
  57. }
  58. /* ================================================== */
  59. short adc_pins[] = { A0, A1, A2, A3 };
  60. #define NB_PASSE 4
  61. /* -------------------------------------------------- */
  62. void updatevalues(short *ptr)
  63. {
  64. short foo, pass;
  65. for (foo=0; foo<NBVAL; foo++) { ptr[foo] = 0; }
  66. digitalWrite(LED_BUILTIN, HIGH);
  67. for (pass=0; pass<NB_PASSE; pass++) {
  68. for (foo=0; foo<NBVAL; foo++) {
  69. ptr[foo] += analogRead(adc_pins[foo]);
  70. delay(50);
  71. }
  72. }
  73. for (foo=0; foo<NBVAL; foo++) { ptr[foo] /= NB_PASSE; }
  74. digitalWrite(LED_BUILTIN, LOW);
  75. }
  76. /* -------------------------------------------------- */
  77. void sendvalues(short *ptr)
  78. {
  79. int foo;
  80. prt("T");
  81. for (foo=0; foo<NBVAL; foo++) {
  82. prt(" ");
  83. prt(ptr[foo]);
  84. }
  85. prt('\n');
  86. }
  87. /* -------------------------------------------------- */
  88. void update_and_send(short *pvalues)
  89. {
  90. updatevalues(pvalues);
  91. sendvalues(pvalues);
  92. }
  93. /* ================================================== */
  94. void loop() {
  95. static int foo = 0;
  96. int key;
  97. short values[NBVAL], temp_interne;
  98. update_and_send(values);
  99. temp_interne = values[1];
  100. if (temp_interne < parametres.temp_mini) {
  101. prtln("M temp is low");
  102. delay(500);
  103. controle_frigo(0);
  104. }
  105. else if (temp_interne > parametres.temp_maxi){
  106. prtln("M temp is hight");
  107. delay(500);
  108. controle_frigo(1);
  109. }
  110. if (foo++ > 5) {
  111. readlight(); foo = 0;
  112. }
  113. /* check for external request */
  114. if (Serial.available()) {
  115. key = Serial.read();
  116. switch (key) {
  117. case 'U':
  118. prtln("M cli request");
  119. phytocli();
  120. break;
  121. case '+': /* allume le frigo */
  122. controle_frigo(1);
  123. break;
  124. case '-': /* eteint le frigo */
  125. controle_frigo(0);
  126. break;
  127. case '1': /* allume le ventilo */
  128. controle_ventilo(1);
  129. break;
  130. case '0': /* eteint le ventilo */
  131. controle_ventilo(0);
  132. break;
  133. default:
  134. prt("M "); prt(key);
  135. prtln(" bad control code");
  136. break;
  137. }
  138. }
  139. delay(parametres.delai);
  140. /* ETERNAL LOOP END HERE */
  141. }
  142. /* -------------------------------------------------- */