Commit 664f7f67f90dbf746e282c3fcdf6f5d2929cfac1
1 parent
fb352dd1
programme coté capteur terminé avec implémentation du capteur de luminosité
Showing
1 changed file
with
47 additions
and
47 deletions
Show diff stats
code_arduino/serialArduino/serialArduino.ino
... | ... | @@ -15,7 +15,7 @@ |
15 | 15 | BSD license, all text above must be included in any redistribution |
16 | 16 | ***************************************************************************/ |
17 | 17 | |
18 | -byte requestBytes[3]; // for incoming serial data | |
18 | +byte requestBytes[3]; // for incoming xBee data | |
19 | 19 | byte answerBytes[3]; |
20 | 20 | |
21 | 21 | |
... | ... | @@ -23,6 +23,7 @@ byte answerBytes[3]; |
23 | 23 | #include <SPI.h> |
24 | 24 | #include <Adafruit_Sensor.h> |
25 | 25 | #include <Adafruit_BME280.h> |
26 | +#include <SoftwareSerial.h> | |
26 | 27 | |
27 | 28 | #define BME_SCK 13 |
28 | 29 | #define BME_MISO 12 |
... | ... | @@ -33,10 +34,21 @@ byte answerBytes[3]; |
33 | 34 | //Adafruit_BME280 bme(BME_CS); // hardware SPI |
34 | 35 | Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI |
35 | 36 | |
36 | -unsigned long delayTime; | |
37 | +//on crée la liaison xBee | |
38 | + | |
39 | +SoftwareSerial xBee(2, 3); | |
40 | + | |
41 | + | |
42 | +int luxPin = A0; | |
43 | +float rawRange = 1024; | |
44 | +float logRange = 5.0; | |
37 | 45 | |
38 | 46 | void setup() { |
39 | - Serial.begin(9600); | |
47 | + xBee.begin(9600); | |
48 | + analogReference(EXTERNAL); | |
49 | + char requestBytes[3]; | |
50 | + //tableau d'octets contenant les données | |
51 | + byte answerBytes[4]; | |
40 | 52 | bme.begin(); |
41 | 53 | } |
42 | 54 | |
... | ... | @@ -44,60 +56,29 @@ void setup() { |
44 | 56 | void loop() { |
45 | 57 | |
46 | 58 | |
47 | - if (Serial.available() <= 3 && Serial.available() >= 1 ) { | |
48 | - Serial.readBytes(requestBytes,3); | |
59 | + if (xBee.available() <= 3 && xBee.available() >= 1 ) { | |
60 | + xBee.readBytes(requestBytes,3); | |
49 | 61 | 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 |
50 | 62 | { |
51 | 63 | //on charge les donnée dans la chaîne de bytes d'envoi (modélisé par un tableau) |
64 | + | |
52 | 65 | answerBytes[0] = pressureToByte(bme.readPressure()); |
53 | 66 | answerBytes[1] = tempToByte(bme.readTemperature()); |
54 | 67 | answerBytes[2] = humidityToByte(bme.readHumidity()); |
55 | - | |
56 | - Serial.write(answerBytes,3); | |
57 | - /*Serial.println(answerBytes[0]+845,DEC); | |
58 | - Serial.println(answerBytes[1],DEC); | |
59 | - Serial.println(answerBytes[2],DEC);*/ | |
68 | + answerBytes[3] = luxToByte(getLux()); | |
69 | + | |
60 | 70 | |
71 | + //On envoie le buffer constituée des 4 donnée | |
72 | + xBee.write(answerBytes,4); | |
73 | + | |
74 | + //on vide le buffer série pour éviter un décalage des requêtes | |
75 | + while(xBee.available() > 0) | |
76 | + xBee.read(); | |
61 | 77 | } |
62 | 78 | } |
63 | 79 | // printValues(); |
64 | 80 | } |
65 | 81 | |
66 | - | |
67 | -void printValues() { | |
68 | - Serial.print("Temperature = "); | |
69 | - Serial.print(bme.readTemperature()); | |
70 | - Serial.println(" *C"); | |
71 | - | |
72 | - Serial.print("Pressure = "); | |
73 | - | |
74 | - Serial.print(bme.readPressure() / 100.0F); | |
75 | - Serial.println(" hPa"); | |
76 | - | |
77 | - Serial.print("Approx. Altitude = "); | |
78 | - // Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); | |
79 | - Serial.println(" m"); | |
80 | - | |
81 | - Serial.print("Humidity = "); | |
82 | - Serial.print(bme.readHumidity()); | |
83 | - Serial.println(" %"); | |
84 | - | |
85 | - Serial.println(pressureToByte(bme.readPressure())+845); | |
86 | - Serial.println(tempToByte(bme.readTemperature())); | |
87 | - Serial.println(humidityToByte(bme.readHumidity())); | |
88 | - | |
89 | - // Serial.println(sizeof(bme.readPressure())); | |
90 | - // Serial.println(sizeof(int)); | |
91 | - //sizeof(bme.readTemperature()); | |
92 | - //Serial.println(500.9); | |
93 | - //Serial.println(arrondiSuperieur(500.9)); | |
94 | - delay(1000); | |
95 | - | |
96 | - | |
97 | - | |
98 | - Serial.println("*******************************************"); | |
99 | -} | |
100 | - | |
101 | 82 | int arrondiSuperieur(float val) |
102 | 83 | { |
103 | 84 | int newVal = 0; |
... | ... | @@ -110,9 +91,11 @@ int arrondiSuperieur(float val) |
110 | 91 | return newVal; |
111 | 92 | } |
112 | 93 | |
113 | -int pressureToByte(float pressure) | |
94 | +//Ces fonctions convertissent chaque donnée en un seul octet | |
95 | + | |
96 | +byte pressureToByte(float pressure) | |
114 | 97 | { |
115 | - return (int)(arrondiSuperieur(pressure/100.0)-845); | |
98 | + return (byte)(arrondiSuperieur(pressure/100.0)-845); | |
116 | 99 | } |
117 | 100 | |
118 | 101 | byte tempToByte(float temp) |
... | ... | @@ -125,4 +108,21 @@ byte humidityToByte(float humidity) |
125 | 108 | return (byte)arrondiSuperieur(humidity); |
126 | 109 | } |
127 | 110 | |
111 | +byte luxToByte(float lux) | |
112 | +{ | |
113 | + return (byte)(lux*255/10000); | |
114 | +} | |
115 | + | |
116 | +//https://cdn-learn.adafruit.com/downloads/pdf/adafruit-ga1a12s202-log-scale-analog-light-sensor.pdf | |
117 | +float getLux() | |
118 | +{ | |
119 | + int raw = analogRead(luxPin); | |
120 | + float logLux = raw * logRange / rawRange; | |
121 | + float res = pow(10, logLux); | |
122 | + if(res > 10000.0) | |
123 | + { | |
124 | + res = 10000.0; | |
125 | + } | |
126 | + return res; | |
127 | +} | |
128 | 128 | ... | ... |