//-----------------------------------------------------
//2024 (C)(R) All Rights Reserved Constantinos Challoumis
//This program produces random numbers of temperature
//and humidity. Also, there is a DHT22 for the control
//of the temperature and humidity. The data extracted via server to
//node-red.
//-----------------------------------------------------
#include <WiFi.h>
#include <PubSubClient.h>
#include "DHTesp.h"
#include <DHT.h>
const int DHT_PIN = 9;
DHTesp dhtSensor;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
float temp = 0;
float hum = 0;
int value = 0;
char msg1[100];
char msg2[100];
float randNumber1; // random number
float randNumber2; // random number
float temperature1;
float humidity1;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(2, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
} else {
digitalWrite(2, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("Connected");
// Once connected, publish an announcement...
client.publish("iotfrontier/mqtt", "iotfrontier");
// ... and resubscribe
client.subscribe("iotfrontier/mqtt");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
#define DHT_PIN 9
// Uncomment the following line for DHT11
// DHT dht(DHT_PIN, DHT11);
// Comment out the above line and uncomment the following line for DHT22
DHT dht(DHT_PIN, DHT22);
void setup() {
pinMode(2, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
/*TempAndHumidity data = dhtSensor.getTempAndHumidity();
String temp = String(data.temperature, 2);
Serial.print("Temperature (C): ");
Serial.println(temp);
client.publish("iotfrontier/temperature", temp.c_str());
String hum = String(data.humidity, 2);
Serial.print("Humidity (%): ");
Serial.println(hum);
client.publish("iotfrontier/humidity", hum.c_str());*/
randNumber1 = random(-20,70)*1;
randNumber2 = random(0,100)*1;
String msg1 = String(randNumber1, 2);
Serial.print("Temperature dynamic (C): ");
Serial.println(msg1);
client.publish("iotfrontier/temperature1", msg1.c_str());
String msg2 = String(randNumber2, 2);
Serial.print("Humidity dynamic (%): ");
Serial.println(msg2);
client.publish("iotfrontier/humidity1", msg2.c_str());
delay(1000);
delay(1000); // Wait for 1 seconds between readings
float temperature = dht.readTemperature(); // Read temperature in Celsius
float humidity = dht.readHumidity(); // Read humidity
// Check if any reads failed and print the results to the serial monitor
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
} /*else {
Serial.print("Temperature (C): ");
Serial.print(temperature);
Serial.print("Humidity (%): ");
Serial.print(humidity);
Serial.println(" %");
}*/
String temp = String(temperature, 2);
Serial.print("Temperature (C): ");
Serial.println(temp);
client.publish("iotfrontier/temperature", temp.c_str());
String hum = String(humidity, 2);
Serial.print("Humidity (%): ");
Serial.println(hum);
client.publish("iotfrontier/humidity", hum.c_str());
}
}
void loop1() {
}
// Initialize min and max values
//int minVal = -20;
//int maxVal = 70;
//int randomNumber;
// Perform the task for 20 iterations
//for (int i = 0; i < 100; i++) {
// Generate a random number within the current min and max limits
// int randomNumber = random(minVal, maxVal);
// Print the randomly generated number on the serial monitor
// Serial.println(randomNumber);
// Update min and max values for the next iteration
// randomNumber = random(minVal, maxVal);
// Add a delay for better readability (optional)
// delay(500);
// }
// End the program after 20 iterations
// while (true) {
// delay(1000);
// }
// Serial.print("Humidity1 (%): ");
// Serial.println(randomNumber);
// client.publish("iotfrontier/humidity1", randomNumber);
// delay(1000);
//client.loop();
//randNumber1 = random(-20,70);
//randNumber2 = random(0,100);
//sprintf(msg1, " %i ", randNumber1);
//sprintf(msg2, " %i ", randNumber2);
//client.publish("iotfrontier/temperature1", msg1);
//client.publish("iotfrontier/humidity1", msg2);
//Serial.println(msg1);
//Serial.println(msg2);
//delay(1000);