Projet

Général

Profil

Codage_Arduino.txt

Code necessaire pour faire fonctionner les 2 capteurs Humidité/temperature et Co2 en simultané - Luc Hanneuse, 11/09/2019 15:41

 
1
vincent.vandeputte.60@gmail.com
2

    
3
/*This test code is write for Arduino AVR Series(UNO, Leonardo, Mega)
4
  If you want to use with LinkIt ONE, please connect the module to D0/1 and modify:
5

    
6
  // #include <SoftwareSerial.h>
7
  // SoftwareSerial s_serial(2, 3);      // TX, RX
8

    
9
  #define sensor Serial1
10
*/
11

    
12

    
13
#include <SoftwareSerial.h>
14
SoftwareSerial s_serial(2, 3);      // TX, RX
15

    
16
#define sensor s_serial
17

    
18
const unsigned char cmd_get_sensor[] =
19
{
20
    0xff, 0x01, 0x86, 0x00, 0x00,
21
    0x00, 0x00, 0x00, 0x79
22
};
23

    
24
unsigned char dataRevice[9];
25
int temperature;
26
int CO2PPM;
27

    
28
bool dataRecieve(void)
29
{
30
    byte data[9];
31
    int i = 0;
32

    
33
    //transmit command data
34
    for(i = 0; i < sizeof(cmd_get_sensor); i++)
35
    {
36
        sensor.write(cmd_get_sensor[i]);
37
    }
38
    delay(10);
39
    //begin reveiceing data
40
    if(sensor.available())
41
    {
42
        while(sensor.available())
43
        {
44
            for(int i=0;i<9; i++)
45
            {
46
                data[i] = sensor.read();
47
            }
48
        }
49
    }
50

    
51
    if((i != 9) || (1 + (0xFF ^ (byte)(data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7]))) != data[8])
52
    {
53
        return false;
54
    }
55

    
56
    CO2PPM = (int)data[2] * 256 + (int)data[3];
57
    temperature = (int)data[4] - 40;
58

    
59
    return true;
60
}
61

    
62

    
63
// Example testing sketch for various DHT humidity/temperature sensors
64
// Written by ladyada, public domain
65

    
66
#include "DHT.h"
67

    
68
#define DHTPIN A0     // what pin we're connected to
69

    
70
// Uncomment whatever type you're using!
71
//#define DHTTYPE DHT11   // DHT 11 
72
#define DHTTYPE DHT22   // DHT 22  (AM2302)
73
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
74

    
75
// Connect pin 1 (on the left) of the sensor to +5V
76
// Connect pin 2 of the sensor to whatever your DHTPIN is
77
// Connect pin 4 (on the right) of the sensor to GROUND
78
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
79

    
80
DHT dht(DHTPIN, DHTTYPE);
81

    
82
void setup() 
83
{
84
    Serial.begin(115200); 
85
    Serial.println("DHTxx test!");
86

    
87
    dht.begin();
88

    
89
    sensor.begin(9600);
90
    Serial.begin(115200);
91
    Serial.println("get a 'g', begin to read from sensor!");
92
    Serial.println("********************************************************");
93
    Serial.println();
94
}
95

    
96
void loop() 
97
{
98
    // Reading temperature or humidity takes about 250 milliseconds!
99
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
100
    float h = dht.readHumidity();
101
    float t = dht.readTemperature();
102

    
103
    // check if returns are valid, if they are NaN (not a number) then something went wrong!
104
    if (isnan(h)) 
105
        Serial.println("Failed to read from DHT");
106
    else 
107
    {
108
      if(dataRecieve())
109
      {
110
          Serial.print("{");Serial.print(t);Serial.print(",");Serial.print(CO2PPM);Serial.print(", ");Serial.print(h);Serial.println("}|");
111
      }
112
      else 
113
        Serial.println("Failed to read from MH-Z16");
114
    }
115
    delay(1000);
116
}
(1-1/8)