0% found this document useful (0 votes)
194 views7 pages

Esp32 Et MQTT

The document describes code for an ESP32 device to measure temperature and humidity with a DHT11 sensor and send the readings to an MQTT broker over WiFi. It includes code to connect to WiFi, connect to the MQTT broker, read the sensor values, publish the readings as JSON payloads to MQTT topics, and display the readings on an OLED screen. A second section provides simplified code without display to measure soil moisture levels and publish to MQTT.

Uploaded by

Llouis Physics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
194 views7 pages

Esp32 Et MQTT

The document describes code for an ESP32 device to measure temperature and humidity with a DHT11 sensor and send the readings to an MQTT broker over WiFi. It includes code to connect to WiFi, connect to the MQTT broker, read the sensor values, publish the readings as JSON payloads to MQTT topics, and display the readings on an OLED screen. A second section provides simplified code without display to measure soil moisture levels and publish to MQTT.

Uploaded by

Llouis Physics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

2019/12/11 00:22 1/7 ESP32 et mqtt

ESP32 et mqtt

Code pour le Heltec ESP32 lora (wifi)

Envoi vers broker mqtt en wifi

==== Sonde de température et d'humidité DHT11 ====

Librairies utilisées

Arduino/hardware/heltec/esp32/libraries/WiFi

Bibliothèques utilisées

DHT_sensor_library_for_ESPx version 1.0.9 dans le dossier:


Arduino/libraries/DHT_sensor_library_for_ESPx
WiFi version 1.0 dans le dossier: Arduino/hardware/heltec/esp32/libraries/WiFi
PubSubClient version 2.6 dans le dossier: Arduino/libraries/PubSubClient
SPI version 1.0 dans le dossier: Arduino/hardware/heltec/esp32/libraries/SPI
Wire version 1.0 dans le dossier: Arduino/hardware/heltec/esp32/libraries/Wire
OLED version 4.0.1 dans le dossier: Arduino/hardware/heltec/esp32/libraries/OLED

|dht11

#include <DHTesp.h>

#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <math.h>
#include <Wire.h>
#include "SSD1306.h"

// Config Heltec 32
//-----------------------------------
// Pin definetion of WIFI LoRa 32
// HelTec AutoMation 2017 [email protected]
// Pin definetion of WIFI LoRa 32
// HelTec AutoMation 2017 [email protected]
#define SCK 5 // GPIO5 -- SX127x's SCK
#define MISO 19 // GPIO19 -- SX127x's MISO
#define MOSI 27 // GPIO27 -- SX127x's MOSI
#define SS 18 // GPIO18 -- SX127x's CS
#define RST 14 // GPIO14 -- SX127x's RESET
#define DI0 26 // GPIO26 -- SX127x's IRQ(Interrupt Request)
#define SDA 4
#define SCL 15
#define RSTOLED 16 //RST must be set by software

Wiki techno du collège Vallis Aeria - https://technovallis.frama.wiki/


Last update: 2018/10/20 00:39 esp_32_et_mqtt https://technovallis.frama.wiki/esp_32_et_mqtt

#define Light 25
#define V2 1

#ifdef V2 //WIFI Kit series V1 not support Vext control


#define Vext 21
#endif

const char* ssid = "xxxx";


const char* password = "xxxxx";
const char* mqttServer = "192.x.x.x";
const int mqttPort = 1883;
const char* mqttUser = "";
const char* mqttPassword = "";

WiFiClient espClient;
PubSubClient client(espClient);

//-----------------------------------
/** Pin number for DHT11 data pin */
int dhtPin = 17;
DHTesp dht;
unsigned int counter = 0;
//-----------------------------------
// Config display
//-----------------------------------
SSD1306 display(0x3c, SDA, SCL, RSTOLED);
//-----------------------------------

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
//conf sensor
dht.setup(dhtPin, DHTesp::DHT11);
//-----------------------------------
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}

Serial.println("Connected to the WiFi network");

client.setServer(mqttServer, mqttPort);

while (!client.connected()) {
Serial.println("Connecting to MQTT...");

if (client.connect("ESP32Client", mqttUser, mqttPassword )) {

Serial.println("connected");

} else {

https://technovallis.frama.wiki/ Printed on 2019/12/11 00:22


2019/12/11 00:22 3/7 ESP32 et mqtt

Serial.print("failed with state ");


Serial.print(client.state());
delay(2000);

}
}

// Condiguration écran OLED


//-----------------------------------
pinMode(16,OUTPUT);
pinMode(25,OUTPUT);

digitalWrite(16, LOW); // set GPIO16 low to reset OLED


delay(1500);
digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in
high

display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
// logo();
delay(1500);
display.clear();
//-----------------------------------
}
//-----------------------------------
void loop() {
client.loop();
// lire les données prend un peu de temps
delay(100);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very
slow sensor)

float temperature= dht.getTemperature();


float humidity= dht.getHumidity();

// Check if any reads failed and exit early (to try again).
if (dht.getStatus() != 0) {
Serial.println("DHT11 error status: " +
String(dht.getStatusString()));
}

Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");

Wiki techno du collège Vallis Aeria - https://technovallis.frama.wiki/


Last update: 2018/10/20 00:39 esp_32_et_mqtt https://technovallis.frama.wiki/esp_32_et_mqtt

// Prepare a JSON payload string

String payload = "";


payload += temperature;
// Send payload
char temp1[100];
payload.toCharArray( temp1, 100 );

//Publication MQTT température


client.publish("esp/temp", temp1);

String payload1 = "";


payload1 += humidity;
// Send payload
char hum1[100];
payload1.toCharArray( hum1, 100 );

//Publication MQTT humidité


client.publish("esp/hum", hum1);

Serial.println("temp1 ? ");
Serial.println( temp1 );
Serial.println("hum1 ? ");
Serial.println( hum1 );
Serial.print("Envoi n°");
Serial.println(counter);
delay(10000);
counter++;

//Affichage écran oled


display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, "Humidité = " + String(humidity));
display.drawString(0, 20, "Température = " + String(temperature));

display.drawString(90, 40, String(counter));


display.display();

Sonde d'humidité d'un milieu sans affichage

|moisture.ino

#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>

https://technovallis.frama.wiki/ Printed on 2019/12/11 00:22


2019/12/11 00:22 5/7 ESP32 et mqtt

#include <math.h>
#include <Wire.h>

// Config Heltec 32
//-----------------------------------
// Pin definetion of WIFI LoRa 32
// HelTec AutoMation 2017 [email protected]
#define SCK 5 // GPIO5 -- SX127x's SCK
#define MISO 19 // GPIO19 -- SX127x's MISO
#define MOSI 27 // GPIO27 -- SX127x's MOSI
#define SS 18 // GPIO18 -- SX127x's CS
#define RST 14 // GPIO14 -- SX127x's RESET
#define DI0 26 // GPIO26 -- SX127x's IRQ(Interrupt Request)

const char* ssid = "SSID"; // SSID du point d'accès wifi


const char* password = "Mot de passe"; // SSID du point d'accès wifi
const char* mqttServer = "192.168.1.x"; //Adresse du Serveur mqtt
const int mqttPort = 1883; //Adresse du Serveur mqtt
const char* mqttUser = ""; //Utilisateur si définit
const char* mqttPassword = ""; //mot de passe si défini

WiFiClient espClient;
PubSubClient client(espClient);

// Config moisture sensor v1.4 -- WORKS W/ 3.3V & 5V


// dry soil = 0, humid soil = 300, wet soil = 700
//-----------------------------------
#define MOISTURE_SENSOR 35 // pin 35
int moisture = 0; // value (between 0 - 700)

unsigned int counter = 0;


//-----------------------------------

void setup() {

Serial.begin(115200);
WiFi.begin(ssid, password);
//conf sensor
pinMode(MOISTURE_SENSOR, INPUT); // read data from sensor

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.println("Connecting to WiFi..");
}

Serial.println("Connected to the WiFi network");

client.setServer(mqttServer, mqttPort);

while (!client.connected()) {
Serial.println("Connecting to MQTT...");

Wiki techno du collège Vallis Aeria - https://technovallis.frama.wiki/


Last update: 2018/10/20 00:39 esp_32_et_mqtt https://technovallis.frama.wiki/esp_32_et_mqtt

if (client.connect("ESP32Client", mqttUser, mqttPassword )) {

Serial.println("connected");

} else {

Serial.print("failed with state ");


Serial.print(client.state());
delay(2000);

}
}

//-----------------------------------
void loop() {
client.loop(); //relance la connexion wifi - intéressant en cas de
coupure d'accès momentané

// read moisture informations


moisture = analogRead(MOISTURE_SENSOR);

// lire les données prend un peu de temps


delay(100);
Serial.println("Lecture : ");
Serial.print(String(moisture));
Serial.println(" d'Humidité");

// Prepare a JSON payload string - Il est important de convertir les


données pour leur envoi.
// String payload = "Champ1=";
// payload += moisture;
String payload = "";
payload += moisture;

// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );

//Publication MQTT
client.publish("esp/test", attributes);

Serial.println("attribut ? ");
Serial.println( attributes );
Serial.print("Envoi n°");
Serial.println(counter);
delay(10000);
counter++;

https://technovallis.frama.wiki/ Printed on 2019/12/11 00:22


2019/12/11 00:22 7/7 ESP32 et mqtt

From:
https://technovallis.frama.wiki/ - Wiki techno du collège Vallis Aeria

Permanent link:
https://technovallis.frama.wiki/esp_32_et_mqtt

Last update: 2018/10/20 00:39

Wiki techno du collège Vallis Aeria - https://technovallis.frama.wiki/

You might also like