#include <DHT.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#define DHTPIN 16
#define DHTTYPE DHT22
#define LED1PIN 25
#define LED2PIN 26
#define LED3PIN 27
DHT dht(DHTPIN, DHTTYPE);
// global variables for sensor readings:
float temperature ;
float humidity;
// counter counts the number of loop rounds
int counter;
// Wifi settings and initializations:
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WiFiClientSecure wifiClient;
// MQTT settings, credentials and initializations:
const char* mqtt_server = "<yourclusterhere>.s1.eu.hivemq.cloud";
const int mqtt_port = 8883;
const char* mqtt_user = "labdevice";
const char* mqtt_password = "Itsfun1!";
const char* mqtt_client_id = "thing-petri"; // <== replace with your name
const char* topic = "smartcampus/edu/tiko/laboratory/petri/dht22"; // <== replace with your name
PubSubClient client(wifiClient);
boolean rc;
// timer delay for MQTT publishing:
const long timerDelay = 10000;
void setup() {
// original initializations here ...
// Connect WIFI:
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("\nWiFi connected");
// Set MQTT server settings:
wifiClient.setInsecure(); // Käytä epävarmaa yhteyttä (jos SSL-varmennetta ei ole määritetty)
client.setServer(mqtt_server, mqtt_port);
// Connect MQTT:
connectToMQTT();
Serial.println("done.");
// first line in the CSV format is a list of attribute names to be outputted:
Serial.println("Data, Temperature, Humidity");
counter = 1;
}
void loop() {
// create a MQTT connection:
if (!client.connected()) {
connectToMQTT();
}
client.loop();
// call subroutine to read observations:
readObservations();
// write observations to Serial (CSV) and LED pins:
writeObservations();
// publish lates observations to MQTT topic:
static unsigned long lastTime = 0;
if (millis() - lastTime > timerDelay) {
lastTime = millis();
publishObservations();
}
delay(5000); // this speeds up the simulation
counter++;
} // end loop
void readObservations() {
temperature = dht.readTemperature();
humidity = dht.readHumidity();
}
void writeObservations() {
// Write to LED PINS HERE
Serial.print(counter);
Serial.print(", ");
// Print the values of temperature in Celsius
Serial.print(temperature);
Serial.print(", ");
// print Humidity in percent
Serial.println(humidity);
}
// (re)connect to MQTT server:
void connectToMQTT() {
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
if (client.connect(mqtt_client_id, mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" trying again in 5 seconds");
delay(5000);
}
}
}
// publish latest observations to MQTT topic:
void publishObservations() {
// Luo satunnaiset mittaukset
float humidity = random(0, 100);
float temp = random(150, 250) / 10.0;
// construct the JSON frame to be sent:
String payload =
"{\"humidity\": " +
String(humidity, 1) +
", \"temperature\": " +
String(temperature, 1) + "}";
// Julkaise viesti
if (client.publish(topic, payload.c_str())) {
Serial.println("Message sent: " + payload);
} else {
Serial.println("Message failed");
}
}