// Mesure des gaz
#include <Wire.h>
#include <dht.h>
#include <SPI.h>
#include <SD.h>
File myFile;
#define DHT11PIN 8
#define pwmPin 7
dht1wire DHT(DHT11PIN, dht::DHT11);
String data ="";
void setup()
{
// Initialize the Bridge and the Serial
Serial.begin(9600);
pinMode(pwmPin, INPUT);
if (!SD.begin(4)) {
// Serial.println("initialization failed!");
return;
}
//Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
}
void loop()
{
//float pinA0 = analogRead(A0)*24.44+400;
//int co2= pinA0;
float pinA3 = analogRead(A3);
int co= pinA3;
data = co;
unsigned long th = pulseIn(pwmPin, HIGH, 3000000); // use microseconds
unsigned long tl = pulseIn(pwmPin, LOW, 3000000);
//tpwm = th + tl; // actual pulse width
//Serial.println("PWM-time: ");
//Serial.println(th);
//Serial.println(tl);
//Serial.println(" us");
unsigned long co3 = 5000*(th-2000)/(th+tl-4000);
//Serial.println(co3);
data += ",";
data += co3;
//Serial.println(co2);
//Serial.println(co);
//analogWrite(A3, HIGH);
// delay(50); // Getting an analog read apparently takes 100uSec
//pinA3 = analogRead(A3);
//Serial.println(pinA3);
unsigned long b = micros();
dht::ReadStatus chk = DHT.read();
unsigned long e = micros();
Serial.print(F("\nRead sensor: "));
switch (chk)
{
case dht::OK:
Serial.print(F("OK, took "));
Serial.print (e - b); Serial.println(F(" usec"));
break;
case dht::ERROR_CHECKSUM:
Serial.println(F("Checksum error"));
break;
case dht::ERROR_TIMEOUT:
Serial.println(F("Timeout error"));
break;
case dht::ERROR_CONNECT:
Serial.println(F("Connect error"));
break;
case dht::ERROR_ACK_L:
Serial.println(F("AckL error"));
break;
case dht::ERROR_ACK_H:
Serial.println(F("AckH error"));
break;
default:
Serial.println(F("Unknown error"));
break;
}
//Serial.print(F("Humidity (%): "));
int humi = DHT.getHumidity();
//Serial.println(humi);
//Serial.print(F("Temperature (°C): "));
int temp = DHT.getTemperature();
data += ",";
data += humi/10;
data += ",";
data += temp/10;
//Serial.println(temp);
//Serial.print(F("Dew Point (°C): "));
//Serial.println(DHT.dewPoint());
Serial.println(data);
// nothing happens after setup
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
//Serial.print("Writing to test.txt...");
myFile.println(data);
// close the file:
myFile.close();
//Serial.println("done.");
}
//mesure toutes les 5 minutes
delay(300000);
data="";
}