mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-05 12:54:57 +00:00
Compare commits
2 Commits
4f79695d9c
...
4c5cea71b2
Author | SHA1 | Date | |
---|---|---|---|
|
4c5cea71b2 | ||
|
dcd3f1006d |
95
src/Arduino/Sensors/Sensors.ino
Normal file
95
src/Arduino/Sensors/Sensors.ino
Normal file
@@ -0,0 +1,95 @@
|
||||
#include <DHT.h>
|
||||
#include <Wire.h>
|
||||
#include "Adafruit_SGP30.h"
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
Adafruit_SGP30 sgp;
|
||||
|
||||
// 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 = "Ip adres rasperry pi";
|
||||
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() {
|
||||
// Start de seriële monitor
|
||||
Serial.begin(9600);
|
||||
|
||||
dht.begin();
|
||||
|
||||
pinMode(MQ5_PIN, INPUT);
|
||||
|
||||
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() {
|
||||
float h = dht.readHumidity();
|
||||
float t = dht.readTemperature();
|
||||
|
||||
int mq5Value = analogRead(MQ5_PIN);
|
||||
|
||||
// Check of de sensorwaarden geldig zijn
|
||||
if (isnan(h) || isnan(t) || isnan(mq5Value)) {
|
||||
Serial.println("Fout bij het lezen van de sensors!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (! sgp.IAQmeasure()) {
|
||||
Serial.println("Measurement failed");
|
||||
return;
|
||||
}
|
||||
|
||||
// Print de sensorwaarden naar de seriële monitor
|
||||
Serial.print("Luchtvochtigheid: ");
|
||||
Serial.print(h);
|
||||
Serial.print(" %\tTemperatuur: ");
|
||||
Serial.print(t);
|
||||
Serial.println(" *C");
|
||||
|
||||
Serial.print("MQ5 waarde: ");
|
||||
Serial.println(mq5Value);
|
||||
|
||||
Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
|
||||
Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");
|
||||
|
||||
delay(500);
|
||||
}
|
@@ -1,59 +0,0 @@
|
||||
#include <DHT.h>
|
||||
#include <Wire.h>
|
||||
#include "Adafruit_SGP30.h"
|
||||
|
||||
Adafruit_SGP30 sgp;
|
||||
|
||||
// define pins and type of DHT sensor
|
||||
#define DHTPIN 4
|
||||
#define DHTTYPE DHT11
|
||||
#define MQ5_PIN 2
|
||||
#define SDA_PIN 10
|
||||
#define SCL_PIN 11
|
||||
|
||||
DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
void setup() {
|
||||
//initialize serial communication and the sensors
|
||||
Serial.begin(9600);
|
||||
|
||||
dht.begin();
|
||||
|
||||
pinMode(MQ5_PIN, INPUT);
|
||||
|
||||
Wire.begin(SDA_PIN, SCL_PIN);
|
||||
Serial.println("SGP30 test");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
float h = dht.readHumidity();
|
||||
float t = dht.readTemperature();
|
||||
|
||||
int mq5Value = analogRead(MQ5_PIN);
|
||||
|
||||
// if sensor isn't connected properly display error message
|
||||
if (isnan(h) || isnan(t) || isnan(mq5Value)) {
|
||||
Serial.println("Fout bij het lezen van de sensors!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (! sgp.IAQmeasure()) {
|
||||
Serial.println("Measurement failed");
|
||||
return;
|
||||
}
|
||||
|
||||
// puts sensor values in the serial monitor
|
||||
Serial.print("Luchtvochtigheid: ");
|
||||
Serial.print(h);
|
||||
Serial.print(" %\tTemperatuur: ");
|
||||
Serial.print(t);
|
||||
Serial.println(" *C");
|
||||
|
||||
Serial.print("MQ5 waarde: ");
|
||||
Serial.println(mq5Value);
|
||||
|
||||
Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
|
||||
Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");
|
||||
|
||||
delay(500);
|
||||
}
|
Reference in New Issue
Block a user