Blame view

code_arduino/serialArduino/serialArduino.ino 3.17 KB
a4d85595   aknockae   Ajout de serialAr...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /***************************************************************************
    This is a library for the BME280 humidity, temperature & pressure sensor
  
    Designed specifically to work with the Adafruit BME280 Breakout
    ----> http://www.adafruit.com/products/2650
  
    These sensors use I2C or SPI to communicate, 2 or 4 pins are required
    to interface. The device's I2C address is either 0x76 or 0x77.
  
    Adafruit invests time and resources providing this open source code,
    please support Adafruit andopen-source hardware by purchasing products
    from Adafruit!
  
    Written by Limor Fried & Kevin Townsend for Adafruit Industries.
    BSD license, all text above must be included in any redistribution
   ***************************************************************************/
  
664f7f67   aknockae   programme coté ca...
18
  byte requestBytes[3];   // for incoming xBee data
a4d85595   aknockae   Ajout de serialAr...
19
20
21
22
23
24
25
  byte answerBytes[3];
  
  
  #include <Wire.h>
  #include <SPI.h>
  #include <Adafruit_Sensor.h>
  #include <Adafruit_BME280.h>
664f7f67   aknockae   programme coté ca...
26
  #include <SoftwareSerial.h>
a4d85595   aknockae   Ajout de serialAr...
27
28
29
30
31
32
33
34
35
36
  
  #define BME_SCK 13
  #define BME_MISO 12
  #define BME_MOSI 11
  #define BME_CS 10
  
  //Adafruit_BME280 bme; // I2C
  //Adafruit_BME280 bme(BME_CS); // hardware SPI
  Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
  
664f7f67   aknockae   programme coté ca...
37
38
39
40
41
42
43
44
  //on crée la liaison xBee
  
  SoftwareSerial xBee(2, 3);
  
  
  int luxPin = A0;
  float rawRange = 1024;
  float logRange = 5.0; 
a4d85595   aknockae   Ajout de serialAr...
45
46
  
  void setup() {
664f7f67   aknockae   programme coté ca...
47
48
49
50
51
      xBee.begin(9600);
      analogReference(EXTERNAL);
      char requestBytes[3];
      //tableau d'octets contenant les données
      byte answerBytes[4];
a4d85595   aknockae   Ajout de serialAr...
52
53
54
55
56
57
58
      bme.begin();
  }
  
  
  void loop() { 
  
  
664f7f67   aknockae   programme coté ca...
59
60
      if (xBee.available() <= 3 && xBee.available() >= 1 ) {
        xBee.readBytes(requestBytes,3);
a4d85595   aknockae   Ajout de serialAr...
61
62
63
        if(requestBytes[0] == 'G' && requestBytes[1] == 'E' && requestBytes[2] == 'T') //Si 3 octets correspondant à G,E et T sont envoyé on envoie les données météo
        {
          //on charge les donnée dans la chaîne de bytes d'envoi (modélisé par un tableau)
664f7f67   aknockae   programme coté ca...
64
          
a4d85595   aknockae   Ajout de serialAr...
65
66
67
          answerBytes[0] = pressureToByte(bme.readPressure());
          answerBytes[1] = tempToByte(bme.readTemperature());
          answerBytes[2] = humidityToByte(bme.readHumidity());
664f7f67   aknockae   programme coté ca...
68
69
          answerBytes[3] = luxToByte(getLux());
  
a4d85595   aknockae   Ajout de serialAr...
70
  
664f7f67   aknockae   programme coté ca...
71
72
73
74
75
76
          //On envoie le buffer constituée des 4 donnée
          xBee.write(answerBytes,4);
  
          //on vide le buffer série pour éviter un décalage des requêtes
          while(xBee.available() > 0)
              xBee.read();
a4d85595   aknockae   Ajout de serialAr...
77
78
79
80
81
        }
      }
     // printValues();
  }
  
a4d85595   aknockae   Ajout de serialAr...
82
83
84
85
86
87
88
89
90
91
92
93
  int arrondiSuperieur(float val)
  {
    int newVal = 0;
    //arrondi supérieur
    if(val-(int)val >= 0.5)
      newVal = (int)val+1;
    else
      newVal = (int)val;
  
    return newVal;
  }
  
664f7f67   aknockae   programme coté ca...
94
95
96
  //Ces fonctions convertissent chaque donnée en un seul octet
  
  byte pressureToByte(float pressure)
a4d85595   aknockae   Ajout de serialAr...
97
  {
664f7f67   aknockae   programme coté ca...
98
    return (byte)(arrondiSuperieur(pressure/100.0)-845);
a4d85595   aknockae   Ajout de serialAr...
99
100
101
102
103
104
105
106
107
108
109
110
  }
  
  byte tempToByte(float temp)
  {
    return (byte)arrondiSuperieur(temp);
  }
  
  byte humidityToByte(float humidity)
  {
    return (byte)arrondiSuperieur(humidity);
  }
  
664f7f67   aknockae   programme coté ca...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  byte luxToByte(float lux)
  {
    return (byte)(lux*255/10000);
  }
  
  //https://cdn-learn.adafruit.com/downloads/pdf/adafruit-ga1a12s202-log-scale-analog-light-sensor.pdf
  float getLux()
  {
    int raw = analogRead(luxPin); 
    float logLux = raw * logRange / rawRange;
    float res = pow(10, logLux);
    if(res > 10000.0)
    {
      res = 10000.0;
    }
    return res;
  }
a4d85595   aknockae   Ajout de serialAr...