serialArduino.ino 3.34 KB
/***************************************************************************
  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
 ***************************************************************************/

byte requestBytes[3];   // for incoming serial data
byte answerBytes[3];


#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#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

unsigned long delayTime;

void setup() {
    Serial.begin(9600);
    bme.begin();
}


void loop() { 


    if (Serial.available() <= 3 && Serial.available() >= 1 ) {
      Serial.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);*/

      }
    }
   // 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;
  //arrondi supérieur
  if(val-(int)val >= 0.5)
    newVal = (int)val+1;
  else
    newVal = (int)val;

  return newVal;
}

int pressureToByte(float pressure)
{
  return (int)(arrondiSuperieur(pressure/100.0)-845);
}

byte tempToByte(float temp)
{
  return (byte)arrondiSuperieur(temp);
}

byte humidityToByte(float humidity)
{
  return (byte)arrondiSuperieur(humidity);
}