diff --git a/code_arduino/serialArduino/serialArduino.ino b/code_arduino/serialArduino/serialArduino.ino index cbf9d24..aa1af87 100644 --- a/code_arduino/serialArduino/serialArduino.ino +++ b/code_arduino/serialArduino/serialArduino.ino @@ -15,7 +15,7 @@ BSD license, all text above must be included in any redistribution ***************************************************************************/ -byte requestBytes[3]; // for incoming serial data +byte requestBytes[3]; // for incoming xBee data byte answerBytes[3]; @@ -23,6 +23,7 @@ byte answerBytes[3]; #include #include #include +#include #define BME_SCK 13 #define BME_MISO 12 @@ -33,10 +34,21 @@ byte answerBytes[3]; //Adafruit_BME280 bme(BME_CS); // hardware SPI Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI -unsigned long delayTime; +//on crée la liaison xBee + +SoftwareSerial xBee(2, 3); + + +int luxPin = A0; +float rawRange = 1024; +float logRange = 5.0; void setup() { - Serial.begin(9600); + xBee.begin(9600); + analogReference(EXTERNAL); + char requestBytes[3]; + //tableau d'octets contenant les données + byte answerBytes[4]; bme.begin(); } @@ -44,60 +56,29 @@ void setup() { void loop() { - if (Serial.available() <= 3 && Serial.available() >= 1 ) { - Serial.readBytes(requestBytes,3); + if (xBee.available() <= 3 && xBee.available() >= 1 ) { + xBee.readBytes(requestBytes,3); 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) + answerBytes[0] = pressureToByte(bme.readPressure()); answerBytes[1] = tempToByte(bme.readTemperature()); answerBytes[2] = humidityToByte(bme.readHumidity()); - - Serial.write(answerBytes,3); - /*Serial.println(answerBytes[0]+845,DEC); - Serial.println(answerBytes[1],DEC); - Serial.println(answerBytes[2],DEC);*/ + answerBytes[3] = luxToByte(getLux()); + + //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(); } } // printValues(); } - -void printValues() { - Serial.print("Temperature = "); - Serial.print(bme.readTemperature()); - Serial.println(" *C"); - - Serial.print("Pressure = "); - - Serial.print(bme.readPressure() / 100.0F); - Serial.println(" hPa"); - - Serial.print("Approx. Altitude = "); - // Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); - Serial.println(" m"); - - Serial.print("Humidity = "); - Serial.print(bme.readHumidity()); - Serial.println(" %"); - - Serial.println(pressureToByte(bme.readPressure())+845); - Serial.println(tempToByte(bme.readTemperature())); - Serial.println(humidityToByte(bme.readHumidity())); - - // Serial.println(sizeof(bme.readPressure())); - // Serial.println(sizeof(int)); - //sizeof(bme.readTemperature()); - //Serial.println(500.9); - //Serial.println(arrondiSuperieur(500.9)); - delay(1000); - - - - Serial.println("*******************************************"); -} - int arrondiSuperieur(float val) { int newVal = 0; @@ -110,9 +91,11 @@ int arrondiSuperieur(float val) return newVal; } -int pressureToByte(float pressure) +//Ces fonctions convertissent chaque donnée en un seul octet + +byte pressureToByte(float pressure) { - return (int)(arrondiSuperieur(pressure/100.0)-845); + return (byte)(arrondiSuperieur(pressure/100.0)-845); } byte tempToByte(float temp) @@ -125,4 +108,21 @@ byte humidityToByte(float humidity) return (byte)arrondiSuperieur(humidity); } +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; +} -- libgit2 0.21.2