DD2-monitor/simulator/rdtemp/rdtemp.ino

174 lines
3.6 KiB
Arduino
Raw Normal View History

/*
2019-04-29 20:40:22 +11:00
* Phytotron
* lecture des capteurs de temperature LM35
*/
/* -------------------------------------------------- */
#define NBVAL 4
2019-04-13 01:10:56 +11:00
#define DELAI 1000
2019-05-14 18:28:32 +11:00
#define RELAIS_FRIGO 42
#define RELAIS_VENTILO 40
#define prt(a) Serial.print(a)
#define prtln(a) Serial.println(a)
2019-05-14 02:49:21 +11:00
2019-05-01 01:49:21 +11:00
typedef struct {
unsigned short magic;
char tag[4];
short delai;
2019-05-13 23:26:05 +11:00
short temp_maxi;
short temp_mini;
short control;
2019-05-01 01:49:21 +11:00
} Global;
Global parametres;
/* -------------------------------------------------- */
void setup() {
2019-05-23 00:48:21 +11:00
short foo;
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
2019-05-14 02:49:21 +11:00
pinMode(RELAIS_FRIGO, OUTPUT);
2019-05-14 18:28:32 +11:00
pinMode(RELAIS_VENTILO, OUTPUT);
Serial.print("M booting\n");
2019-05-23 00:48:21 +11:00
for (foo=0; foo<10; foo++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
digitalWrite(LED_BUILTIN, LOW);
delay(50);
}
/* XXX */
/* changing the voltage reference of the ADC
* greatly increase the prcision on the limited
* range of our temperatures.
*/
analogReference(INTERNAL1V1); // Pour Arduino Mega2560
2019-05-14 02:49:21 +11:00
read_config(0, &parametres);
// display_config(&parametres);
2019-04-14 20:33:52 +11:00
Serial.print("M running now\n");
2019-03-28 07:52:16 +11:00
}
2019-02-23 03:16:01 +11:00
/* ================================================== */
2019-04-28 06:44:48 +11:00
/*
* the light captor is on A4 pin
*/
void readlight(void)
{
short value;
delay(100);
value = analogRead(A4);
Serial.print("L ");
Serial.print(value);
Serial.print('\n');
}
/* ================================================== */
short adc_pins[] = { A0, A1, A2, A3 };
2019-04-04 03:38:32 +11:00
#define NB_PASSE 4
2019-02-23 03:16:01 +11:00
/* -------------------------------------------------- */
void updatevalues(short *ptr)
{
2019-04-04 03:38:32 +11:00
short foo, pass;
2019-04-04 10:24:40 +11:00
for (foo=0; foo<NBVAL; foo++) { ptr[foo] = 0; }
2019-02-24 02:39:04 +11:00
digitalWrite(LED_BUILTIN, HIGH);
2019-04-04 03:38:32 +11:00
for (pass=0; pass<NB_PASSE; pass++) {
for (foo=0; foo<NBVAL; foo++) {
ptr[foo] += analogRead(adc_pins[foo]);
2019-04-13 01:10:56 +11:00
delay(50);
2019-04-04 03:38:32 +11:00
}
2019-02-24 02:39:04 +11:00
}
2019-04-04 10:24:40 +11:00
for (foo=0; foo<NBVAL; foo++) { ptr[foo] /= NB_PASSE; }
2019-02-24 02:39:04 +11:00
digitalWrite(LED_BUILTIN, LOW);
}
/* -------------------------------------------------- */
2019-02-23 03:16:01 +11:00
void sendvalues(short *ptr)
{
int foo;
2019-05-23 00:48:21 +11:00
prt("T");
for (foo=0; foo<NBVAL; foo++) {
2019-05-23 00:48:21 +11:00
prt(" ");
prt(ptr[foo]);
}
2019-05-23 00:48:21 +11:00
prt('\n');
}
/* -------------------------------------------------- */
2019-05-24 03:07:31 +11:00
void update_and_send(short *pvalues)
2019-02-23 03:16:01 +11:00
{
2019-05-24 03:07:31 +11:00
updatevalues(pvalues);
sendvalues(pvalues);
2019-04-28 06:44:48 +11:00
2019-02-23 03:16:01 +11:00
}
/* ================================================== */
void loop() {
2019-04-28 06:44:48 +11:00
static int foo = 0;
2019-05-14 02:49:21 +11:00
int key;
2019-05-24 03:07:31 +11:00
short values[NBVAL], temp_interne;
2019-05-14 02:49:21 +11:00
2019-05-24 03:07:31 +11:00
update_and_send(values);
temp_interne = values[1];
if (temp_interne < parametres.temp_mini) {
prtln("M temp is low");
delay(500);
controle_frigo(0);
}
else if (temp_interne > parametres.temp_maxi){
prtln("M temp is hight");
delay(500);
controle_frigo(1);
}
2019-04-28 06:44:48 +11:00
if (foo++ > 5) {
readlight(); foo = 0;
2019-04-30 02:46:14 +11:00
}
2019-05-24 03:07:31 +11:00
2019-05-14 18:28:32 +11:00
/* check for external request */
2019-05-14 02:49:21 +11:00
if (Serial.available()) {
key = Serial.read();
switch (key) {
case 'U':
2019-05-23 00:48:21 +11:00
prtln("M cli request");
2019-05-14 02:49:21 +11:00
phytocli();
break;
case '+': /* allume le frigo */
controle_frigo(1);
break;
case '-': /* eteint le frigo */
controle_frigo(0);
break;
case '1': /* allume le ventilo */
controle_ventilo(1);
break;
case '0': /* eteint le ventilo */
controle_ventilo(0);
break;
2019-05-14 02:49:21 +11:00
default:
2019-05-21 22:52:21 +11:00
prt("M "); prt(key);
prtln(" bad control code");
2019-05-14 02:49:21 +11:00
break;
}
2019-05-23 00:48:21 +11:00
}
2019-05-24 03:07:31 +11:00
2019-05-22 18:50:25 +11:00
delay(parametres.delai);
2019-05-14 18:28:32 +11:00
/* ETERNAL LOOP END HERE */
}
/* -------------------------------------------------- */