
Grâce à la carte Arduino et le module ESP8266 connectés à l’Internet des objets (IoT: Internet of Things), il est possible de suivre en temps réel la température et l’humidité mesurées par le capteur DTH11.
Pour créer des applications d’enregistrement de données capteurs, on peut utiliser ThingSpeak qui est une API et une application open source pour l’Internet des objets, permettant de stocker et de collecter les données des objets connectés.
Pour réaliser le montage
Pour le module ESP8266, on connecte:
Pour la capteur DHT11, on connecte:
#include <Adafruit_ESP8266.h>
#include <SoftwareSerial.h>
#define ESP_RX 3
#define ESP_TX 4
#define ESP_RST 8
SoftwareSerial softser(ESP_RX, ESP_TX);
// Must declare output stream before Adafruit_ESP8266 constructor; can be
// a SoftwareSerial stream, or Serial/Serial1/etc. for UART.
Adafruit_ESP8266 wifi(&softser, &Serial, ESP_RST);
// Must call begin() on the stream(s) before using Adafruit_ESP8266 object.
#define ESP_SSID “HUAWEI Y5 2019” // Your network name here
#define ESP_PASS “b582058c4d86” // Your network password here
String API = “KM7MYIGX8G2X6GA0”; // CHANGE ME
#define HOST “api.thingspeak.com” // Find/Google your email provider’s SMTP outgoing server name for unencrypted email
#define PORT 80 // Find/Google your email provider’s SMTP outgoing port for unencrypted email
String field = “field1”;
int count = 0; // we’ll use this int to keep track of which command we need to send next
bool send_flag = false; // we’ll use this flag to know when to send the email commands
#include <dht11.h>
#define DHT11PIN 5 // broche DATA -> broche 4
dht11 DHT11;
void setup() {
char buffer[50];
// This might work with other firmware versions (no guarantees)
// by providing a string to ID the tail end of the boot message:
// comment/replace this if you are using something other than v 0.9.2.4!
wifi.setBootMarker(F(“Version:0.9.2.4]\r\n\r\nready”));
softser.begin(9600); // Soft serial connection to ESP8266
Serial.begin(57600); while(!Serial); // UART serial debug
// Test if module is ready
Serial.print(F(“Hard reset…”));
if(!wifi.hardReset()) {
Serial.println(F(“no response from module.”));
for(;;);
}
Serial.println(F(“OK.”));
Serial.print(F(“Soft reset…”));
if(!wifi.softReset()) {
Serial.println(F(“no response from module.”));
for(;;);
}
Serial.println(F(“OK.”));
Serial.print(F(“Checking firmware version…”));
wifi.println(F(“AT+GMR”));
if(wifi.readLine(buffer, sizeof(buffer))) {
Serial.println(buffer);
wifi.find(); // Discard the ‘OK’ that follows
} else {
Serial.println(F(“error”));
}
Serial.print(F(“Connecting to WiFi…”));
if(wifi.connectToAP(F(ESP_SSID), F(ESP_PASS))) {
// IP addr check isn’t part of library yet, but
// we can manually request and place in a string.
Serial.print(F(“OK\nChecking IP addr…”));
wifi.println(F(“AT+CIFSR”));
if(wifi.readLine(buffer, sizeof(buffer))) {
Serial.println(buffer);
wifi.find(); // Discard the ‘OK’ that follows
Serial.print(F(“Connecting to host…”));
Serial.print(“Connected..”);
wifi.println(“AT+CIPMUX=0”); // configure for single connection,
//we should only be connected to one SMTP server
wifi.find();
wifi.closeTCP(); // close any open TCP connections
wifi.find();
Serial.println(“Type \”send it\” to send an email”);
} else { // IP addr check failed
Serial.println(F(“error”));
}
} else { // WiFi connection failed
Serial.println(F(“FAIL”));
}
}
void loop() {
send_flag = true;
if(send_flag){ // the send_flat is set, this means we are or need to start sending SMTP commands
if(do_next()){ // execute the next command
count++; // increment the count so that the next command will be executed next time.
}
}
}
boolean do_next()
{
switch(count){
case 0:
Serial.println(“Connecting…”);
return wifi.connectTCP(F(HOST), PORT);
break;
case 1:
DHT11.read(DHT11PIN);
char charVal2[100];
char charVal1[100]=”GET /update?api_key=KM7MYIGX8G2X6GA0&field1=”;
//itoa(sensor, charVal2, 10);
dtostrf((float)DHT11.temperature, 4, 2, charVal2);
strcat(charVal1,charVal2);
strcat(charVal1,”&field2=”);
dtostrf((float)DHT11.humidity, 4, 2, charVal2);
strcat(charVal1,charVal2);
return wifi.cipSend(charVal1); // Envoi des données au site Thinkspeak
delay(60000);
}
}