Professional Documents
Culture Documents
Esp32 Et MQTT PDF
Esp32 Et MQTT PDF
ESP32 et mqtt
Librairies utilisées
Arduino/hardware/heltec/esp32/libraries/WiFi
Bibliothèques utilisées
|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 support@heltec.cn
// Pin definetion of WIFI LoRa 32
// HelTec AutoMation 2017 support@heltec.cn
#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
#define Light 25
#define V2 1
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..");
}
client.setServer(mqttServer, mqttPort);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
Serial.println("connected");
} else {
}
}
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)
// 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");
Serial.println("temp1 ? ");
Serial.println( temp1 );
Serial.println("hum1 ? ");
Serial.println( hum1 );
Serial.print("Envoi n°");
Serial.println(counter);
delay(10000);
counter++;
|moisture.ino
#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <math.h>
#include <Wire.h>
// Config Heltec 32
//-----------------------------------
// Pin definetion of WIFI LoRa 32
// HelTec AutoMation 2017 support@heltec.cn
#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)
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
//conf sensor
pinMode(MOISTURE_SENSOR, INPUT); // read data from sensor
client.setServer(mqttServer, mqttPort);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
Serial.println("connected");
} else {
}
}
//-----------------------------------
void loop() {
client.loop(); //relance la connexion wifi - intéressant en cas de
coupure d'accès momentané
// 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++;
From:
https://technovallis.frama.wiki/ - Wiki techno du collège Vallis Aeria
Permanent link:
https://technovallis.frama.wiki/esp_32_et_mqtt