Étape 3: Téléchargez le Code sur l’Arduino
/ * Inspire en grande partie de code par pression, altitude et la température de la BMP085 Jim Lindblom Get. Serial.Print essayez-le à 9600 bauds à serial monitor. * / #include < dht11.h > < Wire.h > de #include #include < LiquidCrystal_I2C.h > LiquidCrystal_I2C lcd(0x27,16,2) ; dht11 DHT11 ; #define DHT11PIN 2 #define BMP085_ADDRESS 0x77 / / adresse I2C de BMP085 const unsigned char OSS = 0 ; Suréchantillonnage réglage / / étalonnage valeurs int ac1 ; ac2 int ; int ac3 ; unsigned int ac4 ; unsigned int ac5 ; unsigned int ac6 ; int b1 ; int b2 ; int Mo ; int mc ; int md ; B5 est calculé en bmp085GetTemperature(...), cette variable est également utilisée dans bmp085GetPressure(...) / / so... Temperature(...) doit être appelée avant... Pressure(...). b5 long ; void setup() {Serial.begin(9600) ; Wire.Begin() ; LCD.init() ; initialiser le lcd.backlight() lcd ; bmp085Calibration() ; } void loop() {float température = bmp085GetTemperature(bmp085ReadUT()) ; //MUST être appelée première pression flotteur = bmp085GetPressure(bmp085ReadUP()) ; float atm = pression / 101325; / / « atmosphère type » float altitude = calcAltitude(pressure) ; //Uncompensated caculation - en mètres Serial.println("\n") ; int chk = DHT11.read(DHT11PIN) ; Serial.Print ("capteur de lecture:") ; commutateur (chk) {case 0: Serial.println("OK") ; break ; case -1 : Serial.println (« Checksum error"); break ; case -2 : Serial.println ("erreur de délai d’expiration"); break ; default : Serial.println ("erreur inconnue"); break;} Serial.Print ("humidité (%):") ; Serial.println ((float) DHT11.humidity, 2) ; Serial.Print ("température (° c):") ; Serial.println ((float) DHT11.temperature, 2) ; LCD.Print("Hum(%):") ; LCD.Print ((float) DHT11.humidity, 2) ; lcd.setCursor(0,1) ; LCD.Print("Temp(C):") ; LCD.Print ((float) DHT11.temperature, 2) ; Delay(2000) ; LCD.Clear() ; Serial.Print ("température (de):") ; Serial.println(Fahrenheit(DHT11.Temperature), 2) ; Serial.Print ("température (K):") ; Serial.println(Kelvin(DHT11.Temperature), 2) ; LCD.Print("Temp(F):") ; LCD.Print(Fahrenheit(DHT11.Temperature), 2) ; lcd.setCursor(0,1) ; LCD.Print("Temp(K):") ; LCD.Print(Kelvin(DHT11.Temperature), 2) ; Delay(3000) ; LCD.Clear() ; Serial.Print ("Point de rosée (° c):") ; Serial.println (point de rosée (DHT11.temperature, DHT11.humidity)) ; Serial.Print (« rosée PointFast (oC): ") ; Serial.println (dewPointFast (DHT11.temperature, DHT11.humidity)) ; LCD.Print("DewP(C):") ; LCD.Print (point de rosée (DHT11.temperature, DHT11.humidity)) ; lcd.setCursor(0,1) ; LCD.Print("DewPF(C):") ; LCD.Print (dewPointFast (DHT11.temperature, DHT11.humidity)) ; Delay(3000) ; LCD.Clear() ; Serial.Print ("Temperature2:") ; Serial.Print (température, 2) ; afficher 2 décimales Serial.println ("deg C") ; Serial.Print ("pression:") ; Serial.Print (pression, 0) ; nombre entier uniquement. Serial.println (« Pa ») ; LCD.Print ("Temp2:") ; LCD.Print (température, 2) ; afficher 2 décimales lcd.print ("C") ; lcd.setCursor(0,1) ; LCD.Print ("Pres:") ; LCD.Print (pression, 0) ; LCD.Print (« PA ») ; Delay(3000) ; LCD.Clear() ; Serial.Print ("atmosphère Standard:") ; Serial.println (atm, 4) ; afficher 4 décimales Serial.print (« Altitude: ") ; Serial.Print (altitude, 2) ; afficher 2 décimales Serial.println ("M") ; LCD.Print ("Std Atm:") ; LCD.Print (atm, 4) ; afficher 2 décimales lcd.setCursor(0,1) ; LCD.Print ("Alt:") ; LCD.Print (altitude, 2) ; LCD.Print ("M") ; Delay(3000) ; LCD.Clear() ; Serial.println (); delay(100) de //line de pause ; Attendez une seconde et obtenir à nouveau des valeurs
. } / *---(Déclarer des fonctions écrites par l’utilisateur)---* / / / //Celsius à Fahrenheit conversion double (double celsius Fahrenheit) {retour 1,8 * celsius + 32;} //Celsius à la conversion de Kelvin double Kelvin (double celsius) {return celsius + 273.15;} / / point de rosée fonction NOAA / / référence : http://wahiduddin.net/calc/density_algorithms.htm double point de rosée (double celsius, double humidité) {double A0 = 373,15 / (273,15 + celsius); double somme =-7.90298 * (A0-1) ; SUM += 5.02808 * log10(A0) ; SOMME +=-1.3816e-7 * (pow (10, (11.344*(1-1/A0)))-1) ; SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; SUM += log10(1013.246) ; double VP = pow (10, SUM-3) * humidité ; double T = log(VP/0.61078) ; retour de temp var (241.88 * T) / (17,558-T) ; } / / delta max = 0.6544 wrt dewPoint() / / 5 x plus rapide que dewPoint() / / Référence : http://en.wikipedia.org/wiki/Dew_point double dewPointFast (double celsius, double humidité) {double un = 17.271 ; double b = 237.7 ; double temp = (une * celsius) / (b + c) + log(humidity/100) ; double Td = (b * temp) / (a - temp); retour Td;} / / stocke toutes les valeurs d’étalonnage de la bmp085 dans les variables globales / / valeurs d’étalonnage sont requis pour calculer la température et pression / / cette fonction doit être appelée au début de la bmp085Calibration() Sub programme {ac1 = bmp085ReadInt(0xAA) ; ac2 = bmp085ReadInt(0xAC) ; AC3 = bmp085ReadInt(0xAE) ; AC4 = bmp085ReadInt(0xB0) ; AC5 = bmp085ReadInt(0xB2) ; AC6 = bmp085ReadInt(0xB4) ; B1 = bmp085ReadInt(0xB6) ; B2 = bmp085ReadInt(0xB8) ; Mo = bmp085ReadInt(0xBA) ; MC = bmp085ReadInt(0xBC) ; MD = bmp085ReadInt(0xBE) ; } / / Calculer la température en ºC flotteur bmp085GetTemperature (unsigned int ut) {long x1, x 2; x1 = ((ut (long) - (long)ac6)*(long)ac5) >> 15 ; x2 = (mc (long) << 11) / (x 1 + md); b5 = x1 + x2 ; float temp = ((b5 + 8) >> 4); temp = temp 10 ; return temp;} / / calculer pression abandonnée / / valeurs d’étalonnage doivent être connues / / b5 est également requis si bmp085GetTemperature(...) doit être appelée en premier. Valeur retournée sera pression en unités de PA long bmp085GetPressure (unsigned long jusqu'à) {long x1, x2, x 3, b3, b6, p; unsigned long b4, b7 ; b6 = b5 - 4000; / / calculer B3 x1 = (b2 * (b6 * b6) >> 12) >> 11 ; x2 = (ac2 * b6) >> 11 ; x3 = x1 + x2 ; b3 = (((long) ac1) * 4 + x3) <> 2; / / calculer B4 x1 = (ac3 * b6) >> 13 ; x2 = (b1 * ((b6 * b6) >> 12)) >> 16 ; x3 = ((x1 + x2) + 2) > > 2 ; B4 = (ac4 * (unsigned long) (x3 + 32768)) >> 15 ; B7 = ((unsigned long) (up - b3) * (50000 >> OSS)) ; Si (b7 < 0 x 80000000) p = (b7 << 1) / b4 ; d’autre p = (b7/b4) << 1 ; x1 = (p >> 8) * (p >> 8) ; x1 = (x 1 * 3038) >> 16 ; x2 = (-7357 * p) >> 16 ; p = (x1 + x2 + 3791) >> 4 ; long temp = p ; Return temp ; } / / Lecture 1 octet de la BMP085 à « adresse » char bmp085Read (unsigned adresse char) {unsigned données char ; Wire.beginTransmission(BMP085_ADDRESS) ; Wire.Write(Address) ; Wire.endTransmission() ; Wire.requestFrom (BMP085_ADDRESS, 1) ; while (!. Wire.available()) ; Return Wire.read() ; } / / Lecture de 2 octets de la BMP085 / / premier octet sera de « adresse » / / deuxième octet sera de « adresse » + 1 int bmp085ReadInt (unsigned adresse char) {unsigned char msb, lsb ; Wire.beginTransmission(BMP085_ADDRESS) ; Wire.Write(Address) ; Wire.endTransmission() ; Wire.requestFrom (BMP085_ADDRESS, 2) ; while(Wire.available() < 2) ; MSB = Wire.read() ; LSB = Wire.read() ; Return (int) msb << 8 | LSB ; } / / Lire la valeur de la température sans compensation unsigned int bmp085ReadUT() {unsigned int ut; / / écrire 0x2E Registre 0xF4 / / cela demande une température de Wire.beginTransmission(BMP085_ADDRESS) ; Wire.Write(0xF4) ; Wire.Write(0x2E) ; Wire.endTransmission() ; Attendez au moins 4.5ms delay(5) ; Lire les deux octets de registres 0xF6 et 0xF7 ut = bmp085ReadInt(0xF6) ; retour ut ; } / / Lire la valeur de la pression sans compensation unsigned long bmp085ReadUP() {unsigned char msb, lsb, xlsb ; unsigned long haut = 0; / / écrire 0 x 34 + (OSS << 6) en Registre 0xF4 / / demande une pression w / suréchantillonnage définition Wire.beginTransmission(BMP085_ADDRESS) ; Wire.Write(0xF4) ; Wire.Write (0 x 34 + (OSS << 6)) ; Wire.endTransmission() ; Attendre pour la conversion, retard dépend du temps sur le retard de l’OSS (2 + (3 <> (8-OSS); return place;} Sub writeRegister (int deviceAddress, adresse de byte, byte val) {Wire.beginTransmission(deviceAddress); / / début de transmission au dispositif Wire.write(address); / / envoi s’inscrire l’adresse Wire.write(val); / / envoyer la valeur à écrire Wire.endTransmission(); / / fin de transmission} int readRegister (int deviceAddress, adresse octets) {int v ; Wire.beginTransmission(deviceAddress) ; Wire.Write(Address) ; s’inscrire pour lire Wire.endTransmission() ; Wire.requestFrom (deviceAddress, 1) ; lire un octet tandis que (!) Wire.available()) {/ / attente} v = Wire.read() ; Retour à v ; } float calcAltitude (pression flotteur) {float A = pression/101325 ; float B = 1/5.25588 ; float C = pow(A,B) ; C = 1 - C ; C = C /0.0000225577 ; retour de C ; }