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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. Serial.begin(9600);
  24. pinMode(LED_BUILTIN, OUTPUT);
  25. pinMode(RELAIS_FRIGO, OUTPUT);
  26. pinMode(RELAIS_VENTILO, OUTPUT);
  27. Serial.print("M booting\n");
  28. /* XXX */
  29. /* changing the voltage reference of the ADC
  30. * greatly increase the prcision on the limited
  31. * range of our temperatures.
  32. */
  33. analogReference(INTERNAL1V1); // Pour Arduino Mega2560
  34. read_config(0, &parametres);
  35. // display_config(&parametres);
  36. Serial.print("M running now\n");
  37. }
  38. /* ================================================== */
  39. /*
  40. * the light captor is on A4 pin
  41. */
  42. void readlight(void)
  43. {
  44. short value;
  45. delay(100);
  46. value = analogRead(A4);
  47. Serial.print("L ");
  48. Serial.print(value);
  49. Serial.print('\n');
  50. }
  51. /* ================================================== */
  52. short adc_pins[] = { A0, A1, A2, A3 };
  53. #define NB_PASSE 4
  54. /* -------------------------------------------------- */
  55. void updatevalues(short *ptr)
  56. {
  57. short foo, pass;
  58. for (foo=0; foo<NBVAL; foo++) { ptr[foo] = 0; }
  59. digitalWrite(LED_BUILTIN, HIGH);
  60. for (pass=0; pass<NB_PASSE; pass++) {
  61. for (foo=0; foo<NBVAL; foo++) {
  62. ptr[foo] += analogRead(adc_pins[foo]);
  63. delay(50);
  64. }
  65. }
  66. for (foo=0; foo<NBVAL; foo++) { ptr[foo] /= NB_PASSE; }
  67. digitalWrite(LED_BUILTIN, LOW);
  68. }
  69. /* -------------------------------------------------- */
  70. void sendvalues(short *ptr)
  71. {
  72. int foo;
  73. Serial.print("T");
  74. for (foo=0; foo<NBVAL; foo++) {
  75. Serial.print(" ");
  76. Serial.print(ptr[foo]);
  77. }
  78. Serial.print('\n');
  79. }
  80. /* -------------------------------------------------- */
  81. void update_and_send(void)
  82. {
  83. short values[NBVAL];
  84. updatevalues(values);
  85. sendvalues(values);
  86. }
  87. /* ================================================== */
  88. void loop() {
  89. static int foo = 0;
  90. int key;
  91. update_and_send();
  92. if (foo++ > 5) {
  93. readlight(); foo = 0;
  94. }
  95. /* check for external request */
  96. if (Serial.available()) {
  97. key = Serial.read();
  98. switch (key) {
  99. case 'U':
  100. Serial.println("M cli request");
  101. phytocli();
  102. break;
  103. case '+': /* allume le frigo */
  104. controle_frigo(1);
  105. break;
  106. case '-': /* eteint le frigo */
  107. controle_frigo(0);
  108. break;
  109. case '1': /* allume le ventilo */
  110. controle_ventilo(1);
  111. break;
  112. case '0': /* eteint le ventilo */
  113. controle_ventilo(0);
  114. break;
  115. default:
  116. prt("M "); prt(key);
  117. prtln(" bad control code");
  118. break;
  119. }
  120. }
  121. delay(parametres.delai);
  122. /* ETERNAL LOOP END HERE */
  123. }
  124. /* -------------------------------------------------- */