Trois petits code d'exemple pour vous lancer :
Code 1 : Code simple pour utiliser le DHT22
// code to use DHT22 ( need DHT library from adafruit )
#include "DHT.h"
#define DHTPIN D3 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(DHTPIN, INPUT_PULLUP);
}
void loop() {
// Wait a few seconds between measurements.
delay(1000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) ) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
}
Pour le câblage utiliser le schémas fournis sur la page de la boutique du capteur dht22 . ( pas besoin de mettre la résistance de pull up, on utilise une résistance de pull up interne à l'esp8266 )
Code 2 : Utilisation du DS1820B
// Need Dallas Temperature library, including the One Wire Library
#include "OneWire.h"
#include "DallasTemperature.h"
const uint8_t DS18B20PIN = D4;
OneWire oneWire(DS18B20PIN);
DallasTemperature ds(&oneWire);
void setup() {
Serial.begin(115200); // définition de l'ouverture du port série
ds.begin(); // sonde activée
pinMode(DS18B20PIN, INPUT_PULLUP);
}
void loop() {
ds.requestTemperatures();
float t = ds.getTempCByIndex(0);
Serial.print(t);
Serial.println( "C");
delay(1000);
}
Pour le câblage c'est assez simple : 3 fils :
Rouge sur 5V
Noir sur GND
Jaune sur Le signal D4 (cf le code )
Code 3 pour le capteur d'humidité qui intègre à la fois la mesure analogique et la mesure tout ou rien
// Simple code for humidity sensor using both analog and digital read pins
const uint8_t analogInPin = A0; // Analog input pin that the potentiometer is attached to
const uint8_t digitalPin = D8;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(digitalPin, INPUT_PULLUP);
}
void loop() {
// read the analog in value:
int16_t readValue = analogRead(analogInPin);
Serial.print("Read = ");
Serial.print(readValue);
// map it to the range of the analog out:
uint8_t outputValue = map(readValue, 0, 1023, 0, 100);
// float outputValue = (readValue / 1023.0 ) * 3.3;
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 1000 before the next loop for the analog-to-digital
// converter to settle after the last reading:
if(digitalRead(digitalPin)) {
Serial.println("High!");
} else {
Serial.println("Low!");
}
delay(1000);
}
Il faut brancher le VCC du capteur sur 3.3V GND sur GND , brancher la pin A0 du capteur sur la pin A0 de l'ESP8266 ( qui est la seule broche avec fonctionnalité analogique de l'esp8266 ) et brancher la broche D0 du capteur sur une autre broche de l'esp8266 ( branché sur D8 dans le code ) .













