// Pour utiliser le controleur multiwii, mettre duemilanove #include "DHT.h" #define DHTPIN 9 // Digital pin connected to the DHT sensor #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); for (int pin=A0; pin < A6; pin++) pinMode(pin,INPUT); // analogReference(DEFAULT); dht.begin(); } const float VOLT = 5; static bool debug = false; float readvolt(int pin) { if (debug){ Serial.print("pin="); Serial.print(pin); } const float VPS = VOLT / 1024.0; //volts per step unsigned val = analogRead(pin); if (debug){ Serial.print(" val="); Serial.print(val); } float volt = val * VPS; if (debug){ Serial.print (" volt="); Serial.println(volt); } return volt; } float readlum(int pin) { float myfloat = readvolt(pin); return myfloat; } float readhumid(int pin, float &temp) { float h = dht.readHumidity(); temp = dht.readTemperature(); #if 0 float far = dht.readTemperature(true); Serial.print ("Farenheight="); Serial.println(far); #endif return h; } float readtemp(int pin) { float myfloat = readvolt(pin); //Serial.print (" myfloat="); //Serial.println(myfloat); float res = (VOLT/myfloat-1)*12000; const double A= 1.279336835E-3; const double B= 2.045395490E-4; const double C= 2.537322864E-7; float temp = 1.0/(A+B*log(res)+C*powf(log(res),3))-273.15; //printf ("log %lf\n",3600*log(res)); //debug_printf("Values: HEX 0x%02x DEC %d reading %4.3f volts. resistance %4.3f ohms temp %4.3f\n", val, val, myfloat,res,temp); return temp; } void loop() { static unsigned totals[6]={0,0,0,0,0,0}; for (unsigned pin=0; pin<6; pin++){ unsigned val = analogRead(pin+A0); totals[pin] = (totals[pin]+val)/2; } #if 0 unsigned long now = micros(); static unsigned long last; if (now - last > 1000000) { Serial.println ("hello world"); last = now; } #endif if (Serial.available() > 0) { static char line[20]; // read the incoming byte: char byte = Serial.read(); if (byte == '\n'){ //Serial.print("I received: "); //Serial.println(line); bool is_read = strncmp(line,"read",4)==0; debug = strncmp(line,"READ",4)==0; if (is_read || debug){ int pos = 0; const char *pt = line+4; float dhttemp=0; bool dhttemp_filled = false; while (*pt != '\0'){ if (debug){ Serial.print("total="); Serial.print(totals[pos]); Serial.print(" "); } int pin = pos + A0; switch(*pt){ case 'T': { float temp; if (dhttemp_filled){ dhttemp_filled = false; temp = dhttemp; pos--; }else{ temp = readtemp (pin); //Serial.print ("pin="); //Serial.println(pin); } Serial.print ("temp="); Serial.println(temp); } break; case 'V': { float volts = readvolt (pin); Serial.print ("volt="); Serial.println(volts); } break; case 'L': { float lum = readlum (pin); Serial.print ("lum="); Serial.println(lum); } break; case 'H': { float humid = readhumid (pin,dhttemp); dhttemp_filled = true; Serial.print ("humid="); Serial.println(humid); pos--; // Annule le pos++ plus loin parce que le DHT n'utilise pas d'entrée analogue //Serial.print ("dhttemp="); //Serial.println(dhttemp); } break; case '_': Serial.println("skip"); break; default: Serial.print ("Invalid letter: "); Serial.println (*pt); } pt++; pos++; } Serial.println("fin"); debug = false; }else{ Serial.println ("Commande invalide"); } line[0] = '\0'; }else{ size_t len = strlen(line); if (len < sizeof(line)-1){ line[len] = byte; line[len+1] = '\0'; } } } }