mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-05 12:54:57 +00:00
restructure en code toegevoegd voor mqtt
This commit is contained in:
@@ -1,20 +1,51 @@
|
||||
#include <DHT.h>
|
||||
#include <Wire.h>
|
||||
#include "Adafruit_SGP30.h"
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
Adafruit_SGP30 sgp;
|
||||
|
||||
// define pins and type of DHT sensor
|
||||
// Definieert de pins voor de sensoren
|
||||
#define DHTPIN 4
|
||||
#define DHTTYPE DHT11
|
||||
|
||||
#define MQ5_PIN 2
|
||||
|
||||
#define SDA_PIN 10
|
||||
#define SCL_PIN 11
|
||||
|
||||
DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
// WiFi en MQTT instellingen
|
||||
const char* ssid = "iotroam";
|
||||
const char* password = "6LAFiQVnUO";
|
||||
const char* mqtt_server = "";
|
||||
const char* mqtt_topic = "sensors/data";
|
||||
|
||||
// MQTT client
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
// Functie om verbinding te maken met WiFi
|
||||
void setup_wifi() {
|
||||
delay(10);
|
||||
Serial.println("Verbinden met WiFi...");
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("\nWiFi verbonden!");
|
||||
}
|
||||
|
||||
// Callback voor MQTT (niet gebruikt in deze toepassing, maar vereist)
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
// Hier kun je reageren op inkomende MQTT-berichten
|
||||
|
||||
void setup() {
|
||||
//initialize serial communication and the sensors
|
||||
// Start de seriële monitor
|
||||
Serial.begin(9600);
|
||||
|
||||
dht.begin();
|
||||
@@ -23,6 +54,11 @@ void setup() {
|
||||
|
||||
Wire.begin(SDA_PIN, SCL_PIN);
|
||||
Serial.println("SGP30 test");
|
||||
|
||||
// Verbind met WiFi en MQTT-broker
|
||||
setup_wifi();
|
||||
client.setServer(mqtt_server, 1883);
|
||||
client.setCallback(callback);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -31,7 +67,7 @@ void loop() {
|
||||
|
||||
int mq5Value = analogRead(MQ5_PIN);
|
||||
|
||||
// if sensor isn't connected properly display error message
|
||||
// Check of de sensorwaarden geldig zijn
|
||||
if (isnan(h) || isnan(t) || isnan(mq5Value)) {
|
||||
Serial.println("Fout bij het lezen van de sensors!");
|
||||
return;
|
||||
@@ -42,7 +78,7 @@ void loop() {
|
||||
return;
|
||||
}
|
||||
|
||||
// puts sensor values in the serial monitor
|
||||
// Print de sensorwaarden naar de seriële monitor
|
||||
Serial.print("Luchtvochtigheid: ");
|
||||
Serial.print(h);
|
||||
Serial.print(" %\tTemperatuur: ");
|
Reference in New Issue
Block a user