#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#define DHTTYPE DHT22 // DHT22 // DHT21
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// const char* ssid = "YOUR WIFI SSID";
// const char* password = "YOUR WIFI PASSWORD";
const char* mqtt_broker = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
// Change the digits 123456789 for a unique ID
char OutTopic[] = "Curso_IoT/123456789";
char InTopic[] = "Curso_IoT/123456789/control";
char onevalueTopic[] = "Curso_IoT/123456789/onevalue";
float temperature = 0;
float humidity = 0;
uint8_t DHTPin = 13;
DHT dht(DHTPin, DHTTYPE);
long previousMillis = 0;
long sampling_period = 8000; // mS, do not lower this valuer lower than 5000
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(".");
}
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();
Serial.print("Message recieved: ");
if ((char)payload[0] == '1') {
Serial.println("ON");
} else if ((char)payload[0] == '0'){
Serial.println("Off");
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Change the ClientID to something different and unique
String clientId = "Client1234567890";
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
client.subscribe(InTopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
delay(2000);// Wait 2 seconds before retrying
}
}
}
void get_sensor_values(){
temperature = dht.readTemperature();
humidity = dht.readHumidity();
Serial.print(temperature,1);
Serial.print('\t');
Serial.println(humidity,0);
}
void setup() {
Serial.begin(115200);
pinMode(DHTPin, INPUT);
dht.begin();
setup_wifi();
client.setServer(mqtt_broker,1883);
client.setCallback(callback);
previousMillis = millis();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
if (now - previousMillis > sampling_period) {
get_sensor_values();
// "n" is the number of the IoT Node, change it as you please
String JSON_msg = "{\"n\":\"123456789\",\"ToC\":" + String(temperature,1);
JSON_msg += ",\"RH\":" + String(humidity,0) + "}";
Serial.print("Publish message: ");
Serial.println(JSON_msg);
// MQTT Publish to topic
char JSON_msg_array[60];
int JSON_msg_length = JSON_msg.length();
JSON_msg.toCharArray(JSON_msg_array, 60);
Serial.println(JSON_msg_array);
if (client.connected()) {
client.publish(OutTopic, JSON_msg_array);
Serial.print("Published to topic:");
Serial.println(OutTopic);
char chrToC[5];
String strToC = String(humidity,0);
strToC.toCharArray(chrToC,5);
client.publish(onevalueTopic, chrToC);
Serial.println(chrToC);
}else{
Serial.print("Not connected to broker... couldn't send MQTT message...");
}
previousMillis = millis();
}
}