119 lines
2.8 KiB
C++
119 lines
2.8 KiB
C++
/*
|
||
Projekt: MQTT Wifi DHT11 Sensor
|
||
Beschreibung: Dieses Sketch kann zur Nutzung auf einem Arduino UNO R4 Wifi mit dem Sensor DHT11 genutzt werden
|
||
|
||
Autor: Joachim Hummel
|
||
Datum: 08.02.2024 Erstellung
|
||
|
||
Arduino-Board: Arduino Uno R4 WiFi
|
||
|
||
Lizenz: Apache 2.0
|
||
|
||
Hinweise:
|
||
- Die Nutzung des Code beinhaltet keinerlei Garantie oder Gew<65>hrliestung.
|
||
Die Nutzung erfolgt auf eigenes Risiko ohne Rechtsanspruch
|
||
|
||
|
||
Credits:
|
||
- Danke f<>r die Bereitstellung von Codeteilen aus der Arduino IDE
|
||
|
||
Weitere Informationen findest du in der README.md-Datei oder auf GitHub.
|
||
*/
|
||
#include <ArduinoJson.h>
|
||
#include <ArduinoJson.hpp>
|
||
#include <WiFiS3.h>
|
||
#include <PubSubClient.h>
|
||
#include <Wire.h>
|
||
#include <DHT.h> // Replace "Adafruit_BME680.h" with "DHT.h"
|
||
#include "arduino_secrets.h"
|
||
|
||
#define DHTPIN 8
|
||
#define DHTTYPE DHT11
|
||
DHT dht(DHTPIN, DHTTYPE);
|
||
|
||
// please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||
char ssid[] = SECRET_SSID; // your network SSID (name)
|
||
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
|
||
const char* mqtt_server = "192.168.10.7"; //IP of your MQTT Broker
|
||
|
||
WiFiClient espClient;
|
||
PubSubClient client(espClient);
|
||
unsigned long lastMsg = 0;
|
||
#define MSG_BUFFER_SIZE (50)
|
||
char msg[MSG_BUFFER_SIZE];
|
||
int value = 0;
|
||
|
||
void setup_wifi() {
|
||
delay(100);
|
||
Serial.println();
|
||
Serial.print("Connecting to ");
|
||
Serial.println(ssid);
|
||
|
||
|
||
|
||
while (WiFi.status() != WL_CONNECTED) {
|
||
delay(500);
|
||
Serial.print(".");
|
||
}
|
||
|
||
Serial.println("");
|
||
Serial.println("WiFi connected");
|
||
Serial.println("IP address: ");
|
||
Serial.println(WiFi.localIP());
|
||
}
|
||
|
||
void callback(char* topic, byte* payload, unsigned int length) {
|
||
// Handle incoming MQTT messages if needed
|
||
}
|
||
|
||
void reconnect() {
|
||
while (!client.connected()) {
|
||
Serial.print("Attempting MQTT connection...");
|
||
String clientId = "ESP8266Client-";
|
||
clientId += String(random(0xffff), HEX);
|
||
if (client.connect(clientId.c_str())) {
|
||
Serial.println("connected");
|
||
client.publish("outTopic", "hello world");
|
||
client.subscribe("inTopic");
|
||
} else {
|
||
Serial.print("failed, rc=");
|
||
Serial.print(client.state());
|
||
Serial.println(" try again in 5 seconds");
|
||
delay(5000);
|
||
}
|
||
}
|
||
}
|
||
|
||
void setup() {
|
||
Serial.begin(115200);
|
||
while (!Serial);
|
||
Serial.println(F("DHT11 test"));
|
||
|
||
dht.begin();
|
||
|
||
setup_wifi();
|
||
client.setServer(mqtt_server, 1883);
|
||
client.setCallback(callback);
|
||
}
|
||
|
||
void loop() {
|
||
if (!client.connected()) {
|
||
reconnect();
|
||
}
|
||
client.loop();
|
||
StaticJsonDocument<32> doc;
|
||
char output[55];
|
||
|
||
long now = millis();
|
||
if (now - lastMsg > 5000) {
|
||
lastMsg = now;
|
||
float temp = dht.readTemperature();
|
||
float humidity = dht.readHumidity();
|
||
doc["t"] = temp;
|
||
doc["h"] = humidity;
|
||
|
||
serializeJson(doc, output);
|
||
client.publish("/home/sensors", output);
|
||
}
|
||
}
|